diff --git a/src/main/java/techreborn/Core.java b/src/main/java/techreborn/Core.java index 3abb5fc05..aa8e4843d 100644 --- a/src/main/java/techreborn/Core.java +++ b/src/main/java/techreborn/Core.java @@ -90,7 +90,8 @@ public class Core { // Multiblock events MinecraftForge.EVENT_BUS.register(new MultiblockEventHandler()); // IDSU manager - MinecraftForge.EVENT_BUS.register(new IDSUManager()); + IDSUManager.INSTANCE = new IDSUManager(); + MinecraftForge.EVENT_BUS.register(IDSUManager.INSTANCE); FMLCommonHandler.instance().bus().register(new MultiblockServerTickHandler()); LogHelper.info("Initialization Complete"); diff --git a/src/main/java/techreborn/blocks/storage/BlockIDSU.java b/src/main/java/techreborn/blocks/storage/BlockIDSU.java index f023af656..a8b1fbc3b 100644 --- a/src/main/java/techreborn/blocks/storage/BlockIDSU.java +++ b/src/main/java/techreborn/blocks/storage/BlockIDSU.java @@ -2,10 +2,16 @@ package techreborn.blocks.storage; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.ChatComponentText; import net.minecraft.util.IIcon; +import net.minecraft.world.World; import techreborn.blocks.BlockMachineBase; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import techreborn.tiles.iesu.IDSUManager; +import techreborn.tiles.iesu.TileIDSU; public class BlockIDSU extends BlockMachineBase { @@ -44,4 +50,22 @@ public class BlockIDSU extends BlockMachineBase { } + @Override + public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) { + return new TileIDSU(); + } + + + @Override + public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitx, float hity, float hitz) { + if(world.isRemote){ + return true; + } + if(player.isSneaking()){ + player.addChatComponentMessage(new ChatComponentText(IDSUManager.INSTANCE.getSaveDataForWorld(world, 0).getStoredPower() + " eu")); + } else { + IDSUManager.INSTANCE.getSaveDataForWorld(world, 0).addEnergy(150); + } + return true; + } } diff --git a/src/main/java/techreborn/init/ModBlocks.java b/src/main/java/techreborn/init/ModBlocks.java index 87eb7667c..ea357e6a5 100644 --- a/src/main/java/techreborn/init/ModBlocks.java +++ b/src/main/java/techreborn/init/ModBlocks.java @@ -38,6 +38,7 @@ import techreborn.blocks.storage.BlockLesu; import techreborn.blocks.storage.BlockLesuStorage; import techreborn.itemblocks.*; import techreborn.tiles.*; +import techreborn.tiles.iesu.TileIDSU; import techreborn.tiles.lesu.TileLesu; import techreborn.tiles.lesu.TileLesuStorage; import techreborn.util.LogHelper; @@ -207,6 +208,7 @@ public class ModBlocks { Idsu = new BlockIDSU(Material.rock); GameRegistry.registerBlock(Idsu, "idsu"); + GameRegistry.registerTileEntity(TileIDSU.class, "TileIDSUTR"); Aesu = new BlockAesu(Material.rock); GameRegistry.registerBlock(Aesu, ItemBlockAesu.class, "aesu"); diff --git a/src/main/java/techreborn/tiles/iesu/IDSUManager.java b/src/main/java/techreborn/tiles/iesu/IDSUManager.java index 4574e17f7..d2ede2489 100644 --- a/src/main/java/techreborn/tiles/iesu/IDSUManager.java +++ b/src/main/java/techreborn/tiles/iesu/IDSUManager.java @@ -1,12 +1,14 @@ package techreborn.tiles.iesu; 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 net.minecraft.world.World; import net.minecraft.world.storage.ISaveHandler; import net.minecraftforge.event.world.WorldEvent; +import techreborn.util.LogHelper; import java.io.BufferedReader; import java.io.File; @@ -17,20 +19,21 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import java.util.TreeMap; public class IDSUManager { - public static ArrayList worldData = new ArrayList(); + public static IDSUManager INSTANCE; + + public HashMap worldData = new HashMap(); @SubscribeEvent(priority = EventPriority.HIGHEST) public void worldSave(WorldEvent.Save event){ if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){ - for(IDSUWorldSaveData saveData : worldData){ - if(saveData.saveHandler == event.world.getSaveHandler()){ - saveData.save(); - } + if(worldData.containsKey(event.world)){ + worldData.get(event.world).save(); } } } @@ -38,34 +41,44 @@ public class IDSUManager { @SubscribeEvent(priority = EventPriority.HIGHEST) public void worldLoad(WorldEvent.Load event){ if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){ - for(IDSUWorldSaveData saveData : worldData){ - if(saveData.saveHandler == event.world.getSaveHandler()){ - saveData.load(); - return; - } + if(worldData.containsKey(event.world)){ + worldData.get(event.world).load(); + } else { + IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(event.world); + worldData.put(event.world ,worldSaveData); + worldSaveData.load(); } - IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(event.world); - worldData.add(worldSaveData); - worldSaveData.load(); } } @SubscribeEvent(priority = EventPriority.LOWEST) public void worldClosed(WorldEvent.Unload event){ if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){ - for(IDSUWorldSaveData saveData : worldData){ - if(saveData.saveHandler == event.world.getSaveHandler()){ - saveData.save(); - } + 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 class IDSUWorldSaveData { - public HashMap idsuValues = new HashMap(); + public TreeMap idsuValues = new TreeMap(); public World world; @@ -80,25 +93,27 @@ public class IDSUManager { this.saveHandler = world.getSaveHandler(); folder = new File(saveHandler.getWorldDirectory(), "idsuData"); file = new File(folder,"idsu.json"); - idsuValues.put(0, new IDSUValueSaveData()); } public IDSUValueSaveData getSaves(int i){ if(idsuValues.containsKey(i)){ return idsuValues.get(i); + } else { + IDSUValueSaveData data = new IDSUValueSaveData(); + idsuValues.put(i, data); + return data; } - return null; } public void load(){ - //TODO load values from file if(!file.exists()){ return; } try { Gson gson = new Gson(); BufferedReader reader = new BufferedReader(new FileReader(file)); - Type typeOfHashMap = new TypeToken>() { }.getType(); + Type typeOfHashMap = new TypeToken>() { }.getType(); + idsuValues.clear(); idsuValues = gson.fromJson(reader, typeOfHashMap); } catch (Exception e) { e.printStackTrace(); @@ -120,7 +135,7 @@ public class IDSUManager { } } - Gson gson = new Gson(); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); String json = gson.toJson(idsuValues); try { FileWriter writer = new FileWriter(file); @@ -135,10 +150,33 @@ public class IDSUManager { public class IDSUValueSaveData { + public double storedPower = 0; - public int id = 1; + 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; + } } } diff --git a/src/main/java/techreborn/tiles/iesu/TileIDSU.java b/src/main/java/techreborn/tiles/iesu/TileIDSU.java new file mode 100644 index 000000000..a640d69ea --- /dev/null +++ b/src/main/java/techreborn/tiles/iesu/TileIDSU.java @@ -0,0 +1,7 @@ +package techreborn.tiles.iesu; + +import techreborn.tiles.TileMachineBase; + +public class TileIDSU extends TileMachineBase { + +}