Reduces the number of ItemStack#clone calls in inventory clicks

This commit is contained in:
themode 2020-10-22 19:18:08 +02:00
parent 419541b878
commit 34e7bfe69d
5 changed files with 25 additions and 27 deletions

View File

@ -5,7 +5,7 @@ import net.minestom.server.event.CancellableEvent;
import net.minestom.server.item.ItemStack;
/**
* Event when an item is used without clicking a block.
* Event when an item is used without clicking on a block.
*/
public class PlayerUseItemEvent extends CancellableEvent {

View File

@ -7,7 +7,7 @@ import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Direction;
/**
* Used when a player is clicking a block with an item (but is not a block in item form).
* Used when a player is clicking on a block with an item (but is not a block in item form).
*/
public class PlayerUseItemOnBlockEvent extends Event {

View File

@ -45,22 +45,18 @@ public class InventoryClickProcessor {
ItemStack resultClicked;
if (cursorRule.canBeStacked(cursor, clicked)) {
resultCursor = cursor.clone();
resultClicked = clicked.clone();
final int totalAmount = cursorRule.getAmount(cursor) + clickedRule.getAmount(clicked);
if (!clickedRule.canApply(resultClicked, totalAmount)) {
resultCursor = cursorRule.apply(resultCursor, totalAmount - cursorRule.getMaxSize());
resultClicked = clickedRule.apply(resultClicked, clickedRule.getMaxSize());
if (!clickedRule.canApply(clicked, totalAmount)) {
resultCursor = cursorRule.apply(cursor, totalAmount - cursorRule.getMaxSize());
resultClicked = clickedRule.apply(clicked, clickedRule.getMaxSize());
} else {
resultCursor = cursorRule.apply(resultCursor, 0);
resultClicked = clickedRule.apply(resultClicked, totalAmount);
resultCursor = cursorRule.apply(cursor, 0);
resultClicked = clickedRule.apply(clicked, totalAmount);
}
} else {
resultCursor = clicked.clone();
resultClicked = cursor.clone();
resultCursor = clicked;
resultClicked = cursor;
}
clickResult.setClicked(resultClicked);
@ -88,14 +84,12 @@ public class InventoryClickProcessor {
ItemStack resultClicked;
if (clickedRule.canBeStacked(clicked, cursor)) {
resultClicked = clicked.clone();
final int amount = clicked.getAmount() + 1;
if (!clickedRule.canApply(resultClicked, amount)) {
if (!clickedRule.canApply(clicked, amount)) {
return clickResult;
} else {
resultCursor = cursor.clone();
resultCursor = cursorRule.apply(resultCursor, cursorRule.getAmount(resultCursor) - 1);
resultClicked = clickedRule.apply(resultClicked, amount);
resultCursor = cursorRule.apply(cursor, cursorRule.getAmount(cursor) - 1);
resultClicked = clickedRule.apply(clicked, amount);
}
} else {
if (cursor.isAir()) {
@ -114,8 +108,8 @@ public class InventoryClickProcessor {
resultClicked = cursor.clone();
resultClicked = clickedRule.apply(resultClicked, 1);
} else {
resultCursor = clicked.clone();
resultClicked = cursor.clone();
resultCursor = clicked;
resultClicked = cursor;
}
}
}
@ -151,17 +145,17 @@ public class InventoryClickProcessor {
if (clicked.isAir()) {
// Set held item [key] to slot
resultClicked = cursor.clone();
resultClicked = cursor;
resultHeld = ItemStack.getAirItem();
} else {
if (cursor.isAir()) {
// if held item [key] is air then set clicked to held
resultClicked = ItemStack.getAirItem();
resultHeld = clicked.clone();
resultHeld = clicked;
} else {
// Otherwise replace held item and held
resultClicked = cursor.clone();
resultHeld = clicked.clone();
resultClicked = cursor;
resultHeld = clicked;
}
}
@ -296,7 +290,7 @@ public class InventoryClickProcessor {
slotItem = stackingRule.apply(slotItem, amount);
finalCursorAmount -= slotSize;
} else {
int removedAmount = amount - maxSize;
final int removedAmount = amount - maxSize;
slotItem = stackingRule.apply(slotItem, maxSize);
finalCursorAmount -= removedAmount;
}

View File

@ -5,7 +5,10 @@ import net.minestom.server.inventory.InventoryModifier;
import net.minestom.server.inventory.click.ClickType;
/**
* Can be added to any {@link InventoryModifier} in order to listen to any issued clicks.
* Can be added to any {@link InventoryModifier}
* using {@link net.minestom.server.inventory.Inventory#addInventoryCondition(InventoryCondition)}
* or {@link net.minestom.server.inventory.PlayerInventory#addInventoryCondition(InventoryCondition)}
* in order to listen to any issued clicks.
*/
@FunctionalInterface
public interface InventoryCondition {
@ -14,7 +17,7 @@ public interface InventoryCondition {
* Called when a {@link Player} clicks in the inventory where this {@link InventoryCondition} has been added to.
*
* @param player the player who clicked in the inventory
* @param slot the slot clicked
* @param slot the slot clicked, can be -999 if the click is out of the inventory
* @param clickType the click type
* @param inventoryConditionResult the result of this callback
*/

View File

@ -518,6 +518,7 @@ public class ItemStack implements DataContainer {
* @return a cloned item stack
*/
public synchronized ItemStack clone() {
System.out.println("clone");
ItemStack itemStack = new ItemStack(material, amount, damage);
itemStack.setDisplayName(displayName);
itemStack.setUnbreakable(unbreakable);