2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs.controllers;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
|
import net.minecraft.util.WeightedRandom;
|
|
|
|
|
import noppes.npcs.CustomNpcs;
|
|
|
|
|
import noppes.npcs.LogWriter;
|
2022-11-15 15:25:53 -05:00
|
|
|
import noppes.npcs.api.handler.INaturalSpawnsHandler;
|
|
|
|
|
import noppes.npcs.api.handler.data.INaturalSpawn;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.controllers.data.SpawnData;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.zip.GZIPInputStream;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2022-06-29 00:06:38 -05:00
|
|
|
public class SpawnController implements INaturalSpawnsHandler {
|
2018-09-13 12:03:28 -04:00
|
|
|
public HashMap<String,List<SpawnData>> biomes = new HashMap<String, List<SpawnData>>() ;
|
|
|
|
|
public ArrayList<SpawnData> data = new ArrayList<SpawnData>();
|
|
|
|
|
public Random random = new Random();
|
|
|
|
|
|
|
|
|
|
public static SpawnController instance;
|
|
|
|
|
|
|
|
|
|
private int lastUsedID = 0;
|
|
|
|
|
|
|
|
|
|
public SpawnController(){
|
|
|
|
|
instance = this;
|
|
|
|
|
loadData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadData(){
|
|
|
|
|
|
|
|
|
|
File saveDir = CustomNpcs.getWorldSaveDirectory();
|
|
|
|
|
if(saveDir == null){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
File file = new File(saveDir, "spawns.dat");
|
|
|
|
|
if(file.exists()){
|
|
|
|
|
loadDataFile(file);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
try {
|
|
|
|
|
File file = new File(saveDir, "spawns.dat_old");
|
|
|
|
|
if(file.exists()){
|
|
|
|
|
loadDataFile(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception ee) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadDataFile(File file) throws IOException{
|
|
|
|
|
DataInputStream var1 = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(file))));
|
|
|
|
|
loadData(var1);
|
|
|
|
|
var1.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void loadData(DataInputStream stream) throws IOException{
|
|
|
|
|
ArrayList<SpawnData> data = new ArrayList<SpawnData>();
|
|
|
|
|
NBTTagCompound nbttagcompound1 = CompressedStreamTools.read(stream);
|
|
|
|
|
lastUsedID = nbttagcompound1.getInteger("lastID");
|
|
|
|
|
NBTTagList nbtlist = nbttagcompound1.getTagList("NPCSpawnData", 10);
|
|
|
|
|
|
|
|
|
|
if(nbtlist != null){
|
|
|
|
|
for(int i = 0; i < nbtlist.tagCount(); i++)
|
|
|
|
|
{
|
|
|
|
|
NBTTagCompound nbttagcompound = nbtlist.getCompoundTagAt(i);
|
|
|
|
|
SpawnData spawn = new SpawnData();
|
|
|
|
|
spawn.readNBT(nbttagcompound);
|
|
|
|
|
data.add(spawn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.data = data;
|
|
|
|
|
fillBiomeData();
|
|
|
|
|
}
|
|
|
|
|
public NBTTagCompound getNBT(){
|
|
|
|
|
NBTTagList list = new NBTTagList();
|
|
|
|
|
for(SpawnData spawn : data){
|
|
|
|
|
NBTTagCompound nbtfactions = new NBTTagCompound();
|
|
|
|
|
spawn.writeNBT(nbtfactions);
|
|
|
|
|
list.appendTag(nbtfactions);
|
|
|
|
|
}
|
|
|
|
|
NBTTagCompound nbttagcompound = new NBTTagCompound();
|
|
|
|
|
nbttagcompound.setInteger("lastID", lastUsedID);
|
|
|
|
|
nbttagcompound.setTag("NPCSpawnData", list);
|
|
|
|
|
return nbttagcompound;
|
|
|
|
|
}
|
|
|
|
|
public void saveData(){
|
|
|
|
|
try {
|
|
|
|
|
File saveDir = CustomNpcs.getWorldSaveDirectory();
|
|
|
|
|
File file = new File(saveDir, "spawns.dat_new");
|
|
|
|
|
File file1 = new File(saveDir, "spawns.dat_old");
|
|
|
|
|
File file2 = new File(saveDir, "spawns.dat");
|
|
|
|
|
CompressedStreamTools.writeCompressed(getNBT(), new FileOutputStream(file));
|
|
|
|
|
if(file1.exists())
|
|
|
|
|
{
|
|
|
|
|
file1.delete();
|
|
|
|
|
}
|
|
|
|
|
file2.renameTo(file1);
|
|
|
|
|
if(file2.exists())
|
|
|
|
|
{
|
|
|
|
|
file2.delete();
|
|
|
|
|
}
|
|
|
|
|
file.renameTo(file2);
|
|
|
|
|
if(file.exists())
|
|
|
|
|
{
|
|
|
|
|
file.delete();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LogWriter.except(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SpawnData getSpawnData(int id) {
|
|
|
|
|
for(SpawnData spawn : data)
|
|
|
|
|
if(spawn.id == id)
|
|
|
|
|
return spawn;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void saveSpawnData(SpawnData spawn) {
|
|
|
|
|
if(spawn.id < 0)
|
|
|
|
|
spawn.id = getUnusedId();
|
|
|
|
|
SpawnData original = getSpawnData(spawn.id);
|
|
|
|
|
if(original == null)
|
|
|
|
|
data.add(spawn);
|
|
|
|
|
else{
|
|
|
|
|
original.readNBT(spawn.writeNBT(new NBTTagCompound()));
|
|
|
|
|
}
|
|
|
|
|
fillBiomeData();
|
|
|
|
|
saveData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void fillBiomeData(){
|
|
|
|
|
HashMap<String,List<SpawnData>> biomes = new HashMap<String, List<SpawnData>>() ;
|
|
|
|
|
for(SpawnData spawn : data){
|
|
|
|
|
for(String s : spawn.biomes){
|
|
|
|
|
List<SpawnData> list = biomes.get(s);
|
|
|
|
|
if(list == null)
|
|
|
|
|
biomes.put(s, (list = new ArrayList<SpawnData>()));
|
|
|
|
|
list.add(spawn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.biomes = biomes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getUnusedId(){
|
|
|
|
|
lastUsedID++;
|
|
|
|
|
return lastUsedID;
|
|
|
|
|
}
|
|
|
|
|
public void removeSpawnData(int id) {
|
|
|
|
|
ArrayList<SpawnData> data = new ArrayList<SpawnData>();
|
|
|
|
|
|
|
|
|
|
for(SpawnData spawn : this.data){
|
|
|
|
|
if(spawn.id == id)
|
|
|
|
|
continue;
|
|
|
|
|
data.add(spawn);
|
|
|
|
|
}
|
|
|
|
|
this.data = data;
|
|
|
|
|
|
|
|
|
|
fillBiomeData();
|
|
|
|
|
saveData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<SpawnData> getSpawnList(String biome) {
|
|
|
|
|
return biomes.get(biome);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SpawnData getRandomSpawnData(String biome, boolean isAir) {
|
|
|
|
|
List<SpawnData> list = getSpawnList(biome);
|
|
|
|
|
if(list == null || list.isEmpty())
|
|
|
|
|
return null;
|
|
|
|
|
return (SpawnData) WeightedRandom.getRandomItem(this.random, list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Map<String,Integer> getScroll() {
|
|
|
|
|
Map<String,Integer> map = new HashMap<String,Integer>();
|
|
|
|
|
for(SpawnData spawn : data){
|
|
|
|
|
map.put(spawn.name, spawn.id);
|
|
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}
|
2022-06-29 00:06:38 -05:00
|
|
|
|
2022-06-29 03:36:52 -05:00
|
|
|
public void save() {
|
2022-06-29 00:06:38 -05:00
|
|
|
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[]{});
|
|
|
|
|
}
|
2022-06-29 03:36:52 -05:00
|
|
|
|
2022-06-29 14:51:09 -05:00
|
|
|
public void addSpawn(INaturalSpawn spawn) {
|
|
|
|
|
((SpawnData) spawn).id = 0;
|
|
|
|
|
this.saveSpawnData((SpawnData) spawn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void removeSpawn(INaturalSpawn spawn) {
|
|
|
|
|
this.removeSpawnData(((SpawnData) spawn).id);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 03:36:52 -05:00
|
|
|
public INaturalSpawn createSpawn() {
|
|
|
|
|
return new SpawnData();
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|