From bd65f5d691b59689bd5f14ce02c118821f5b1728 Mon Sep 17 00:00:00 2001 From: Danielxs01 <daniel.schmidtdsi@gmail.com> Date: Sun, 21 Nov 2021 23:55:34 +0100 Subject: [PATCH] [#23/#25] Fixed new item selection --- .../stellwand/content/guis/SelectItem.java | 68 ++++++++++++------- .../stellwand/content/items/CustomItems.java | 2 +- .../items/ItemBlockButtonReceiver.java | 16 ++--- .../content/items/ItemBlockFiller.java | 3 +- .../content/items/ItemBlockMultisignal.java | 12 ++-- .../content/items/ItemBlockSender.java | 3 +- .../content/items/ItemBlockSignal.java | 3 +- .../stellwand/contentpacks/Content.java | 12 ++-- 8 files changed, 63 insertions(+), 56 deletions(-) diff --git a/src/main/java/net/landofrails/stellwand/content/guis/SelectItem.java b/src/main/java/net/landofrails/stellwand/content/guis/SelectItem.java index 724b10b5..93806670 100644 --- a/src/main/java/net/landofrails/stellwand/content/guis/SelectItem.java +++ b/src/main/java/net/landofrails/stellwand/content/guis/SelectItem.java @@ -1,5 +1,6 @@ package net.landofrails.stellwand.content.guis; +import cam72cam.mod.MinecraftClient; import cam72cam.mod.entity.Player; import cam72cam.mod.gui.GuiRegistry; import cam72cam.mod.gui.helpers.ItemPickerGUI; @@ -13,32 +14,35 @@ import net.landofrails.stellwand.contentpacks.Content; import net.landofrails.stellwand.contentpacks.entries.ContentPack; import net.landofrails.stellwand.contentpacks.types.EntryType; +import net.landofrails.stellwand.utils.exceptions.ContentPackException; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Objects; +import java.util.*; import java.util.function.Consumer; public class SelectItem implements IScreen { // Only initialized once + private static final Map<EntryType, Map.Entry<ItemStack, Consumer<ItemStack>>> ENTRYTYPES = new HashMap<>(); private static GuiRegistry.GUI gui; // Initialized every call - private final List<Button> contentPackButtons = new LinkedList<>(); - private Consumer<ItemStack> selectedItem; - private ItemStack current = null; + private ItemStack current; private EntryType entryType; - private ItemStack defaultItem; public static void init(GuiRegistry.GUI gui) { SelectItem.gui = gui; } + public static void setEntryType(EntryType type, Map.Entry<ItemStack, Consumer<ItemStack>> defaultAndSelectedItem) { + ENTRYTYPES.putIfAbsent(type, defaultAndSelectedItem); + } + @Override public void init(IScreenBuilder screen) { + ItemStack itemStack = MinecraftClient.getPlayer().getHeldItem(Player.Hand.PRIMARY); + this.entryType = getEntryTypeFromItem(itemStack); + generateContentPackButtons(screen, Content.getContentPacksFor(entryType)); } @@ -51,13 +55,9 @@ public void onEnterKey(IScreenBuilder builder) { @Override public void onClose() { if (current != null) - selectedItem.accept(current); + ENTRYTYPES.get(entryType).getValue().accept(current); // Reset variables - contentPackButtons.clear(); - selectedItem = null; entryType = null; - current = null; - defaultItem = null; } @Override @@ -75,7 +75,7 @@ private List<ItemStack> getItemStackForContentPack(ContentPack pack, EntryType t items.add(is); }); if (items.isEmpty()) - items.add(defaultItem); + items.add(ENTRYTYPES.get(entryType).getKey()); return items; } @@ -88,21 +88,25 @@ private CustomItem getCustomItemForEntryType(EntryType type) { case BLOCKSIGNAL: return CustomItems.ITEMBLOCKSIGNAL; case BLOCKFILLER: - default: return CustomItems.ITEMBLOCKFILLER; + case BLOCKRECEIVER: + return CustomItems.ITEMBLOCKRECEIVER; + default: + throw new ContentPackException("I must have forgot to add this EntryType to this method: " + type); } } - private void generateContentPackButtons(IScreenBuilder screen, List<ContentPack> contentPacks) { - for (int i = 0; i < contentPacks.size(); i++) { - ContentPack contentPack = contentPacks.get(i); + private void generateContentPackButtons(IScreenBuilder screen, Collection<ContentPack> contentPacks) { + int index = 0; + for (ContentPack contentPack : contentPacks) { int height = 20; int width = 200; int x = -width / 2; int marginY = 5; int offsetY = -50; - int y = ((height + marginY) * i) + offsetY; - Button button = new Button(screen, x, y, width, height, contentPack.getButtonName()) { + int y = ((height + marginY) * index) + offsetY; + + new Button(screen, x, y, width, height, contentPack.getButtonName()) { @Override public void onClick(Player.Hand hand) { List<ItemStack> selection = getItemStackForContentPack(contentPack, entryType); @@ -113,8 +117,24 @@ public void onClick(Player.Hand hand) { ip.show(); } }; - contentPackButtons.add(button); + + index++; + } + } + + private EntryType getEntryTypeFromItem(ItemStack itemStack) { + if (itemStack.is(CustomItems.ITEMBLOCKFILLER)) { + return EntryType.BLOCKFILLER; + } else if (itemStack.is(CustomItems.ITEMBLOCKMULTISIGNAL)) { + return EntryType.BLOCKMULTISIGNAL; + } else if (itemStack.is(CustomItems.ITEMBLOCKRECEIVER)) { + return EntryType.BLOCKRECEIVER; + } else if (itemStack.is(CustomItems.ITEMBLOCKSENDER)) { + return EntryType.BLOCKSENDER; + } else if (itemStack.is(CustomItems.ITEMBLOCKSIGNAL)) { + return EntryType.BLOCKSIGNAL; } + return null; } /** @@ -125,10 +145,8 @@ public void onClick(Player.Hand hand) { * @param defaultItem Default item if no items are found * @param selectedItem Consumer for returning itemstack */ - public void open(Player player, EntryType type, ItemStack defaultItem, Consumer<ItemStack> selectedItem) { - this.selectedItem = selectedItem; - entryType = type; - this.defaultItem = defaultItem; + public static void open(Player player, EntryType type, ItemStack defaultItem, Consumer<ItemStack> selectedItem) { + setEntryType(type, new AbstractMap.SimpleEntry<>(defaultItem, selectedItem)); gui.open(player); } diff --git a/src/main/java/net/landofrails/stellwand/content/items/CustomItems.java b/src/main/java/net/landofrails/stellwand/content/items/CustomItems.java index a555828f..86c90686 100644 --- a/src/main/java/net/landofrails/stellwand/content/items/CustomItems.java +++ b/src/main/java/net/landofrails/stellwand/content/items/CustomItems.java @@ -43,7 +43,7 @@ public static void register() { } - // Clientsided + // Clientside public static void registerRenderers() { for (CustomItem item : itemList) { if (item instanceof ICustomTexturePath) { diff --git a/src/main/java/net/landofrails/stellwand/content/items/ItemBlockButtonReceiver.java b/src/main/java/net/landofrails/stellwand/content/items/ItemBlockButtonReceiver.java index 396a1cee..500033be 100644 --- a/src/main/java/net/landofrails/stellwand/content/items/ItemBlockButtonReceiver.java +++ b/src/main/java/net/landofrails/stellwand/content/items/ItemBlockButtonReceiver.java @@ -31,6 +31,7 @@ import org.lwjgl.opengl.GL11; import java.util.*; +import java.util.Map.Entry; import java.util.stream.Collectors; public class ItemBlockButtonReceiver extends CustomItem { @@ -67,7 +68,7 @@ public static void init() { } // ContentPack - for (Map.Entry<ContentPackEntry, String> entry : Content.getBlockReceivers().entrySet()) { + for (Entry<ContentPackEntry, String> entry : Content.getBlockReceivers().entrySet()) { try { ContentPackEntry cpe = entry.getKey(); String packId = entry.getValue(); @@ -110,9 +111,9 @@ public List<ItemStack> getItemVariants(CreativeTab creativeTab) { public ItemStack getFirstVarient() { ItemStack is = null; - Iterator<Map.Entry<ContentPackEntry, String>> it = Content.getBlockReceivers().entrySet().iterator(); + Iterator<Entry<ContentPackEntry, String>> it = Content.getBlockReceivers().entrySet().iterator(); if (it.hasNext()) { - Map.Entry<ContentPackEntry, String> entry = it.next(); + Entry<ContentPackEntry, String> entry = it.next(); ContentPackEntry cpe = entry.getKey(); is = new ItemStack(CustomItems.ITEMBLOCKRECEIVER, 1); @@ -132,8 +133,7 @@ public void onClickAir(Player player, World world, Player.Hand hand) { } int sizeInHand = player.getHeldItem(hand).getCount(); - SelectItem si = new SelectItem(); - si.open(player, EntryType.BLOCKRECEIVER, new ItemStack(CustomItems.ITEMBLOCKRECEIVER, 1), item -> { + SelectItem.open(player, EntryType.BLOCKRECEIVER, new ItemStack(CustomItems.ITEMBLOCKRECEIVER, 1), item -> { if (item != null) { player.setHeldItem(hand, item); item.setCount(sizeInHand); @@ -232,11 +232,7 @@ public ClickResult onClickBlock(Player player, World world, Vec3i pos, Player.Ha blockEntity.setContentBlockId(MISSING); blockEntity.renderEntity .setRotation(player.getRotationYawHead()); - if (facing.equals(Facing.DOWN) || facing.equals(Facing.UP)) { - blockEntity.setWallMounted(false); - } else { - blockEntity.setWallMounted(true); - } + blockEntity.setWallMounted(!facing.equals(Facing.DOWN) && !facing.equals(Facing.UP)); } // diff --git a/src/main/java/net/landofrails/stellwand/content/items/ItemBlockFiller.java b/src/main/java/net/landofrails/stellwand/content/items/ItemBlockFiller.java index 921b3a14..ff27c748 100644 --- a/src/main/java/net/landofrails/stellwand/content/items/ItemBlockFiller.java +++ b/src/main/java/net/landofrails/stellwand/content/items/ItemBlockFiller.java @@ -141,8 +141,7 @@ public void onClickAir(Player player, World world, Hand hand) { return; int sizeInHand = player.getHeldItem(hand).getCount(); - SelectItem si = new SelectItem(); - si.open(player, EntryType.BLOCKFILLER, new ItemStack(CustomItems.ITEMBLOCKFILLER, 1), item -> { + SelectItem.open(player, EntryType.BLOCKFILLER, new ItemStack(CustomItems.ITEMBLOCKFILLER, 1), item -> { if (item != null) { player.setHeldItem(hand, item); item.setCount(sizeInHand); diff --git a/src/main/java/net/landofrails/stellwand/content/items/ItemBlockMultisignal.java b/src/main/java/net/landofrails/stellwand/content/items/ItemBlockMultisignal.java index 00644735..3994e094 100644 --- a/src/main/java/net/landofrails/stellwand/content/items/ItemBlockMultisignal.java +++ b/src/main/java/net/landofrails/stellwand/content/items/ItemBlockMultisignal.java @@ -31,6 +31,7 @@ import org.lwjgl.opengl.GL11; import java.util.*; +import java.util.Map.Entry; import java.util.stream.Collectors; public class ItemBlockMultisignal extends CustomItem { @@ -67,7 +68,7 @@ public static void init() { } // ContentPack - for (Map.Entry<ContentPackEntry, String> entry : Content.getBlockMultisignals().entrySet()) { + for (Entry<ContentPackEntry, String> entry : Content.getBlockMultisignals().entrySet()) { try { ContentPackEntry cpe = entry.getKey(); String packId = entry.getValue(); @@ -110,9 +111,9 @@ public List<ItemStack> getItemVariants(CreativeTab creativeTab) { public ItemStack getFirstVarient() { ItemStack is = null; - Iterator<Map.Entry<ContentPackEntry, String>> it = Content.getBlockMultisignals().entrySet().iterator(); + Iterator<Entry<ContentPackEntry, String>> it = Content.getBlockMultisignals().entrySet().iterator(); if (it.hasNext()) { - Map.Entry<ContentPackEntry, String> entry = it.next(); + Entry<ContentPackEntry, String> entry = it.next(); ContentPackEntry cpe = entry.getKey(); is = new ItemStack(CustomItems.ITEMBLOCKMULTISIGNAL, 1); @@ -130,10 +131,9 @@ public void onClickAir(Player player, World world, Player.Hand hand) { if (world.isServer) { return; } - + int sizeInHand = player.getHeldItem(hand).getCount(); - SelectItem si = new SelectItem(); - si.open(player, EntryType.BLOCKMULTISIGNAL, new ItemStack(CustomItems.ITEMBLOCKMULTISIGNAL, 1), item -> { + SelectItem.open(player, EntryType.BLOCKMULTISIGNAL, new ItemStack(CustomItems.ITEMBLOCKMULTISIGNAL, 1), item -> { if (item != null) { player.setHeldItem(hand, item); item.setCount(sizeInHand); diff --git a/src/main/java/net/landofrails/stellwand/content/items/ItemBlockSender.java b/src/main/java/net/landofrails/stellwand/content/items/ItemBlockSender.java index 72ce3ec9..f4b83866 100644 --- a/src/main/java/net/landofrails/stellwand/content/items/ItemBlockSender.java +++ b/src/main/java/net/landofrails/stellwand/content/items/ItemBlockSender.java @@ -142,8 +142,7 @@ public void onClickAir(Player player, World world, Hand hand) { return; int sizeInHand = player.getHeldItem(hand).getCount(); - SelectItem si = new SelectItem(); - si.open(player, EntryType.BLOCKSENDER, new ItemStack(CustomItems.ITEMBLOCKSENDER, 1), item -> { + SelectItem.open(player, EntryType.BLOCKSENDER, new ItemStack(CustomItems.ITEMBLOCKSENDER, 1), item -> { if (item != null) { player.setHeldItem(hand, item); item.setCount(sizeInHand); diff --git a/src/main/java/net/landofrails/stellwand/content/items/ItemBlockSignal.java b/src/main/java/net/landofrails/stellwand/content/items/ItemBlockSignal.java index b1352d9c..ef1058af 100644 --- a/src/main/java/net/landofrails/stellwand/content/items/ItemBlockSignal.java +++ b/src/main/java/net/landofrails/stellwand/content/items/ItemBlockSignal.java @@ -134,8 +134,7 @@ public void onClickAir(Player player, World world, Hand hand) { } int sizeInHand = player.getHeldItem(hand).getCount(); - SelectItem si = new SelectItem(); - si.open(player, EntryType.BLOCKSIGNAL, new ItemStack(CustomItems.ITEMBLOCKSIGNAL, 1), item -> { + SelectItem.open(player, EntryType.BLOCKSIGNAL, new ItemStack(CustomItems.ITEMBLOCKSIGNAL, 1), item -> { if (item != null) { player.setHeldItem(hand, item); item.setCount(sizeInHand); diff --git a/src/main/java/net/landofrails/stellwand/contentpacks/Content.java b/src/main/java/net/landofrails/stellwand/contentpacks/Content.java index ae2c6f73..9feafa8e 100644 --- a/src/main/java/net/landofrails/stellwand/contentpacks/Content.java +++ b/src/main/java/net/landofrails/stellwand/contentpacks/Content.java @@ -8,10 +8,7 @@ import net.landofrails.stellwand.contentpacks.types.EntryType; import net.landofrails.stellwand.utils.exceptions.ContentPackException; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; public class Content { @@ -20,7 +17,7 @@ private Content() { } - private static List<ContentPack> contentPacks = new LinkedList<>(); + private static final List<ContentPack> contentPacks = new LinkedList<>(); public static void addContentPack(ContentPack pack) { @@ -109,7 +106,7 @@ public static void testOutput() { } - public static List<ContentPack> getContentPacksFor(EntryType type) { + public static Collection<ContentPack> getContentPacksFor(EntryType type) { // @formatter:off return contentPacks.stream() .filter( @@ -117,8 +114,7 @@ public static List<ContentPack> getContentPacksFor(EntryType type) { .anyMatch( cpe -> cpe.isType(type) ) - ) - .distinct().collect(Collectors.toList()); + ).collect(Collectors.toSet()); // @formatter:on }