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 15 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
148 changes: 111 additions & 37 deletions src/main/java/ch/njol/skript/expressions/ExprMaxStack.java
Original file line number Diff line number Diff line change
@@ -1,57 +1,131 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.expressions;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.Event;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.Nullable;

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 org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import ch.njol.util.Math2;
import ch.njol.util.coll.CollectionUtils;

/**
* @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}"
})
@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");
}

@SuppressWarnings("null")

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) {
Object itemType = ((ItemType) source).getRandomStackOrMaterial();
if (itemType instanceof ItemStack)
return ((ItemStack) itemType).getMaxStackSize();
return ((Material) itemType).getMaxStackSize();
} else if (source instanceof Inventory) {
return (((Inventory) source).getMaxStackSize());
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
} else {
// Invalid source
return null;
}
}

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

@Override
public Long convert(ItemType itemType) {
Object random = itemType.getRandomStackOrMaterial();
if (random instanceof Material)
return (long) ((Material) random).getMaxStackSize();
return (long) ((ItemStack) random).getMaxStackSize();
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
for (Object source : getExpr().getArray(event)) {
Integer change = null;
if (mode != ChangeMode.RESET)
change = (int) delta[0];
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
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 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
62 changes: 61 additions & 1 deletion src/test/skript/tests/syntaxes/expressions/ExprMaxStack.sk
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
test "max stack":
test "max stack - itemtype":
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"

# edge case
assert max stack size of {_null} is not set with "max stack size of non itemtype expected to fail ##1"
assert max stack size of {_null} is 0 to fail with "max stack size of non itemtype expected to fail ##2"
assert max stack size of diamond and diamond sword is 64 and 1 with "max stack size of itemtypes 'and' case failed"
assert max stack size of diamond or diamond sword is 64 or 1 with "max stack size of itemtypes 'or' case failed"
loop any log and any wool:
assert max stack size of loop-value is 64 with "max stack size of category itemtypes (%loop-value%) 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"

# edge case
loop any log and any wool:
set {_edge} to loop-value
set max stack size of {_edge} to 1
assert max stack size of {_edge} is 1 with "max stack size override of category itemtypes (%{_edge}%) failed"

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}%"