2023-05-30 11:20:31 +02:00
|
|
|
package com.craftaro.ultimatestacker.stackable.item;
|
|
|
|
|
|
|
|
import com.craftaro.ultimatestacker.UltimateStacker;
|
|
|
|
import com.craftaro.ultimatestacker.api.stack.item.StackedItem;
|
|
|
|
import com.craftaro.ultimatestacker.settings.Settings;
|
|
|
|
import com.craftaro.ultimatestacker.utils.Methods;
|
2023-06-11 13:14:56 +02:00
|
|
|
import com.craftaro.core.utils.TextUtils;
|
2023-05-30 11:20:31 +02:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.entity.Item;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.metadata.FixedMetadataValue;
|
|
|
|
|
|
|
|
public class StackedItemImpl implements StackedItem {
|
|
|
|
|
|
|
|
private final Item item;
|
|
|
|
|
|
|
|
public StackedItemImpl(Item item) {
|
|
|
|
this.item = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
public StackedItemImpl(Item item, int amount) {
|
|
|
|
this.item = item;
|
|
|
|
setAmount(amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getAmount() {
|
|
|
|
ItemStack itemStack = item.getItemStack();
|
|
|
|
int amount = itemStack.getAmount();
|
|
|
|
if (item.hasMetadata("US_AMT")) {
|
|
|
|
return item.getMetadata("US_AMT").get(0).asInt();
|
|
|
|
} else {
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setAmount(int amount) {
|
|
|
|
updateItemAmount(item, amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void add(int amount) {
|
|
|
|
updateItemAmount(item, getAmount() + amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void take(int amount) {
|
|
|
|
//check that amount not go below 0
|
|
|
|
if (getAmount() - amount < 0) {
|
|
|
|
amount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateItemAmount(item, getAmount() - amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateItemAmount(Item item, int newAmount) {
|
|
|
|
updateItemAmount(item, item.getItemStack(), newAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the stacked amount for this item
|
|
|
|
*
|
|
|
|
* @param item item entity to update
|
|
|
|
* @param itemStack StackedItem that will represent this item
|
|
|
|
* @param newAmount number of items this item represents
|
|
|
|
*/
|
|
|
|
private void updateItemAmount(Item item, ItemStack itemStack, int newAmount) {
|
|
|
|
boolean blacklisted = UltimateStacker.isMaterialBlacklisted(itemStack);
|
|
|
|
|
|
|
|
if (newAmount > (itemStack.getMaxStackSize() / 2) && !blacklisted) {
|
|
|
|
itemStack.setAmount(Math.max(1, itemStack.getMaxStackSize() / 2));
|
|
|
|
} else {
|
|
|
|
itemStack.setAmount(newAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If amount is 0, Minecraft change the type to AIR
|
|
|
|
if (itemStack.getType() == Material.AIR) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateItemMeta(item, itemStack, newAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateItemMeta(Item item, ItemStack itemStack, int newAmount) {
|
|
|
|
Material material = itemStack.getType();
|
|
|
|
if (material == Material.AIR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
String name = TextUtils.convertToInvisibleString("IS") + Methods.compileItemName(itemStack, newAmount);
|
|
|
|
|
|
|
|
boolean blacklisted = UltimateStacker.isMaterialBlacklisted(itemStack);
|
|
|
|
|
|
|
|
if (newAmount > (itemStack.getMaxStackSize() / 2) && !blacklisted) {
|
|
|
|
item.setMetadata("US_AMT", new FixedMetadataValue(UltimateStacker.getInstance(), newAmount));
|
|
|
|
} else {
|
|
|
|
item.removeMetadata("US_AMT", UltimateStacker.getInstance());
|
|
|
|
}
|
|
|
|
item.setItemStack(itemStack);
|
|
|
|
|
|
|
|
if ((blacklisted && !Settings.ITEM_HOLOGRAM_BLACKLIST.getBoolean())
|
|
|
|
|| !UltimateStacker.getInstance().getItemFile().getBoolean("Items." + material + ".Has Hologram")
|
|
|
|
|| !Settings.ITEM_HOLOGRAMS.getBoolean()
|
|
|
|
|| newAmount < Settings.ITEM_MIN_HOLOGRAM_SIZE.getInt())
|
|
|
|
return;
|
|
|
|
|
|
|
|
item.setCustomName(name);
|
|
|
|
item.setCustomNameVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Item getItem() {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Item destroy() {
|
|
|
|
item.removeMetadata("US_AMT", UltimateStacker.getInstance());
|
|
|
|
item.setCustomName(null);
|
|
|
|
item.setCustomNameVisible(true);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|