Skip to content

Commit

Permalink
PN-89 Failed attempt to fix #239
Browse files Browse the repository at this point in the history
  • Loading branch information
joserobjr committed May 10, 2020
1 parent 1f473c9 commit 5bc617a
Show file tree
Hide file tree
Showing 25 changed files with 68 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/main/java/cn/nukkit/inventory/AnvilInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import lombok.NonNull;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;

Expand Down Expand Up @@ -63,7 +63,7 @@ public void updateResult() {
int repairMaterial = getRepairMaterial(target);
Item result = target.clone();
int levelCost = getRepairCost(result) + (sacrifice.isNull() ? 0 : getRepairCost(sacrifice));
Map<Integer, Enchantment> enchantmentMap = new LinkedHashMap<>();
Map<Integer, Enchantment> enchantmentMap = new HashMap<>();
for (Enchantment enchantment : target.getEnchantments()) {
enchantmentMap.put(enchantment.getId(), enchantment);
}
Expand Down
42 changes: 16 additions & 26 deletions src/main/java/cn/nukkit/item/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -631,37 +631,27 @@ public void addEnchantment(Enchantment... enchantments) {
tag = this.getNamedTag();
}

ListTag<CompoundTag> ench;
if (!tag.contains("ench")) {
ench = new ListTag<>("ench");
tag.putList(ench);
} else {
ench = tag.getList("ench", CompoundTag.class);
Enchantment[] current = getEnchantments();
Map<Integer, Enchantment> enchantmentMap = new HashMap<>(current.length + enchantments.length);
for (Enchantment enchantment : current) {
enchantmentMap.put(enchantment.getId(), enchantment);
}

for (Enchantment enchantment : enchantments) {
boolean found = false;

for (int k = 0; k < ench.size(); k++) {
CompoundTag entry = ench.get(k);
if (entry.getShort("id") == enchantment.getId()) {
ench.add(k, new CompoundTag()
.putShort("id", enchantment.getId())
.putShort("lvl", enchantment.getLevel())
);
found = true;
break;
}
}

if (!found) {
ench.add(new CompoundTag()
.putShort("id", enchantment.getId())
.putShort("lvl", enchantment.getLevel())
);
}
enchantmentMap.put(enchantment.getId(), enchantment);
}

SortedSet<Enchantment> updated = new TreeSet<>(enchantmentMap.values());

ListTag<CompoundTag> ench = new ListTag<>("ench");

updated.stream()
.map(enchantment-> new CompoundTag()
.putShort("id", enchantment.getId())
.putShort("lvl", enchantment.getLevel()))
.forEachOrdered(ench::add);

tag.putList(ench);
this.setNamedTag(tag);
}

Expand Down
28 changes: 27 additions & 1 deletion src/main/java/cn/nukkit/item/enchantment/Enchantment.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import cn.nukkit.item.enchantment.trident.EnchantmentTridentImpaling;
import cn.nukkit.item.enchantment.trident.EnchantmentTridentLoyalty;
import cn.nukkit.item.enchantment.trident.EnchantmentTridentRiptide;
import com.google.common.base.Preconditions;

import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -27,7 +28,7 @@
* author: MagicDroidX
* Nukkit Project
*/
public abstract class Enchantment implements Cloneable {
public abstract class Enchantment implements Cloneable, Comparable<Enchantment> {

protected static Enchantment[] enchantments;

Expand Down Expand Up @@ -140,13 +141,17 @@ public static Enchantment[] getEnchantments() {
protected int level = 1;

protected final String name;

private int orderBase = Integer.MAX_VALUE;
private int orderSub = Integer.MAX_VALUE;

protected Enchantment(int id, String name, int weight, EnchantmentType type) {
this.id = id;
this.weight = weight;
this.type = type;

this.name = name;

}

public int getLevel() {
Expand Down Expand Up @@ -234,6 +239,20 @@ public boolean isMajor() {
return false;
}

public int getOrder() {
return orderBase * 1000 + orderSub;
}

protected void setOrderBase(int orderBase) {
Preconditions.checkArgument(orderBase > Integer.MAX_VALUE / 1000, "orderBase is too big. Max is Integer.MAX_VALUE / 1000");
this.orderBase = orderBase;
}

protected void setOrderSub(int orderSub) {
Preconditions.checkArgument(orderSub >= 0 && orderSub < 1000, "orderSub must be in range of 0..1000");
this.orderSub = orderSub;
}

@Override
protected Enchantment clone() {
try {
Expand All @@ -243,6 +262,13 @@ protected Enchantment clone() {
}
}

@Override
public final int compareTo(Enchantment o) {
int comp = Integer.compare(getOrder(), getOrder());
if (comp != 0) return comp;
else return Integer.compare(getId(), o.getId());
}

public static final String[] words = {"the", "elder", "scrolls", "klaatu", "berata", "niktu", "xyzzy", "bless", "curse", "light", "darkness", "fire", "air", "earth", "water", "hot", "dry", "cold", "wet", "ignite", "snuff", "embiggen", "twist", "shorten", "stretch", "fiddle", "destroy", "imbue", "galvanize", "enchant", "free", "limited", "range", "of", "towards", "inside", "sphere", "cube", "self", "other", "ball", "mental", "physical", "grow", "shrink", "demon", "elemental", "spirit", "animal", "creature", "beast", "humanoid", "undead", "fresh", "stale"};

public static String getRandomName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class EnchantmentDurability extends Enchantment {
protected EnchantmentDurability() {
super(ID_DURABILITY, "durability", 5, EnchantmentType.BREAKABLE);
setOrderBase(14000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class EnchantmentEfficiency extends Enchantment {
protected EnchantmentEfficiency() {
super(ID_EFFICIENCY, "digging", 10, EnchantmentType.DIGGER);
setOrderBase(16000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class EnchantmentFireAspect extends Enchantment {
protected EnchantmentFireAspect() {
super(ID_FIRE_ASPECT, "fire", 2, EnchantmentType.SWORD);
setOrderBase(8000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class EnchantmentKnockback extends Enchantment {
protected EnchantmentKnockback() {
super(ID_KNOCKBACK, "knockback", 5, EnchantmentType.SWORD);
setOrderBase(18000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class EnchantmentLure extends Enchantment {
protected EnchantmentLure() {
super(ID_LURE, "fishingSpeed", 2, EnchantmentType.FISHING_ROD);
setOrderBase(20000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/
public class EnchantmentMending extends Enchantment {
protected EnchantmentMending() {

super(ID_MENDING, "mending", 2, EnchantmentType.ALL);
setOrderBase();
}

@Override
Expand All @@ -26,4 +26,4 @@ public int getMaxEnchantAbility(int level) {
public boolean isCompatibleWith(Enchantment enchantment) {
return super.isCompatibleWith(enchantment) && enchantment.id != ID_BOW_INFINITY;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public class EnchantmentThorns extends Enchantment {
protected EnchantmentThorns() {
super(ID_THORNS, "thorns", 2, EnchantmentType.ARMOR_TORSO);
setOrderBase(6000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class EnchantmentWaterBreath extends Enchantment {
protected EnchantmentWaterBreath() {
super(ID_WATER_BREATHING, "oxygen", 2, EnchantmentType.ARMOR_HEAD);
setOrderBase(5000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class EnchantmentWaterWalker extends Enchantment {
protected EnchantmentWaterWalker() {
super(ID_WATER_WALKER, "waterWalker", 2, EnchantmentType.ARMOR_FEET);
setOrderBase(3000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class EnchantmentWaterWorker extends Enchantment {
protected EnchantmentWaterWorker() {
super(ID_WATER_WORKER, "waterWorker", 2, EnchantmentType.ARMOR_HEAD);
setOrderBase(1000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class EnchantmentBowFlame extends EnchantmentBow {
public EnchantmentBowFlame() {
super(Enchantment.ID_BOW_FLAME, "arrowFire", 2);
setOrderBase(9000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class EnchantmentBowInfinity extends EnchantmentBow {
public EnchantmentBowInfinity() {
super(Enchantment.ID_BOW_INFINITY, "arrowInfinite", 1);
setOrderBase(17000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class EnchantmentBowKnockback extends EnchantmentBow {
public EnchantmentBowKnockback() {
super(Enchantment.ID_BOW_KNOCKBACK, "arrowKnockback", 2);
setOrderBase(23000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class EnchantmentBowPower extends EnchantmentBow {
public EnchantmentBowPower() {
super(Enchantment.ID_BOW_POWER, "arrowDamage", 10);
setOrderBase(22000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class EnchantmentDamageArthropods extends EnchantmentDamage {

public EnchantmentDamageArthropods() {
super(ID_DAMAGE_ARTHROPODS, "arthropods", 5, TYPE.SMITE);
setOrderBase(7000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
public class EnchantmentLootDigging extends EnchantmentLoot {
public EnchantmentLootDigging() {
super(Enchantment.ID_FORTUNE_DIGGING, "lootBonusDigger", 2, EnchantmentType.DIGGER);
setOrderBase(10000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
public class EnchantmentLootFishing extends EnchantmentLoot {
public EnchantmentLootFishing() {
super(Enchantment.ID_FORTUNE_FISHING, "lootBonusFishing", 2, EnchantmentType.FISHING_ROD);
setOrderBase(13000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
public class EnchantmentLootWeapon extends EnchantmentLoot {
public EnchantmentLootWeapon() {
super(Enchantment.ID_LOOTING, "lootBonus", 2, EnchantmentType.SWORD);
setOrderBase(12000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class EnchantmentProtectionExplosion extends EnchantmentProtection {

public EnchantmentProtectionExplosion() {
super(ID_PROTECTION_EXPLOSION, "explosion", 2, TYPE.EXPLOSION);
setOrderBase(2000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class EnchantmentTridentChanneling extends EnchantmentTrident {
public EnchantmentTridentChanneling() {
super(Enchantment.ID_TRIDENT_CHANNELING, "channeling", 1);
setOrderBase(15000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class EnchantmentTridentImpaling extends EnchantmentTrident {
public EnchantmentTridentImpaling() {
super(Enchantment.ID_TRIDENT_IMPALING, "impaling", 2);
setOrderBase(11000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class EnchantmentTridentLoyalty extends EnchantmentTrident {
public EnchantmentTridentLoyalty() {
super(Enchantment.ID_TRIDENT_LOYALTY, "loyalty", 5);
setOrderBase(19000);
}

@Override
Expand Down

0 comments on commit 5bc617a

Please sign in to comment.