Skip to content

Commit

Permalink
adds better wearables definitions for trainers
Browse files Browse the repository at this point in the history
this lets you specify each worn slot, instead of just "bag", applied example for Zinnia wearing her anklet
  • Loading branch information
Thutmose committed Sep 6, 2022
1 parent 86d2b60 commit b5ada12
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String, List<ItemStack>> wornItems = Maps.newHashMap();

public String tradeTemplate = "default";
public List<PokedexEntry> pokemon = Lists.newArrayList();
public TrainerTrades trades;
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/pokecube/adventures/entity/trainer/TrainerBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
28 changes: 20 additions & 8 deletions src/main/java/pokecube/adventures/utils/TrainerEntryLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -22,29 +23,33 @@
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<Drop> 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;
Boolean replace;

List<SpawnRule> spawns = Lists.newArrayList();
List<String> tags = Lists.newArrayList();
List<Worn> worn = Lists.newArrayList();

@Override
public String toString()
Expand Down Expand Up @@ -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<ItemStack>();
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/pokecube/core/client/gui/WearableSetupGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,14 @@ public static IPokemob getRenderMob(final IPokemob realMob)

List<String> components;

Map<String, EnumWearable> wearableNames = Maps.newHashMap();
Map<String, Integer> slots = Maps.newHashMap();
Map<String, EnumWearable> wearableNames = EnumWearable.wearableNames;
Map<String, Integer> slots = EnumWearable.slotsNames;
List<String> 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);
}
Expand Down
35 changes: 26 additions & 9 deletions src/main/java/thut/wearables/EnumWearable.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<IWearableChecker> checkers = Sets.newHashSet();
static
{
Expand All @@ -35,6 +37,25 @@ public enum EnumWearable
EnumWearable.checkers.add(new DefaultChecker());
}

public static final Map<String, EnumWearable> wearableNames = Maps.newHashMap();
public static final Map<String, Integer> 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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -135,17 +154,15 @@ 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)
{
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -409,9 +416,16 @@
}
],
"gender": "Male",
"bag": {
"id": "pokecube_adventures:bag"
}
"worn": [
{
"key": "__BACK__",
"options":[
{
"id": "pokecube_adventures:bag"
}
]
}
]
}
]
}

0 comments on commit b5ada12

Please sign in to comment.