mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-18 13:49:05 +00:00
Npc API:
-Added natural spawn controller support
This commit is contained in:
1 parent
700fff3faf
commit
7d0b124422
6 files changed
+161
-6
No files matched your search
@@ -20,8 +20,10 @@ import net.minecraft.util.WeightedRandom;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
import noppes.npcs.LogWriter;
|
||||
import noppes.npcs.controllers.data.SpawnData;
|
||||
import noppes.npcs.scripted.interfaces.handler.INaturalSpawnsHandler;
|
||||
import noppes.npcs.scripted.interfaces.handler.data.INaturalSpawn;
|
||||
|
||||
public class SpawnController {
|
||||
public class SpawnController implements INaturalSpawnsHandler {
|
||||
public HashMap<String,List<SpawnData>> biomes = new HashMap<String, List<SpawnData>>() ;
|
||||
public ArrayList<SpawnData> data = new ArrayList<SpawnData>();
|
||||
public Random random = new Random();
|
||||
@@ -33,7 +35,6 @@ public class SpawnController {
|
||||
public SpawnController(){
|
||||
instance = this;
|
||||
loadData();
|
||||
|
||||
}
|
||||
|
||||
private void loadData(){
|
||||
@@ -190,4 +191,29 @@ public class SpawnController {
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public void saveAllData() {
|
||||
for (SpawnData spawn : this.data) {
|
||||
if (spawn.id < 0) {
|
||||
spawn.id = getUnusedId();
|
||||
}
|
||||
SpawnData original = getSpawnData(spawn.id);
|
||||
if (original == null) {
|
||||
this.data.add(spawn);
|
||||
} else {
|
||||
original.readNBT(spawn.writeNBT(new NBTTagCompound()));
|
||||
}
|
||||
}
|
||||
|
||||
this.fillBiomeData();
|
||||
this.saveData();
|
||||
}
|
||||
|
||||
public INaturalSpawn[] getSpawns() {
|
||||
return data.toArray(new INaturalSpawn[]{});
|
||||
}
|
||||
|
||||
public INaturalSpawn[] getSpawns(String biome) {
|
||||
return getSpawnList(biome).toArray(new INaturalSpawn[]{});
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,29 @@
|
||||
package noppes.npcs.controllers.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityList;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.WeightedRandom;
|
||||
import noppes.npcs.NBTTags;
|
||||
import noppes.npcs.controllers.ServerCloneController;
|
||||
import noppes.npcs.scripted.CustomNPCsException;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
import noppes.npcs.scripted.interfaces.IWorld;
|
||||
import noppes.npcs.scripted.interfaces.entity.ICustomNpc;
|
||||
import noppes.npcs.scripted.interfaces.entity.IEntity;
|
||||
import noppes.npcs.scripted.interfaces.handler.data.INaturalSpawn;
|
||||
|
||||
public class SpawnData extends WeightedRandom.Item{
|
||||
public class SpawnData extends WeightedRandom.Item implements INaturalSpawn {
|
||||
public List<String> biomes = new ArrayList<String>();
|
||||
public int id = -1;
|
||||
public String name = "";
|
||||
public NBTTagCompound compound1 = new NBTTagCompound();
|
||||
public boolean liquid = false;
|
||||
public boolean air = false;
|
||||
|
||||
public SpawnData() {
|
||||
super(10);
|
||||
@@ -26,9 +37,10 @@ public class SpawnData extends WeightedRandom.Item{
|
||||
itemWeight = 1;
|
||||
|
||||
biomes = NBTTags.getStringList(compound.getTagList("SpawnBiomes", 10));
|
||||
|
||||
compound1 = compound.getCompoundTag("SpawnCompound1");
|
||||
|
||||
|
||||
liquid = compound.getBoolean("Liquid");
|
||||
air = compound.getBoolean("Air");
|
||||
}
|
||||
|
||||
public NBTTagCompound writeNBT(NBTTagCompound compound) {
|
||||
@@ -37,9 +49,68 @@ public class SpawnData extends WeightedRandom.Item{
|
||||
compound.setInteger("SpawnWeight", itemWeight);
|
||||
|
||||
compound.setTag("SpawnBiomes", NBTTags.nbtStringList(biomes));
|
||||
|
||||
compound.setTag("SpawnCompound1", compound1);
|
||||
|
||||
compound.setBoolean("Liquid", liquid);
|
||||
compound.setBoolean("Air", air);
|
||||
return compound;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
if (name != null && !name.isEmpty()) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setEntity(IEntity entity) {
|
||||
NBTTagCompound compound = new NBTTagCompound();
|
||||
if (!entity.getMCEntity().isEntityAlive()) {
|
||||
throw new CustomNPCsException("Cannot save dead entities");
|
||||
} else {
|
||||
ServerCloneController.Instance.cleanTags(compound1);
|
||||
compound1 = compound;
|
||||
}
|
||||
}
|
||||
|
||||
public IEntity getEntity(IWorld world) {
|
||||
try {
|
||||
Entity entity = EntityList.createEntityFromNBT(compound1, world.getMCWorld());
|
||||
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 spawnsInLiquid(boolean spawns) {
|
||||
this.liquid = spawns;
|
||||
}
|
||||
|
||||
public boolean spawnsInLiquid() {
|
||||
return this.liquid;
|
||||
}
|
||||
|
||||
public String[] getBiomes() {
|
||||
return biomes.toArray(new String[]{});
|
||||
}
|
||||
|
||||
public void setBiomes(String[] biomes) {
|
||||
this.biomes = new ArrayList<>(Arrays.asList(biomes));
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ import net.minecraft.util.EntityDamageSource;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import net.minecraft.world.biome.BiomeGenBase;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
import noppes.npcs.NoppesUtilServer;
|
||||
import noppes.npcs.controllers.*;
|
||||
@@ -94,10 +95,24 @@ public class NpcAPI extends AbstractNpcAPI {
|
||||
return ServerCloneController.Instance;
|
||||
}
|
||||
|
||||
public INaturalSpawnsHandler getNaturalSpawns() { return SpawnController.instance; }
|
||||
|
||||
public ITransportHandler getLocations() {
|
||||
return TransportController.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getAllBiomeNames() {
|
||||
List<String> biomes = new ArrayList<String>();
|
||||
for (BiomeGenBase base : BiomeGenBase.getBiomeGenArray()) {
|
||||
if (base != null && base.biomeName != null) {
|
||||
biomes.add(base.biomeName);
|
||||
}
|
||||
}
|
||||
|
||||
return biomes.toArray(new String[]{});
|
||||
}
|
||||
|
||||
public IEntity getIEntity(Entity entity) {
|
||||
if(entity == null)
|
||||
return null;
|
||||
|
||||
@@ -55,8 +55,12 @@ public abstract class AbstractNpcAPI {
|
||||
|
||||
public abstract ICloneHandler getClones();
|
||||
|
||||
public abstract INaturalSpawnsHandler getNaturalSpawns();
|
||||
|
||||
public abstract ITransportHandler getLocations();
|
||||
|
||||
public abstract String[] getAllBiomeNames();
|
||||
|
||||
public abstract ICustomNpc createNPC(World var1);
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package noppes.npcs.scripted.interfaces.handler;
|
||||
|
||||
import noppes.npcs.scripted.interfaces.handler.data.INaturalSpawn;
|
||||
|
||||
public interface INaturalSpawnsHandler {
|
||||
|
||||
void saveAllData();
|
||||
|
||||
INaturalSpawn[] getSpawns();
|
||||
|
||||
INaturalSpawn[] getSpawns(String biome);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package noppes.npcs.scripted.interfaces.handler.data;
|
||||
|
||||
import noppes.npcs.scripted.interfaces.IWorld;
|
||||
import noppes.npcs.scripted.interfaces.entity.IEntity;
|
||||
|
||||
public interface INaturalSpawn {
|
||||
|
||||
void setName(String name);
|
||||
|
||||
String getName();
|
||||
|
||||
void setEntity(IEntity entity);
|
||||
|
||||
IEntity getEntity(IWorld world);
|
||||
|
||||
void setWeight(int weight);
|
||||
|
||||
int getWeight();
|
||||
|
||||
void spawnsInLiquid(boolean spawns);
|
||||
|
||||
boolean spawnsInLiquid();
|
||||
|
||||
String[] getBiomes();
|
||||
|
||||
void setBiomes(String[] biomes);
|
||||
}
|
||||
Reference in new issue
Block a user