Skip to content

Commit

Permalink
set to max durability instead of deleting item
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 6, 2024
1 parent 6f83220 commit 7235008
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package me.xginko.aef.modules.illegals.items;

import me.xginko.aef.enums.AEFPermission;
import me.xginko.aef.enums.IllegalHandling;
import me.xginko.aef.enums.ItemLegality;
import me.xginko.aef.utils.ItemUtil;
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 @@ -61,13 +63,13 @@ public ItemLegality legalityOf(ItemStack itemStack) {
return ItemLegality.ILLEGAL;
}

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

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

if (itemStack.getType().getMaxDurability() == 0) {
Damageable damageable = (Damageable) itemStack.getItemMeta();

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

Expand All @@ -85,9 +87,33 @@ public ItemLegality legalityOf(ItemStack itemStack) {

@Override
public void handleItem(ItemStack itemStack, ItemLegality legality) {
// We need to always take action here as we cant reliably prevent usage otherwise
if (legality != ItemLegality.LEGAL) {
itemStack.setAmount(0);
if (handling == IllegalHandling.PREVENT_USE_ONLY) return;

switch (legality) {
case CONTAINS_ILLEGAL -> itemStack.setAmount(0);
case ILLEGAL -> {
ItemMeta itemMeta = itemStack.getItemMeta();

if (itemMeta.isUnbreakable()) {
itemMeta.setUnbreakable(false);
itemStack.setItemMeta(itemMeta);
}

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

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

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

if (damageable.getDamage() > itemStack.getType().getMaxDurability() || damageable.getDamage() < 0) {
damageable.setDamage(itemStack.getType().getMaxDurability());
itemStack.setItemMeta(damageable);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.cryptomorin.xseries.XMaterial;
import me.xginko.aef.enums.AEFPermission;
import me.xginko.aef.enums.IllegalHandling;
import me.xginko.aef.enums.ItemLegality;
import me.xginko.aef.utils.ItemUtil;
import me.xginko.aef.utils.PlatformUtil;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Collections;
import java.util.EnumSet;
Expand Down Expand Up @@ -70,10 +72,10 @@ public ItemLegality legalityOf(ItemStack itemStack) {
if (itemStack.getDurability() > itemStack.getType().getMaxDurability() || itemStack.getDurability() < 0) {
return ItemLegality.ILLEGAL;
}
} else {
if (itemStack.getDurability() > 2031) {
return ItemLegality.ILLEGAL;
}
}

else if (itemStack.getDurability() > 2031) {
return ItemLegality.ILLEGAL;
}
}

Expand All @@ -86,9 +88,31 @@ public ItemLegality legalityOf(ItemStack itemStack) {

@Override
public void handleItem(ItemStack itemStack, ItemLegality legality) {
// We need to always take action here as we cant reliably prevent usage otherwise
if (legality != ItemLegality.LEGAL) {
itemStack.setAmount(0);
if (handling == IllegalHandling.PREVENT_USE_ONLY) return;

switch (legality) {
case CONTAINS_ILLEGAL:
itemStack.setAmount(0);
return;
case ILLEGAL:
ItemMeta itemMeta = itemStack.getItemMeta();

if (itemMeta.isUnbreakable()) {
itemMeta.setUnbreakable(false);
itemStack.setItemMeta(itemMeta);
}

if (!skipZeroDurability || itemStack.getType().getMaxDurability() != 0) {
if (itemStack.getDurability() > itemStack.getType().getMaxDurability() || itemStack.getDurability() < 0) {
itemStack.setDurability(itemStack.getType().getMaxDurability());
itemStack.setItemMeta(itemMeta);
}
}

else if (itemStack.getDurability() > 2031) {
itemStack.setDurability(itemStack.getType().getMaxDurability());
itemStack.setItemMeta(itemMeta);
}
}
}
}

0 comments on commit 7235008

Please sign in to comment.