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

236 lines
6.0 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 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.controllers.ServerCloneController;
import noppes.npcs.scripted.CustomNPCsException;
import noppes.npcs.scripted.NpcAPI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
2018-09-13 12:03:28 -04:00
2022-06-29 00:06:38 -05:00
public class SpawnData extends WeightedRandom.Item implements INaturalSpawn {
2018-09-13 12:03:28 -04:00
public List<String> biomes = new ArrayList<String>();
public int id = -1;
public String name = "";
public NBTTagCompound compound1 = new NBTTagCompound();
2022-06-29 14:21:55 -05:00
public NBTTagCompound compound2 = new NBTTagCompound();
public NBTTagCompound compound3 = new NBTTagCompound();
public NBTTagCompound compound4 = new NBTTagCompound();
public NBTTagCompound compound5 = new NBTTagCompound();
public boolean animalSpawning = true;
public boolean monsterSpawning = false;
public boolean liquidSpawning = false;
public boolean airSpawning = false;
public int spawnHeightMin;
public int spawnHeightMax;
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));
compound1 = compound.getCompoundTag("SpawnCompound1");
2022-06-29 14:21:55 -05:00
compound2 = compound.getCompoundTag("SpawnCompound2");
compound3 = compound.getCompoundTag("SpawnCompound3");
compound4 = compound.getCompoundTag("SpawnCompound4");
compound5 = compound.getCompoundTag("SpawnCompound5");
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));
compound.setTag("SpawnCompound1", compound1);
2022-06-29 14:21:55 -05:00
compound.setTag("SpawnCompound2", compound2);
compound.setTag("SpawnCompound3", compound3);
compound.setTag("SpawnCompound4", compound4);
compound.setTag("SpawnCompound5", compound5);
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) {
if (slot < 1)
slot = 1;
if (slot > 5)
slot = 5;
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 {
2022-06-29 14:21:55 -05:00
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;
}
2022-06-29 00:06:38 -05:00
}
}
2022-06-29 14:21:55 -05:00
public IEntity getEntity(IWorld world, int slot) {
if (slot < 1)
slot = 1;
if (slot > 5)
slot = 5;
2022-06-29 00:06:38 -05:00
try {
2022-06-29 14:21:55 -05:00
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());
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 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
}