From b5ada1224314d70c9516ecb47b2471074a7ca159 Mon Sep 17 00:00:00 2001 From: Thutmose Date: Tue, 6 Sep 2022 19:11:49 -0400 Subject: [PATCH] adds better wearables definitions for trainers this lets you specify each worn slot, instead of just "bag", applied example for Zinnia wearing her anklet --- .../capabilities/utils/TypeTrainer.java | 5 +-- .../entity/trainer/TrainerBase.java | 19 ++++++---- .../adventures/utils/TrainerEntryLoader.java | 28 ++++++++++----- .../wearables/layers/WearableWrapper.java | 7 +--- .../core/client/gui/WearableSetupGui.java | 16 ++------- .../java/thut/wearables/EnumWearable.java | 35 ++++++++++++++----- .../database/trainer/trainers.json | 12 ++++++- .../database/trainer/trainers.json | 28 +++++++++++---- 8 files changed, 96 insertions(+), 54 deletions(-) diff --git a/src/main/java/pokecube/adventures/capabilities/utils/TypeTrainer.java b/src/main/java/pokecube/adventures/capabilities/utils/TypeTrainer.java index d6fa1d1618..da87dbe7e9 100644 --- a/src/main/java/pokecube/adventures/capabilities/utils/TypeTrainer.java +++ b/src/main/java/pokecube/adventures/capabilities/utils/TypeTrainer.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Random; import java.util.function.Predicate; @@ -464,10 +465,10 @@ else for (final String type2 : types) /** 1 = male, 2 = female, 3 = both */ public byte genders = 1; - public boolean hasBag = false; - public ItemStack bag = ItemStack.EMPTY; public boolean hasBelt = false; + public Map> wornItems = Maps.newHashMap(); + public String tradeTemplate = "default"; public List pokemon = Lists.newArrayList(); public TrainerTrades trades; diff --git a/src/main/java/pokecube/adventures/entity/trainer/TrainerBase.java b/src/main/java/pokecube/adventures/entity/trainer/TrainerBase.java index be9f74caa6..7fc166a5fc 100644 --- a/src/main/java/pokecube/adventures/entity/trainer/TrainerBase.java +++ b/src/main/java/pokecube/adventures/entity/trainer/TrainerBase.java @@ -240,18 +240,23 @@ public void setNpcType(final NpcType type) if (this.getItemInHand(InteractionHand.OFF_HAND).isEmpty() && !this.pokemobsCap.getType().held.isEmpty()) this.setItemInHand(InteractionHand.OFF_HAND, this.pokemobsCap.getType().held.copy()); - if (this.pokemobsCap.getType().hasBag) - { - final PlayerWearables worn = ThutWearables.getWearables(this); - if (worn.getWearable(EnumWearable.BACK).isEmpty()) - worn.setWearable(EnumWearable.BACK, this.pokemobsCap.getType().bag.copy()); - } + final PlayerWearables worn = ThutWearables.getWearables(this); if (this.pokemobsCap.getType().hasBelt) { - final PlayerWearables worn = ThutWearables.getWearables(this); if (worn.getWearable(EnumWearable.WAIST).isEmpty()) worn.setWearable(EnumWearable.WAIST, PokecubeItems.getStack("mega_belt")); } + for (var entry : this.pokemobsCap.getType().wornItems.entrySet()) + { + var key = entry.getKey(); + var stack = entry.getValue().get(0); + if (entry.getValue().size() > 1) + stack = entry.getValue().get(this.getRandom().nextInt(entry.getValue().size())); + var slot = EnumWearable.wearableNames.get(key); + var subSlot = EnumWearable.slotsNames.get(key); + ItemStack old = worn.getWearable(slot, subSlot); + if (old.isEmpty()) worn.setWearable(slot, stack, subSlot); + } } } diff --git a/src/main/java/pokecube/adventures/utils/TrainerEntryLoader.java b/src/main/java/pokecube/adventures/utils/TrainerEntryLoader.java index 520b96c9ab..754d938359 100644 --- a/src/main/java/pokecube/adventures/utils/TrainerEntryLoader.java +++ b/src/main/java/pokecube/adventures/utils/TrainerEntryLoader.java @@ -2,6 +2,7 @@ import java.io.BufferedReader; import java.io.FileNotFoundException; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -22,22 +23,25 @@ import pokecube.core.utils.Tools; import thut.api.util.JsonUtil; import thut.core.common.ThutCore; +import thut.wearables.EnumWearable; public class TrainerEntryLoader { - public static class Bag extends Drop - {} - public static class Held extends Drop {} + public static class Worn + { + public String key; + public List options = Lists.newArrayList(); + } + public static class TrainerEntry { String tradeTemplate = "default"; String type; String pokemon; String gender; - Bag bag; boolean belt = true; Held held; Held reward; @@ -45,6 +49,7 @@ public static class TrainerEntry List spawns = Lists.newArrayList(); List tags = Lists.newArrayList(); + List worn = Lists.newArrayList(); @Override public String toString() @@ -116,15 +121,22 @@ public static void makeEntries() : new TypeTrainer(name); type.spawns.clear(); type.pokemon.clear(); + type.wornItems.clear(); final byte male = 1; final byte female = 2; type.tradeTemplate = entry.tradeTemplate; - type.hasBag = entry.bag != null; if (entry.tags != null) entry.tags.forEach(s -> type.tags.add(new ResourceLocation(s))); - if (type.hasBag) + + for (Worn w : entry.worn) { - final ItemStack bag = Tools.getStack(entry.bag.getValues()); - type.bag = bag; + var list = new ArrayList(); + for (Drop d : w.options) + { + ItemStack stack = Tools.getStack(d.getValues()); + list.add(stack); + } + if (EnumWearable.slotsNames.containsKey(w.key)) type.wornItems.put(w.key, list); + else PokecubeAPI.LOGGER.warn("Invalid key {} for worn items for {}", w.key, name); } if (entry.spawns != null) entry.spawns.forEach(rule -> { Float weight; diff --git a/src/main/java/pokecube/compat/wearables/layers/WearableWrapper.java b/src/main/java/pokecube/compat/wearables/layers/WearableWrapper.java index a0ee50c6e7..5c8b4a587c 100644 --- a/src/main/java/pokecube/compat/wearables/layers/WearableWrapper.java +++ b/src/main/java/pokecube/compat/wearables/layers/WearableWrapper.java @@ -40,12 +40,7 @@ public class WearableWrapper static { - for (final EnumWearable wearable : EnumWearable.values()) if (wearable.slots == 2) - { - WearableWrapper.addedNames.add("__" + wearable + "_right__"); - WearableWrapper.addedNames.add("__" + wearable + "_left__"); - } - else WearableWrapper.addedNames.add("__" + wearable + "__"); + addedNames.addAll(EnumWearable.wearableNames.keySet()); } private static class WearableRenderWrapper extends X3dPart diff --git a/src/main/java/pokecube/core/client/gui/WearableSetupGui.java b/src/main/java/pokecube/core/client/gui/WearableSetupGui.java index d338d3cd63..591252555c 100644 --- a/src/main/java/pokecube/core/client/gui/WearableSetupGui.java +++ b/src/main/java/pokecube/core/client/gui/WearableSetupGui.java @@ -176,26 +176,14 @@ public static IPokemob getRenderMob(final IPokemob realMob) List components; - Map wearableNames = Maps.newHashMap(); - Map slots = Maps.newHashMap(); + Map wearableNames = EnumWearable.wearableNames; + Map slots = EnumWearable.slotsNames; List sortedSlots = Lists.newArrayList(); int worn_index = 0; public WearableSetupGui() { super(TComponent.translatable("pokecube.model_reloader")); - for (final EnumWearable w : EnumWearable.values()) if (w.slots == 2) - { - wearableNames.put("__" + w + "_right__", w); - slots.put("__" + w + "_right__", 0); - wearableNames.put("__" + w + "_left__", w); - slots.put("__" + w + "_left__", 1); - } - else - { - wearableNames.put("__" + w + "__", w); - slots.put("__" + w + "__", 0); - } sortedSlots.addAll(slots.keySet()); sortedSlots.sort(null); } diff --git a/src/main/java/thut/wearables/EnumWearable.java b/src/main/java/thut/wearables/EnumWearable.java index 82f7b826c8..bd6745f43c 100644 --- a/src/main/java/thut/wearables/EnumWearable.java +++ b/src/main/java/thut/wearables/EnumWearable.java @@ -1,7 +1,9 @@ package thut.wearables; +import java.util.Map; import java.util.Set; +import com.google.common.collect.Maps; import com.google.common.collect.Sets; import net.minecraft.world.entity.LivingEntity; @@ -14,7 +16,7 @@ public enum EnumWearable FINGER(2, 0), WRIST(2, 2), ANKLE(2, 4), NECK(6), BACK(7), WAIST(8), EAR(2, 9), EYE(11), HAT(12); - static EnumWearable[] BYINDEX = new EnumWearable[13]; + static EnumWearable[] BYINDEX = new EnumWearable[13]; static Set checkers = Sets.newHashSet(); static { @@ -35,6 +37,25 @@ public enum EnumWearable EnumWearable.checkers.add(new DefaultChecker()); } + public static final Map wearableNames = Maps.newHashMap(); + public static final Map slotsNames = Maps.newHashMap(); + + static + { + for (final EnumWearable w : EnumWearable.values()) if (w.slots == 2) + { + wearableNames.put("__" + w + "_right__", w); + slotsNames.put("__" + w + "_right__", 0); + wearableNames.put("__" + w + "_left__", w); + slotsNames.put("__" + w + "_left__", 1); + } + else + { + wearableNames.put("__" + w + "__", w); + slotsNames.put("__" + w + "__", 0); + } + } + public static boolean canTakeOff(final LivingEntity wearer, final ItemStack stack, final int index) { if (stack.isEmpty()) return true; @@ -106,8 +127,7 @@ public static EnumWearable getWearable(final int index) return EnumWearable.BYINDEX[index]; } - public static void interact(final Player player, final ItemStack item, final int index, - final UseOnContext context) + public static void interact(final Player player, final ItemStack item, final int index, final UseOnContext context) { if (item.isEmpty()) return; final EnumWearable slot = EnumWearable.getWearable(index); @@ -121,8 +141,7 @@ public static void putOn(final LivingEntity wearer, final ItemStack stack, final if (stack.isEmpty()) return; final EnumWearable slot = EnumWearable.getWearable(index); final int subIndex = EnumWearable.getSubIndex(index); - for (final IWearableChecker checker : EnumWearable.checkers) - checker.onPutOn(wearer, stack, slot, subIndex); + for (final IWearableChecker checker : EnumWearable.checkers) checker.onPutOn(wearer, stack, slot, subIndex); } public static void registerWearableChecker(final IWearableChecker checker) @@ -135,8 +154,7 @@ public static void takeOff(final LivingEntity wearer, final ItemStack stack, fin if (stack.isEmpty()) return; final EnumWearable slot = EnumWearable.getWearable(index); final int subIndex = EnumWearable.getSubIndex(index); - for (final IWearableChecker checker : EnumWearable.checkers) - checker.onTakeOff(wearer, stack, slot, subIndex); + for (final IWearableChecker checker : EnumWearable.checkers) checker.onTakeOff(wearer, stack, slot, subIndex); } public static void tick(final LivingEntity wearer, final ItemStack stack, final int index) @@ -144,8 +162,7 @@ public static void tick(final LivingEntity wearer, final ItemStack stack, final if (stack.isEmpty()) return; final EnumWearable slot = EnumWearable.getWearable(index); final int subIndex = EnumWearable.getSubIndex(index); - for (final IWearableChecker checker : EnumWearable.checkers) - checker.onUpdate(wearer, stack, slot, subIndex); + for (final IWearableChecker checker : EnumWearable.checkers) checker.onUpdate(wearer, stack, slot, subIndex); } public final int slots; diff --git a/src/main/resources/data/pokecube_legends/database/trainer/trainers.json b/src/main/resources/data/pokecube_legends/database/trainer/trainers.json index a76edb94ba..efd9929e4e 100644 --- a/src/main/resources/data/pokecube_legends/database/trainer/trainers.json +++ b/src/main/resources/data/pokecube_legends/database/trainer/trainers.json @@ -75,7 +75,17 @@ { "type": "zinnia", "pokemon": "whismur,goodra,tyrantrum,altaria,noivern,salamence,rayquaza", - "gender": "Female" + "gender": "Female", + "worn": [ + { + "key": "__ANKLE_right__", + "options":[ + { + "id": "pokecube:mega_ankletzinnia" + } + ] + } + ] }, { "type": "jessie", diff --git a/src/main/resources/data/pokecube_mobs/database/trainer/trainers.json b/src/main/resources/data/pokecube_mobs/database/trainer/trainers.json index db8a28a38d..4624303399 100644 --- a/src/main/resources/data/pokecube_mobs/database/trainer/trainers.json +++ b/src/main/resources/data/pokecube_mobs/database/trainer/trainers.json @@ -52,10 +52,17 @@ } ], "gender": "Male", - "bag": { - "id": "pokecube_adventures:bag", - "tag": "{\"display\": {\"color\":6192150}}" - } + "worn": [ + { + "key": "__BACK__", + "options":[ + { + "id": "pokecube_adventures:bag", + "tag": "{\"display\": {\"color\":6192150}}" + } + ] + } + ] }, { "type": "fisherman_trainer", @@ -409,9 +416,16 @@ } ], "gender": "Male", - "bag": { - "id": "pokecube_adventures:bag" - } + "worn": [ + { + "key": "__BACK__", + "options":[ + { + "id": "pokecube_adventures:bag" + } + ] + } + ] } ] } \ No newline at end of file