Skip to content

Commit

Permalink
fixed heart dupe when using hearts in offhand
Browse files Browse the repository at this point in the history
  • Loading branch information
KartoffelChipss committed Jun 3, 2024
1 parent 6e4a2d6 commit 05afa3e
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.strassburger.lifestealz.LifeStealZ;
Expand All @@ -20,6 +21,7 @@ public class InteractionListener implements Listener {
public void onPlayerInteraction(PlayerInteractEvent event) {
ItemStack item = event.getItem();
Player player = event.getPlayer();
EquipmentSlot hand = event.getHand(); // Track which hand is being used

boolean worldIsWhitelisted = LifeStealZ.getInstance().getConfig().getStringList("worlds").contains(player.getLocation().getWorld().getName());

Expand Down Expand Up @@ -50,17 +52,12 @@ public void onPlayerInteraction(PlayerInteractEvent event) {
return;
}

// Clone the ItemStack and modify the clone
ItemStack updatedItem = item.clone();
updatedItem.setAmount(item.getAmount() - 1);

// If the stack size is greater than 1, update the item meta for the remaining items
if (updatedItem.getAmount() > 0) {
updatedItem.setItemMeta(item.getItemMeta());
if (hand == EquipmentSlot.HAND) {
updateItemInHand(player, item, player.getInventory().getHeldItemSlot());
} else if (hand == EquipmentSlot.OFF_HAND) {
updateItemInHand(player, item, 40);
}

player.getInventory().setItem(player.getInventory().getHeldItemSlot(), updatedItem);

playerData.setMaxhp(newHearts);
LifeStealZ.getInstance().getPlayerDataStorage().save(playerData);
LifeStealZ.setMaxHealth(player, newHearts);
Expand Down Expand Up @@ -89,4 +86,13 @@ public void onPlayerInteraction(PlayerInteractEvent event) {
}
}
}
}

private void updateItemInHand(Player player, ItemStack item, int slot) {
ItemStack updatedItem = item.clone();
updatedItem.setAmount(item.getAmount() - 1);

if (updatedItem.getAmount() > 0) updatedItem.setItemMeta(item.getItemMeta());

player.getInventory().setItem(slot, updatedItem);
}
}

0 comments on commit 05afa3e

Please sign in to comment.