Files
CustomNPC-Plus/src/main/java/noppes/npcs/controllers/data/SpawnData.java
T

201 lines
5.4 KiB
Java
Raw Normal View History

package noppes.npcs.controllers.data;
2018-09-13 12:03:28 -04:00
2022-06-29 00:06:38 -05:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
2018-09-13 12:03:28 -04:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.WeightedRandom;
import net.minecraftforge.common.DimensionManager;
2018-09-13 12:03:28 -04:00
import noppes.npcs.NBTTags;
2022-11-15 15:25:53 -05:00
import noppes.npcs.api.IWorld;
import noppes.npcs.api.entity.IEntity;
import noppes.npcs.api.handler.data.INaturalSpawn;
2023-01-12 21:33:06 -05:00
import noppes.npcs.scripted.CustomNPCsException;
import noppes.npcs.scripted.NpcAPI;
import java.util.*;
2018-09-13 12:03:28 -04:00
2022-06-29 00:06:38 -05:00
public class SpawnData extends WeightedRandom.Item implements INaturalSpawn {
public List<String> biomes = new ArrayList<>();
public HashSet<Integer> dimensions = new HashSet<>();
2018-09-13 12:03:28 -04:00
public int id = -1;
public String name = "";
public HashMap<Integer,NBTTagCompound> spawnCompounds = new HashMap<>();
public boolean animalSpawning = true;
public boolean monsterSpawning = false;
public boolean liquidSpawning = false;
public boolean airSpawning = false;
public int spawnHeightMin;
public int spawnHeightMax = 100;
2018-09-13 12:03:28 -04:00
public SpawnData() {
super(10);
}
public void readNBT(NBTTagCompound compound) {
id = compound.getInteger("SpawnId");
name = compound.getString("SpawnName");
itemWeight = compound.getInteger("SpawnWeight");
if(itemWeight == 0)
itemWeight = 1;
biomes = NBTTags.getStringList(compound.getTagList("SpawnBiomes", 10));
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));
}
}
2022-06-29 00:06:38 -05:00
animalSpawning = compound.getBoolean("AnimalSpawning");
monsterSpawning = compound.getBoolean("MonsterSpawning");
liquidSpawning = compound.getBoolean("LiquidSpawning");
airSpawning = compound.getBoolean("CaveSpawning");
spawnHeightMin = compound.getInteger("HeightMin");
2022-10-29 16:46:53 -05:00
if (!compound.hasKey("HeightMax")) {
spawnHeightMax = 100;
} else {
spawnHeightMax = compound.getInteger("HeightMax");
}
2018-09-13 12:03:28 -04:00
}
public NBTTagCompound writeNBT(NBTTagCompound compound) {
compound.setInteger("SpawnId", id);
compound.setString("SpawnName", name);
compound.setInteger("SpawnWeight", itemWeight);
compound.setTag("SpawnBiomes", NBTTags.nbtStringList(biomes));
if (!this.dimensions.isEmpty()) {
compound.setTag("SpawnDimensions", NBTTags.nbtIntegerSet(dimensions));
}
Set<Map.Entry<Integer,NBTTagCompound>> entries = this.spawnCompounds.entrySet();
for (Map.Entry<Integer,NBTTagCompound> entry : entries) {
2023-06-03 17:10:10 -05:00
compound.setTag("SpawnCompound" + entry.getKey(), entry.getValue() != null ? entry.getValue() : new NBTTagCompound());
}
2022-06-29 00:06:38 -05:00
compound.setBoolean("AnimalSpawning", animalSpawning);
compound.setBoolean("MonsterSpawning", monsterSpawning);
compound.setBoolean("LiquidSpawning", liquidSpawning);
compound.setBoolean("CaveSpawning", airSpawning);
compound.setInteger("HeightMin", spawnHeightMin);
compound.setInteger("HeightMax", spawnHeightMax);
2018-09-13 12:03:28 -04:00
return compound;
}
2022-06-29 00:06:38 -05:00
public void setName(String name) {
if (name != null && !name.isEmpty()) {
this.name = name;
}
}
public String getName() {
return this.name;
}
2022-06-29 14:21:55 -05:00
public void setEntity(IEntity entity, int slot) {
2022-06-29 00:06:38 -05:00
NBTTagCompound compound = new NBTTagCompound();
2022-10-31 17:48:53 -05:00
if (entity != null && !entity.getMCEntity().writeToNBTOptional(compound)) {
throw new CustomNPCsException("Entity could not be written to NBT");
2022-06-29 00:06:38 -05:00
} else {
this.spawnCompounds.put(slot,compound);
2022-06-29 00:06:38 -05:00
}
}
2022-06-29 14:21:55 -05:00
public IEntity getEntity(IWorld world, int slot) {
if (!this.spawnCompounds.containsKey(slot)) {
return null;
}
2022-06-29 00:06:38 -05:00
try {
Entity entity = EntityList.createEntityFromNBT(this.spawnCompounds.get(slot), world.getMCWorld());
2022-06-29 00:06:38 -05:00
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]);
}
2022-06-29 00:06:38 -05:00
public void setWeight(int weight) {
if (weight < 1)
weight = 1;
if (weight > 100)
weight = 100;
this.itemWeight = weight;
}
public int getWeight() {
return this.itemWeight;
}
public void setMinHeight(int height) {
this.spawnHeightMin = height;
}
public int getMinHeight() {
return this.spawnHeightMin;
}
public void setMaxHeight(int height) {
this.spawnHeightMax = height;
}
public int getMaxHeight() {
return this.spawnHeightMax;
}
public void spawnsLikeAnimal(boolean spawns) {
this.animalSpawning = spawns;
}
public boolean spawnsLikeAnimal() {
return animalSpawning;
}
public void spawnsLikeMonster(boolean spawns) {
this.monsterSpawning = spawns;
}
public boolean spawnsLikeMonster() {
return monsterSpawning;
}
2022-06-29 00:06:38 -05:00
public void spawnsInLiquid(boolean spawns) {
this.liquidSpawning = spawns;
2022-06-29 00:06:38 -05:00
}
public boolean spawnsInLiquid() {
return this.liquidSpawning;
}
public void spawnsInAir(boolean spawns) {
this.airSpawning = spawns;
}
public boolean spawnsInAir() {
return this.airSpawning;
2022-06-29 00:06:38 -05:00
}
public String[] getBiomes() {
return biomes.toArray(new String[]{});
}
public void setBiomes(String[] biomes) {
this.biomes = new ArrayList<>(Arrays.asList(biomes));
}
2018-09-13 12:03:28 -04:00
}