From a021f851dc656f2d4f2d35acd1d20fc62f21ac58 Mon Sep 17 00:00:00 2001 From: JohanVonElectrum Date: Mon, 28 Jun 2021 18:23:11 +0200 Subject: [PATCH] Items stuff + allStack + stack size command --- gradle.properties | 2 +- .../johan_carpet/JohanExtension.java | 3 +- .../johan_carpet/JohanSettings.java | 18 +++++- .../johan_carpet/commands/CommandItem.java | 58 +++++++++++++++++++ .../johan_carpet/mixins/BucketItemMixin.java | 2 +- .../johan_carpet/mixins/ItemMixin.java | 19 ++++++ .../johan_carpet/mixins/ItemsMixin.java | 25 ++++++++ .../johan_carpet/utils/HttpHelper.java | 1 + src/main/resources/johan_carpet.mixins.json | 2 + 9 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/johanvonelectrum/johan_carpet/commands/CommandItem.java create mode 100644 src/main/java/com/johanvonelectrum/johan_carpet/mixins/ItemMixin.java create mode 100644 src/main/java/com/johanvonelectrum/johan_carpet/mixins/ItemsMixin.java diff --git a/gradle.properties b/gradle.properties index 9de9555..613987d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -10,7 +10,7 @@ org.gradle.jvmargs=-Xmx1G carpet_core_version=1.4.16+v201105 # Mod Properties - mod_version = 2021.6.8 + mod_version = 2021.6.28 maven_group = com.johanvonelectrum archives_base_name = johan-carpet diff --git a/src/main/java/com/johanvonelectrum/johan_carpet/JohanExtension.java b/src/main/java/com/johanvonelectrum/johan_carpet/JohanExtension.java index a959322..3f8b3fc 100644 --- a/src/main/java/com/johanvonelectrum/johan_carpet/JohanExtension.java +++ b/src/main/java/com/johanvonelectrum/johan_carpet/JohanExtension.java @@ -13,7 +13,7 @@ public class JohanExtension implements CarpetExtension { public static final String MOD_NAME = "Johan Carpet Addon"; public static final String MOD_ID = "johan-carpet"; - public static final String VERSION = "2021.6.8"; + public static final String VERSION = "2021.6.28"; public static void noop() {} @@ -35,6 +35,7 @@ public void registerCommands(CommandDispatcher dispatcher) CommandSignal.register(dispatcher); CommandComputation.register(dispatcher); CommandBatch.register(dispatcher); + CommandItem.register(dispatcher); } private static Instant lastUpdateCheck = Instant.MIN; diff --git a/src/main/java/com/johanvonelectrum/johan_carpet/JohanSettings.java b/src/main/java/com/johanvonelectrum/johan_carpet/JohanSettings.java index 23bc3b0..090f7c0 100644 --- a/src/main/java/com/johanvonelectrum/johan_carpet/JohanSettings.java +++ b/src/main/java/com/johanvonelectrum/johan_carpet/JohanSettings.java @@ -83,7 +83,7 @@ public String validate(ServerCommandSource serverCommandSource, ParsedRule> ITEMS = new HashMap<>(); + + public static void register(CommandDispatcher dispatcher) { + LiteralArgumentBuilder literalargumentbuilder = literal("item") + .requires((player) -> JohanSettings.commandItem) + .then(argument("stack_size", IntegerArgumentType.integer(1, 64)) + .executes(context -> getItemCount(context.getSource(), IntegerArgumentType.getInteger(context, "stack_size"), false)) + .then(argument("list", BoolArgumentType.bool()) + .executes(context -> getItemCount(context.getSource(), IntegerArgumentType.getInteger(context, "stack_size"), BoolArgumentType.getBool(context, "list"))) + ) + ); + + dispatcher.register(literalargumentbuilder); + } + + private static int getItemCount(ServerCommandSource source, int stackSize, boolean list) throws CommandSyntaxException { + List items = ITEMS.get(stackSize); + if (items != null) { + source.sendFeedback(new LiteralText("There are " + items.size() + " items whose stack size is " + stackSize + "."), false); + + if (!list) + return 1; + + String result = items.stream() + .map(n -> Registry.ITEM.getId(n).toString()) + .collect(Collectors.joining("\n", "", "")); + source.sendFeedback(new LiteralText(result), false); + return 0; + } else + source.sendError(new LiteralText("There are no items whose stack size is " + stackSize + ".")); + + return 0; + } + +} diff --git a/src/main/java/com/johanvonelectrum/johan_carpet/mixins/BucketItemMixin.java b/src/main/java/com/johanvonelectrum/johan_carpet/mixins/BucketItemMixin.java index 321f16c..8b388d8 100644 --- a/src/main/java/com/johanvonelectrum/johan_carpet/mixins/BucketItemMixin.java +++ b/src/main/java/com/johanvonelectrum/johan_carpet/mixins/BucketItemMixin.java @@ -28,7 +28,7 @@ public class BucketItemMixin { @Shadow @Final private Fluid fluid; - @Inject(method = "use", at = @At("HEAD"), cancellable = true) + @Inject(method = "use", at = @At("INVOKE"), cancellable = true) private void use(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable> cir) { ItemStack itemStack = user.getStackInHand(hand); boolean sponge = JohanSettings.bucketSponge && diff --git a/src/main/java/com/johanvonelectrum/johan_carpet/mixins/ItemMixin.java b/src/main/java/com/johanvonelectrum/johan_carpet/mixins/ItemMixin.java new file mode 100644 index 0000000..a1aa3f7 --- /dev/null +++ b/src/main/java/com/johanvonelectrum/johan_carpet/mixins/ItemMixin.java @@ -0,0 +1,19 @@ +package com.johanvonelectrum.johan_carpet.mixins; + +import com.johanvonelectrum.johan_carpet.JohanSettings; +import net.minecraft.item.Item; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(Item.class) +public class ItemMixin { + + @Inject(method = "getMaxCount", at=@At("HEAD"), cancellable = true) + public final void getMaxCount(CallbackInfoReturnable cir) { + if (JohanSettings.allStackable) + cir.setReturnValue(64); + } + +} diff --git a/src/main/java/com/johanvonelectrum/johan_carpet/mixins/ItemsMixin.java b/src/main/java/com/johanvonelectrum/johan_carpet/mixins/ItemsMixin.java new file mode 100644 index 0000000..9c91074 --- /dev/null +++ b/src/main/java/com/johanvonelectrum/johan_carpet/mixins/ItemsMixin.java @@ -0,0 +1,25 @@ +package com.johanvonelectrum.johan_carpet.mixins; + +import com.johanvonelectrum.johan_carpet.commands.CommandItem; +import net.minecraft.item.Item; +import net.minecraft.item.Items; +import net.minecraft.util.Identifier; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.util.ArrayList; +import java.util.List; + +@Mixin(Items.class) +public class ItemsMixin { + + @Inject(method = "register(Lnet/minecraft/util/Identifier;Lnet/minecraft/item/Item;)Lnet/minecraft/item/Item;", at = @At("HEAD")) + private static void register(Identifier id, Item item, CallbackInfoReturnable cir) { + List eqSizeItems = CommandItem.ITEMS.computeIfAbsent(item.getMaxCount(), k -> new ArrayList<>()); + + eqSizeItems.add(item); + } + +} diff --git a/src/main/java/com/johanvonelectrum/johan_carpet/utils/HttpHelper.java b/src/main/java/com/johanvonelectrum/johan_carpet/utils/HttpHelper.java index 3ff76d4..d005a57 100644 --- a/src/main/java/com/johanvonelectrum/johan_carpet/utils/HttpHelper.java +++ b/src/main/java/com/johanvonelectrum/johan_carpet/utils/HttpHelper.java @@ -26,6 +26,7 @@ public static String getLatestRelease() { exception.printStackTrace(); response = JohanExtension.VERSION; } + System.out.println("Latest version: " + response); return response; } diff --git a/src/main/resources/johan_carpet.mixins.json b/src/main/resources/johan_carpet.mixins.json index ca13288..62acd0b 100644 --- a/src/main/resources/johan_carpet.mixins.json +++ b/src/main/resources/johan_carpet.mixins.json @@ -18,6 +18,8 @@ "HeightmapMixin", "HopperBlockEntityMixin", "ItemFrameEntityMixin", + "ItemMixin", + "ItemsMixin", "MinecraftServerMixin", "PlayerManagerMixin", "ServerChunkManagerMixin",