From f27599075af94e2c2aae37d30736bb0112ba54a2 Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Mon, 20 Mar 2023 02:00:58 -0500 Subject: [PATCH] As many slots as possible can be added to natural spawn entries now. --- src/main/java/noppes/npcs/NPCSpawning.java | 10 +- .../java/noppes/npcs/PacketHandlerServer.java | 17 +- .../client/gui/GuiNpcMobSpawnerSelector.java | 4 +- .../npcs/client/gui/SubGuiNpcDimensions.java | 119 ++++++++++ .../gui/global/GuiNpcNaturalSpawns.java | 208 +++++++++--------- .../npcs/controllers/SpawnController.java | 16 +- .../npcs/controllers/data/SpawnData.java | 111 ++++------ .../assets/customnpcs/lang/en_US.lang | 2 + 8 files changed, 291 insertions(+), 196 deletions(-) create mode 100644 src/main/java/noppes/npcs/client/gui/SubGuiNpcDimensions.java diff --git a/src/main/java/noppes/npcs/NPCSpawning.java b/src/main/java/noppes/npcs/NPCSpawning.java index 0363aae1b..ef5056ef7 100644 --- a/src/main/java/noppes/npcs/NPCSpawning.java +++ b/src/main/java/noppes/npcs/NPCSpawning.java @@ -78,13 +78,9 @@ public class NPCSpawning { x += world.rand.nextInt(b1) - world.rand.nextInt(b1); z += world.rand.nextInt(b1) - world.rand.nextInt(b1); - - - - Block block = world.getBlock(x, y, z); String name = world.getBiomeGenForCoords(x, z).biomeName; - SpawnData data = SpawnController.instance.getRandomSpawnData(name, block.getMaterial() == Material.air); + SpawnData data = SpawnController.instance.getRandomSpawnData(name, world.provider.dimensionId); if (data == null || !canCreatureTypeSpawnAtLocation(data, world, x, y, z) || world.getClosestPlayer(x, y, z, 24.0D) != null) continue; @@ -115,7 +111,7 @@ public class NPCSpawning { public static void performWorldGenSpawning(World world, int x, int z, Random rand){ BiomeGenBase biome = world.getBiomeGenForCoords(x + 8, z + 8); while (rand.nextFloat() < biome.getSpawningChance()){ - SpawnData data = SpawnController.instance.getRandomSpawnData(biome.biomeName, true); + SpawnData data = SpawnController.instance.getRandomSpawnData(biome.biomeName, world.provider.dimensionId); if(data == null) continue; @@ -153,7 +149,7 @@ public class NPCSpawning { EntityLiving entityliving; try{ - NBTTagCompound[] allCompoundList = new NBTTagCompound[]{data.compound1,data.compound2,data.compound3,data.compound4,data.compound5}; + NBTTagCompound[] allCompoundList = data.spawnCompounds.values().toArray(new NBTTagCompound[0]); ArrayList entities = new ArrayList<>(); for (NBTTagCompound compound : allCompoundList) { Entity entity; diff --git a/src/main/java/noppes/npcs/PacketHandlerServer.java b/src/main/java/noppes/npcs/PacketHandlerServer.java index 18957d7c0..422d8cfca 100644 --- a/src/main/java/noppes/npcs/PacketHandlerServer.java +++ b/src/main/java/noppes/npcs/PacketHandlerServer.java @@ -88,6 +88,13 @@ public class PacketHandlerServer{ } else if (type == EnumPacketServer.QuestLogToServer) { updateQuestLogData(buffer, player); return; + } else if(type == EnumPacketServer.DimensionsGet){ + HashMap map = new HashMap(); + for(int id : DimensionManager.getStaticDimensionIDs()){ + WorldProvider provider = DimensionManager.createProviderFor(id); + map.put(provider.getDimensionName(), id); + } + NoppesUtilServer.sendScrollData(player, map); } if(type.needsNpc && npc == null){ @@ -330,15 +337,7 @@ public class PacketHandlerServer{ } private void featherPackets(EnumPacketServer type, ByteBuf buffer, EntityPlayerMP player) throws IOException { - if(type == EnumPacketServer.DimensionsGet){ - HashMap map = new HashMap(); - for(int id : DimensionManager.getStaticDimensionIDs()){ - WorldProvider provider = DimensionManager.createProviderFor(id); - map.put(provider.getDimensionName(), id); - } - NoppesUtilServer.sendScrollData(player, map); - } - else if(type == EnumPacketServer.DimensionTeleport){ + if(type == EnumPacketServer.DimensionTeleport){ int dimension = buffer.readInt(); WorldServer world = MinecraftServer.getServer().worldServerForDimension(dimension); ChunkCoordinates coords = world.getEntrancePortalLocation(); diff --git a/src/main/java/noppes/npcs/client/gui/GuiNpcMobSpawnerSelector.java b/src/main/java/noppes/npcs/client/gui/GuiNpcMobSpawnerSelector.java index 96b8905a9..1f620f8d5 100644 --- a/src/main/java/noppes/npcs/client/gui/GuiNpcMobSpawnerSelector.java +++ b/src/main/java/noppes/npcs/client/gui/GuiNpcMobSpawnerSelector.java @@ -111,7 +111,9 @@ public class GuiNpcMobSpawnerSelector extends SubGuiInterface implements IGuiDat if(sel == null) return null; - return ClientCloneController.Instance.getCloneData(player, sel, activeTab); + NBTTagCompound compound = ClientCloneController.Instance.getCloneData(player, sel, activeTab); + compound.setString("ClonedName", sel); + return compound; } public void buttonEvent(GuiButton guibutton) diff --git a/src/main/java/noppes/npcs/client/gui/SubGuiNpcDimensions.java b/src/main/java/noppes/npcs/client/gui/SubGuiNpcDimensions.java new file mode 100644 index 000000000..8af8a10c1 --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/SubGuiNpcDimensions.java @@ -0,0 +1,119 @@ +package noppes.npcs.client.gui; + +import net.minecraft.client.gui.GuiButton; +import noppes.npcs.client.Client; +import noppes.npcs.client.gui.util.*; +import noppes.npcs.constants.EnumPacketServer; +import noppes.npcs.controllers.data.SpawnData; + +import java.util.*; + +public class SubGuiNpcDimensions extends SubGuiInterface implements IScrollData +{ + private final SpawnData data; + private GuiCustomScroll allDimensions; + private HashMap dimensionMap = new HashMap<>(); + private GuiCustomScroll dataDimensions; + + public SubGuiNpcDimensions(SpawnData data){ + this.data = data; + setBackground("menubg.png"); + xSize = 346; + ySize = 216; + closeOnEsc = true; + Client.sendData(EnumPacketServer.DimensionsGet); + } + + public void initGui(){ + super.initGui(); + if(allDimensions == null){ + allDimensions = new GuiCustomScroll(this,0); + allDimensions.setSize(140, 180); + } + allDimensions.guiLeft = guiLeft + 4; + allDimensions.guiTop = guiTop + 14; + this.addScroll(allDimensions); + addLabel(new GuiNpcLabel(1, "spawning.availableDimensions", guiLeft + 4, guiTop + 4)); + + if(dataDimensions == null){ + dataDimensions = new GuiCustomScroll(this,1); + dataDimensions.setSize(140, 180); + } + dataDimensions.guiLeft = guiLeft + 200; + dataDimensions.guiTop = guiTop + 14; + this.addScroll(dataDimensions); + addLabel(new GuiNpcLabel(2, "spawning.spawningDimensions", guiLeft + 200, guiTop + 4)); + + ArrayList dimensions = new ArrayList<>(); + Set> entrySet = this.dimensionMap.entrySet(); + for (Map.Entry entry : entrySet) { + if (this.data.dimensions.contains(entry.getValue())) { + dimensions.add(entry.getKey()); + } + } + dataDimensions.setList(dimensions); + + addButton(new GuiNpcButton(1, guiLeft + 145, guiTop + 40, 55, 20, ">")); + addButton(new GuiNpcButton(2, guiLeft + 145, guiTop + 62, 55, 20, "<")); + + addButton(new GuiNpcButton(3, guiLeft + 145, guiTop + 90, 55, 20, ">>")); + addButton(new GuiNpcButton(4, guiLeft + 145, guiTop + 112, 55, 20, "<<")); + + + addButton(new GuiNpcButton(66, guiLeft + 260, guiTop + 194, 60, 20, "gui.done")); + } + + protected void actionPerformed(GuiButton guibutton){ + GuiNpcButton button = (GuiNpcButton) guibutton; + if(button.id == 1){ + if(allDimensions.hasSelected()){ + data.dimensions.add(dimensionMap.get(allDimensions.getSelected())); + allDimensions.selected = -1; + initGui(); + } + } + if(button.id == 2){ + if(dataDimensions.hasSelected()){ + data.dimensions.remove(dimensionMap.get(dataDimensions.getSelected())); + dataDimensions.selected = -1; + initGui(); + } + } + if(button.id == 3){ + data.dimensions.clear(); + this.data.dimensions.addAll(this.dimensionMap.values()); + allDimensions.selected = -1; + initGui(); + } + if(button.id == 4){ + data.dimensions.clear(); + allDimensions.selected = -1; + initGui(); + } + if(button.id == 66){ + close(); + } + } + + @Override + public void setData(Vector list, HashMap data) { + this.allDimensions.setList(list); + this.dimensionMap = data; + + ArrayList dimensions = new ArrayList<>(); + Set> entrySet = this.dimensionMap.entrySet(); + for (Map.Entry entry : entrySet) { + if (this.data.dimensions.contains(entry.getValue())) { + dimensions.add(entry.getKey()); + } + } + dataDimensions.setList(dimensions); + + initGui(); + } + + @Override + public void setSelected(String selected) { + + } +} diff --git a/src/main/java/noppes/npcs/client/gui/global/GuiNpcNaturalSpawns.java b/src/main/java/noppes/npcs/client/gui/global/GuiNpcNaturalSpawns.java index 1086bc3c0..c5f39b282 100644 --- a/src/main/java/noppes/npcs/client/gui/global/GuiNpcNaturalSpawns.java +++ b/src/main/java/noppes/npcs/client/gui/global/GuiNpcNaturalSpawns.java @@ -6,22 +6,22 @@ import net.minecraft.util.StatCollector; import noppes.npcs.client.Client; import noppes.npcs.client.gui.GuiNpcMobSpawnerSelector; import noppes.npcs.client.gui.SubGuiNpcBiomes; +import noppes.npcs.client.gui.SubGuiNpcDimensions; import noppes.npcs.client.gui.SubGuiSpawningOptions; import noppes.npcs.client.gui.util.*; import noppes.npcs.constants.EnumPacketServer; +import noppes.npcs.controllers.data.DialogImage; import noppes.npcs.controllers.data.SpawnData; import noppes.npcs.entity.EntityNPCInterface; -import java.util.HashMap; -import java.util.Vector; +import java.util.*; public class GuiNpcNaturalSpawns extends GuiNPCInterface2 implements IGuiData, IScrollData, ITextfieldListener, ICustomScrollListener, ISliderListener{ - - private GuiCustomScroll scroll; + private GuiCustomScroll scrollNaturalSpawns; + private GuiCustomScroll spawnEntryScroll; private HashMap data = new HashMap(); private SpawnData spawn = new SpawnData(); - private int slotSelected; public GuiNpcNaturalSpawns(EntityNPCInterface npc) { super(npc); @@ -31,59 +31,62 @@ public class GuiNpcNaturalSpawns extends GuiNPCInterface2 implements IGuiData, I @Override public void initGui(){ super.initGui(); - if(scroll == null){ - scroll = new GuiCustomScroll(this,0); - scroll.setSize(143, 208); + if(this.scrollNaturalSpawns == null){ + this.scrollNaturalSpawns = new GuiCustomScroll(this,0); + this.scrollNaturalSpawns.setSize(143, 208); } - scroll.guiLeft = guiLeft + 214; - scroll.guiTop = guiTop + 4; - this.addScroll(scroll); + this.scrollNaturalSpawns.guiLeft = guiLeft + 214; + this.scrollNaturalSpawns.guiTop = guiTop + 4; + this.addScroll(this.scrollNaturalSpawns); this.addButton(new GuiNpcButton(1,guiLeft + 358, guiTop + 38, 58, 20, "gui.add")); this.addButton(new GuiNpcButton(2,guiLeft + 358, guiTop + 61, 58, 20, "gui.remove")); - if(spawn.id >= 0) + if(this.spawn.id >= 0) showSpawn(); - } private void showSpawn() { addLabel(new GuiNpcLabel(1,"gui.title", guiLeft + 4, guiTop + 8)); - addTextField(new GuiNpcTextField(1, this, this.fontRendererObj, guiLeft + 60, guiTop + 3, 140, 20, spawn.name)); + addTextField(new GuiNpcTextField(1, this, this.fontRendererObj, guiLeft + 60, guiTop + 3, 140, 20, this.spawn.name)); int y = guiTop + 30; addLabel(new GuiNpcLabel(3,"spawning.biomes", guiLeft + 4, y)); - addButton(new GuiNpcButton(3, guiLeft + 120, y - 5, 50, 20, "selectServer.edit")); + addButton(new GuiNpcButton(3, guiLeft + 40, y - 5, 50, 20, "selectServer.edit")); + + addLabel(new GuiNpcLabel(11,StatCollector.translateToLocal("Dimensions"), guiLeft + 95, y)); + addButton(new GuiNpcButton(11, guiLeft + 150, y - 5, 50, 20, "selectServer.edit")); addLabel(new GuiNpcLabel(4,"spawning.options", guiLeft + 4, y += 22)); - addButton(new GuiNpcButton(4, guiLeft + 120, y - 5, 50, 20, "selectServer.edit")); + addButton(new GuiNpcButton(4, guiLeft + 90, y - 5, 50, 20, "selectServer.edit")); - addSlider(new GuiNpcSlider(this, 5, guiLeft + 4, y += 17, 180, 20, (float)spawn.itemWeight / 100)); + addSlider(new GuiNpcSlider(this, 5, guiLeft + 4, y += 17, 180, 20, (float)this.spawn.itemWeight / 100)); - this.addButton(new GuiNpcButton(25, guiLeft + 14, y += 23,20,20, "X")); - addLabel(new GuiNpcLabel(6, "1:", guiLeft + 4, y + 5)); - this.addButton(new GuiNpcButton(6, guiLeft + 36, y, 170, 20, getTitle(spawn.compound1))); + if (this.spawnEntryScroll == null) { + this.spawnEntryScroll = new GuiCustomScroll(this, 20, false); + } + this.spawnEntryScroll.guiLeft = guiLeft + 4; + this.spawnEntryScroll.guiTop = y + 40; + this.spawnEntryScroll.setSize(50, 102); + ArrayList list = new ArrayList<>(); + Set keySet = this.spawn.spawnCompounds.keySet(); + for (int i : keySet) { + list.add(String.valueOf(i)); + } + this.spawnEntryScroll.setList(list); + addScroll(this.spawnEntryScroll); - this.addButton(new GuiNpcButton(26, guiLeft + 14, y += 22,20,20, "X")); - addLabel(new GuiNpcLabel(7, "2:", guiLeft + 4, y + 5)); - this.addButton(new GuiNpcButton(7, guiLeft + 36, y, 170, 20, getTitle(spawn.compound2))); - - this.addButton(new GuiNpcButton(27, guiLeft + 14, y += 22,20,20, "X")); - addLabel(new GuiNpcLabel(8, "3:", guiLeft + 4, y + 5)); - this.addButton(new GuiNpcButton(8, guiLeft + 36, y, 170, 20, getTitle(spawn.compound3))); - - this.addButton(new GuiNpcButton(28, guiLeft + 14, y += 22,20,20, "X")); - addLabel(new GuiNpcLabel(9, "4:", guiLeft + 4, y + 5)); - this.addButton(new GuiNpcButton(9, guiLeft + 36, y, 170, 20, getTitle(spawn.compound4))); - - this.addButton(new GuiNpcButton(29, guiLeft + 14, y += 22,20,20, "X")); - addLabel(new GuiNpcLabel(10, "5:", guiLeft + 4, y + 5)); - this.addButton(new GuiNpcButton(10, guiLeft + 36, y, 170, 20, getTitle(spawn.compound5))); + addButton(new GuiNpcButton(21, guiLeft + 6, y += 20, 20, 20, "+")); + if (this.spawnEntryScroll.hasSelected()) { + int selected = Integer.parseInt(this.spawnEntryScroll.getSelected()); + addButton(new GuiNpcButton(22, guiLeft + 32, y, 20, 20, "-")); + addTextField(new GuiNpcTextField(25, this, guiLeft + 60, y += 10, 30, 20, String.valueOf(selected))); + addButton(new GuiNpcButton(26, guiLeft + 92, y, 100, 20, this.getTitle(this.spawn.spawnCompounds.get(selected)))); + } } private String getTitle(NBTTagCompound compound) { if(compound != null && compound.hasKey("ClonedName")) return compound.getString("ClonedName"); - return "gui.selectnpc"; } @@ -99,84 +102,105 @@ public class GuiNpcNaturalSpawns extends GuiNPCInterface2 implements IGuiData, I SpawnData spawn = new SpawnData(); spawn.name = name; Client.sendData(EnumPacketServer.NaturalSpawnSave, spawn.writeNBT(new NBTTagCompound())); - + this.spawnEntryScroll.selected = -1; } if(id == 2){ - if(data.containsKey(scroll.getSelected())) { + if(data.containsKey(scrollNaturalSpawns.getSelected())) { Client.sendData(EnumPacketServer.NaturalSpawnRemove, spawn.id); spawn = new SpawnData(); - scroll.clear(); + scrollNaturalSpawns.clear(); + this.spawnEntryScroll.selected = -1; } } if(id == 3){ setSubGui(new SubGuiNpcBiomes(spawn)); } + if(id == 11){ + setSubGui(new SubGuiNpcDimensions(spawn)); + } if(id == 4){ setSubGui(new SubGuiSpawningOptions(spawn)); } - - if(id >= 6 && id <= 10){ - slotSelected = id - 5; + if (id == 21) { + int addId = 0; + if (this.spawnEntryScroll.hasSelected()) { + int selected = Integer.parseInt(spawnEntryScroll.getSelected()); + ArrayList keys = new ArrayList<>(this.spawn.spawnCompounds.keySet()); + int keyIndex = keys.indexOf(selected); + do { + addId = keys.get(keyIndex) + 1; + keyIndex++; + } while (this.spawn.spawnCompounds.containsKey(addId)); + } else if (this.spawn.spawnCompounds.size() > 0) { + addId = (Integer) this.spawn.spawnCompounds.keySet().toArray()[this.spawn.spawnCompounds.size() - 1] + 1; + } + this.spawn.spawnCompounds.put(addId, new NBTTagCompound()); + this.initGui(); + } + if (id == 22) { + if (this.spawnEntryScroll.hasSelected()) { + int selected = Integer.parseInt(spawnEntryScroll.getSelected()); + this.spawn.spawnCompounds.remove(selected); + this.spawnEntryScroll.selected = -1; + this.initGui(); + } + } + if (id == 26) { setSubGui(new GuiNpcMobSpawnerSelector()); } - - if(id == 25){ - spawn.compound1 = new NBTTagCompound(); - initGui(); - } - if(id == 26){ - spawn.compound2 = new NBTTagCompound(); - initGui(); - } - if(id == 27){ - spawn.compound3 = new NBTTagCompound(); - initGui(); - } - if(id == 28){ - spawn.compound4 = new NBTTagCompound(); - initGui(); - } - if(id == 29){ - spawn.compound5 = new NBTTagCompound(); - initGui(); - } } @Override - public void unFocused(GuiNpcTextField guiNpcTextField) { - String name = guiNpcTextField.getText(); - if(name.isEmpty() || data.containsKey(name)){ - guiNpcTextField.setText(spawn.name); + public void unFocused(GuiNpcTextField textfield) { + if (textfield.id == 1) { + String name = textfield.getText(); + if (name.isEmpty() || data.containsKey(name)) { + textfield.setText(spawn.name); + } else { + String old = spawn.name; + data.remove(old); + spawn.name = name; + data.put(spawn.name, spawn.id); + scrollNaturalSpawns.replace(old, spawn.name); + } } - else{ - String old = spawn.name; - data.remove(old); - spawn.name = name; - data.put(spawn.name, spawn.id); - scroll.replace(old, spawn.name); + if (textfield.id == 25 && this.spawnEntryScroll.hasSelected()) { + int selected = Integer.parseInt(spawnEntryScroll.getSelected()); + if (spawnEntryScroll.getList().contains(String.valueOf(textfield.getInteger()))) { + textfield.setText(String.valueOf(selected)); + return; + } + NBTTagCompound compound = spawn.spawnCompounds.get(selected); + spawn.spawnCompounds.remove(selected); + spawn.spawnCompounds.put(textfield.getInteger(), compound); + spawnEntryScroll.selected = -1; + initGui(); } } @Override public void setData(Vector list, HashMap data) { - String name = scroll.getSelected(); + String name = scrollNaturalSpawns.getSelected(); this.data = data; - scroll.setList(list); + scrollNaturalSpawns.setList(list); if(name != null) - scroll.setSelected(name); + scrollNaturalSpawns.setSelected(name); initGui(); } @Override public void customScrollClicked(int i, int j, int k, GuiCustomScroll guiCustomScroll) { - if(guiCustomScroll.id == 0){ + if (guiCustomScroll.id == 0){ save(); - String selected = scroll.getSelected(); + String selected = scrollNaturalSpawns.getSelected(); spawn = new SpawnData(); Client.sendData(EnumPacketServer.NaturalSpawnGet, data.get(selected)); } + if (guiCustomScroll.id == 20) { + initGui(); + } } @Override @@ -188,33 +212,14 @@ public class GuiNpcNaturalSpawns extends GuiNPCInterface2 implements IGuiData, I @Override public void setSelected(String selected) { - } public void closeSubGui(SubGuiInterface gui) { super.closeSubGui(gui); - if(gui instanceof GuiNpcMobSpawnerSelector){ + if(gui instanceof GuiNpcMobSpawnerSelector && this.spawnEntryScroll.hasSelected()){ GuiNpcMobSpawnerSelector selector = (GuiNpcMobSpawnerSelector) gui; - NBTTagCompound compound = selector.getCompound(); - if (compound != null) { - switch (slotSelected) { - case 1: - spawn.compound1 = compound; - break; - case 2: - spawn.compound2 = compound; - break; - case 3: - spawn.compound3 = compound; - break; - case 4: - spawn.compound4 = compound; - break; - case 5: - spawn.compound5 = compound; - break; - } - } + int selected = Integer.parseInt(this.spawnEntryScroll.getSelected()); + this.spawn.spawnCompounds.put(selected,selector.getCompound()); initGui(); } } @@ -233,13 +238,10 @@ public class GuiNpcNaturalSpawns extends GuiNPCInterface2 implements IGuiData, I @Override public void mousePressed(GuiNpcSlider guiNpcSlider) { - // TODO Auto-generated method stub - } @Override public void mouseReleased(GuiNpcSlider guiNpcSlider) { spawn.itemWeight = (int) (guiNpcSlider.sliderValue * 100); } - } diff --git a/src/main/java/noppes/npcs/controllers/SpawnController.java b/src/main/java/noppes/npcs/controllers/SpawnController.java index a4c8df58e..57e4d3d03 100644 --- a/src/main/java/noppes/npcs/controllers/SpawnController.java +++ b/src/main/java/noppes/npcs/controllers/SpawnController.java @@ -168,10 +168,20 @@ public class SpawnController implements INaturalSpawnsHandler { return biomes.get(biome); } - public SpawnData getRandomSpawnData(String biome, boolean isAir) { - List list = getSpawnList(biome); - if(list == null || list.isEmpty()) + public SpawnData getRandomSpawnData(String biome, int dimensionId) { + List biomeList = getSpawnList(biome); + if (biomeList == null) return null; + + ArrayList list = new ArrayList<>(); + for (SpawnData data : biomeList) { + if (data.dimensions.contains(dimensionId)) { + list.add(data); + } + } + if(list.isEmpty()) + return null; + return (SpawnData) WeightedRandom.getRandomItem(this.random, list); } diff --git a/src/main/java/noppes/npcs/controllers/data/SpawnData.java b/src/main/java/noppes/npcs/controllers/data/SpawnData.java index f3296cc27..7436cd2de 100644 --- a/src/main/java/noppes/npcs/controllers/data/SpawnData.java +++ b/src/main/java/noppes/npcs/controllers/data/SpawnData.java @@ -4,27 +4,23 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.EntityList; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.WeightedRandom; +import net.minecraftforge.common.DimensionManager; import noppes.npcs.NBTTags; import noppes.npcs.api.IWorld; import noppes.npcs.api.entity.IEntity; import noppes.npcs.api.handler.data.INaturalSpawn; -import noppes.npcs.controllers.ServerCloneController; import noppes.npcs.scripted.CustomNPCsException; import noppes.npcs.scripted.NpcAPI; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; public class SpawnData extends WeightedRandom.Item implements INaturalSpawn { - public List biomes = new ArrayList(); + public List biomes = new ArrayList<>(); + public HashSet dimensions = new HashSet<>(); public int id = -1; public String name = ""; - public NBTTagCompound compound1 = new NBTTagCompound(); - public NBTTagCompound compound2 = new NBTTagCompound(); - public NBTTagCompound compound3 = new NBTTagCompound(); - public NBTTagCompound compound4 = new NBTTagCompound(); - public NBTTagCompound compound5 = new NBTTagCompound(); + + public HashMap spawnCompounds = new HashMap<>(); public boolean animalSpawning = true; public boolean monsterSpawning = false; @@ -32,7 +28,7 @@ public class SpawnData extends WeightedRandom.Item implements INaturalSpawn { public boolean airSpawning = false; public int spawnHeightMin; - public int spawnHeightMax; + public int spawnHeightMax = 100; public SpawnData() { super(10); @@ -46,11 +42,20 @@ public class SpawnData extends WeightedRandom.Item implements INaturalSpawn { itemWeight = 1; biomes = NBTTags.getStringList(compound.getTagList("SpawnBiomes", 10)); - compound1 = compound.getCompoundTag("SpawnCompound1"); - compound2 = compound.getCompoundTag("SpawnCompound2"); - compound3 = compound.getCompoundTag("SpawnCompound3"); - compound4 = compound.getCompoundTag("SpawnCompound4"); - compound5 = compound.getCompoundTag("SpawnCompound5"); + if (!compound.hasKey("SpawnDimensions")) { + dimensions.addAll(Arrays.asList(DimensionManager.getStaticDimensionIDs())); + } else { + dimensions = NBTTags.getIntegerSet(compound.getTagList("SpawnDimensions", 10)); + } + + this.spawnCompounds.clear(); + Set keys = compound.func_150296_c(); + for (Object key : keys) { + if (((String)key).startsWith("SpawnCompound")) { + int i = Integer.parseInt(((String)key).replace("SpawnCompound","")); + this.spawnCompounds.put(i,compound.getCompoundTag("SpawnCompound"+i)); + } + } animalSpawning = compound.getBoolean("AnimalSpawning"); monsterSpawning = compound.getBoolean("MonsterSpawning"); @@ -71,11 +76,14 @@ public class SpawnData extends WeightedRandom.Item implements INaturalSpawn { compound.setInteger("SpawnWeight", itemWeight); compound.setTag("SpawnBiomes", NBTTags.nbtStringList(biomes)); - compound.setTag("SpawnCompound1", compound1); - compound.setTag("SpawnCompound2", compound2); - compound.setTag("SpawnCompound3", compound3); - compound.setTag("SpawnCompound4", compound4); - compound.setTag("SpawnCompound5", compound5); + if (!this.dimensions.isEmpty()) { + compound.setTag("SpawnDimensions", NBTTags.nbtIntegerSet(dimensions)); + } + + Set> entries = this.spawnCompounds.entrySet(); + for (Map.Entry entry : entries) { + compound.setTag("SpawnCompound"+entry.getKey(),entry.getValue()); + } compound.setBoolean("AnimalSpawning", animalSpawning); compound.setBoolean("MonsterSpawning", monsterSpawning); @@ -98,73 +106,30 @@ public class SpawnData extends WeightedRandom.Item implements INaturalSpawn { } public void setEntity(IEntity entity, int slot) { - if (slot < 1) - slot = 1; - if (slot > 5) - slot = 5; - NBTTagCompound compound = new NBTTagCompound(); if (entity != null && !entity.getMCEntity().writeToNBTOptional(compound)) { throw new CustomNPCsException("Entity could not be written to NBT"); } else { - switch (slot) { - case 1: - ServerCloneController.Instance.cleanTags(compound1); - compound1 = compound; - break; - case 2: - ServerCloneController.Instance.cleanTags(compound2); - compound2 = compound; - break; - case 3: - ServerCloneController.Instance.cleanTags(compound3); - compound3 = compound; - break; - case 4: - ServerCloneController.Instance.cleanTags(compound4); - compound4 = compound; - break; - case 5: - ServerCloneController.Instance.cleanTags(compound5); - compound5 = compound; - break; - } + this.spawnCompounds.put(slot,compound); } } public IEntity getEntity(IWorld world, int slot) { - if (slot < 1) - slot = 1; - if (slot > 5) - slot = 5; - + if (!this.spawnCompounds.containsKey(slot)) { + return null; + } try { - NBTTagCompound compound = new NBTTagCompound(); - switch (slot) { - case 1: - compound = compound1; - break; - case 2: - compound = compound2; - break; - case 3: - compound = compound3; - break; - case 4: - compound = compound4; - break; - case 5: - compound = compound5; - break; - } - - Entity entity = EntityList.createEntityFromNBT(compound, world.getMCWorld()); + Entity entity = EntityList.createEntityFromNBT(this.spawnCompounds.get(slot), world.getMCWorld()); return NpcAPI.Instance().getIEntity(entity); } catch (Exception e) { throw new CustomNPCsException("Error creating entity from spawn data:\n" + e.getMessage()); } } + public Integer[] getSlots() { + return this.spawnCompounds.keySet().toArray(new Integer[0]); + } + public void setWeight(int weight) { if (weight < 1) weight = 1; diff --git a/src/main/resources/assets/customnpcs/lang/en_US.lang b/src/main/resources/assets/customnpcs/lang/en_US.lang index 63c1528a7..368da4e90 100644 --- a/src/main/resources/assets/customnpcs/lang/en_US.lang +++ b/src/main/resources/assets/customnpcs/lang/en_US.lang @@ -230,6 +230,8 @@ availability.editIcon=For the next 10 seconds you can set any item icon by right spawning.availableBiomes=Available Biomes spawning.spawningBiomes=Spawning Biomes +spawning.availableDimensions=Available Dimensions +spawning.spawningDimensions=Spawning Dimensions spawning.biomes=Biomes spawning.weightedChance=Weighted Chance spawning.options=Spawning Options