Skip to content

Commit

Permalink
fix unbreakable module
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 6, 2024
1 parent eb71550 commit ad56d51
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.EnumSet;
import java.util.List;
Expand Down Expand Up @@ -53,12 +52,26 @@ public boolean shouldEnable() {

@Override
public ItemLegality legalityOf(ItemStack itemStack) {
if (itemStack == null || itemStack.getType().isAir()) {
if (itemStack == null || itemStack.getType().isAir() || !itemStack.hasItemMeta()) {
return ItemLegality.LEGAL;
}

if (!useWhitelist || blacklistMode == whitelistedTypes.contains(itemStack.getType())) {
if (isUnbreakable(itemStack)) {
if (itemStack.getItemMeta().isUnbreakable()) {
return ItemLegality.ILLEGAL;
}

Damageable damageable = (Damageable) itemStack.getItemMeta();

if (!damageable.hasDamage()) {
return ItemLegality.LEGAL;
}

if (itemStack.getType().getMaxDurability() == 0) {
return ItemLegality.LEGAL;
}

if (damageable.getDamage() > itemStack.getType().getMaxDurability() || damageable.getDamage() < 0) {
return ItemLegality.ILLEGAL;
}
}
Expand All @@ -77,20 +90,4 @@ public void handleItem(ItemStack itemStack, ItemLegality legality) {
itemStack.setAmount(0);
}
}

private boolean isUnbreakable(ItemStack itemStack) {
if (!itemStack.hasItemMeta()) return false;

ItemMeta meta = itemStack.getItemMeta();
if (meta.isUnbreakable()) return true;

Damageable damageMeta = (Damageable) meta;
if (!damageMeta.hasDamage()) return false;

final short itemMaxDurability = itemStack.getType().getMaxDurability();
if (itemMaxDurability == 0) return false;

final int remainingDurability = itemMaxDurability - damageMeta.getDamage();
return remainingDurability > itemMaxDurability || remainingDurability < 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,19 @@ public ItemLegality legalityOf(ItemStack itemStack) {
}

if (!useWhitelist || blacklistMode == whitelistedTypes.contains(itemStack.getType())) {
if (isUnbreakable(itemStack)) {
if (itemStack.getItemMeta().isUnbreakable()) {
return ItemLegality.ILLEGAL;
}

if (!skipZeroDurability || itemStack.getType().getMaxDurability() != 0) {
if (itemStack.getDurability() > itemStack.getType().getMaxDurability() || itemStack.getDurability() < 0) {
return ItemLegality.ILLEGAL;
}
} else {
if (itemStack.getDurability() > 2031) {
return ItemLegality.ILLEGAL;
}
}
}

if (checkStored) {
Expand All @@ -81,17 +91,4 @@ public void handleItem(ItemStack itemStack, ItemLegality legality) {
itemStack.setAmount(0);
}
}

private boolean isUnbreakable(ItemStack itemStack) {
final int durability = itemStack.getDurability();
final short maxDurability = itemStack.getType().getMaxDurability();
if (!skipZeroDurability || maxDurability != 0) {
if (durability > maxDurability) {
return true;
}
return durability < 0;
} else {
return durability > 2031;
}
}
}

0 comments on commit ad56d51

Please sign in to comment.