Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changeable Max Stack Size for Items and Inventory #6698

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d54b217
Changeable max stack size for item and inventory
NotSoDelayed May 14, 2024
1c823c2
Changeable max stack size for item and inventory
NotSoDelayed May 15, 2024
af605cd
Fix required plugins versioning
NotSoDelayed May 15, 2024
9cb9a62
Fix human error and removing redundant debug message
NotSoDelayed May 15, 2024
9e58338
Fix breaking change to inventory max stack size in 1.20.5
NotSoDelayed May 15, 2024
ecd7720
Requested changes
NotSoDelayed May 31, 2024
92ad57f
Merge branch 'dev/feature' into feature/max-stack-size-api
NotSoDelayed Jul 1, 2024
21401d0
Annotation correction
NotSoDelayed Jul 1, 2024
7c924d3
More tests
NotSoDelayed Jul 1, 2024
eb3ed68
Check for ItemType#getRandom() nullability
NotSoDelayed Jul 17, 2024
cf08878
Merge remote-tracking branch 'upstream/dev/feature' into feature/max-…
NotSoDelayed Jul 17, 2024
93fb7ed
Check for ItemType#getRandom() nullability
NotSoDelayed Jul 17, 2024
75f6549
Remove license header (#6684)
NotSoDelayed Jul 19, 2024
05ff733
Merge branch 'dev/feature' into feature/max-stack-size-api
NotSoDelayed Jul 20, 2024
6238428
Merge branch 'dev/feature' into feature/max-stack-size-api
Moderocky Sep 13, 2024
f4f79a9
Java 17 and some code optimisations
NotSoDelayed Sep 16, 2024
d381b90
Requested changes
NotSoDelayed Sep 17, 2024
45f7f2a
Improve examples
NotSoDelayed Sep 17, 2024
49439d1
Requested changes
NotSoDelayed Sep 17, 2024
c9b0d50
Minor logic change for changer
NotSoDelayed Oct 13, 2024
9b850f5
Merge branch 'dev/feature' into feature/max-stack-size-api
NotSoDelayed Oct 13, 2024
56fb787
Merge branch 'dev/feature' into feature/max-stack-size-api
NotSoDelayed Nov 16, 2024
cc29c0e
chezburger simplified
NotSoDelayed Nov 16, 2024
5f388d0
Merge branch 'dev/feature' into feature/max-stack-size-api
sovdeeth Nov 23, 2024
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
124 changes: 110 additions & 14 deletions src/main/java/ch/njol/skript/expressions/ExprMaxStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,131 @@
*/
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.RequiredPlugins;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.util.Math2;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.Nullable;

/**
* @author joeuguce99
*/
@Name("Maximum Stack Size")
@Description("The maximum stack size of the specified material, e.g. 64 for torches, 16 for buckets, and 1 for swords.")
@Examples("send \"You can only pick up %max stack size of player's tool% of %type of (player's tool)%\" to player")
@Since("2.1")
public class ExprMaxStack extends SimplePropertyExpression<ItemType, Long> {
@Description({
"The maximum stack size of an item (e.g. 64 for torches, 16 for buckets, and 1 for swords), or of an inventory to have items to stack up to.",
"Since MC 1.20.5 onwards, the maximum stack size of items can be changed (any integer from 1 to 99), and the said item can be stacked up to the set value, up to the maximum stack size of an inventory."
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
})
@Examples({
"send \"You can only pick up %max stack size of player's tool% of %type of (player's tool)%\" to player",
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
"",
"set the maximum stack size of inventory of all players to 16",
"",
"add 8 to the maximum stack size of player's tool",
"",
"reset the maximum stack size of {_gui}"
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
})
@Since("2.1, INSERT VERSION (changeable, inventory support)")
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
@RequiredPlugins("Spigot 1.20.5+ (changeable item max stack size)")
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
public class ExprMaxStack extends SimplePropertyExpression<Object, Integer> {

static {
register(ExprMaxStack.class, Long.class, "max[imum] stack[[ ]size]", "itemtype");
register(ExprMaxStack.class, Integer.class, "max[imum] stack[[ ]size]", "itemtypes/inventories");
}

private static final boolean CHANGEABLE_ITEM_STACK_SIZE = Skript.methodExists(ItemMeta.class, "setMaxStackSize", Integer.class);

@Override
@Nullable
public Integer convert(Object source) {
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
if (source instanceof ItemType) {
return ((ItemType) source).getRandom().getMaxStackSize();
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
} else if (source instanceof Inventory) {
return (((Inventory) source).getMaxStackSize());
} else {
// Invalid source -- return null
return null;
}
}

@SuppressWarnings("null")

@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
switch (mode) {
case ADD:
case DELETE:
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
case REMOVE:
case RESET:
case SET:
if (!CHANGEABLE_ITEM_STACK_SIZE && ItemType.class.isAssignableFrom(getExpr().getReturnType())) {
Skript.error("Changers for maximum stack size of items requires Minecraft 1.20.5 or newer");
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
return CollectionUtils.array(Integer.class);
default:
return null;
}
}

@Override
public Long convert(final ItemType i) {
return (long) i.getRandom().getMaxStackSize();
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
for (Object source : getExpr().getArray(event)) {
Integer change = null;
if (mode != ChangeMode.DELETE && mode != ChangeMode.RESET)
change = (int) delta[0];
if (source instanceof ItemType) {
if (!CHANGEABLE_ITEM_STACK_SIZE)
continue;
ItemType itemType = ((ItemType) source);
int size = itemType.getRandom().getMaxStackSize();
switch (mode) {
case ADD:
size += change;
break;
case SET:
size = change;
break;
case REMOVE:
size -= change;
break;
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
}
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
ItemMeta meta = itemType.getItemMeta();
// Minecraft only accepts stack size from 1 to 99
meta.setMaxStackSize(change != null ? Math2.fit(1, size, 99) : null);
itemType.setItemMeta(meta);
} else if (source instanceof Inventory) {
Inventory inv = ((Inventory) source);
int size = inv.getMaxStackSize();
switch (mode) {
case ADD:
size += change;
break;
case SET:
size = change;
break;
case REMOVE:
size -= change;
break;
case DELETE:
case RESET:
size = Bukkit.createInventory(null, inv.getType()).getMaxStackSize();
break;
}
inv.setMaxStackSize(size);
}
}
}

@Override
public Class<? extends Long> getReturnType() {
return Long.class;
public Class<? extends Integer> getReturnType() {
return Integer.class;
}

@Override
Expand Down
46 changes: 46 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprMaxStack.sk
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,49 @@ test "max stack":
assert max stack size of diamond sword is 1 with "diamond sword max stack size failed"
assert max stack size of bucket is 16 with "bucket max stack size failed"
assert max stack size of dirt is 64 with "dirt max stack size failed"

test "max stack override - itemtype" when running minecraft "1.20.5":
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
set {_item} to diamond
set max stack size of {_item} to 32
assert max stack size of {_item} is 32 with "diamond should have max stack size of 32"
add 2 to max stack size of {_item}
assert max stack size of {_item} is 34 with "diamond should have max stack size of 34"
remove 4 from max stack size of {_item}
assert max stack size of {_item} is 30 with "diamond should have max stack size of 30"
reset max stack size of {_item}
assert max stack size of {_item} is 64 with "diamond should have max stack size of 64"

set {_item} to bucket
set max stack size of {_item} to 24
assert max stack size of {_item} is 24 with "bucket should have max stack size of 24"
add 2 to max stack size of {_item}
assert max stack size of {_item} is 26 with "bucket should have max stack size of 26"
remove 4 from max stack size of {_item}
assert max stack size of {_item} is 22 with "bucket should have max stack size of 22"
reset max stack size of {_item}
assert max stack size of {_item} is 16 with "bucket should have max stack size of 16"

set {_item} to diamond sword
set max stack size of {_item} to 16
assert max stack size of {_item} is 16 with "diamond sword should have max stack size of 16"
add 2 to max stack size of {_item}
assert max stack size of {_item} is 18 with "diamond sword should have max stack size of 18"
remove 4 from max stack size of {_item}
assert max stack size of {_item} is 14 with "diamond sword should have max stack size of 14"
reset max stack size of {_item}
assert max stack size of {_item} is 1 with "diamond sword should have max stack size of 1"

test "max stack override - inventory":
set {_default} to 99 # 1.20.5 and newer
if running below minecraft "1.20.5":
set {_default} to 64
set {_inv} to a new chest inventory with 1 rows
assert max stack size of {_inv} is {_default} with "inventory should have max stack size of %{_default}%"
set max stack size of {_inv} to 32
assert max stack size of {_inv} is 32 with "inventory should have max stack size of 32"
add 2 to max stack size of {_inv}
assert max stack size of {_inv} is 34 with "inventory should have max stack size of 34"
remove 4 from max stack size of {_inv}
assert max stack size of {_inv} is 30 with "inventory should have max stack size of 30"
reset max stack size of {_inv}
assert max stack size of {_inv} is {_default} with "inventory should have factory max stack size of %{_default}%"