Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private String buildLootItemsText(EntityType entityType, Map<ItemSignature, Long
// Create material-to-amount map for quick lookups
Map<Material, Long> materialAmountMap = new HashMap<>();
for (Map.Entry<ItemSignature, Long> entry : storedItems.entrySet()) {
Material material = entry.getKey().getTemplateRef().getType();
Material material = entry.getKey().getMaterial();
materialAmountMap.merge(material, entry.getValue(), Long::sum);
}

Expand Down Expand Up @@ -333,8 +333,7 @@ private String buildLootItemsText(EntityType entityType, Map<ItemSignature, Long
sortedItems.sort(Comparator.comparing(e -> e.getKey().getMaterialName()));

for (Map.Entry<ItemSignature, Long> entry : sortedItems) {
ItemStack templateItem = entry.getKey().getTemplateRef();
Material material = templateItem.getType();
Material material = entry.getKey().getMaterial();
long amount = entry.getValue();

String materialName = languageManager.getVanillaItemName(material);
Expand Down Expand Up @@ -606,7 +605,7 @@ private int calculatePercentage(long current, long maximum) {
private List<Component> buildLootItemComponents(EntityType entityType, Map<ItemSignature, Long> storedItems) {
Map<Material, Long> materialAmountMap = new HashMap<>();
for (Map.Entry<ItemSignature, Long> entry : storedItems.entrySet()) {
Material material = entry.getKey().getTemplateRef().getType();
Material material = entry.getKey().getMaterial();
materialAmountMap.merge(material, entry.getValue(), Long::sum);
}

Expand All @@ -633,7 +632,7 @@ private List<Component> buildLootItemComponents(EntityType entityType, Map<ItemS
new ArrayList<>(storedItems.entrySet());
sortedItems.sort(Comparator.comparing(e -> e.getKey().getMaterialName()));
for (Map.Entry<ItemSignature, Long> entry : sortedItems) {
Material material = entry.getKey().getTemplateRef().getType();
Material material = entry.getKey().getMaterial();
long amount = entry.getValue();
String formattedAmount = languageManager.formatNumber(amount);
components.add(languageManager.buildTranslatableGuiLootLine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private ItemStack createSpawnerInfoButton(Player player, SpawnerData spawner, Ma
private List<Component> buildSellInfoLootComponents(SpawnerData spawner, Map<ItemSignature, Long> storedItems) {
Map<Material, Long> materialAmountMap = new HashMap<>();
for (Map.Entry<ItemSignature, Long> entry : storedItems.entrySet()) {
Material material = entry.getKey().getTemplateRef().getType();
Material material = entry.getKey().getMaterial();
materialAmountMap.merge(material, entry.getValue(), Long::sum);
}

Expand All @@ -230,7 +230,7 @@ private List<Component> buildSellInfoLootComponents(SpawnerData spawner, Map<Ite
List<Map.Entry<ItemSignature, Long>> sortedItems = new ArrayList<>(storedItems.entrySet());
sortedItems.sort(Comparator.comparing(e -> e.getKey().getMaterialName()));
for (Map.Entry<ItemSignature, Long> entry : sortedItems) {
Material material = entry.getKey().getTemplateRef().getType();
Material material = entry.getKey().getMaterial();
long amount = entry.getValue();
String formattedAmount = languageManager.formatNumber(amount);
components.add(languageManager.buildTranslatableGuiLootLine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ private List<Component> buildStorageInfoLootComponents(SpawnerData spawner,
Map<ItemSignature, Long> storedItems) {
Map<Material, Long> materialAmountMap = new HashMap<>();
for (Map.Entry<ItemSignature, Long> entry : storedItems.entrySet()) {
Material mat = entry.getKey().getTemplateRef().getType();
Material mat = entry.getKey().getMaterial();
materialAmountMap.merge(mat, entry.getValue(), Long::sum);
}

Expand Down Expand Up @@ -622,7 +622,7 @@ private List<Component> buildStorageInfoLootComponents(SpawnerData spawner,
List<Map.Entry<ItemSignature, Long>> sortedItems = new ArrayList<>(storedItems.entrySet());
sortedItems.sort(Comparator.comparing(e -> e.getKey().getMaterialName()));
for (Map.Entry<ItemSignature, Long> entry : sortedItems) {
Material mat = entry.getKey().getTemplateRef().getType();
Material mat = entry.getKey().getMaterial();
long amount = entry.getValue();
String formattedAmount = languageManager.formatNumber(amount);
components.add(languageManager.buildTranslatableGuiLootLine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private int calculateSlots(Map<ItemSignature, Long> items) {
return items.entrySet().stream()
.mapToInt(entry -> {
long amount = entry.getValue();
int maxStackSize = entry.getKey().getTemplateRef().getMaxStackSize();
int maxStackSize = entry.getKey().getMaxStackSize();
// Use integer division with ceiling function
return (int) ((amount + maxStackSize - 1) / maxStackSize);
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github.nighter.smartspawner.spawner.properties;

import lombok.Getter;
import lombok.experimental.Accessors;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
Expand All @@ -13,11 +14,10 @@ public class ItemSignature {
@Getter private final Material material;
@Getter private final int maxStackSize;
@Getter private final int damage;
@Getter private final boolean hasItemMeta;
@Getter @Accessors(fluent = true) private final boolean hasItemMeta;

public ItemSignature(ItemStack item) {
this.template = item.clone();
this.template.setAmount(1);
this.template = item.asQuantity(1); // Clone with new amount
this.material = template.getType();
this.maxStackSize = template.getMaxStackSize();

Expand Down Expand Up @@ -77,7 +77,7 @@ public ItemStack getTemplate() {
}

// Non-cloning method for internal use
public ItemStack getTemplateRef() {
public ItemStack getUnsafeTemplateRef() {
return template;
}

Expand All @@ -92,4 +92,4 @@ private int extractDamage(ItemMeta meta) {
return 0;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,9 @@ public void incrementSellValue(Map<ItemSignature, Long> itemsAdded,

double addedValue = 0.0;
for (Map.Entry<ItemSignature, Long> entry : itemsAdded.entrySet()) {
// Use getTemplateRef() to avoid cloning - we only need to read properties
ItemStack template = entry.getKey().getTemplateRef();
long amount = entry.getValue();
double itemPrice = findItemPrice(template, priceCache);
double itemPrice = findItemPrice(entry.getKey(), priceCache);
if (itemPrice > 0.0) {
addedValue += itemPrice * amount;
addedValue += itemPrice * entry.getValue();
}
}

Expand Down Expand Up @@ -599,12 +596,9 @@ public void decrementSellValue(List<ItemStack> itemsRemoved, Map<String, Double>

double removedValue = 0.0;
for (Map.Entry<ItemSignature, Long> entry : consolidated.entrySet()) {
// Use getTemplateRef() to avoid cloning - we only need to read properties
ItemStack template = entry.getKey().getTemplateRef();
long amount = entry.getValue();
double itemPrice = findItemPrice(template, priceCache);
double itemPrice = findItemPrice(entry.getKey(), priceCache);
if (itemPrice > 0.0) {
removedValue += itemPrice * amount;
removedValue += itemPrice * entry.getValue();
}
}

Expand All @@ -630,12 +624,9 @@ public void recalculateSellValue() {
double totalValue = 0.0;

for (Map.Entry<ItemSignature, Long> entry : items.entrySet()) {
// Use getTemplateRef() to avoid cloning - we only need to read properties
ItemStack template = entry.getKey().getTemplateRef();
long amount = entry.getValue();
double itemPrice = findItemPrice(template, priceCache);
double itemPrice = findItemPrice(entry.getKey(), priceCache);
if (itemPrice > 0.0) {
totalValue += itemPrice * amount;
totalValue += itemPrice * entry.getValue();
}
}

Expand Down Expand Up @@ -678,45 +669,51 @@ public Map<String, Double> createPriceCache() {
/**
* Finds item price using the cache
*/
private double findItemPrice(ItemStack item, Map<String, Double> priceCache) {
if (item == null || priceCache == null) {
private double findItemPrice(ItemSignature itemSignature, Map<String, Double> priceCache) {
if (priceCache == null) {
return 0.0;
}
String itemKey = createItemKey(item);
String itemKey = createItemKey(itemSignature);
Double price = priceCache.get(itemKey);
return price != null ? price : 0.0;
}

/**
* Creates a unique key for an item (same logic as SpawnerSellManager)
* Convenience overload
*/
private String createItemKey(ItemStack item) {
if (item == null) {
return "null";
}
private String createItemKey(ItemStack itemStack) {
if (itemStack == null) return "null";

return createItemKey(new ItemSignature(itemStack));
}

/**
* Creates a unique key for an item (same logic as SpawnerSellManager)
* TODO: this feels very wrong ngl
*/
private String createItemKey(ItemSignature itemSignature) {
StringBuilder key = new StringBuilder();
key.append(item.getType().name());
key.append(itemSignature.getMaterial().name());

// Add enchantments if present
if (item.hasItemMeta() && item.getItemMeta().hasEnchants()) {
ItemMeta meta = itemSignature.getUnsafeTemplateRef().getItemMeta(); // Read-only
if (itemSignature.hasItemMeta() && meta.hasEnchants()) {
key.append("_enchants:");
item.getItemMeta().getEnchants().entrySet().stream()
meta.getEnchants().entrySet().stream()
.sorted(java.util.Map.Entry.comparingByKey(java.util.Comparator.comparing(enchantment -> enchantment.getKey().toString())))
.forEach(entry -> key.append(entry.getKey().getKey()).append(":").append(entry.getValue()).append(","));
}

// Add custom model data if present
if (item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
if (itemSignature.hasItemMeta()) {
if (VersionInitializer.hasCustomModelData(meta)) {
key.append("_cmd:").append(VersionInitializer.getCustomModelDataString(meta));
}
}

// Add display name if present
if (item.hasItemMeta() && item.getItemMeta().hasDisplayName()) {
key.append("_name:").append(item.getItemMeta().displayName());
if (itemSignature.hasItemMeta() && meta.hasDisplayName()) {
key.append("_name:").append(meta.displayName());
}

return key.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public Map<Integer, ItemStack> getDisplayInventory() {
if (preferredSortMaterial != null) {
sortedEntriesCache.sort((e1, e2) -> {
// Use getTemplateRef() to avoid cloning - we only need to read the type
boolean e1Preferred = e1.getKey().getTemplateRef().getType() == preferredSortMaterial;
boolean e2Preferred = e2.getKey().getTemplateRef().getType() == preferredSortMaterial;
boolean e1Preferred = e1.getKey().getMaterial() == preferredSortMaterial;
boolean e2Preferred = e2.getKey().getMaterial() == preferredSortMaterial;

if (e1Preferred && !e2Preferred) return -1;
if (!e1Preferred && e2Preferred) return 1;
Expand All @@ -153,15 +153,14 @@ public Map<Integer, ItemStack> getDisplayInventory() {

ItemSignature sig = entry.getKey();
long totalAmount = entry.getValue();
ItemStack templateItem = sig.getTemplateRef();
int maxStackSize = templateItem.getMaxStackSize();
int maxStackSize = sig.getMaxStackSize();

// Create as many stacks as needed for this item type
while (totalAmount > 0 && currentSlot < maxSlots) {
int stackSize = (int) Math.min(totalAmount, maxStackSize);

// Create the display item only once per slot
ItemStack displayItem = templateItem.clone();
ItemStack displayItem = sig.getTemplate();
displayItem.setAmount(stackSize);

// Store in cache
Expand Down Expand Up @@ -203,7 +202,7 @@ public int getUsedSlots() {
int estimatedSlots = 0;
for (Map.Entry<ItemSignature, Long> entry : consolidatedItems.entrySet()) {
long amount = entry.getValue();
int maxStackSize = entry.getKey().getTemplateRef().getMaxStackSize();
int maxStackSize = entry.getKey().getMaxStackSize();
estimatedSlots += (int) Math.ceil((double) amount / maxStackSize);
if (estimatedSlots >= maxSlots) {
return maxSlots; // Cap at max slots
Expand Down Expand Up @@ -250,8 +249,8 @@ public void sortItems(org.bukkit.Material preferredMaterial) {
this.sortedEntriesCache = consolidatedItems.entrySet().stream()
.sorted((e1, e2) -> {
// Use getTemplateRef() to avoid cloning - we only need to read the type
boolean e1Preferred = e1.getKey().getTemplateRef().getType() == preferredMaterial;
boolean e2Preferred = e2.getKey().getTemplateRef().getType() == preferredMaterial;
boolean e1Preferred = e1.getKey().getMaterial() == preferredMaterial;
boolean e2Preferred = e2.getKey().getMaterial() == preferredMaterial;

if (e1Preferred && !e2Preferred) return -1;
if (!e1Preferred && e2Preferred) return 1;
Expand Down Expand Up @@ -296,4 +295,4 @@ public void resize(int newMaxSlots) {
// but they won't be accessible in the display
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ private SellResult calculateSellValue(Map<ItemSignature, Long> consolidatedItems
ArrayList<ItemStack> itemsToRemove = new ArrayList<>();

for (Map.Entry<ItemSignature, Long> entry : consolidatedItems.entrySet()) {
ItemStack templateRef = entry.getKey().getTemplateRef();
ItemSignature signature = entry.getKey();
long amount = entry.getValue();
int maxStackSize = templateRef.getMaxStackSize();
int maxStackSize = signature.getMaxStackSize();

totalItemsSold += amount;

Expand All @@ -199,7 +199,7 @@ private SellResult calculateSellValue(Map<ItemSignature, Long> consolidatedItems

long remaining = amount;
while (remaining > 0) {
ItemStack stack = templateRef.clone();
ItemStack stack = signature.getTemplate();
stack.setAmount((int) Math.min(remaining, maxStackSize));
itemsToRemove.add(stack);
remaining -= stack.getAmount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public static List<String> serializeInventory(Map<ItemSignature, Long> items) {

for (Map.Entry<ItemSignature, Long> entry : items.entrySet()) {
// Use getTemplateRef() to avoid cloning - we only need to read properties
ItemStack template = entry.getKey().getTemplateRef();
Material material = template.getType();
ItemSignature signature = entry.getKey();
Material material = signature.getMaterial();
ItemGroup group = groupedItems.computeIfAbsent(material, ItemGroup::new);

if (material == Material.TIPPED_ARROW) {
PotionMeta meta = (PotionMeta) template.getItemMeta();
PotionMeta meta = (PotionMeta) signature.getUnsafeTemplateRef().getItemMeta(); // Read-only
if (meta != null && meta.getBasePotionType() != null) {
group.addPotionArrow(meta.getBasePotionType(), entry.getValue().intValue());
} else {
Expand All @@ -57,7 +57,7 @@ public static List<String> serializeInventory(Map<ItemSignature, Long> items) {
}
} else if (isDestructibleItem(material)) {
// Use modern damage system instead of durability
int damage = getDamageValue(template);
int damage = signature.getDamage();
group.addItem(damage, entry.getValue().intValue());
} else {
// For non-destructible items, always use damage 0
Expand Down Expand Up @@ -157,16 +157,6 @@ public static Map<ItemStack, Integer> deserializeInventory(List<String> data) {
return result;
}

/**
* Get damage value from ItemStack using modern API
*/
private static int getDamageValue(ItemStack item) {
if (item.getItemMeta() instanceof Damageable damageable) {
return damageable.getDamage();
}
return 0;
}

/**
* Set damage value to ItemStack using modern API
*/
Expand Down Expand Up @@ -209,4 +199,4 @@ public static boolean isDestructibleItem(Material material) {
|| name.equals("WARPED_FUNGUS_ON_A_STICK")
|| name.equals("MACE");
}
}
}
Loading