From e1e95a872ddd71edc4b30d111323123fc920d990 Mon Sep 17 00:00:00 2001 From: Hubry Date: Thu, 26 Dec 2019 06:24:04 +0100 Subject: [PATCH] Improve Corporea Index keybind requests (#3065) Keybind requests now send an itemstack instead of the item name. Items in inventories ignore their own NBT, NBT matching made less strict Chat is no longer touched by keybind requests Recipe book buttons can now be a source of keybind requests Keybind requests work again with shift and ctrl Matchers are in charge of their index request formatting Index event now uses a matcher (this and previous change break api) Indexes ignore spectators --- .../corporea/CorporeaIndexRequestEvent.java | 4 +- .../CorporeaRequestDefaultMatchers.java | 18 ++++++- .../api/corporea/ICorporeaRequestMatcher.java | 8 ++- .../core/handler/CorporeaInputHandler.java | 43 +++++++++++---- .../tile/corporea/TileCorporeaIndex.java | 35 +++++++------ .../botania/common/lib/LibObfuscation.java | 6 +++ .../botania/common/network/PacketHandler.java | 3 +- .../network/PacketIndexKeybindRequest.java | 52 +++++++++++++++++++ 8 files changed, 137 insertions(+), 32 deletions(-) create mode 100644 src/main/java/vazkii/botania/common/network/PacketIndexKeybindRequest.java diff --git a/src/main/java/vazkii/botania/api/corporea/CorporeaIndexRequestEvent.java b/src/main/java/vazkii/botania/api/corporea/CorporeaIndexRequestEvent.java index f4d0b20fd..99e50a1da 100644 --- a/src/main/java/vazkii/botania/api/corporea/CorporeaIndexRequestEvent.java +++ b/src/main/java/vazkii/botania/api/corporea/CorporeaIndexRequestEvent.java @@ -20,11 +20,11 @@ import net.minecraftforge.eventbus.api.Event; @Cancelable public class CorporeaIndexRequestEvent extends Event { public final ServerPlayerEntity requester; - public String request; + public ICorporeaRequestMatcher request; public int requestCount; public ICorporeaSpark indexSpark; - public CorporeaIndexRequestEvent(ServerPlayerEntity requester, String request, int requestCount, ICorporeaSpark indexSpark) { + public CorporeaIndexRequestEvent(ServerPlayerEntity requester, ICorporeaRequestMatcher request, int requestCount, ICorporeaSpark indexSpark) { this.requester = requester; this.request = request; this.requestCount = requestCount; diff --git a/src/main/java/vazkii/botania/api/corporea/CorporeaRequestDefaultMatchers.java b/src/main/java/vazkii/botania/api/corporea/CorporeaRequestDefaultMatchers.java index dfd938fd5..e206658cd 100644 --- a/src/main/java/vazkii/botania/api/corporea/CorporeaRequestDefaultMatchers.java +++ b/src/main/java/vazkii/botania/api/corporea/CorporeaRequestDefaultMatchers.java @@ -12,8 +12,11 @@ package vazkii.botania.api.corporea; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundNBT; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.StringTextComponent; +import org.apache.commons.lang3.text.WordUtils; +import vazkii.botania.common.core.helper.ItemNBTHelper; -import java.util.function.Function; import java.util.regex.Pattern; /** @@ -79,6 +82,12 @@ public final class CorporeaRequestDefaultMatchers { tag.putBoolean(TAG_REQUEST_CONTAINS, contains); } + @Override + @SuppressWarnings("deprecation") + public ITextComponent getRequestName() { + return new StringTextComponent(WordUtils.capitalizeFully(expression)); + } + private boolean equalOrContain(String str) { return contains ? str.contains(expression) : str.equals(expression); } @@ -103,7 +112,7 @@ public final class CorporeaRequestDefaultMatchers { @Override public boolean isStackValid(ItemStack stack) { - return !stack.isEmpty() && !match.isEmpty() && stack.isItemEqual(match) && (!checkNBT || ItemStack.areItemStackTagsEqual(stack, match)); + return !stack.isEmpty() && !match.isEmpty() && stack.isItemEqual(match) && (!checkNBT || ItemNBTHelper.matchTag(match.getTag(), stack.getTag())); } public static ICorporeaRequestMatcher createFromNBT(CompoundNBT tag) { @@ -116,6 +125,11 @@ public final class CorporeaRequestDefaultMatchers { tag.put(TAG_REQUEST_STACK, cmp); tag.putBoolean(TAG_REQUEST_CHECK_NBT, checkNBT); } + + @Override + public ITextComponent getRequestName() { + return match.getDisplayName(); + } } private CorporeaRequestDefaultMatchers() {} diff --git a/src/main/java/vazkii/botania/api/corporea/ICorporeaRequestMatcher.java b/src/main/java/vazkii/botania/api/corporea/ICorporeaRequestMatcher.java index 90b480748..d7c9b3ce0 100644 --- a/src/main/java/vazkii/botania/api/corporea/ICorporeaRequestMatcher.java +++ b/src/main/java/vazkii/botania/api/corporea/ICorporeaRequestMatcher.java @@ -12,8 +12,7 @@ package vazkii.botania.api.corporea; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundNBT; - -import java.util.function.Function; +import net.minecraft.util.text.ITextComponent; /** * An interface for a Corporea Request matcher. Accepts an ItemStack and returns whether it fulfills the request. @@ -31,4 +30,9 @@ public interface ICorporeaRequestMatcher { * Serialize to NBT data, for the Corporea Retainer's benefit. */ void writeToNBT(CompoundNBT tag); + + /** + * Returns the pretty name of the requested item, for printing request feedback on Corporea Indexes. + */ + ITextComponent getRequestName(); } diff --git a/src/main/java/vazkii/botania/client/core/handler/CorporeaInputHandler.java b/src/main/java/vazkii/botania/client/core/handler/CorporeaInputHandler.java index 12b20e32f..6a5fd944e 100644 --- a/src/main/java/vazkii/botania/client/core/handler/CorporeaInputHandler.java +++ b/src/main/java/vazkii/botania/client/core/handler/CorporeaInputHandler.java @@ -11,18 +11,26 @@ package vazkii.botania.client.core.handler; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.recipebook.RecipeBookGui; +import net.minecraft.client.gui.recipebook.RecipeBookPage; +import net.minecraft.client.gui.recipebook.RecipeWidget; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.inventory.ContainerScreen; -import net.minecraft.client.util.InputMappings; +import net.minecraft.client.gui.screen.inventory.InventoryScreen; import net.minecraft.inventory.container.Slot; import net.minecraft.item.ItemStack; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.client.event.GuiScreenEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.ObfuscationReflectionHelper; import vazkii.botania.client.core.proxy.ClientProxy; +import vazkii.botania.common.Botania; import vazkii.botania.common.block.tile.corporea.TileCorporeaIndex; import vazkii.botania.common.lib.LibMisc; +import vazkii.botania.common.lib.LibObfuscation; +import vazkii.botania.common.network.PacketHandler; +import vazkii.botania.common.network.PacketIndexKeybindRequest; import java.util.function.Predicate; import java.util.function.Supplier; @@ -39,8 +47,9 @@ public class CorporeaInputHandler { @SubscribeEvent public static void buttonPressed(GuiScreenEvent.KeyboardKeyPressedEvent.Pre event) { Minecraft mc = Minecraft.getInstance(); - if(mc.world == null || !supportedGuiFilter.test(mc.currentScreen) - || !ClientProxy.CORPOREA_REQUEST.isActiveAndMatches(InputMappings.getInputByCode(event.getKeyCode(), event.getScanCode())) + + if(mc.world == null || !supportedGuiFilter.test(mc.currentScreen) || event.getKeyCode() == 0 + || ClientProxy.CORPOREA_REQUEST.getKey().getKeyCode() != event.getKeyCode() || TileCorporeaIndex.InputHandler.getNearbyIndexes(mc.player).isEmpty()) return; @@ -57,10 +66,9 @@ public class CorporeaInputHandler { count = max / 2; if(count > 0) { - String full = count + " " + stack.getDisplayName().getString(); - - mc.ingameGUI.getChatGUI().addToSentMessages(full); - mc.player.sendChatMessage(full); + ItemStack requested = stack.copy(); + requested.setCount(count); + PacketHandler.sendToServer(new PacketIndexKeybindRequest(requested)); event.setCanceled(true); } } @@ -70,9 +78,26 @@ public class CorporeaInputHandler { Screen screen = Minecraft.getInstance().currentScreen; if(screen instanceof ContainerScreen) { Slot slotUnderMouse = ((ContainerScreen) screen).getSlotUnderMouse(); - if(slotUnderMouse != null) - return slotUnderMouse.getStack(); + if(slotUnderMouse != null) { + ItemStack stack = slotUnderMouse.getStack().copy(); + stack.setTag(null); // Wipe NBT of inventory items before request, as player items will often have data + return stack; // that's better to ignore. This is still an improvement over matching names only. + } } + + if (screen instanceof InventoryScreen && ((InventoryScreen) screen).getRecipeGui().isVisible()) { + RecipeBookGui recipeBook = ((InventoryScreen) screen).getRecipeGui(); + try { + RecipeBookPage page = ObfuscationReflectionHelper.getPrivateValue(RecipeBookGui.class, recipeBook, LibObfuscation.RECIPE_BOOK_PAGE); + RecipeWidget widget = ObfuscationReflectionHelper.getPrivateValue(RecipeBookPage.class, page, LibObfuscation.HOVERED_BUTTON); + if (widget != null) { + return widget.getRecipe().getRecipeOutput(); + } + } catch (Exception e) { + Botania.LOGGER.error("Failed to get hovered recipe gui button", e); + } + } + return jeiPanelSupplier.get(); } } diff --git a/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java b/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java index 96a3d28d4..d43d0c259 100644 --- a/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java +++ b/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java @@ -10,9 +10,9 @@ */ package vazkii.botania.common.block.tile.corporea; -import net.minecraft.client.Minecraft; import net.minecraft.entity.item.ItemEntity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.tileentity.TileEntityType; @@ -20,14 +20,11 @@ import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.text.Style; import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TranslationTextComponent; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.ServerChatEvent; import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.registries.ObjectHolder; -import org.apache.commons.lang3.text.WordUtils; import vazkii.botania.api.corporea.CorporeaHelper; import vazkii.botania.api.corporea.CorporeaIndexRequestEvent; import vazkii.botania.api.corporea.ICorporeaRequestMatcher; @@ -255,9 +252,22 @@ public class TileCorporeaIndex extends TileCorporeaBase implements ICorporeaRequ set.remove(index); } + public void performPlayerRequest(ServerPlayerEntity player, ICorporeaRequestMatcher request, int count) { + CorporeaIndexRequestEvent indexReqEvent = new CorporeaIndexRequestEvent(player, request, count, this.getSpark()); + if(!MinecraftForge.EVENT_BUS.post(indexReqEvent)) { + this.doCorporeaRequest(request, count, this.getSpark()); + + player.sendMessage(new TranslationTextComponent("botaniamisc.requestMsg", count, request.getRequestName(), CorporeaHelper.lastRequestMatches, CorporeaHelper.lastRequestExtractions).setStyle(new Style().setColor(TextFormatting.LIGHT_PURPLE))); + CorporeaRequestTrigger.INSTANCE.trigger(player, player.getServerWorld(), this.getPos(), CorporeaHelper.lastRequestExtractions); + } + } + public static final class InputHandler { @SubscribeEvent(priority = EventPriority.HIGHEST) public void onChatMessage(ServerChatEvent event) { + if(event.getPlayer().isSpectator()) + return; + List nearbyIndexes = getNearbyIndexes(event.getPlayer()); if(!nearbyIndexes.isEmpty()) { String msg = event.getMessage().toLowerCase().trim(); @@ -272,7 +282,6 @@ public class TileCorporeaIndex extends TileCorporeaBase implements ICorporeaRequ IRegexStacker stacker = patterns.get(pattern); count = stacker.getCount(matcher); name = stacker.getName(matcher).toLowerCase().trim(); - pattern.toString(); } } @@ -281,14 +290,8 @@ public class TileCorporeaIndex extends TileCorporeaBase implements ICorporeaRequ if(!stack.isEmpty()) name = stack.getDisplayName().getString().toLowerCase().trim(); } - - CorporeaIndexRequestEvent indexReqEvent = new CorporeaIndexRequestEvent(event.getPlayer(), name, count, spark); - if(!MinecraftForge.EVENT_BUS.post(indexReqEvent)) { - index.doCorporeaRequest(CorporeaHelper.createMatcher(name), count, spark); - - event.getPlayer().sendMessage(new TranslationTextComponent("botaniamisc.requestMsg", count, WordUtils.capitalizeFully(name), CorporeaHelper.lastRequestMatches, CorporeaHelper.lastRequestExtractions).setStyle(new Style().setColor(TextFormatting.LIGHT_PURPLE))); - CorporeaRequestTrigger.INSTANCE.trigger(event.getPlayer(), event.getPlayer().getServerWorld(), index.getPos(), CorporeaHelper.lastRequestExtractions); - } + + index.performPlayerRequest(event.getPlayer(), CorporeaHelper.createMatcher(name), count); } } @@ -303,11 +306,11 @@ public class TileCorporeaIndex extends TileCorporeaBase implements ICorporeaRequ } } - public static interface IRegexStacker { + public interface IRegexStacker { - public int getCount(Matcher m); + int getCount(Matcher m); - public String getName(Matcher m); + String getName(Matcher m); } diff --git a/src/main/java/vazkii/botania/common/lib/LibObfuscation.java b/src/main/java/vazkii/botania/common/lib/LibObfuscation.java index e3926021f..f13626e47 100644 --- a/src/main/java/vazkii/botania/common/lib/LibObfuscation.java +++ b/src/main/java/vazkii/botania/common/lib/LibObfuscation.java @@ -13,4 +13,10 @@ package vazkii.botania.common.lib; public final class LibObfuscation { // EntityLiving public static final String GET_LIVING_SOUND = "func_184639_G"; + + // RecipeBookGui + public static final String RECIPE_BOOK_PAGE = "field_193022_s"; + + // RecipeBookPage + public static final String HOVERED_BUTTON = "field_194201_b"; } diff --git a/src/main/java/vazkii/botania/common/network/PacketHandler.java b/src/main/java/vazkii/botania/common/network/PacketHandler.java index 8dbfecac6..9ee9c6b0f 100644 --- a/src/main/java/vazkii/botania/common/network/PacketHandler.java +++ b/src/main/java/vazkii/botania/common/network/PacketHandler.java @@ -14,7 +14,7 @@ import net.minecraftforge.fml.network.simple.SimpleChannel; import vazkii.botania.common.lib.LibMisc; public final class PacketHandler { - private static final String PROTOCOL = "1"; + private static final String PROTOCOL = "2"; public static final SimpleChannel HANDLER = NetworkRegistry.newSimpleChannel( new ResourceLocation(LibMisc.MOD_ID, "chan"), () -> PROTOCOL, @@ -30,6 +30,7 @@ public final class PacketHandler { HANDLER.registerMessage(id++, PacketJump.class, PacketJump::encode, PacketJump::decode, PacketJump::handle); HANDLER.registerMessage(id++, PacketItemAge.class, PacketItemAge::encode, PacketItemAge::decode, PacketItemAge::handle); HANDLER.registerMessage(id++, PacketSyncRecipes.class, PacketSyncRecipes::encode, PacketSyncRecipes::decode, PacketSyncRecipes::handle); + HANDLER.registerMessage(id++, PacketIndexKeybindRequest.class, PacketIndexKeybindRequest::encode, PacketIndexKeybindRequest::decode, PacketIndexKeybindRequest::handle); } /** diff --git a/src/main/java/vazkii/botania/common/network/PacketIndexKeybindRequest.java b/src/main/java/vazkii/botania/common/network/PacketIndexKeybindRequest.java new file mode 100644 index 000000000..72bdf37b8 --- /dev/null +++ b/src/main/java/vazkii/botania/common/network/PacketIndexKeybindRequest.java @@ -0,0 +1,52 @@ +/** + * This class was created by . It's distributed as + * part of the Botania Mod. Get the Source Code in github: + * https://github.com/Vazkii/Botania + * + * Botania is Open Source and distributed under the + * Botania License: http://botaniamod.net/license.php + * + * File Created @ [gru 25 2019, 19:33] + */ +package vazkii.botania.common.network; + +import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.network.PacketBuffer; +import net.minecraftforge.fml.network.NetworkEvent; +import vazkii.botania.api.corporea.CorporeaHelper; +import vazkii.botania.common.block.tile.corporea.TileCorporeaIndex; + +import java.util.function.Supplier; + +public class PacketIndexKeybindRequest { + private final ItemStack stack; + + public PacketIndexKeybindRequest(ItemStack stack) { + this.stack = stack; + } + + public static PacketIndexKeybindRequest decode(PacketBuffer buf) { + return new PacketIndexKeybindRequest(buf.readItemStack()); + } + + public static void encode(PacketIndexKeybindRequest msg, PacketBuffer buf) { + buf.writeItemStack(msg.stack); + } + + public static void handle(PacketIndexKeybindRequest message, Supplier ctx) { + ctx.get().enqueueWork(() -> { + ServerPlayerEntity player = ctx.get().getSender(); + if (player.isSpectator()) + return; + + boolean checkNBT = message.stack.getTag() != null && !message.stack.getTag().isEmpty(); + for (TileCorporeaIndex index : TileCorporeaIndex.InputHandler.getNearbyIndexes(player)) { + if (index.getSpark() != null) { + index.performPlayerRequest(player, CorporeaHelper.createMatcher(message.stack, checkNBT), message.stack.getCount()); + } + } + }); + ctx.get().setPacketHandled(true); + } +}