Skip to content
This repository has been archived by the owner on Jun 21, 2020. It is now read-only.

Commit

Permalink
兼容RPGItem中...
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSoulHanxi committed May 20, 2018
1 parent 3d9f2de commit 70422cc
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 55 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ dependencies {
transitive = false
}

compile fileTree(dir: 'lib', include: ['*.jar'])

testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile 'org.mockito:mockito-core:1.10.19'
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/blue/lapis/pore/impl/PoreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -589,22 +589,27 @@ public void configureDbConfig(ServerConfig config) {

@Override
public boolean addRecipe(Recipe recipe) {
throw new NotImplementedException("TODO");
return false;
//throw new NotImplementedException("TODO");
}

@Override
public List<Recipe> getRecipesFor(ItemStack result) {
throw new NotImplementedException("TODO");
List<Recipe> list = new ArrayList<Recipe>();
return list;
//throw new NotImplementedException("TODO");
}

@Override
public Iterator<Recipe> recipeIterator() {
throw new NotImplementedException("TODO");
List<Recipe> list = new ArrayList<Recipe>();
return list.iterator();
//throw new NotImplementedException("TODO");
}

@Override
public void clearRecipes() {
throw new NotImplementedException("TODO");
//throw new NotImplementedException("TODO");
}

@Override
Expand Down
152 changes: 101 additions & 51 deletions src/main/java/blue/lapis/pore/impl/inventory/meta/PoreItemMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.bukkit.enchantments.EnchantmentWrapper;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.meta.ItemMeta;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.mutable.DisplayNameData;
import org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData;
import org.spongepowered.api.data.manipulator.mutable.item.HideData;
Expand All @@ -40,12 +41,7 @@
import org.spongepowered.api.text.Text;
import org.spongepowered.common.item.enchantment.SpongeEnchantment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;

public class PoreItemMeta extends PoreWrapper<ItemStack> implements ItemMeta {

Expand All @@ -57,9 +53,9 @@ public PoreItemMeta(ItemStack holder) {

@Override
public boolean hasDisplayName() {
Optional<DisplayNameData> displayName = getHandle().get(DisplayNameData.class);
Optional<Text> displayName = getHandle().get(Keys.DISPLAY_NAME);
if (displayName.isPresent()) {
if (displayName.get().displayName().exists()) {
if (!displayName.get().isEmpty()) {
return true;
}
}
Expand All @@ -68,27 +64,23 @@ public boolean hasDisplayName() {

@Override
public String getDisplayName() {
Optional<DisplayNameData> displayName = getHandle().get(DisplayNameData.class);
Optional<Text> displayName = getHandle().get(Keys.DISPLAY_NAME);
if (displayName.isPresent()) {
return PoreText.convert(displayName.get().displayName().get());
return PoreText.convert(displayName.get());
}
return null;
}

@Override
public void setDisplayName(String name) {
Optional<DisplayNameData> displayName = getHandle().getOrCreate(DisplayNameData.class);
if (displayName.isPresent()) {
getHandle().offer(displayName.get().displayName().set(PoreText.convert(name)));
}

getHandle().offer(Keys.DISPLAY_NAME, PoreText.convert(name));
}

@Override
public boolean hasLore() {
Optional<LoreData> lore = getHandle().get(LoreData.class);
Optional<List<Text>> lore = getHandle().get(Keys.ITEM_LORE);
if (lore.isPresent()) {
if (lore.get().lore().exists()) {
if (lore.get().isEmpty()) {
return true;
}
}
Expand All @@ -98,39 +90,39 @@ public boolean hasLore() {
@Override
public List<String> getLore() {
List<String> lores = new ArrayList<String>();
Optional<LoreData> lore = getHandle().get(LoreData.class);
Optional<List<Text>> lore = getHandle().get(Keys.ITEM_LORE);
if (lore.isPresent()) {
for (Text text : lore.get().lore()) {
for (Text text : lore.get()) {
lores.add(PoreText.convert(text));
System.out.println(PoreText.convert(text));
}
} else {
lores.add("");
}
return lores;
}

@Override
public void setLore(List<String> lore) {
Optional<LoreData> loreData = getHandle().get(LoreData.class);
if (loreData.isPresent()) {
List<Text> text = new ArrayList<Text>();
for (String string : lore) {
text.add(PoreText.convert(string));
}
loreData.get().setElements(text);
List<Text> text = new ArrayList<Text>();
for (String string : lore) {
text.add(PoreText.convert(string));
}
getHandle().offer(Keys.ITEM_LORE, text);
}

@Override
public boolean hasEnchants() {
Optional<EnchantmentData> enchants = getHandle().get(EnchantmentData.class);
return enchants.isPresent() && enchants.get().asList().size() > 0 ;
Optional<List<org.spongepowered.api.item.enchantment.Enchantment>> enchants = getHandle().get(Keys.ITEM_ENCHANTMENTS);
return enchants.isPresent() && enchants.get().size() > 0 ;
}

@Override
public boolean hasEnchant(Enchantment ench) {
Optional<EnchantmentData> enchants = getHandle().get(EnchantmentData.class);
Optional<List<org.spongepowered.api.item.enchantment.Enchantment>> enchants = getHandle().get(Keys.ITEM_ENCHANTMENTS);
org.spongepowered.api.item.enchantment.Enchantment target = getEnchant(ench);
if (enchants.isPresent()) {
for (org.spongepowered.api.item.enchantment.Enchantment itmEnch : enchants.get().asList()) {
for (org.spongepowered.api.item.enchantment.Enchantment itmEnch : enchants.get()) {
if (itmEnch.getType().equals(target)) {
return true;
}
Expand All @@ -141,10 +133,10 @@ public boolean hasEnchant(Enchantment ench) {

@Override
public int getEnchantLevel(Enchantment ench) {
Optional<EnchantmentData> enchants = getHandle().get(EnchantmentData.class);
Optional<List<org.spongepowered.api.item.enchantment.Enchantment>> enchants = getHandle().get(Keys.ITEM_ENCHANTMENTS);
org.spongepowered.api.item.enchantment.Enchantment target = getEnchant(ench);
if (enchants.isPresent()) {
for (org.spongepowered.api.item.enchantment.Enchantment itmEnch : enchants.get().asList()) {
for (org.spongepowered.api.item.enchantment.Enchantment itmEnch : enchants.get()) {
if (itmEnch.getType().equals(target)) {
return itmEnch.getLevel();
}
Expand All @@ -155,10 +147,10 @@ public int getEnchantLevel(Enchantment ench) {

@Override
public Map<Enchantment, Integer> getEnchants() {
Optional<EnchantmentData> enchants = getHandle().get(EnchantmentData.class);
Optional<List<org.spongepowered.api.item.enchantment.Enchantment>> enchants = getHandle().get(Keys.ITEM_ENCHANTMENTS);
Map<Enchantment, Integer> map = new HashMap<Enchantment, Integer>();
if (enchants.isPresent()) {
for (org.spongepowered.api.item.enchantment.Enchantment itmEnch : enchants.get().asList()) {
for (org.spongepowered.api.item.enchantment.Enchantment itmEnch : enchants.get()) {
map.put(Enchantment.getByName(itmEnch.getType().getName()), itmEnch.getLevel());
}
}
Expand All @@ -167,27 +159,29 @@ public Map<Enchantment, Integer> getEnchants() {

@Override
public boolean addEnchant(Enchantment ench, int level, boolean ignoreLevelRestriction) {
Optional<EnchantmentData> enchants = getHandle().getOrCreate(EnchantmentData.class);
Optional<List<org.spongepowered.api.item.enchantment.Enchantment>> enchants = getHandle().get(Keys.ITEM_ENCHANTMENTS);
if (enchants.isPresent()) {
if (level > ench.getMaxLevel() && !ignoreLevelRestriction) {
level = ench.getMaxLevel();
}

org.spongepowered.api.item.enchantment.Enchantment copy = getEnchant(ench);
getHandle().offer(enchants.get().addElement(new SpongeEnchantment(copy.getType(), level)));
enchants.get().add(new SpongeEnchantment(copy.getType(), level));
getHandle().offer(Keys.ITEM_ENCHANTMENTS, enchants.get());
return true;
}
return false;
}

@Override
public boolean removeEnchant(Enchantment ench) {
Optional<EnchantmentData> enchants = getHandle().get(EnchantmentData.class);
Optional<List<org.spongepowered.api.item.enchantment.Enchantment>> enchants = getHandle().get(Keys.ITEM_ENCHANTMENTS);
org.spongepowered.api.item.enchantment.Enchantment target = getEnchant(ench);
if (enchants.isPresent()) {
for (org.spongepowered.api.item.enchantment.Enchantment itmEnch : enchants.get().asList()) {
for (org.spongepowered.api.item.enchantment.Enchantment itmEnch : enchants.get()) {
if (itmEnch.getType().equals(target)) {
getHandle().offer(enchants.get().remove(itmEnch));
enchants.get().remove(itmEnch);
getHandle().offer(Keys.ITEM_ENCHANTMENTS, enchants.get());;
return true;
}
}
Expand All @@ -197,10 +191,10 @@ public boolean removeEnchant(Enchantment ench) {

@Override
public boolean hasConflictingEnchant(Enchantment ench) {
Optional<EnchantmentData> enchants = getHandle().get(EnchantmentData.class);
Optional<List<org.spongepowered.api.item.enchantment.Enchantment>> enchants = getHandle().get(Keys.ITEM_ENCHANTMENTS);
org.spongepowered.api.item.enchantment.Enchantment target = getEnchant(ench);
if (enchants.isPresent()) {
for (org.spongepowered.api.item.enchantment.Enchantment itmEnch : enchants.get().asList()) {
for (org.spongepowered.api.item.enchantment.Enchantment itmEnch : enchants.get()) {
if (!itmEnch.getType().isCompatibleWith(target.getType())) {
return true;
}
Expand All @@ -223,22 +217,22 @@ public void addItemFlags(ItemFlag... itemFlags) {
for (ItemFlag flag : itemFlags) {
switch (flag) {
case HIDE_ATTRIBUTES:
getHandle().getOrCreate(HideData.class).get().hideAttributes().set(true);
getHandle().offer(Keys.HIDE_ATTRIBUTES, true);
break;
case HIDE_DESTROYS:
getHandle().getOrCreate(HideData.class).get().hideCanDestroy().set(true);
getHandle().offer(Keys.HIDE_CAN_DESTROY, true);
break;
case HIDE_ENCHANTS:
getHandle().getOrCreate(HideData.class).get().hideEnchantments().set(true);
getHandle().offer(Keys.HIDE_ENCHANTMENTS, true);
break;
case HIDE_PLACED_ON:
getHandle().getOrCreate(HideData.class).get().hideCanPlace().set(true);
getHandle().offer(Keys.HIDE_CAN_PLACE, true);
break;
case HIDE_POTION_EFFECTS:
getHandle().getOrCreate(HideData.class).get().hideMiscellaneous().set(true);
getHandle().offer(Keys.HIDE_MISCELLANEOUS, true);
break;
case HIDE_UNBREAKABLE:
getHandle().getOrCreate(HideData.class).get().hideUnbreakable().set(true);
getHandle().offer(Keys.HIDE_UNBREAKABLE, true);
break;
default:
throw new NotImplementedException("TODO");
Expand All @@ -248,18 +242,68 @@ public void addItemFlags(ItemFlag... itemFlags) {

@Override
public void removeItemFlags(ItemFlag... itemFlags) {
throw new NotImplementedException("TODO");
for (ItemFlag flag : itemFlags) {
switch (flag) {
case HIDE_ATTRIBUTES:
getHandle().offer(Keys.HIDE_ATTRIBUTES, false);
break;
case HIDE_DESTROYS:
getHandle().offer(Keys.HIDE_CAN_DESTROY, false);
break;
case HIDE_ENCHANTS:
getHandle().offer(Keys.HIDE_ENCHANTMENTS, false);
break;
case HIDE_PLACED_ON:
getHandle().offer(Keys.HIDE_CAN_PLACE, false);
break;
case HIDE_POTION_EFFECTS:
getHandle().offer(Keys.HIDE_MISCELLANEOUS, false);
break;
case HIDE_UNBREAKABLE:
getHandle().offer(Keys.HIDE_UNBREAKABLE, false);
break;
default:
throw new NotImplementedException("TODO");
}
}
}

@Override
public Set<ItemFlag> getItemFlags() {
//return ImmutableSet.of();
throw new NotImplementedException("TODO");
HashSet<ItemFlag> set = new HashSet<ItemFlag>();
if (getHandle().get(Keys.HIDE_ATTRIBUTES).orElse(false))
set.add(ItemFlag.HIDE_ATTRIBUTES);
if (getHandle().get(Keys.HIDE_CAN_DESTROY).orElse(false))
set.add(ItemFlag.HIDE_DESTROYS);
if (getHandle().get(Keys.HIDE_CAN_PLACE).orElse(false))
set.add(ItemFlag.HIDE_PLACED_ON);
if (getHandle().get(Keys.HIDE_ENCHANTMENTS).orElse(false))
set.add(ItemFlag.HIDE_ENCHANTS);
if (getHandle().get(Keys.HIDE_MISCELLANEOUS).orElse(false))
set.add(ItemFlag.HIDE_POTION_EFFECTS);
if (getHandle().get(Keys.HIDE_UNBREAKABLE).orElse(false))
set.add(ItemFlag.HIDE_UNBREAKABLE);
return set;
}

@Override
public boolean hasItemFlag(ItemFlag flag) {
throw new NotImplementedException("TODO");
switch (flag) {
case HIDE_ATTRIBUTES:
return getHandle().get(Keys.HIDE_ATTRIBUTES).orElse(false);
case HIDE_DESTROYS:
return getHandle().get(Keys.HIDE_CAN_DESTROY).orElse(false);
case HIDE_ENCHANTS:
return getHandle().get(Keys.HIDE_ENCHANTMENTS).orElse(false);
case HIDE_PLACED_ON:
return getHandle().get(Keys.HIDE_CAN_PLACE).orElse(false);
case HIDE_POTION_EFFECTS:
return getHandle().get(Keys.HIDE_MISCELLANEOUS).orElse(false);
case HIDE_UNBREAKABLE:
return getHandle().get(Keys.HIDE_UNBREAKABLE).orElse(false);
default:
throw new NotImplementedException("TODO");
}
}

@Override
Expand All @@ -280,6 +324,12 @@ public boolean isUnbreakable() {
@Override
public void setUnbreakable(boolean unbreakable) {
this.unbreakable = unbreakable;
if (unbreakable){
addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
} else {
removeItemFlags(ItemFlag.HIDE_UNBREAKABLE);
}

}

@Override
Expand Down

0 comments on commit 70422cc

Please sign in to comment.