Added basic test cases for the IDSU manager, it looks like it works :)
This commit is contained in:
parent
97e8492801
commit
2bed98e6f0
5 changed files with 97 additions and 25 deletions
|
@ -90,7 +90,8 @@ public class Core {
|
||||||
// Multiblock events
|
// Multiblock events
|
||||||
MinecraftForge.EVENT_BUS.register(new MultiblockEventHandler());
|
MinecraftForge.EVENT_BUS.register(new MultiblockEventHandler());
|
||||||
// IDSU manager
|
// IDSU manager
|
||||||
MinecraftForge.EVENT_BUS.register(new IDSUManager());
|
IDSUManager.INSTANCE = new IDSUManager();
|
||||||
|
MinecraftForge.EVENT_BUS.register(IDSUManager.INSTANCE);
|
||||||
FMLCommonHandler.instance().bus().register(new MultiblockServerTickHandler());
|
FMLCommonHandler.instance().bus().register(new MultiblockServerTickHandler());
|
||||||
|
|
||||||
LogHelper.info("Initialization Complete");
|
LogHelper.info("Initialization Complete");
|
||||||
|
|
|
@ -2,10 +2,16 @@ package techreborn.blocks.storage;
|
||||||
|
|
||||||
import net.minecraft.block.material.Material;
|
import net.minecraft.block.material.Material;
|
||||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
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.util.IIcon;
|
||||||
|
import net.minecraft.world.World;
|
||||||
import techreborn.blocks.BlockMachineBase;
|
import techreborn.blocks.BlockMachineBase;
|
||||||
import cpw.mods.fml.relauncher.Side;
|
import cpw.mods.fml.relauncher.Side;
|
||||||
import cpw.mods.fml.relauncher.SideOnly;
|
import cpw.mods.fml.relauncher.SideOnly;
|
||||||
|
import techreborn.tiles.iesu.IDSUManager;
|
||||||
|
import techreborn.tiles.iesu.TileIDSU;
|
||||||
|
|
||||||
public class BlockIDSU extends BlockMachineBase {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ import techreborn.blocks.storage.BlockLesu;
|
||||||
import techreborn.blocks.storage.BlockLesuStorage;
|
import techreborn.blocks.storage.BlockLesuStorage;
|
||||||
import techreborn.itemblocks.*;
|
import techreborn.itemblocks.*;
|
||||||
import techreborn.tiles.*;
|
import techreborn.tiles.*;
|
||||||
|
import techreborn.tiles.iesu.TileIDSU;
|
||||||
import techreborn.tiles.lesu.TileLesu;
|
import techreborn.tiles.lesu.TileLesu;
|
||||||
import techreborn.tiles.lesu.TileLesuStorage;
|
import techreborn.tiles.lesu.TileLesuStorage;
|
||||||
import techreborn.util.LogHelper;
|
import techreborn.util.LogHelper;
|
||||||
|
@ -207,6 +208,7 @@ public class ModBlocks {
|
||||||
|
|
||||||
Idsu = new BlockIDSU(Material.rock);
|
Idsu = new BlockIDSU(Material.rock);
|
||||||
GameRegistry.registerBlock(Idsu, "idsu");
|
GameRegistry.registerBlock(Idsu, "idsu");
|
||||||
|
GameRegistry.registerTileEntity(TileIDSU.class, "TileIDSUTR");
|
||||||
|
|
||||||
Aesu = new BlockAesu(Material.rock);
|
Aesu = new BlockAesu(Material.rock);
|
||||||
GameRegistry.registerBlock(Aesu, ItemBlockAesu.class, "aesu");
|
GameRegistry.registerBlock(Aesu, ItemBlockAesu.class, "aesu");
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
package techreborn.tiles.iesu;
|
package techreborn.tiles.iesu;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import cpw.mods.fml.common.eventhandler.EventPriority;
|
import cpw.mods.fml.common.eventhandler.EventPriority;
|
||||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraft.world.storage.ISaveHandler;
|
import net.minecraft.world.storage.ISaveHandler;
|
||||||
import net.minecraftforge.event.world.WorldEvent;
|
import net.minecraftforge.event.world.WorldEvent;
|
||||||
|
import techreborn.util.LogHelper;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -17,20 +19,21 @@ import java.lang.reflect.Type;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
|
||||||
public class IDSUManager {
|
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)
|
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||||
public void worldSave(WorldEvent.Save event){
|
public void worldSave(WorldEvent.Save event){
|
||||||
if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){
|
if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){
|
||||||
for(IDSUWorldSaveData saveData : worldData){
|
if(worldData.containsKey(event.world)){
|
||||||
if(saveData.saveHandler == event.world.getSaveHandler()){
|
worldData.get(event.world).save();
|
||||||
saveData.save();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,34 +41,44 @@ public class IDSUManager {
|
||||||
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||||
public void worldLoad(WorldEvent.Load event){
|
public void worldLoad(WorldEvent.Load event){
|
||||||
if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){
|
if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){
|
||||||
for(IDSUWorldSaveData saveData : worldData){
|
if(worldData.containsKey(event.world)){
|
||||||
if(saveData.saveHandler == event.world.getSaveHandler()){
|
worldData.get(event.world).load();
|
||||||
saveData.load();
|
} else {
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(event.world);
|
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(event.world);
|
||||||
worldData.add(worldSaveData);
|
worldData.put(event.world ,worldSaveData);
|
||||||
worldSaveData.load();
|
worldSaveData.load();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SubscribeEvent(priority = EventPriority.LOWEST)
|
@SubscribeEvent(priority = EventPriority.LOWEST)
|
||||||
public void worldClosed(WorldEvent.Unload event){
|
public void worldClosed(WorldEvent.Unload event){
|
||||||
if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){
|
if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){
|
||||||
for(IDSUWorldSaveData saveData : worldData){
|
if(worldData.containsKey(event.world)){
|
||||||
if(saveData.saveHandler == event.world.getSaveHandler()){
|
worldData.get(event.world).save();
|
||||||
saveData.save();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//this clears the data ready for a new world
|
||||||
worldData.clear();
|
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 class IDSUWorldSaveData {
|
||||||
|
|
||||||
public HashMap<Integer, IDSUValueSaveData> idsuValues = new HashMap<Integer, IDSUValueSaveData>();
|
public TreeMap<Integer, IDSUValueSaveData> idsuValues = new TreeMap<Integer, IDSUValueSaveData>();
|
||||||
|
|
||||||
public World world;
|
public World world;
|
||||||
|
|
||||||
|
@ -80,25 +93,27 @@ public class IDSUManager {
|
||||||
this.saveHandler = world.getSaveHandler();
|
this.saveHandler = world.getSaveHandler();
|
||||||
folder = new File(saveHandler.getWorldDirectory(), "idsuData");
|
folder = new File(saveHandler.getWorldDirectory(), "idsuData");
|
||||||
file = new File(folder,"idsu.json");
|
file = new File(folder,"idsu.json");
|
||||||
idsuValues.put(0, new IDSUValueSaveData());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDSUValueSaveData getSaves(int i){
|
public IDSUValueSaveData getSaves(int i){
|
||||||
if(idsuValues.containsKey(i)){
|
if(idsuValues.containsKey(i)){
|
||||||
return idsuValues.get(i);
|
return idsuValues.get(i);
|
||||||
|
} else {
|
||||||
|
IDSUValueSaveData data = new IDSUValueSaveData();
|
||||||
|
idsuValues.put(i, data);
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load(){
|
public void load(){
|
||||||
//TODO load values from file
|
|
||||||
if(!file.exists()){
|
if(!file.exists()){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
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);
|
idsuValues = gson.fromJson(reader, typeOfHashMap);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -120,7 +135,7 @@ public class IDSUManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Gson gson = new Gson();
|
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||||
String json = gson.toJson(idsuValues);
|
String json = gson.toJson(idsuValues);
|
||||||
try {
|
try {
|
||||||
FileWriter writer = new FileWriter(file);
|
FileWriter writer = new FileWriter(file);
|
||||||
|
@ -135,10 +150,33 @@ public class IDSUManager {
|
||||||
|
|
||||||
public class IDSUValueSaveData {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
7
src/main/java/techreborn/tiles/iesu/TileIDSU.java
Normal file
7
src/main/java/techreborn/tiles/iesu/TileIDSU.java
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package techreborn.tiles.iesu;
|
||||||
|
|
||||||
|
import techreborn.tiles.TileMachineBase;
|
||||||
|
|
||||||
|
public class TileIDSU extends TileMachineBase {
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue