Skip to content

Commit aaa49cb

Browse files
Copilotjpenilla
andauthored
Add 26.1-compatible ItemInput handling for Bukkit ItemStack parsing (#143)
* Initial plan * fix: support 26.1 item stack signature Co-authored-by: jpenilla <11360596+jpenilla@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jpenilla <11360596+jpenilla@users.noreply.github.com>
1 parent 6b10a98 commit aaa49cb

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

cloud-bukkit/src/main/java/org/incendo/cloud/bukkit/data/ProtoItemStack.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ public interface ProtoItemStack {
5353
/**
5454
* Create a new {@link ItemStack} from the state of this {@link ProtoItemStack}.
5555
*
56-
* @param stackSize stack size
57-
* @param respectMaximumStackSize whether to respect the maximum stack size for the material
56+
* @param stackSize stack size
5857
* @return the created {@link ItemStack}
5958
* @throws IllegalArgumentException if the {@link ItemStack} could not be created, due to max stack size or other reasons
6059
* @since 1.5.0
6160
*/
62-
@NonNull ItemStack createItemStack(int stackSize, boolean respectMaximumStackSize)
63-
throws IllegalArgumentException;
61+
@NonNull ItemStack createItemStack(int stackSize) throws IllegalArgumentException;
6462
}

cloud-bukkit/src/main/java/org/incendo/cloud/bukkit/parser/ItemStackParser.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ private static final class ModernParser<C> implements ArgumentParser.FutureArgum
158158
private static final Method CREATE_ITEM_STACK_METHOD = CraftBukkitReflection.firstNonNullOrThrow(
159159
() -> "Couldn't find createItemStack method on ItemInput",
160160
CraftBukkitReflection.findMethod(ITEM_INPUT_CLASS, "a", int.class, boolean.class),
161-
CraftBukkitReflection.findMethod(ITEM_INPUT_CLASS, "createItemStack", int.class, boolean.class)
161+
CraftBukkitReflection.findMethod(ITEM_INPUT_CLASS, "createItemStack", int.class, boolean.class),
162+
CraftBukkitReflection.findMethod(ITEM_INPUT_CLASS, "createItemStack", int.class)
162163
);
163164
private static final Method AS_BUKKIT_COPY_METHOD = CraftBukkitReflection
164165
.needMethod(CRAFT_ITEM_STACK_CLASS, "asBukkitCopy", NMS_ITEM_STACK_CLASS);
@@ -272,11 +273,14 @@ public boolean hasExtraData() {
272273
}
273274

274275
@Override
275-
public @NonNull ItemStack createItemStack(final int stackSize, final boolean respectMaximumStackSize) {
276+
public @NonNull ItemStack createItemStack(final int stackSize) {
276277
try {
278+
final Object nmsItemStack = CREATE_ITEM_STACK_METHOD.getParameterCount() == 1
279+
? CREATE_ITEM_STACK_METHOD.invoke(this.itemInput, stackSize)
280+
: CREATE_ITEM_STACK_METHOD.invoke(this.itemInput, stackSize, true);
277281
return (ItemStack) AS_BUKKIT_COPY_METHOD.invoke(
278282
null,
279-
CREATE_ITEM_STACK_METHOD.invoke(this.itemInput, stackSize, respectMaximumStackSize)
283+
nmsItemStack
280284
);
281285
} catch (final InvocationTargetException ex) {
282286
final Throwable cause = ex.getCause();
@@ -333,9 +337,8 @@ public boolean hasExtraData() {
333337
}
334338

335339
@Override
336-
public @NonNull ItemStack createItemStack(final int stackSize, final boolean respectMaximumStackSize)
337-
throws IllegalArgumentException {
338-
if (respectMaximumStackSize && stackSize > this.material.getMaxStackSize()) {
340+
public @NonNull ItemStack createItemStack(final int stackSize) throws IllegalArgumentException {
341+
if (stackSize > this.material.getMaxStackSize()) {
339342
throw new IllegalArgumentException(String.format(
340343
"The maximum stack size for %s is %d",
341344
this.material,

examples/example-bukkit/src/main/java/org/incendo/cloud/examples/bukkit/builder/feature/minecraft/ItemStackExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void registerFeature(
5858
"amount", integerParser(),
5959
(sender, proto, amount) -> {
6060
try {
61-
return ArgumentParseResult.successFuture(proto.createItemStack(amount, true));
61+
return ArgumentParseResult.successFuture(proto.createItemStack(amount));
6262
} catch (final IllegalArgumentException ex) {
6363
return ArgumentParseResult.failureFuture(ex);
6464
}

examples/example-bukkit/src/main/java/org/incendo/cloud/examples/bukkit/builder/feature/minecraft/ItemStackPredicateExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ private void executeTestItem(final @NonNull CommandContext<CommandSender> ctx) {
5959
final ProtoItemStack protoItemStack = ctx.get("item");
6060
final ItemStackPredicate predicate = ctx.get("predicate");
6161
ctx.sender().sendMessage("result: " + predicate.test(
62-
protoItemStack.createItemStack(1, true)));
62+
protoItemStack.createItemStack(1)));
6363
}
6464
}

0 commit comments

Comments
 (0)