From 30e51b1b5ff4d987323b5d3c928567bad038dc4c Mon Sep 17 00:00:00 2001 From: Gugle Date: Tue, 2 Apr 2024 01:50:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=BA=9B=E9=9B=B7?= =?UTF-8?q?=E5=87=BB=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../anvilcraft/inventory/RoyalAnvilMenu.java | 17 ++++++++ .../anvilcraft/mixin/BeaconMenuMixin.java | 43 +++++++++++++++++++ src/main/resources/anvilcraft.accesswidener | 1 + src/main/resources/anvilcraft.mixins.json | 1 + 4 files changed, 62 insertions(+) create mode 100644 src/main/java/dev/dubhe/anvilcraft/mixin/BeaconMenuMixin.java diff --git a/src/main/java/dev/dubhe/anvilcraft/inventory/RoyalAnvilMenu.java b/src/main/java/dev/dubhe/anvilcraft/inventory/RoyalAnvilMenu.java index 5fee47125..77d41bfb0 100644 --- a/src/main/java/dev/dubhe/anvilcraft/inventory/RoyalAnvilMenu.java +++ b/src/main/java/dev/dubhe/anvilcraft/inventory/RoyalAnvilMenu.java @@ -1,9 +1,13 @@ package dev.dubhe.anvilcraft.inventory; +import dev.dubhe.anvilcraft.init.ModItems; import dev.dubhe.anvilcraft.init.ModMenuTypes; import net.minecraft.Util; import net.minecraft.network.chat.Component; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.LightningBolt; import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AnvilMenu; import net.minecraft.world.inventory.ContainerLevelAccess; import net.minecraft.world.inventory.MenuType; @@ -12,6 +16,7 @@ import net.minecraft.world.item.Items; import net.minecraft.world.item.enchantment.Enchantment; import net.minecraft.world.item.enchantment.EnchantmentHelper; +import net.minecraft.world.level.Level; import org.jetbrains.annotations.NotNull; import java.util.Map; @@ -150,4 +155,16 @@ public void createResult() { this.resultSlots.setItem(0, itemStack2); this.broadcastChanges(); } + + @Override + protected void onTake(Player player, ItemStack stack) { + super.onTake(player, stack); + Level level = player.level(); + if (level.isClientSide()) return; + int curedNumber = player.getInventory().countItem(ModItems.CURSED_GOLD_INGOT) + player.getInventory().countItem(ModItems.CURSED_GOLD_NUGGET) + player.getInventory().countItem(ModItems.CURSED_GOLD_BLOCK); + if (curedNumber <= 0) return; + LightningBolt bolt = new LightningBolt(EntityType.LIGHTNING_BOLT, level); + bolt.setPos(player.getX(), player.getY(), player.getZ()); + level.addFreshEntity(bolt); + } } diff --git a/src/main/java/dev/dubhe/anvilcraft/mixin/BeaconMenuMixin.java b/src/main/java/dev/dubhe/anvilcraft/mixin/BeaconMenuMixin.java new file mode 100644 index 000000000..0760237cd --- /dev/null +++ b/src/main/java/dev/dubhe/anvilcraft/mixin/BeaconMenuMixin.java @@ -0,0 +1,43 @@ +package dev.dubhe.anvilcraft.mixin; + +import dev.dubhe.anvilcraft.init.ModItems; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.inventory.BeaconMenu; +import net.minecraft.world.inventory.ContainerLevelAccess; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.GameRules; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.Optional; + +@Mixin(BeaconMenu.class) +public class BeaconMenuMixin { + @Shadow + @Final + private BeaconMenu.PaymentSlot paymentSlot; + + @Shadow + @Final + private ContainerLevelAccess access; + + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") + @Inject(method = "updateEffects", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/inventory/BeaconMenu$PaymentSlot;remove(I)Lnet/minecraft/world/item/ItemStack;")) + private void updateEffects(Optional primaryEffect, Optional secondaryEffect, CallbackInfo ci) { + ItemStack item = this.paymentSlot.getItem(); + if (!item.is(ModItems.CURSED_GOLD_INGOT)) return; + this.access.execute((level, pos) -> { + if (!(level instanceof ServerLevel serverLevel)) return; + MinecraftServer server = serverLevel.getServer(); + GameRules.BooleanValue rule = server.getGameRules().getRule(GameRules.RULE_WEATHER_CYCLE); + if (!rule.get()) return; + serverLevel.setWeatherParameters(0, ServerLevel.THUNDER_DURATION.sample(serverLevel.getRandom()), true, true); + }); + } +} diff --git a/src/main/resources/anvilcraft.accesswidener b/src/main/resources/anvilcraft.accesswidener index 73633bd1c..3d88efbee 100644 --- a/src/main/resources/anvilcraft.accesswidener +++ b/src/main/resources/anvilcraft.accesswidener @@ -3,6 +3,7 @@ accessible class net/minecraft/world/item/crafting/Ingredient$ItemValue accessible class net/minecraft/server/MinecraftServer$ReloadableResources accessible class net/minecraft/world/item/crafting/Ingredient$Value accessible class net/minecraft/world/item/crafting/Ingredient$TagValue +accessible class net/minecraft/world/inventory/BeaconMenu$PaymentSlot extendable method net/minecraft/client/gui/screens/inventory/AbstractContainerScreen renderSlot (Lnet/minecraft/client/gui/GuiGraphics;Lnet/minecraft/world/inventory/Slot;)V accessible method net/minecraft/server/MinecraftServer$ReloadableResources resourceManager ()Lnet/minecraft/server/packs/resources/CloseableResourceManager; accessible method net/minecraft/world/level/block/entity/HopperBlockEntity tryMoveInItem (Lnet/minecraft/world/Container;Lnet/minecraft/world/Container;Lnet/minecraft/world/item/ItemStack;ILnet/minecraft/core/Direction;)Lnet/minecraft/world/item/ItemStack; diff --git a/src/main/resources/anvilcraft.mixins.json b/src/main/resources/anvilcraft.mixins.json index 2a99b0970..4cb5dd778 100644 --- a/src/main/resources/anvilcraft.mixins.json +++ b/src/main/resources/anvilcraft.mixins.json @@ -7,6 +7,7 @@ "AbstractCauldronBlockMixin", "AnvilBlockMixin", "AxeItemMixin", + "BeaconMenuMixin", "BlockStateInjector", "BucketItemMixin", "ClientboundUpdateRecipesPacketMixin",