Skip to content

Commit

Permalink
Fix inventory issue
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankTCA committed May 19, 2024
1 parent 497c28e commit 11ae51c
Showing 1 changed file with 65 additions and 35 deletions.
100 changes: 65 additions & 35 deletions src/main/java/org/infotoast/dropsoverhaul/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,87 @@ public Listener(Plugin pl) {
this.pl = pl;
}
private Random rand = new Random();
ItemLists lists = new ItemLists();

private ItemStack getResultItemStack(Player breaker, Material mat) {
ItemStack item;
item = new ItemStack(mat);
switch (mat) {
case GRASS_BLOCK:
if (breaker.getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH))
item = new ItemStack(Material.GRASS_BLOCK);
else
item = new ItemStack(Material.DIRT);
break;
case STONE:
if (breaker.getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH))
item = new ItemStack(Material.STONE);
else
item = new ItemStack(Material.COBBLESTONE);
break;
case GRAVEL:
if (new Random().nextInt(8) == 0)
item = new ItemStack(Material.FLINT);
else
item = new ItemStack(Material.GRAVEL);
break;
}
return item;
}

private void processToolDamage(Player breaker) {
if (lists.isTool(breaker.getInventory().getItemInMainHand().getType()))
if (breaker.getInventory().getItemInMainHand().getItemMeta() instanceof Damageable) {
if (!(breaker.getInventory().getItemInMainHand().containsEnchantment(Enchantment.UNBREAKING) && rand.nextInt(100) > (100 / (breaker.getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.UNBREAKING) + 1))))
breaker.getInventory().getItemInMainHand().setDurability((short)(((Damageable)breaker.getInventory().getItemInMainHand().getItemMeta()).getDamage() + 1));
if (breaker.getInventory().getItemInMainHand().getDurability() >= breaker.getInventory().getItemInMainHand().getType().getMaxDurability() && breaker.getInventory().getItemInMainHand().getType().getMaxDurability() > 0)
breaker.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
}
}
@EventHandler
public void onBlockBreak(BlockBreakEvent evt) {
ItemLists lists = new ItemLists();
Player breaker = evt.getPlayer();
Block blocc = evt.getBlock();
Material mat = blocc.getType();
if (lists.isDeletable(mat)) {
if (breaker.getGameMode() == GameMode.SURVIVAL) {
ItemStack item;
if (lists.requiresPickaxe(mat))
if (lists.toolBreaksStone(breaker.getInventory().getItemInMainHand().getType()))
item = getResultItemStack(breaker, mat);
else { evt.setCancelled(true); blocc.setType(Material.AIR, true); return; }
else
item = getResultItemStack(breaker, mat);

if (breaker.getInventory().firstEmpty() == -1) {
System.out.println("Inventory is full.");
Material resultMat = item.getType();
if (breaker.getInventory().contains(resultMat)) {
System.out.println("Inventory contains valid item.");
ItemStack[] storageContents = breaker.getInventory().getStorageContents();
for (ItemStack stack : storageContents) {
if (stack.getType() == resultMat) {
if (stack.getAmount() < resultMat.getMaxStackSize()) {
stack.add();
blocc.setType(Material.AIR, true);
processToolDamage(breaker);
evt.setCancelled(true);
return;
}
}
}
}
FileConfiguration conf = pl.getCustomConfig();
if (!conf.getBoolean("full-inventory-delete")) return;
blocc.setType(Material.AIR, true);
processToolDamage(breaker);
evt.setCancelled(true);
return;
}
ItemStack item;
if (lists.requiresPickaxe(mat))
if (lists.toolBreaksStone(breaker.getInventory().getItemInMainHand().getType()))
item = new ItemStack(mat);
else { evt.setCancelled(true); blocc.setType(Material.AIR, true); return; }
else
item = new ItemStack(mat);
switch (mat) {
case GRASS_BLOCK:
if (breaker.getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH))
item = new ItemStack(Material.GRASS_BLOCK);
else
item = new ItemStack(Material.DIRT);
break;
case STONE:
if (breaker.getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH))
item = new ItemStack(Material.STONE);
else
item = new ItemStack(Material.COBBLESTONE);
break;
case GRAVEL:
if (new Random().nextInt(8) == 0)
item = new ItemStack(Material.FLINT);
else
item = new ItemStack(Material.GRAVEL);
break;
}

breaker.getInventory().addItem(item);
blocc.setType(Material.AIR, true);
if (lists.isTool(breaker.getInventory().getItemInMainHand().getType()))
if (breaker.getInventory().getItemInMainHand().getItemMeta() instanceof Damageable) {
if (!(breaker.getInventory().getItemInMainHand().containsEnchantment(Enchantment.UNBREAKING) && rand.nextInt(100) > (100 / (breaker.getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.UNBREAKING) + 1))))
breaker.getInventory().getItemInMainHand().setDurability((short)(((Damageable)breaker.getInventory().getItemInMainHand().getItemMeta()).getDamage() + 1));
if (breaker.getInventory().getItemInMainHand().getDurability() >= breaker.getInventory().getItemInMainHand().getType().getMaxDurability() && breaker.getInventory().getItemInMainHand().getType().getMaxDurability() > 0)
breaker.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
}
processToolDamage(breaker);
evt.setCancelled(true);
}
}
Expand Down

0 comments on commit 11ae51c

Please sign in to comment.