A load of work on the system, seams a lot is broken.
This commit is contained in:
parent
d8e6e88797
commit
598badc375
17 changed files with 268 additions and 53 deletions
|
@ -0,0 +1,19 @@
|
|||
package techreborn.tiles.idsu;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.EventPriority;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class ClientSideIDSUManager {
|
||||
|
||||
public static IDSUManager CLIENT = new IDSUManager();
|
||||
|
||||
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||
public void worldLoad(WorldEvent.Load event){
|
||||
CLIENT = new IDSUManager();
|
||||
}
|
||||
|
||||
}
|
230
src/main/java/techreborn/tiles/idsu/IDSUManager.java
Normal file
230
src/main/java/techreborn/tiles/idsu/IDSUManager.java
Normal file
|
@ -0,0 +1,230 @@
|
|||
package techreborn.tiles.idsu;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import cpw.mods.fml.common.eventhandler.EventPriority;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.storage.ISaveHandler;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import techreborn.packets.PacketSendIDSUManager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
|
||||
public class IDSUManager {
|
||||
|
||||
|
||||
public static IDSUManager INSTANCE;
|
||||
|
||||
public HashMap<World, IDSUWorldSaveData> worldData = new HashMap<World, IDSUWorldSaveData>();
|
||||
|
||||
//@SideOnly(Side.SERVER)
|
||||
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||
public void worldSave(WorldEvent.Save event){
|
||||
if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){
|
||||
if(worldData.containsKey(event.world)){
|
||||
worldData.get(event.world).save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//@SideOnly(Side.SERVER)
|
||||
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||
public void worldLoad(WorldEvent.Load event){
|
||||
if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){
|
||||
if(worldData.containsKey(event.world)){
|
||||
worldData.get(event.world).load();
|
||||
} else {
|
||||
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(event.world);
|
||||
worldData.put(event.world ,worldSaveData);
|
||||
worldSaveData.load();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//@SideOnly(Side.SERVER)
|
||||
@SubscribeEvent(priority = EventPriority.LOWEST)
|
||||
public void worldClosed(WorldEvent.Unload event){
|
||||
if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){
|
||||
if(worldData.containsKey(event.world)){
|
||||
worldData.get(event.world).save();
|
||||
}
|
||||
}
|
||||
//this clears the data ready for a new world
|
||||
worldData.clear();
|
||||
}
|
||||
|
||||
public IDSUValueSaveData getSaveDataForWorld(World world, int channel){
|
||||
if(worldData.containsKey(world)){
|
||||
return worldData.get(world).getSaves(channel);
|
||||
} else {
|
||||
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(world);
|
||||
worldData.put(world ,worldSaveData);
|
||||
worldSaveData.load();
|
||||
return worldSaveData.getSaves(channel);
|
||||
}
|
||||
//LogHelper.fatal("FAILED TO GET SAVEDATA!!! This should NEVER have been called, report to TechReborn DEV!");
|
||||
//return new IDSUValueSaveData();
|
||||
}
|
||||
|
||||
|
||||
public IDSUWorldSaveData getWorldDataFormWorld(World world){
|
||||
//System.out.println(world);
|
||||
if(worldData.containsKey(world)){
|
||||
return worldData.get(world);
|
||||
} else {
|
||||
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(world);
|
||||
worldData.put(world ,worldSaveData);
|
||||
worldSaveData.load();
|
||||
return worldSaveData;
|
||||
}
|
||||
}
|
||||
|
||||
//@SideOnly(Side.SERVER)
|
||||
public PacketSendIDSUManager getPacket(World world, EntityPlayer player){
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(getWorldDataFormWorld(world).idsuValues);
|
||||
if(getWorldDataFormWorld(world).idsuValues.isEmpty()){
|
||||
json = "EMPTY";
|
||||
}
|
||||
return new PacketSendIDSUManager(json, player);
|
||||
}
|
||||
|
||||
//@SideOnly(Side.CLIENT)
|
||||
public void loadFromString(String json, World world){
|
||||
if(json.equals("EMPTY")){
|
||||
return;
|
||||
}
|
||||
IDSUWorldSaveData worldSaveData;
|
||||
if(worldData.containsKey(world)){
|
||||
worldSaveData = worldData.get(world);
|
||||
} else {
|
||||
worldSaveData = new IDSUWorldSaveData(world);
|
||||
worldData.put(world ,worldSaveData);
|
||||
}
|
||||
Gson gson = new Gson();
|
||||
Type typeOfHashMap = new TypeToken<TreeMap<Integer, IDSUValueSaveData>>() { }.getType();
|
||||
worldSaveData.idsuValues.clear();
|
||||
worldSaveData.idsuValues = gson.fromJson(json, typeOfHashMap);
|
||||
//System.out.println(world);
|
||||
}
|
||||
|
||||
|
||||
public class IDSUWorldSaveData {
|
||||
|
||||
public TreeMap<Integer, IDSUValueSaveData> idsuValues = new TreeMap<Integer, IDSUValueSaveData>();
|
||||
|
||||
public World world;
|
||||
|
||||
ISaveHandler saveHandler;
|
||||
|
||||
File folder;
|
||||
|
||||
File file;
|
||||
|
||||
public IDSUWorldSaveData(World world) {
|
||||
this.world = world;
|
||||
this.saveHandler = world.getSaveHandler();
|
||||
folder = new File(saveHandler.getWorldDirectory(), "idsuData");
|
||||
file = new File(folder,"idsu.json");
|
||||
}
|
||||
|
||||
public IDSUValueSaveData getSaves(int i){
|
||||
if(idsuValues.containsKey(i)){
|
||||
return idsuValues.get(i);
|
||||
} else {
|
||||
IDSUValueSaveData data = new IDSUValueSaveData();
|
||||
idsuValues.put(i, data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
//@SideOnly(Side.SERVER)
|
||||
public void load(){
|
||||
if(!file.exists()){
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Gson gson = new Gson();
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
Type typeOfHashMap = new TypeToken<TreeMap<Integer, IDSUValueSaveData>>() { }.getType();
|
||||
idsuValues.clear();
|
||||
idsuValues = gson.fromJson(reader, typeOfHashMap);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//@SideOnly(Side.SERVER)
|
||||
public void save(){
|
||||
if(idsuValues.isEmpty()){
|
||||
return;
|
||||
}
|
||||
if(!file.exists()){
|
||||
if(!folder.exists()){
|
||||
folder.mkdirs();
|
||||
}
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
String json = gson.toJson(idsuValues);
|
||||
try {
|
||||
FileWriter writer = new FileWriter(file);
|
||||
writer.write(json);
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class IDSUValueSaveData {
|
||||
|
||||
public double storedPower = 0;
|
||||
|
||||
public String name = "";
|
||||
|
||||
public IDSUValueSaveData(double storedPower, String name) {
|
||||
this.storedPower = storedPower;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
public IDSUValueSaveData(double storedPower) {
|
||||
this.storedPower = storedPower;
|
||||
}
|
||||
|
||||
public IDSUValueSaveData() {
|
||||
}
|
||||
|
||||
public double getStoredPower() {
|
||||
return storedPower;
|
||||
}
|
||||
|
||||
public void setStoredPower(double storedPower) {
|
||||
this.storedPower = storedPower;
|
||||
}
|
||||
|
||||
public void addEnergy(double storedPower) {
|
||||
this.storedPower += storedPower;
|
||||
}
|
||||
}
|
||||
}
|
23
src/main/java/techreborn/tiles/idsu/TileIDSU.java
Normal file
23
src/main/java/techreborn/tiles/idsu/TileIDSU.java
Normal file
|
@ -0,0 +1,23 @@
|
|||
package techreborn.tiles.idsu;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import techreborn.packets.PacketHandler;
|
||||
import techreborn.tiles.TileMachineBase;
|
||||
|
||||
public class TileIDSU extends TileMachineBase {
|
||||
|
||||
public int channelID = 0;
|
||||
|
||||
public void handleGuiInputFromClient(int buttonID, int channel, EntityPlayer player, String name) {
|
||||
if(buttonID == 4){
|
||||
channelID = channel;
|
||||
IDSUManager.INSTANCE.getSaveDataForWorld(worldObj, channel);//Do this to init the save data :)
|
||||
if(name != "BLANK!!!"){
|
||||
IDSUManager.INSTANCE.getSaveDataForWorld(worldObj, channel).name = name;
|
||||
}
|
||||
if(worldObj.isRemote){
|
||||
PacketHandler.sendPacketToPlayer(IDSUManager.INSTANCE.getPacket(worldObj, player), player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue