Added basic test cases for the IDSU manager, it looks like it works :)

This commit is contained in:
modmuss50 2015-06-15 21:18:12 +01:00
parent 97e8492801
commit 2bed98e6f0
5 changed files with 97 additions and 25 deletions

View file

@ -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");

View file

@ -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;
}
}

View file

@ -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");

View file

@ -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<IDSUWorldSaveData> worldData = new ArrayList<IDSUWorldSaveData>();
public static IDSUManager INSTANCE;
public HashMap<World, IDSUWorldSaveData> worldData = new HashMap<World, IDSUWorldSaveData>();
@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.add(worldSaveData);
worldData.put(event.world ,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<Integer, IDSUValueSaveData> idsuValues = new HashMap<Integer, IDSUValueSaveData>();
public TreeMap<Integer, IDSUValueSaveData> idsuValues = new TreeMap<Integer, IDSUValueSaveData>();
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<Map<Integer, IDSUValueSaveData>>() { }.getType();
Type typeOfHashMap = new TypeToken<TreeMap<Integer, IDSUValueSaveData>>() { }.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;
}
}
}

View file

@ -0,0 +1,7 @@
package techreborn.tiles.iesu;
import techreborn.tiles.TileMachineBase;
public class TileIDSU extends TileMachineBase {
}