From 9e55b44b710f38c63a4bfd17d0b07318d5c68535 Mon Sep 17 00:00:00 2001 From: Daniel She Date: Mon, 25 Feb 2019 21:18:26 +0800 Subject: REI v2.3.1 (#40) - API Changes - Updated Config Screen - Added Tipped Arrows Recipes - Updated Mappings - Added IRecipeHelper - Turning things to Optional - Removed Cheats button, now included with the new config button - Buttons for switching gamemodes / time / weather (default: off) [Maybe not in this update] - Clickable Labels - 3+ recipes at the same time - Fixed RecipeBaseWidget bad rendering when too big - Fixed #42 Patched up item deleting & cheating - Choose Page Dialog --- .../me/shedaniel/rei/RoughlyEnoughItemsCore.java | 19 +- .../me/shedaniel/rei/api/IDisplaySettings.java | 11 + .../java/me/shedaniel/rei/api/IRecipeCategory.java | 53 ++- .../java/me/shedaniel/rei/api/IRecipeDisplay.java | 3 +- .../java/me/shedaniel/rei/api/IRecipeHelper.java | 44 +++ .../java/me/shedaniel/rei/api/IRecipePlugin.java | 6 +- .../java/me/shedaniel/rei/client/ClientHelper.java | 34 +- .../java/me/shedaniel/rei/client/ConfigHelper.java | 62 +--- .../java/me/shedaniel/rei/client/REIConfig.java | 8 +- .../java/me/shedaniel/rei/client/RecipeHelper.java | 60 +++- src/main/java/me/shedaniel/rei/client/Weather.java | 38 ++ .../shedaniel/rei/gui/ContainerScreenOverlay.java | 220 +++++++++--- .../me/shedaniel/rei/gui/RecipeViewingScreen.java | 381 +++++++++++++++++++++ .../me/shedaniel/rei/gui/config/ConfigEntry.java | 171 ++++++--- .../rei/gui/config/ConfigEntryListWidget.java | 2 +- .../me/shedaniel/rei/gui/config/ConfigScreen.java | 211 ++++++++++-- .../rei/gui/credits/CreditsEntryListWidget.java | 2 +- .../shedaniel/rei/gui/credits/CreditsScreen.java | 10 +- .../me/shedaniel/rei/gui/widget/ButtonWidget.java | 4 +- .../rei/gui/widget/ClickableLabelWidget.java | 36 ++ .../shedaniel/rei/gui/widget/DraggableWidget.java | 71 ++++ .../java/me/shedaniel/rei/gui/widget/IWidget.java | 7 +- .../shedaniel/rei/gui/widget/ItemListOverlay.java | 16 +- .../shedaniel/rei/gui/widget/ItemSlotWidget.java | 12 +- .../me/shedaniel/rei/gui/widget/LabelWidget.java | 9 +- .../shedaniel/rei/gui/widget/RecipeBaseWidget.java | 21 +- .../rei/gui/widget/RecipeChoosePageWidget.java | 165 +++++++++ .../rei/gui/widget/RecipePageLabelWidget.java | 113 ++++++ .../rei/gui/widget/RecipeViewingWidgetScreen.java | 299 ---------------- .../me/shedaniel/rei/gui/widget/TabWidget.java | 9 +- .../shedaniel/rei/gui/widget/TextFieldWidget.java | 127 ++++--- .../rei/mixin/MixinClientPlayNetworkHandler.java | 3 +- .../shedaniel/rei/mixin/MixinContainerScreen.java | 2 +- .../rei/mixin/MixinCraftingTableScreen.java | 6 +- .../rei/mixin/MixinPlayerInventoryScreen.java | 6 +- .../shedaniel/rei/mixin/MixinTextureManager.java | 29 ++ .../rei/plugin/DefaultBlastingDisplay.java | 5 +- .../rei/plugin/DefaultBrewingDisplay.java | 5 +- .../rei/plugin/DefaultCampfireDisplay.java | 10 +- .../shedaniel/rei/plugin/DefaultCustomDisplay.java | 70 ++++ .../me/shedaniel/rei/plugin/DefaultPlugin.java | 52 ++- .../shedaniel/rei/plugin/DefaultShapedDisplay.java | 6 +- .../rei/plugin/DefaultShapelessDisplay.java | 5 +- .../rei/plugin/DefaultSmeltingDisplay.java | 5 +- .../rei/plugin/DefaultSmokingDisplay.java | 6 +- .../rei/plugin/DefaultStoneCuttingDisplay.java | 10 +- .../me/shedaniel/rei/update/UpdateChecker.java | 2 +- 47 files changed, 1763 insertions(+), 683 deletions(-) create mode 100644 src/main/java/me/shedaniel/rei/api/IDisplaySettings.java create mode 100644 src/main/java/me/shedaniel/rei/api/IRecipeHelper.java create mode 100644 src/main/java/me/shedaniel/rei/client/Weather.java create mode 100644 src/main/java/me/shedaniel/rei/gui/RecipeViewingScreen.java create mode 100644 src/main/java/me/shedaniel/rei/gui/widget/ClickableLabelWidget.java create mode 100644 src/main/java/me/shedaniel/rei/gui/widget/DraggableWidget.java create mode 100644 src/main/java/me/shedaniel/rei/gui/widget/RecipeChoosePageWidget.java create mode 100644 src/main/java/me/shedaniel/rei/gui/widget/RecipePageLabelWidget.java delete mode 100644 src/main/java/me/shedaniel/rei/gui/widget/RecipeViewingWidgetScreen.java create mode 100644 src/main/java/me/shedaniel/rei/mixin/MixinTextureManager.java create mode 100644 src/main/java/me/shedaniel/rei/plugin/DefaultCustomDisplay.java (limited to 'src/main/java/me') diff --git a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java index faa647b3f..604d22f2e 100644 --- a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java +++ b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java @@ -3,6 +3,7 @@ package me.shedaniel.rei; import com.google.common.collect.Maps; import me.shedaniel.rei.api.IItemRegisterer; import me.shedaniel.rei.api.IPluginDisabler; +import me.shedaniel.rei.api.IRecipeHelper; import me.shedaniel.rei.api.IRecipePlugin; import me.shedaniel.rei.client.ConfigHelper; import me.shedaniel.rei.client.GuiHelper; @@ -19,7 +20,6 @@ import net.fabricmc.loader.FabricLoader; import net.minecraft.client.resource.language.I18n; import net.minecraft.item.ItemStack; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.sortme.ChatMessageType; import net.minecraft.text.StringTextComponent; import net.minecraft.text.TranslatableTextComponent; import net.minecraft.util.Identifier; @@ -29,6 +29,7 @@ import org.apache.logging.log4j.Logger; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Optional; public class RoughlyEnoughItemsCore implements ClientModInitializer, ModInitializer { @@ -41,7 +42,7 @@ public class RoughlyEnoughItemsCore implements ClientModInitializer, ModInitiali private static final Map plugins = Maps.newHashMap(); private static ConfigHelper configHelper; - public static RecipeHelper getRecipeHelper() { + public static IRecipeHelper getRecipeHelper() { return RECIPE_HELPER; } @@ -68,11 +69,11 @@ public class RoughlyEnoughItemsCore implements ClientModInitializer, ModInitiali return new LinkedList<>(plugins.values()); } - public static Identifier getPluginIdentifier(IRecipePlugin plugin) { + public static Optional getPluginIdentifier(IRecipePlugin plugin) { for(Identifier identifier : plugins.keySet()) - if (plugins.get(identifier).equals(plugin)) - return identifier; - return null; + if (identifier != null && plugins.get(identifier).equals(plugin)) + return Optional.of(identifier); + return Optional.empty(); } @SuppressWarnings("deprecation") @@ -87,7 +88,7 @@ public class RoughlyEnoughItemsCore implements ClientModInitializer, ModInitiali } ClientTickCallback.EVENT.register(GuiHelper::onTick); - if (getConfigHelper().checkUpdates()) + if (getConfigHelper().getConfig().checkUpdates) ClientTickCallback.EVENT.register(UpdateChecker::onTick); new UpdateChecker().onInitializeClient(); @@ -108,9 +109,9 @@ public class RoughlyEnoughItemsCore implements ClientModInitializer, ModInitiali ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer(); ItemStack stack = packetByteBuf.readItemStack(); if (player.inventory.insertStack(stack.copy())) - player.sendChatMessage(new StringTextComponent(I18n.translate("text.rei.cheat_items").replaceAll("\\{item_name}", stack.copy().getDisplayName().getFormattedText()).replaceAll("\\{item_count}", stack.copy().getAmount() + "").replaceAll("\\{player_name}", player.getEntityName())), ChatMessageType.SYSTEM); + player.addChatMessage(new StringTextComponent(I18n.translate("text.rei.cheat_items").replaceAll("\\{item_name}", stack.copy().getDisplayName().getFormattedText()).replaceAll("\\{item_count}", stack.copy().getAmount() + "").replaceAll("\\{player_name}", player.getEntityName())), false); else - player.sendChatMessage(new TranslatableTextComponent("text.rei.failed_cheat_items"), ChatMessageType.SYSTEM); + player.addChatMessage(new TranslatableTextComponent("text.rei.failed_cheat_items"), false); }); } diff --git a/src/main/java/me/shedaniel/rei/api/IDisplaySettings.java b/src/main/java/me/shedaniel/rei/api/IDisplaySettings.java new file mode 100644 index 000000000..1cb315fb2 --- /dev/null +++ b/src/main/java/me/shedaniel/rei/api/IDisplaySettings.java @@ -0,0 +1,11 @@ +package me.shedaniel.rei.api; + +public interface IDisplaySettings { + + public int getDisplayHeight(IRecipeCategory category); + + public int getDisplayWidth(IRecipeCategory category, T display); + + public int getMaximumRecipePerPage(IRecipeCategory category); + +} diff --git a/src/main/java/me/shedaniel/rei/api/IRecipeCategory.java b/src/main/java/me/shedaniel/rei/api/IRecipeCategory.java index 123fa20df..9665e6a89 100644 --- a/src/main/java/me/shedaniel/rei/api/IRecipeCategory.java +++ b/src/main/java/me/shedaniel/rei/api/IRecipeCategory.java @@ -1,12 +1,9 @@ package me.shedaniel.rei.api; -import com.mojang.blaze3d.platform.GlStateManager; +import me.shedaniel.rei.gui.RecipeViewingScreen; import me.shedaniel.rei.gui.widget.IWidget; import me.shedaniel.rei.gui.widget.RecipeBaseWidget; -import me.shedaniel.rei.gui.widget.RecipeViewingWidgetScreen; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.Drawable; -import net.minecraft.client.render.GuiLighting; +import net.minecraft.client.gui.DrawableHelper; import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; @@ -24,21 +21,45 @@ public interface IRecipeCategory { public String getCategoryName(); - default public boolean usesFullPage() { - return false; - } - default public List setupDisplay(Supplier recipeDisplaySupplier, Rectangle bounds) { return Arrays.asList(new RecipeBaseWidget(bounds)); } - default public void drawCategoryBackground(Rectangle bounds) { - GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F); - GuiLighting.disable(); - MinecraftClient.getInstance().getTextureManager().bindTexture(RecipeViewingWidgetScreen.CHEST_GUI_TEXTURE); - new Drawable() { - - }.drawTexturedRect((int) bounds.getX(), (int) bounds.getY(), 0, 0, (int) bounds.getWidth(), (int) bounds.getHeight()); + default public void drawCategoryBackground(Rectangle bounds, int mouseX, int mouseY, float delta) { + new RecipeBaseWidget(bounds).draw(mouseX, mouseY, delta); + DrawableHelper.drawRect(bounds.x + 17, bounds.y + 5, bounds.x + bounds.width - 17, bounds.y + 17, RecipeViewingScreen.SUB_COLOR.getRGB()); + DrawableHelper.drawRect(bounds.x + 17, bounds.y + 21, bounds.x + bounds.width - 17, bounds.y + 33, RecipeViewingScreen.SUB_COLOR.getRGB()); + } + + default public IDisplaySettings getDisplaySettings() { + return new IDisplaySettings() { + @Override + public int getDisplayHeight(IRecipeCategory category) { + return 66; + } + + @Override + public int getDisplayWidth(IRecipeCategory category, T display) { + return 150; + } + + @Override + public int getMaximumRecipePerPage(IRecipeCategory category) { + return 99; + } + }; + } + + default public int getDisplayHeight() { + return getDisplaySettings().getDisplayHeight(this); + } + + default public int getDisplayWidth(T display) { + return getDisplaySettings().getDisplayWidth(this, display); + } + + default public int getMaximumRecipePerPage() { + return getDisplaySettings().getMaximumRecipePerPage(this); } default public boolean checkTags() { diff --git a/src/main/java/me/shedaniel/rei/api/IRecipeDisplay.java b/src/main/java/me/shedaniel/rei/api/IRecipeDisplay.java index 1a19b21c5..9d390385c 100644 --- a/src/main/java/me/shedaniel/rei/api/IRecipeDisplay.java +++ b/src/main/java/me/shedaniel/rei/api/IRecipeDisplay.java @@ -6,10 +6,11 @@ import net.minecraft.recipe.Recipe; import net.minecraft.util.Identifier; import java.util.List; +import java.util.Optional; public interface IRecipeDisplay { - public abstract T getRecipe(); + public abstract Optional getRecipe(); public List> getInput(); diff --git a/src/main/java/me/shedaniel/rei/api/IRecipeHelper.java b/src/main/java/me/shedaniel/rei/api/IRecipeHelper.java new file mode 100644 index 000000000..8d9651f32 --- /dev/null +++ b/src/main/java/me/shedaniel/rei/api/IRecipeHelper.java @@ -0,0 +1,44 @@ +package me.shedaniel.rei.api; + +import me.shedaniel.rei.RoughlyEnoughItemsCore; +import net.minecraft.item.ItemStack; +import net.minecraft.recipe.RecipeManager; +import net.minecraft.util.Identifier; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public interface IRecipeHelper { + + public static IRecipeHelper getInstance() { + return RoughlyEnoughItemsCore.getRecipeHelper(); + } + + public int getRecipeCount(); + + public List findCraftableByItems(List inventoryItems); + + public void registerCategory(IRecipeCategory category); + + public void registerDisplay(Identifier categoryIdentifier, IRecipeDisplay display); + + public Map> getRecipesFor(ItemStack stack); + + public RecipeManager getRecipeManager(); + + public List getAllCategories(); + + public Map> getUsagesFor(ItemStack stack); + + public Optional getSpeedCraftButtonArea(IRecipeCategory category); + + public void registerSpeedCraftButtonArea(Identifier category, SpeedCraftAreaSupplier rectangle); + + public List getSpeedCraftFunctional(IRecipeCategory category); + + public void registerSpeedCraftFunctional(Identifier category, SpeedCraftFunctional functional); + + Map> getAllRecipes(); + +} diff --git a/src/main/java/me/shedaniel/rei/api/IRecipePlugin.java b/src/main/java/me/shedaniel/rei/api/IRecipePlugin.java index 6ba6c70e7..a64ba67a4 100644 --- a/src/main/java/me/shedaniel/rei/api/IRecipePlugin.java +++ b/src/main/java/me/shedaniel/rei/api/IRecipePlugin.java @@ -8,11 +8,11 @@ public interface IRecipePlugin { public void registerItems(IItemRegisterer itemRegisterer); - public void registerPluginCategories(RecipeHelper recipeHelper); + public void registerPluginCategories(IRecipeHelper recipeHelper); - public void registerRecipeDisplays(RecipeHelper recipeHelper); + public void registerRecipeDisplays(IRecipeHelper recipeHelper); - public void registerSpeedCraft(RecipeHelper recipeHelper); + public void registerSpeedCraft(IRecipeHelper recipeHelper); default public int getPriority() { return 0; diff --git a/src/main/java/me/shedaniel/rei/client/ClientHelper.java b/src/main/java/me/shedaniel/rei/client/ClientHelper.java index 474fdf1ce..87dc00895 100644 --- a/src/main/java/me/shedaniel/rei/client/ClientHelper.java +++ b/src/main/java/me/shedaniel/rei/client/ClientHelper.java @@ -5,9 +5,10 @@ import io.netty.buffer.Unpooled; import me.shedaniel.rei.RoughlyEnoughItemsCore; import me.shedaniel.rei.api.IRecipeCategory; import me.shedaniel.rei.api.IRecipeDisplay; +import me.shedaniel.rei.api.IRecipeHelper; import me.shedaniel.rei.gui.ContainerScreenOverlay; +import me.shedaniel.rei.gui.RecipeViewingScreen; import me.shedaniel.rei.gui.config.ConfigScreen; -import me.shedaniel.rei.gui.widget.RecipeViewingWidgetScreen; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.keybinding.FabricKeyBinding; import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; @@ -16,9 +17,11 @@ import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.client.Mouse; import net.minecraft.client.gui.Screen; +import net.minecraft.client.gui.ingame.CreativePlayerInventoryScreen; import net.minecraft.client.util.InputUtil; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; +import net.minecraft.text.TranslatableTextComponent; import net.minecraft.util.DefaultedList; import net.minecraft.util.Identifier; import net.minecraft.util.PacketByteBuf; @@ -72,7 +75,7 @@ public class ClientHelper implements ClientModInitializer { } public static void sendDeletePacket() { - if (MinecraftClient.getInstance().interactionManager.hasCreativeInventory()) { + if (GuiHelper.getLastContainerScreen() instanceof CreativePlayerInventoryScreen) { MinecraftClient.getInstance().player.inventory.setCursorStack(ItemStack.EMPTY); return; } @@ -88,27 +91,31 @@ public class ClientHelper implements ClientModInitializer { return false; } } else { - Identifier location = Registry.ITEM.getId(cheatedStack.getItem()); + Identifier identifier = Registry.ITEM.getId(cheatedStack.getItem()); String tagMessage = cheatedStack.copy().getTag() != null && !cheatedStack.copy().getTag().isEmpty() ? cheatedStack.copy().getTag().asString() : ""; - String madeUpCommand = RoughlyEnoughItemsCore.getConfigHelper().getGiveCommandPrefix() + " " + MinecraftClient.getInstance().player.getEntityName() + " " + location.toString() + tagMessage + (cheatedStack.getAmount() != 1 ? " " + cheatedStack.getAmount() : ""); - if (madeUpCommand.length() > 256) - madeUpCommand = RoughlyEnoughItemsCore.getConfigHelper().getGiveCommandPrefix() + " " + MinecraftClient.getInstance().player.getEntityName() + " " + location.toString() + (cheatedStack.getAmount() != 1 ? " " + cheatedStack.getAmount() : ""); + String og = cheatedStack.getAmount() != 1 ? RoughlyEnoughItemsCore.getConfigHelper().getConfig().giveCommand.replaceAll(" \\{count}", "").replaceAll("\\{count}", "") : RoughlyEnoughItemsCore.getConfigHelper().getConfig().giveCommand; + String madeUpCommand = og.replaceAll("\\{player_name}", MinecraftClient.getInstance().player.getEntityName()).replaceAll("\\{item_identifier}", identifier.toString()).replaceAll("\\{nbt}", tagMessage).replaceAll("\\{count}", String.valueOf(cheatedStack.getAmount())); + if (madeUpCommand.length() > 256) { + madeUpCommand = og.replaceAll("\\{player_name}", MinecraftClient.getInstance().player.getEntityName()).replaceAll("\\{item_identifier}", identifier.toString()).replaceAll("\\{nbt}", "").replaceAll("\\{count}", String.valueOf(cheatedStack.getAmount())); + MinecraftClient.getInstance().player.addChatMessage(new TranslatableTextComponent("text.rei.too_long_nbt"), false); + } + System.out.println(madeUpCommand); MinecraftClient.getInstance().player.sendChatMessage(madeUpCommand); return true; } } public static boolean executeRecipeKeyBind(ContainerScreenOverlay overlay, ItemStack stack) { - Map> map = RecipeHelper.getInstance().getRecipesFor(stack); + Map> map = IRecipeHelper.getInstance().getRecipesFor(stack); if (map.keySet().size() > 0) - MinecraftClient.getInstance().openScreen(new RecipeViewingWidgetScreen(MinecraftClient.getInstance().window, map)); + MinecraftClient.getInstance().openScreen(new RecipeViewingScreen(MinecraftClient.getInstance().window, map)); return map.keySet().size() > 0; } public static boolean executeUsageKeyBind(ContainerScreenOverlay overlay, ItemStack stack) { - Map> map = RecipeHelper.getInstance().getUsagesFor(stack); + Map> map = IRecipeHelper.getInstance().getUsagesFor(stack); if (map.keySet().size() > 0) - MinecraftClient.getInstance().openScreen(new RecipeViewingWidgetScreen(MinecraftClient.getInstance().window, map)); + MinecraftClient.getInstance().openScreen(new RecipeViewingScreen(MinecraftClient.getInstance().window, map)); return map.keySet().size() > 0; } @@ -126,6 +133,13 @@ public class ClientHelper implements ClientModInitializer { return inventoryStacks; } + public static boolean executeViewAllRecipesKeyBind(ContainerScreenOverlay lastOverlay) { + Map> map = IRecipeHelper.getInstance().getAllRecipes(); + if (map.keySet().size() > 0) + MinecraftClient.getInstance().openScreen(new RecipeViewingScreen(MinecraftClient.getInstance().window, map)); + return map.keySet().size() > 0; + } + @Override public void onInitializeClient() { this.cheating = false; diff --git a/src/main/java/me/shedaniel/rei/client/ConfigHelper.java b/src/main/java/me/shedaniel/rei/client/ConfigHelper.java index 2331cb8bd..d31ec4b88 100644 --- a/src/main/java/me/shedaniel/rei/client/ConfigHelper.java +++ b/src/main/java/me/shedaniel/rei/client/ConfigHelper.java @@ -61,72 +61,16 @@ public class ConfigHelper { saveConfig(); } - public REIItemListOrdering getItemListOrdering() { - return config.itemListOrdering; - } - - public void setItemListOrdering(REIItemListOrdering ordering) { - config.itemListOrdering = ordering; - } - - public boolean isAscending() { - return config.isAscending; - } - - public void setAscending(boolean ascending) { - config.isAscending = ascending; + public REIConfig getConfig() { + return config; } public boolean craftableOnly() { - return craftableOnly && config.enableCraftableOnlyButton; + return craftableOnly; } public void toggleCraftableOnly() { craftableOnly = !craftableOnly; } - public boolean showCraftableOnlyButton() { - return config.enableCraftableOnlyButton; - } - - public void setShowCraftableOnlyButton(boolean enableCraftableOnlyButton) { - config.enableCraftableOnlyButton = enableCraftableOnlyButton; - } - - public String getGiveCommandPrefix() { - return config.giveCommandPrefix; - } - - public boolean sideSearchField() { - return config.sideSearchField; - } - - public void setSideSearchField(boolean sideSearchField) { - config.sideSearchField = sideSearchField; - } - - public boolean checkUpdates() { - return config.checkUpdates; - } - - public void setCheckUpdates(boolean checkUpdates) { - config.checkUpdates = checkUpdates; - } - - public boolean isMirrorItemPanel() { - return config.mirrorItemPanel; - } - - public void setMirrorItemPanel(boolean mirrorItemPanel) { - config.mirrorItemPanel = mirrorItemPanel; - } - - public boolean isLoadingDefaultPlugin() { - return config.loadDefaultPlugin; - } - - public void setLoadingDefaultPlugin(boolean loadDefaultPlugin) { - config.loadDefaultPlugin = loadDefaultPlugin; - } - } diff --git a/src/main/java/me/shedaniel/rei/client/REIConfig.java b/src/main/java/me/shedaniel/rei/client/REIConfig.java index be4eabfeb..320cb7afb 100644 --- a/src/main/java/me/shedaniel/rei/client/REIConfig.java +++ b/src/main/java/me/shedaniel/rei/client/REIConfig.java @@ -11,9 +11,15 @@ public class REIConfig { public boolean isAscending = true; public boolean enableCraftableOnlyButton = true; public boolean sideSearchField = false; - public String giveCommandPrefix = "/give"; + public String giveCommand = "/give {player_name} {item_identifier}{nbt} {count}"; + public String gamemodeCommand = "/gamemode {gamemode}"; + public String weatherCommand = "/weather {weather}"; public boolean checkUpdates = true; public boolean mirrorItemPanel = false; public boolean loadDefaultPlugin = true; + public boolean disableCreditsButton = false; + public int maxRecipePerPage = 3; + public boolean fixRamUsage = false; + public boolean showUtilsButtons = false; } diff --git a/src/main/java/me/shedaniel/rei/client/RecipeHelper.java b/src/main/java/me/shedaniel/rei/client/RecipeHelper.java index 96490c6dd..10a3b8cfe 100644 --- a/src/main/java/me/shedaniel/rei/client/RecipeHelper.java +++ b/src/main/java/me/shedaniel/rei/client/RecipeHelper.java @@ -11,20 +11,19 @@ import net.minecraft.util.Identifier; import java.awt.*; import java.util.*; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; -public class RecipeHelper { +public class RecipeHelper implements IRecipeHelper { + private final AtomicInteger recipeCount = new AtomicInteger(); private final Map> recipeCategoryListMap = Maps.newHashMap(); private final List categories = Lists.newArrayList(); private final Map speedCraftAreaSupplierMap = Maps.newHashMap(); private final Map> speedCraftFunctionalMap = Maps.newHashMap(); private RecipeManager recipeManager; - public static RecipeHelper getInstance() { - return RoughlyEnoughItemsCore.getRecipeHelper(); - } - + @Override public List findCraftableByItems(List inventoryItems) { List craftables = new ArrayList<>(); for(List value : recipeCategoryListMap.values()) @@ -54,17 +53,21 @@ public class RecipeHelper { return craftables.stream().distinct().collect(Collectors.toList()); } + @Override public void registerCategory(IRecipeCategory category) { categories.add(category); recipeCategoryListMap.put(category.getIdentifier(), Lists.newLinkedList()); } + @Override public void registerDisplay(Identifier categoryIdentifier, IRecipeDisplay display) { if (!recipeCategoryListMap.containsKey(categoryIdentifier)) return; + recipeCount.incrementAndGet(); recipeCategoryListMap.get(categoryIdentifier).add(display); } + @Override public Map> getRecipesFor(ItemStack stack) { Map> categoriesMap = new HashMap<>(); categories.forEach(f -> categoriesMap.put(f.getIdentifier(), Lists.newArrayList())); @@ -87,10 +90,12 @@ public class RecipeHelper { return categories.stream().filter(category -> category.getIdentifier().equals(identifier)).findFirst().orElse(null); } + @Override public RecipeManager getRecipeManager() { return recipeManager; } + @Override public Map> getUsagesFor(ItemStack stack) { Map> categoriesMap = new HashMap<>(); categories.forEach(f -> categoriesMap.put(f.getIdentifier(), Lists.newArrayList())); @@ -119,28 +124,31 @@ public class RecipeHelper { return recipeCategoryListMap; } - public List getCategories() { + @Override + public List getAllCategories() { return new LinkedList<>(categories); } - public SpeedCraftAreaSupplier getSpeedCraftButtonArea(IRecipeCategory category) { + @Override + public Optional getSpeedCraftButtonArea(IRecipeCategory category) { if (!speedCraftAreaSupplierMap.containsKey(category.getIdentifier())) - return bounds -> { - return new Rectangle((int) bounds.getMaxX() - 16, (int) bounds.getMaxY() - 16, 10, 10); - }; - return speedCraftAreaSupplierMap.get(category.getIdentifier()); + return Optional.of(bounds -> new Rectangle((int) bounds.getMaxX() - 16, (int) bounds.getMaxY() - 16, 10, 10)); + return Optional.ofNullable(speedCraftAreaSupplierMap.get(category.getIdentifier())); } + @Override public void registerSpeedCraftButtonArea(Identifier category, SpeedCraftAreaSupplier rectangle) { speedCraftAreaSupplierMap.put(category, rectangle); } + @Override public List getSpeedCraftFunctional(IRecipeCategory category) { if (speedCraftFunctionalMap.get(category.getIdentifier()) == null) return Lists.newArrayList(); return speedCraftFunctionalMap.get(category.getIdentifier()); } + @Override public void registerSpeedCraftFunctional(Identifier category, SpeedCraftFunctional functional) { List list = speedCraftFunctionalMap.containsKey(category) ? new LinkedList<>(speedCraftFunctionalMap.get(category)) : Lists.newLinkedList(); list.add(functional); @@ -149,6 +157,7 @@ public class RecipeHelper { @SuppressWarnings("deprecation") public void recipesLoaded(RecipeManager recipeManager) { + this.recipeCount.set(0); this.recipeManager = recipeManager; this.recipeCategoryListMap.clear(); this.categories.clear(); @@ -159,16 +168,13 @@ public class RecipeHelper { return second.getPriority() - first.getPriority(); }); RoughlyEnoughItemsCore.LOGGER.info("Loading %d REI plugins: %s", plugins.size(), String.join(", ", plugins.stream().map(plugin -> { - Identifier identifier = RoughlyEnoughItemsCore.getPluginIdentifier(plugin); - return identifier == null ? "NULL" : identifier.toString(); + return RoughlyEnoughItemsCore.getPluginIdentifier(plugin).map(Identifier::toString).orElseGet(() -> "null"); }).collect(Collectors.toList()))); Collections.reverse(plugins); RoughlyEnoughItemsCore.getItemRegisterer().getModifiableItemList().clear(); IPluginDisabler pluginDisabler = RoughlyEnoughItemsCore.getPluginDisabler(); plugins.forEach(plugin -> { - Identifier identifier = RoughlyEnoughItemsCore.getPluginIdentifier(plugin); - if (identifier == null) - identifier = new Identifier("null"); + Identifier identifier = RoughlyEnoughItemsCore.getPluginIdentifier(plugin).orElseGet(() -> new Identifier("null")); if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_ITEMS)) plugin.registerItems(RoughlyEnoughItemsCore.getItemRegisterer()); if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_CATEGORIES)) @@ -178,9 +184,25 @@ public class RecipeHelper { if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_SPEED_CRAFT)) plugin.registerSpeedCraft(this); }); - RoughlyEnoughItemsCore.LOGGER.info("Registered REI Categories: " + String.join(", ", categories.stream().map(category -> { - return category.getCategoryName(); - }).collect(Collectors.toList()))); + RoughlyEnoughItemsCore.LOGGER.info("Registered REI Categories: " + String.join(", ", categories.stream().map(IRecipeCategory::getCategoryName).collect(Collectors.toList()))); + RoughlyEnoughItemsCore.LOGGER.info("Registered %d recipes for REI.", recipeCount.get()); + } + + @Override + public int getRecipeCount() { + return recipeCount.get(); + } + + @Override + public Map> getAllRecipes() { + Map> map = Maps.newLinkedHashMap(); + Map> tempMap = Maps.newLinkedHashMap(); + recipeCategoryListMap.forEach((identifier, recipeDisplays) -> tempMap.put(identifier, new LinkedList<>(recipeDisplays))); + categories.forEach(category -> { + if (tempMap.containsKey(category.getIdentifier())) + map.put(category, tempMap.get(category.getIdentifier())); + }); + return map; } } diff --git a/src/main/java/me/shedaniel/rei/client/Weather.java b/src/main/java/me/shedaniel/rei/client/Weather.java new file mode 100644 index 000000000..b4cc2dc04 --- /dev/null +++ b/src/main/java/me/shedaniel/rei/client/Weather.java @@ -0,0 +1,38 @@ +package me.shedaniel.rei.client; + +public enum Weather { + CLEAR(0, "Clear"), RAIN(1, "Rain"), THUNDER(2, "Thunder"); + + private final int id; + private final String name; + + Weather(int id, String name) { + this.id = id; + this.name = name; + } + + public static Weather byId(int int_1) { + return byId(int_1, CLEAR); + } + + public static Weather byId(int int_1, Weather gameMode_1) { + Weather[] var2 = values(); + int var3 = var2.length; + + for(int var4 = 0; var4 < var3; ++var4) { + Weather gameMode_2 = var2[var4]; + if (gameMode_2.id == int_1) + return gameMode_2; + } + return gameMode_1; + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + +} diff --git a/src/main/java/me/shedaniel/rei/gui/ContainerScreenOverlay.java b/src/main/java/me/shedaniel/rei/gui/ContainerScreenOverlay.java index 896b4b872..abdfb15d0 100644 --- a/src/main/java/me/shedaniel/rei/gui/ContainerScreenOverlay.java +++ b/src/main/java/me/shedaniel/rei/gui/ContainerScreenOverlay.java @@ -5,27 +5,37 @@ import com.mojang.blaze3d.platform.GlStateManager; import me.shedaniel.rei.RoughlyEnoughItemsCore; import me.shedaniel.rei.client.ClientHelper; import me.shedaniel.rei.client.GuiHelper; +import me.shedaniel.rei.client.Weather; import me.shedaniel.rei.gui.credits.CreditsScreen; import me.shedaniel.rei.gui.widget.*; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.audio.PositionedSoundInstance; import net.minecraft.client.gui.ContainerScreen; -import net.minecraft.client.gui.DrawableContainer; -import net.minecraft.client.gui.GuiEventListener; +import net.minecraft.client.gui.InputListener; +import net.minecraft.client.gui.Screen; +import net.minecraft.client.gui.ScreenComponent; import net.minecraft.client.render.GuiLighting; import net.minecraft.client.resource.language.I18n; import net.minecraft.client.util.Window; +import net.minecraft.client.world.ClientWorld; import net.minecraft.item.ItemStack; +import net.minecraft.sound.SoundEvents; import net.minecraft.text.TranslatableTextComponent; +import net.minecraft.util.Identifier; import net.minecraft.util.math.MathHelper; +import net.minecraft.world.GameMode; +import net.minecraft.world.World; import java.awt.*; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; -public class ContainerScreenOverlay extends DrawableContainer { +public class ContainerScreenOverlay extends ScreenComponent { + private static final Identifier CHEST_GUI_TEXTURE = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer.png"); private static final List QUEUED_TOOLTIPS = Lists.newArrayList(); public static String searchTerm = ""; private static int page = 0; @@ -64,36 +74,95 @@ public class ContainerScreenOverlay extends DrawableContainer { } }); page = MathHelper.clamp(page, 0, getTotalPage()); - widgets.add(new ButtonWidget(RoughlyEnoughItemsCore.getConfigHelper().isMirrorItemPanel() ? window.getScaledWidth() - 50 : 10, 10, 40, 20, "") { - @Override - public void draw(int int_1, int int_2, float float_1) { - this.text = getCheatModeText(); - super.draw(int_1, int_2, float_1); - } - - @Override - public void onPressed(int button, double mouseX, double mouseY) { - ClientHelper.setCheating(!ClientHelper.isCheating()); - } - }); - widgets.add(new ButtonWidget(RoughlyEnoughItemsCore.getConfigHelper().isMirrorItemPanel() ? window.getScaledWidth() - 50 : 10, 35, 40, 20, I18n.translate("text.rei.config")) { + widgets.add(new ButtonWidget(RoughlyEnoughItemsCore.getConfigHelper().getConfig().mirrorItemPanel ? window.getScaledWidth() - 30 : 10, 10, 20, 20, "") { @Override public void onPressed(int button, double mouseX, double mouseY) { + if (Screen.isShiftPressed()) { + ClientHelper.setCheating(!ClientHelper.isCheating()); + return; + } ClientHelper.openConfigWindow(GuiHelper.getLastContainerScreen()); } - }); - widgets.add(new ButtonWidget(RoughlyEnoughItemsCore.getConfigHelper().isMirrorItemPanel() ? window.getScaledWidth() - 50 : 10, window.getScaledHeight() - 30, 40, 20, I18n.translate("text.rei.credits")) { + @Override - public void onPressed(int button, double mouseX, double mouseY) { - MinecraftClient.getInstance().openScreen(new CreditsScreen(GuiHelper.getLastContainerScreen())); + public void draw(int mouseX, int mouseY, float partialTicks) { + super.draw(mouseX, mouseY, partialTicks); + GuiLighting.disable(); + if (ClientHelper.isCheating()) + drawRect(getBounds().x, getBounds().y, getBounds().x + 20, getBounds().y + 20, new Color(255, 0, 0, 42).getRGB()); + MinecraftClient.getInstance().getTextureManager().bindTexture(CHEST_GUI_TEXTURE); + GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F); + drawTexturedRect(getBounds().x + 3, getBounds().y + 3, 0, 0, 14, 14); + if (isHighlighted(mouseX, mouseY)) { + List list = new LinkedList<>(Arrays.asList(I18n.translate("text.rei.config_tooltip").split("\n"))); + list.add(" "); + if (!ClientHelper.isCheating()) + list.add("§c§m" + I18n.translate("text.rei.cheating")); + else + list.add("§a" + I18n.translate("text.rei.cheating")); + addTooltip(new QueuedTooltip(new Point(mouseX, mouseY), list)); + } } }); - widgets.add(new LabelWidget(rectangle.x + (rectangle.width / 2), rectangle.y + 10, "") { + if (!RoughlyEnoughItemsCore.getConfigHelper().getConfig().disableCreditsButton) + widgets.add(new ButtonWidget(RoughlyEnoughItemsCore.getConfigHelper().getConfig().mirrorItemPanel ? window.getScaledWidth() - 50 : 10, window.getScaledHeight() - 30, 40, 20, I18n.translate("text.rei.credits")) { + @Override + public void onPressed(int button, double mouseX, double mouseY) { + MinecraftClient.getInstance().openScreen(new CreditsScreen(GuiHelper.getLastContainerScreen())); + } + }); + if (RoughlyEnoughItemsCore.getConfigHelper().getConfig().showUtilsButtons) { + widgets.add(new ButtonWidget(RoughlyEnoughItemsCore.getConfigHelper().getConfig().mirrorItemPanel ? window.getScaledWidth() - 55 : 35, 10, 20, 20, "") { + @Override + public void onPressed(int button, double mouseX, double mouseY) { + MinecraftClient.getInstance().player.sendChatMessage(RoughlyEnoughItemsCore.getConfigHelper().getConfig().gamemodeCommand.replaceAll("\\{gamemode}", getNextGameMode().getName())); + } + + @Override + public void draw(int mouseX, int mouseY, float partialTicks) { + text = getGameModeShortText(getCurrentGameMode()); + super.draw(mouseX, mouseY, partialTicks); + if (isHighlighted(mouseX, mouseY)) { + List list = Arrays.asList(I18n.translate("text.rei.gamemode_button.tooltip", getGameModeText(getNextGameMode())).split("\n")); + addTooltip(new QueuedTooltip(new Point(mouseX, mouseY), list)); + } + } + }); + widgets.add(new ButtonWidget(RoughlyEnoughItemsCore.getConfigHelper().getConfig().mirrorItemPanel ? window.getScaledWidth() - 80 : 60, 10, 20, 20, "") { + @Override + public void onPressed(int button, double mouseX, double mouseY) { + MinecraftClient.getInstance().player.sendChatMessage(RoughlyEnoughItemsCore.getConfigHelper().getConfig().weatherCommand.replaceAll("\\{weather}", getNextWeather().getName().toLowerCase())); + } + + @Override + public void draw(int mouseX, int mouseY, float partialTicks) { + super.draw(mouseX, mouseY, partialTicks); + GuiLighting.disable(); + MinecraftClient.getInstance().getTextureManager().bindTexture(CHEST_GUI_TEXTURE); + GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F); + drawTexturedRect(getBounds().x + 3, getBounds().y + 3, getCurrentWeather().getId() * 14, 14, 14, 14); + if (isHighlighted(mouseX, mouseY)) { + List list = Arrays.asList(I18n.translate("text.rei.weather_button.tooltip", getNextWeather().getName()).split("\n")); + addTooltip(new QueuedTooltip(new Point(mouseX, mouseY), list)); + } + } + }); + } + widgets.add(new ClickableLabelWidget(rectangle.x + (rectangle.width / 2), rectangle.y + 10, "") { @Override public void draw(int mouseX, int mouseY, float partialTicks) { page = MathHelper.clamp(page, 0, getTotalPage()); this.text = String.format("%s/%s", page + 1, getTotalPage() + 1); super.draw(mouseX, mouseY, partialTicks); + if (isHighlighted(mouseX, mouseY)) + GuiHelper.getLastOverlay().addTooltip(new QueuedTooltip(new Point(mouseX, mouseY), Arrays.asList(I18n.translate("text.rei.go_back_first_page").split("\n")))); + } + + @Override + public void onLabelClicked() { + MinecraftClient.getInstance().getSoundLoader().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); + page = 0; + itemListOverlay.updateList(getItemListArea(), page, searchTerm); } }); if (GuiHelper.searchField == null) @@ -111,10 +180,10 @@ public class ContainerScreenOverlay extends DrawableContainer { searchTerm = s; itemListOverlay.updateList(getItemListArea(), page, searchTerm); }); - GuiHelper.searchField.setBounds(getTextFieldArea()); + GuiHelper.searchField.getBounds().setBounds(getTextFieldArea()); this.widgets.add(GuiHelper.searchField); GuiHelper.searchField.setText(searchTerm); - if (RoughlyEnoughItemsCore.getConfigHelper().showCraftableOnlyButton()) + if (RoughlyEnoughItemsCore.getConfigHelper().getConfig().enableCraftableOnlyButton) this.widgets.add(new CraftableToggleButtonWidget(getCraftableToggleArea()) { @Override public void onPressed(int button, double mouseX, double mouseY) { @@ -126,12 +195,77 @@ public class ContainerScreenOverlay extends DrawableContainer { this.itemListOverlay.updateList(getItemListArea(), page, searchTerm); } + private Weather getNextWeather() { + try { + Weather current = getCurrentWeather(); + int next = current.getId() + 1; + if (next >= 3) + next = 0; + return Weather.byId(next); + } catch (Exception e) { + return Weather.CLEAR; + } + } + + private Weather getCurrentWeather() { + ClientWorld world = MinecraftClient.getInstance().world; + if (world.isThundering()) + return Weather.THUNDER; + if (world.getLevelProperties().isRaining()) + return Weather.RAIN; + return Weather.CLEAR; + } + + private String getGameModeShortText(GameMode gameMode) { + switch (gameMode) { + case CREATIVE: + return "C"; + case SURVIVAL: + return "S"; + case ADVENTURE: + return "A"; + case SPECTATOR: + return "SP"; + } + return gameMode.name(); + } + + private String getGameModeText(GameMode gameMode) { + switch (gameMode) { + case CREATIVE: + return "Creative"; + case SURVIVAL: + return "Survival"; + case ADVENTURE: + return "Adventure"; + case SPECTATOR: + return "Spectator"; + } + return gameMode.name(); + } + + private GameMode getNextGameMode() { + try { + GameMode current = getCurrentGameMode(); + int next = current.getId() + 1; + if (next >= 3) + next = 0; + return GameMode.byId(next); + } catch (Exception e) { + return GameMode.INVALID; + } + } + + private GameMode getCurrentGameMode() { + return MinecraftClient.getInstance().getNetworkHandler().method_2871(MinecraftClient.getInstance().player.getGameProfile().getId()).getGameMode(); + } + private Rectangle getTextFieldArea() { - int widthRemoved = RoughlyEnoughItemsCore.getConfigHelper().showCraftableOnlyButton() ? 22 : 0; - if (RoughlyEnoughItemsCore.getConfigHelper().sideSearchField()) + int widthRemoved = RoughlyEnoughItemsCore.getConfigHelper().getConfig().enableCraftableOnlyButton ? 22 : 0; + if (RoughlyEnoughItemsCore.getConfigHelper().getConfig().sideSearchField) return new Rectangle(rectangle.x + 2, window.getScaledHeight() - 22, rectangle.width - 6 - widthRemoved, 18); - if (MinecraftClient.getInstance().currentScreen instanceof RecipeViewingWidgetScreen) { - RecipeViewingWidgetScreen widget = (RecipeViewingWidgetScreen) MinecraftClient.getInstance().currentScreen; + if (MinecraftClient.getInstance().currentScreen instanceof RecipeViewingScreen) { + RecipeViewingScreen widget = (RecipeViewingScreen) MinecraftClient.getInstance().currentScreen; return new Rectangle(widget.getBounds().x, window.getScaledHeight() - 22, widget.getBounds().width - widthRemoved, 18); } return new Rectangle(GuiHelper.getLastMixinContainerScreen().rei_getContainerLeft(), window.getScaledHeight() - 22, GuiHelper.getLastMixinContainerScreen().rei_getContainerWidth() - widthRemoved, 18); @@ -149,7 +283,7 @@ public class ContainerScreenOverlay extends DrawableContainer { } private Rectangle getItemListArea() { - return new Rectangle(rectangle.x + 2, rectangle.y + 24, rectangle.width - 4, rectangle.height - (RoughlyEnoughItemsCore.getConfigHelper().sideSearchField() ? 27 + 22 : 27)); + return new Rectangle(rectangle.x + 2, rectangle.y + 24, rectangle.width - 4, rectangle.height - (RoughlyEnoughItemsCore.getConfigHelper().getConfig().sideSearchField ? 27 + 22 : 27)); } public Rectangle getRectangle() { @@ -168,7 +302,9 @@ public class ContainerScreenOverlay extends DrawableContainer { GuiLighting.disable(); this.draw(mouseX, mouseY, partialTicks); GuiLighting.disable(); - QUEUED_TOOLTIPS.stream().filter(queuedTooltip -> queuedTooltip != null).forEach(queuedTooltip -> MinecraftClient.getInstance().currentScreen.drawTooltip(queuedTooltip.text, queuedTooltip.mouse.x, queuedTooltip.mouse.y)); + Screen currentScreen = MinecraftClient.getInstance().currentScreen; + if (!(currentScreen instanceof RecipeViewingScreen) || !((RecipeViewingScreen) currentScreen).choosePageActivated) + QUEUED_TOOLTIPS.stream().filter(queuedTooltip -> queuedTooltip != null).forEach(queuedTooltip -> MinecraftClient.getInstance().currentScreen.drawTooltip(queuedTooltip.text, queuedTooltip.mouse.x, queuedTooltip.mouse.y)); QUEUED_TOOLTIPS.clear(); GuiLighting.disable(); } @@ -205,11 +341,11 @@ public class ContainerScreenOverlay extends DrawableContainer { } private Rectangle calculateBoundary() { - if (!RoughlyEnoughItemsCore.getConfigHelper().isMirrorItemPanel()) { + if (!RoughlyEnoughItemsCore.getConfigHelper().getConfig().mirrorItemPanel) { int startX = GuiHelper.getLastMixinContainerScreen().rei_getContainerLeft() + GuiHelper.getLastMixinContainerScreen().rei_getContainerWidth() + 10; int width = window.getScaledWidth() - startX; - if (MinecraftClient.getInstance().currentScreen instanceof RecipeViewingWidgetScreen) { - RecipeViewingWidgetScreen widget = (RecipeViewingWidgetScreen) MinecraftClient.getInstance().currentScreen; + if (MinecraftClient.getInstance().currentScreen instanceof RecipeViewingScreen) { + RecipeViewingScreen widget = (RecipeViewingScreen) MinecraftClient.getInstance().currentScreen; startX = widget.getBounds().x + widget.getBounds().width + 10; width = window.getScaledWidth() - startX; } @@ -219,8 +355,8 @@ public class ContainerScreenOverlay extends DrawableContainer { } private int getLeft() { - if (MinecraftClient.getInstance().currentScreen instanceof RecipeViewingWidgetScreen) { - RecipeViewingWidgetScreen widget = (RecipeViewingWidgetScreen) MinecraftClient.getInstance().currentScreen; + if (MinecraftClient.getInstance().currentScreen instanceof RecipeViewingScreen) { + RecipeViewingScreen widget = (RecipeViewingScreen) MinecraftClient.getInstance().currentScreen; return widget.getBounds().x; } if (MinecraftClient.getInstance().player.getRecipeBook().isGuiOpen()) @@ -253,7 +389,7 @@ public class ContainerScreenOverlay extends DrawableContainer { @Override public boolean keyPressed(int int_1, int int_2, int int_3) { - for(GuiEventListener listener : widgets) + for(InputListener listener : widgets) if (listener.keyPressed(int_1, int_2, int_3)) return true; if (ClientHelper.HIDE.matchesKey(int_1, int_2)) { @@ -269,11 +405,11 @@ public class ContainerScreenOverlay extends DrawableContainer { itemStack = ((ItemSlotWidget) widget).getCurrentStack(); break; } - if (itemStack == null && MinecraftClient.getInstance().currentScreen instanceof RecipeViewingWidgetScreen) { - RecipeViewingWidgetScreen recipeViewingWidget = (RecipeViewingWidgetScreen) MinecraftClient.getInstance().currentScreen; - for(GuiEventListener entry : recipeViewingWidget.method_1968()) - if (entry instanceof ItemSlotWidget && ((HighlightableWidget) entry).isHighlighted(point.x, point.y)) { - itemStack = ((ItemSlotWidget) entry).getCurrentStack(); + if (itemStack == null && MinecraftClient.getInstance().currentScreen instanceof RecipeViewingScreen) { + RecipeViewingScreen recipeViewingWidget = (RecipeViewingScreen) MinecraftClient.getInstance().currentScreen; + for(InputListener listener : recipeViewingWidget.getInputListeners()) + if (listener instanceof ItemSlotWidget && ((HighlightableWidget) listener).isHighlighted(point.x, point.y)) { + itemStack = ((ItemSlotWidget) listener).getCurrentStack(); break; } } @@ -293,14 +429,14 @@ public class ContainerScreenOverlay extends DrawableContainer { public boolean charTyped(char char_1, int int_1) { if (!GuiHelper.isOverlayVisible()) return false; - for(GuiEventListener listener : method_1968()) + for(InputListener listener : getInputListeners()) if (listener.charTyped(char_1, int_1)) return true; return super.charTyped(char_1, int_1); } @Override - public List method_1968() { + public List getInputListeners() { return widgets; } diff --git a/src/main/java/me/shedaniel/rei/gui/RecipeViewingScreen.java b/src/main/java/me/shedaniel/rei/gui/RecipeViewingScreen.java new file mode 100644 index 000000000..d3a066e29 --- /dev/null +++ b/src/main/java/me/shedaniel/rei/gui/RecipeViewingScreen.java @@ -0,0 +1,381 @@ +package me.shedaniel.rei.gui; + +import com.google.common.collect.Lists; +import com.mojang.blaze3d.platform.GlStateManager; +import me.shedaniel.rei.RoughlyEnoughItemsCore; +import me.shedaniel.rei.api.*; +import me.shedaniel.rei.client.ClientHelper; +import me.shedaniel.rei.client.GuiHelper; +import me.shedaniel.rei.gui.widget.*; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.audio.PositionedSoundInstance; +import net.minecraft.client.gui.ContainerScreen; +import net.minecraft.client.gui.InputListener; +import net.minecraft.client.gui.Screen; +import net.minecraft.client.render.GuiLighting; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.client.util.Window; +import net.minecraft.sound.SoundEvents; +import net.minecraft.text.TranslatableTextComponent; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.MathHelper; + +import java.awt.*; +import java.util.*; +import java.util.List; +import java.util.function.Supplier; + +public class RecipeViewingScreen extends Screen { + + public static final Identifier CHEST_GUI_TEXTURE = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer.png"); + public static final Color SUB_COLOR = new Color(159, 159, 159); + private static final Identifier CREATIVE_INVENTORY_TABS = new Identifier("textures/gui/container/creative_inventory/tabs.png"); + public int guiWidth; + public int guiHeight; + public int page, categoryPages; + public int largestWidth, largestHeight; + public boolean choosePageActivated; + public RecipeChoosePageWidget recipeChoosePageWidget; + private List widgets; + private List tabs; + private Window window; + private Rectangle bounds; + private Map> categoriesMap; + private List categories; + private IRecipeCategory selectedCategory; + private ButtonWidget recipeBack, recipeNext, categoryBack, categoryNext; + + public RecipeViewingScreen(Window window, Map> categoriesMap) { + this.categoryPages = 0; + this.window = window; + this.widgets = Lists.newArrayList(); + this.bounds = new Rectangle(window.getScaledWidth() / 2 - guiWidth / 2, window.getScaledHeight() / 2 - guiHeight / 2, 176, 186); + this.categoriesMap = categoriesMap; + this.categories = Lists.newArrayList(); + IRecipeHelper.getInstance().getAllCategories().forEach(category -> { + if (categoriesMap.containsKey(category)) + categories.add(category); + }); + this.selectedCategory = categories.get(0); + this.tabs = new ArrayList<>(); + this.choosePageActivated = false; + } + + public static SpeedCraftFunctional getSpeedCraftFunctionalByCategory(ContainerScreen containerScreen, IRecipeCategory category) { + for(SpeedCraftFunctional functional : IRecipeHelper.getInstance().getSpeedCraftFunctional(category)) + for(Class aClass : functional.getFunctioningFor()) + if (containerScreen.getClass().isAssignableFrom(aClass)) + return functional; + return null; + } + + @Override + public boolean keyPressed(int int_1, int int_2, int int_3) { + if ((int_1 == 256 || this.client.options.keyInventory.matchesKey(int_1, int_2)) && this.doesEscapeKeyClose()) { + MinecraftClient.getInstance().openScreen(GuiHelper.getLastContainerScreen()); + GuiHelper.getLastOverlay().onInitialized(); + return true; + } + if (choosePageActivated) { + if (recipeChoosePageWidget.keyPressed(int_1, int_2, int_3)) + return true; + return false; + } + for(InputListener listener : listeners) + if (listener.keyPressed(int_1, int_2, int_3)) + return true; + return super.keyPressed(int_1, int_2, int_3); + } + + @Override + public boolean isPauseScreen() { + return false; + } + + @Override + public void onInitialized() { + super.onInitialized(); + this.tabs.clear(); + this.widgets.clear(); + this.largestWidth = window.getScaledWidth() - 100; + this.largestHeight = window.getScaledHeight() - 40; + this.guiWidth = MathHelper.clamp(getCurrentDisplayed().stream().map(display -> selectedCategory.getDisplayWidth(display)).max(Integer::compareTo).orElse(150) + 30, 0, largestWidth); + this.guiHeight = MathHelper.floor(MathHelper.clamp((selectedCategory.getDisplayHeight() + 7) * (getRecipesPerPage() + 1) + 40f, 186f, (float) largestHeight)); + this.bounds = new Rectangle(window.getScaledWidth() / 2 - guiWidth / 2, window.getScaledHeight() / 2 - guiHeight / 2, guiWidth, guiHeight); + this.page = MathHelper.clamp(page, 0, getTotalPages(selectedCategory) - 1); + + widgets.add(categoryBack = new ButtonWidget((int) bounds.getX() + 5, (int) bounds.getY() + 5, 12, 12, new TranslatableTextComponent("text.rei.left_arrow")) { + @Override + public void onPressed(int button, double mouseX, double mouseY) { + int currentCategoryIndex = categories.indexOf(selectedCategory); + currentCategoryIndex--; + if (currentCategoryIndex < 0) + currentCategoryIndex = categories.size() - 1; + selectedCategory = categories.get(currentCategoryIndex); + categoryPages = MathHelper.floor(currentCategoryIndex / 6d); + RecipeViewingScreen.this.onInitialized(); + } + }); + widgets.add(categoryNext = new ButtonWidget((int) bounds.getMaxX() - 17, (int) bounds.getY() + 5, 12, 12, new TranslatableTextComponent("text.rei.right_arrow")) { + @Override + public void onPressed(int button, double mouseX, double mouseY) { + int currentCategoryIndex = categories.indexOf(selectedCategory); + currentCategoryIndex++; + if (currentCategoryIndex >= categories.size()) + currentCategoryIndex = 0; + selectedCategory = categories.get(currentCategoryIndex); + categoryPages = MathHelper.floor(currentCategoryIndex / 6d); + RecipeViewingScreen.this.onInitialized(); + } + }); + categoryBack.enabled = categories.size() > 1; + categoryNext.enabled = categories.size() > 1; + + widgets.add(recipeBack = new ButtonWidget((int) bounds.getX() + 5, (int) bounds.getY() + 21, 12, 12, new TranslatableTextComponent("text.rei.left_arrow")) { + @Override + public void onPressed(int button, double mouseX, double mouseY) { + page--; + if (page < 0) + page = getTotalPages(selectedCategory) - 1; + RecipeViewingScreen.this.onInitialized(); + } + }); + widgets.add(recipeNext = new ButtonWidget((int) bounds.getMaxX() - 17, (int) bounds.getY() + 21, 12, 12, new TranslatableTextComponent("text.rei.right_arrow")) { + @Override + public void onPressed(int button, double mouseX, double mouseY) { + page++; + if (page >= getTotalPages(selectedCategory)) + page = 0; + RecipeViewingScreen.this.onInitialized(); + } + }); + recipeBack.enabled = categoriesMap.get(selectedCategory).size() > getRecipesPerPage(); + recipeNext.enabled = categoriesMap.get(selectedCategory).size() > getRecipesPerPage(); + + widgets.add(new ClickableLabelWidget((int) bounds.getCenterX(), (int) bounds.getY() + 7, "") { + @Override + public void draw(int mouseX, int mouseY, float partialTicks) { + this.text = selectedCategory.getCategoryName(); + super.draw(mouseX, mouseY, partialTicks); + if (isHighlighted(mouseX, mouseY)) + GuiHelper.getLastOverlay().addTooltip(new QueuedTooltip(new Point(mouseX, mouseY), Arrays.asList(I18n.translate("text.rei.view_all_categories").split("\n")))); + } + + @Override + public void onLabelClicked() { + MinecraftClient.getInstance().getSoundLoader().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); + ClientHelper.executeViewAllRecipesKeyBind(GuiHelper.getLastOverlay()); + } + }); + widgets.add(new ClickableLabelWidget((int) bounds.getCenterX(), (int) bounds.getY() + 23, "") { + @Override + public void draw(int mouseX, int mouseY, float partialTicks) { + this.text = String.format("%d/%d", page + 1, getTotalPages(selectedCategory)); + super.draw(mouseX, mouseY, partialTicks); + if (isHighlighted(mouseX, mouseY)) + GuiHelper.getLastOverlay().addTooltip(new QueuedTooltip(new Point(mouseX, mouseY), Arrays.asList(I18n.translate("text.rei.choose_page").split("\n")))); + } + + @Override + public void onLabelClicked() { + MinecraftClient.getInstance().getSoundLoader().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); + RecipeViewingScreen.this.choosePageActivated = true; + RecipeViewingScreen.this.onInitialized(); + } + }); + for(int i = 0; i < 6; i++) { + int j = i + categoryPages * 6; + if (categories.size() > j) { + TabWidget tab; + tabs.add(tab = new TabWidget(i, this, new Rectangle(bounds.x + 4 + 28 * i, bounds.y - 28, 28, 28)) { + @Override + public boolean onMouseClick(int button, double mouseX, double mouseY) { + if (getBounds().contains(mouseX, mouseY)) { + MinecraftClient.getInstance().getSoundLoader().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); + if (getId() + categoryPages * 6 == categories.indexOf(selectedCategory)) + return false; + selectedCategory = categories.get(getId() + categoryPages * 6); + page = 0; + RecipeViewingScreen.this.onInitialized(); + return true; + } + return false; + } + }); + tab.setItem(categories.get(j).getCategoryIcon(), categories.get(j).getCategoryName(), tab.getId() + categoryPages * 6 == categories.indexOf(selectedCategory)); + } + } + Optional supplier = IRecipeHelper.getInstance().getSpeedCraftB