From f0b99d099fdbfe372b01f40dee32d686940c210b Mon Sep 17 00:00:00 2001 From: Anthony Hilyard Date: Tue, 2 Nov 2021 11:20:54 -0700 Subject: Consolidate item selectors. NewItemPickupEvent is now entirely client-side. --- .../java/com/anthonyhilyard/iceberg/Loader.java | 7 +- .../iceberg/mixin/ClientPacketListenerMixin.java | 33 ++++++++ .../iceberg/mixin/ForgeEventFactoryMixin.java | 31 -------- .../com/anthonyhilyard/iceberg/util/Selectors.java | 88 ++++++++++++++++++++++ 4 files changed, 122 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/anthonyhilyard/iceberg/mixin/ClientPacketListenerMixin.java delete mode 100644 src/main/java/com/anthonyhilyard/iceberg/mixin/ForgeEventFactoryMixin.java create mode 100644 src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java (limited to 'src/main/java') diff --git a/src/main/java/com/anthonyhilyard/iceberg/Loader.java b/src/main/java/com/anthonyhilyard/iceberg/Loader.java index 49e9d9c..7b7911b 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/Loader.java +++ b/src/main/java/com/anthonyhilyard/iceberg/Loader.java @@ -1,7 +1,5 @@ package com.anthonyhilyard.iceberg; -import com.anthonyhilyard.iceberg.network.IcebergNetworkProtocol; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -32,9 +30,6 @@ public class Loader new IcebergServer(); } - // Register the network protocol. - IcebergNetworkProtocol.register(); - ModLoadingContext.get().registerExtensionPoint(IExtensionPoint.DisplayTest.class, () -> new IExtensionPoint.DisplayTest(() -> "ANY", (remote, isServer) -> true)); } @@ -68,4 +63,4 @@ public class Loader // } // } -} \ No newline at end of file +} diff --git a/src/main/java/com/anthonyhilyard/iceberg/mixin/ClientPacketListenerMixin.java b/src/main/java/com/anthonyhilyard/iceberg/mixin/ClientPacketListenerMixin.java new file mode 100644 index 0000000..bef0407 --- /dev/null +++ b/src/main/java/com/anthonyhilyard/iceberg/mixin/ClientPacketListenerMixin.java @@ -0,0 +1,33 @@ +package com.anthonyhilyard.iceberg.mixin; + +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.item.ItemEntity; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.network.protocol.game.ClientboundTakeItemEntityPacket; +import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraftforge.common.MinecraftForge; + +import com.anthonyhilyard.iceberg.events.NewItemPickupEvent; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(ClientPacketListener.class) +public class ClientPacketListenerMixin +{ + @Inject(method = "handleTakeItemEntity", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/ItemStack;shrink(I)V"), + locals = LocalCapture.CAPTURE_FAILEXCEPTION) + private void handleTakeItemEntity(ClientboundTakeItemEntityPacket packet, CallbackInfo info, Entity entity, LivingEntity livingEntity, ItemEntity itemEntity, ItemStack itemStack) + { + if (livingEntity instanceof Player) + { + MinecraftForge.EVENT_BUS.post(new NewItemPickupEvent(livingEntity.getUUID(), itemStack)); + } + } +} diff --git a/src/main/java/com/anthonyhilyard/iceberg/mixin/ForgeEventFactoryMixin.java b/src/main/java/com/anthonyhilyard/iceberg/mixin/ForgeEventFactoryMixin.java deleted file mode 100644 index 2dbc247..0000000 --- a/src/main/java/com/anthonyhilyard/iceberg/mixin/ForgeEventFactoryMixin.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.anthonyhilyard.iceberg.mixin; - -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import net.minecraft.world.entity.item.ItemEntity; -import net.minecraft.world.entity.player.Player; -import net.minecraft.server.level.ServerPlayer; -import net.minecraftforge.event.ForgeEventFactory; -import net.minecraftforge.fml.loading.FMLEnvironment; -import net.minecraftforge.fmllegacy.network.PacketDistributor; - -import com.anthonyhilyard.iceberg.network.IcebergNetworkProtocol; -import com.anthonyhilyard.iceberg.network.NewItemPickupEventPacket; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; - -@Mixin(ForgeEventFactory.class) -public class ForgeEventFactoryMixin -{ - @Inject(method = { "onItemPickup(Lnet/minecraft/world/entity/item/ItemEntity;Lnet/minecraft/world/entity/player/Player;)I" }, - at = { @At("HEAD") }, remap = false) - private static void onItemPickup(ItemEntity entityItem, Player player, CallbackInfoReturnable info) - { - if (player instanceof ServerPlayer && FMLEnvironment.dist.isDedicatedServer()) - { - IcebergNetworkProtocol.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer)player), new NewItemPickupEventPacket(player.getUUID(), entityItem.getItem())); - } - } -} diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java b/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java new file mode 100644 index 0000000..65a2ebd --- /dev/null +++ b/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java @@ -0,0 +1,88 @@ +package com.anthonyhilyard.iceberg.util; + +import net.minecraft.world.item.ItemStack; + +import java.util.HashMap; +import java.util.Map; +import java.util.List; + +import net.minecraft.client.Minecraft; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.TextColor; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.ItemTags; +import net.minecraft.world.item.Rarity; +import net.minecraft.world.item.TooltipFlag; + +public class Selectors +{ + private static Map rarities = new HashMap() {{ + put("common", Rarity.COMMON); + put("uncommon", Rarity.UNCOMMON); + put("rare", Rarity.RARE); + put("epic", Rarity.EPIC); + }}; + + public static boolean itemMatches(ItemStack item, String selector) + { + String itemResourceLocation = item.getItem().getRegistryName().toString(); + if (selector.equals(itemResourceLocation) || selector.equals(itemResourceLocation.replace("minecraft:", ""))) + { + return true; + } + else if (selector.startsWith("#")) + { + TextColor entryColor = TextColor.parseColor(selector); + if (entryColor != null && entryColor.equals(ItemColor.getColorForItem(item, TextColor.fromRgb(0xFFFFFF)))) + { + return true; + } + } + else if (selector.startsWith("!")) + { + if (item.getRarity() == rarities.get(selector.substring(1))) + { + return true; + } + } + else if (selector.startsWith("@")) + { + if (itemResourceLocation.startsWith(selector.substring(1) + ":")) + { + return true; + } + } + else if (selector.startsWith("$")) + { + if (ItemTags.getAllTags().getTagOrEmpty(new ResourceLocation(selector.substring(1))).getValues().contains(item.getItem())) + { + return true; + } + } + else if (selector.startsWith("%")) + { + if (item.getDisplayName().getString().contains(selector.substring(1))) + { + return true; + } + } + else if (selector.startsWith("^")) + { + Minecraft mc = Minecraft.getInstance(); + List lines = item.getTooltipLines(mc.player, TooltipFlag.Default.ADVANCED); + String tooltipText = ""; + + // Skip title line. + for (int n = 1; n < lines.size(); n++) + { + tooltipText += lines.get(n).getString() + '\n'; + } + if (tooltipText.contains(selector.substring(1))) + { + return true; + } + } + + return false; + } +} \ No newline at end of file -- cgit