Some clean up of some code

This commit is contained in:
modmuss50 2015-06-21 14:17:47 +01:00
parent 9e4151f5fa
commit 43da219936
7 changed files with 171 additions and 169 deletions

View file

@ -12,7 +12,7 @@ public class ClientSideIDSUManager {
public static IDSUManager CLIENT = new IDSUManager();
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void worldLoad(WorldEvent.Load event){
public void worldLoad(WorldEvent.Load event) {
CLIENT = new IDSUManager();
}

View file

@ -5,8 +5,6 @@ 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;
@ -25,40 +23,38 @@ import java.util.TreeMap;
public class IDSUManager {
public static IDSUManager INSTANCE;
public static final String savename = "idsu.json";
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)){
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)){
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);
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)){
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();
}
}
@ -66,59 +62,54 @@ public class IDSUManager {
worldData.clear();
}
public IDSUValueSaveData getSaveDataForWorld(World world, int channel){
if(worldData.containsKey(world)){
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);
worldData.put(world, worldSaveData);
worldSaveData.load();
return worldSaveData.getSaves(channel);
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)){
public IDSUWorldSaveData getWorldDataFormWorld(World world) {
if (worldData.containsKey(world)) {
return worldData.get(world);
} else {
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(world);
worldData.put(world ,worldSaveData);
worldData.put(world, worldSaveData);
worldSaveData.load();
return worldSaveData;
return worldSaveData;
}
}
//@SideOnly(Side.SERVER)
public PacketSendIDSUManager getPacket(World world, EntityPlayer player){
public PacketSendIDSUManager getPacket(World world, EntityPlayer player) {
Gson gson = new Gson();
String json = gson.toJson(getWorldDataFormWorld(world).idsuValues);
if(getWorldDataFormWorld(world).idsuValues.isEmpty()){
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")){
public void loadFromString(String json, World world) {
if (json.equals("EMPTY")) {
return;
}
IDSUWorldSaveData worldSaveData;
if(worldData.containsKey(world)){
if (worldData.containsKey(world)) {
worldSaveData = worldData.get(world);
} else {
worldSaveData = new IDSUWorldSaveData(world);
worldData.put(world ,worldSaveData);
worldData.put(world, worldSaveData);
}
Gson gson = new Gson();
Type typeOfHashMap = new TypeToken<TreeMap<Integer, IDSUValueSaveData>>() { }.getType();
Type typeOfHashMap = new TypeToken<TreeMap<Integer, IDSUValueSaveData>>() {
}.getType();
worldSaveData.idsuValues.clear();
worldSaveData.idsuValues = gson.fromJson(json, typeOfHashMap);
//System.out.println(world);
}
@ -138,11 +129,11 @@ public class IDSUManager {
this.world = world;
this.saveHandler = world.getSaveHandler();
folder = new File(saveHandler.getWorldDirectory(), "idsuData");
file = new File(folder,"idsu.json");
file = new File(folder, savename);
}
public IDSUValueSaveData getSaves(int i){
if(idsuValues.containsKey(i)){
public IDSUValueSaveData getSaves(int i) {
if (idsuValues.containsKey(i)) {
return idsuValues.get(i);
} else {
IDSUValueSaveData data = new IDSUValueSaveData();
@ -151,15 +142,15 @@ public class IDSUManager {
}
}
//@SideOnly(Side.SERVER)
public void load(){
if(!file.exists()){
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();
Type typeOfHashMap = new TypeToken<TreeMap<Integer, IDSUValueSaveData>>() {
}.getType();
idsuValues.clear();
idsuValues = gson.fromJson(reader, typeOfHashMap);
} catch (Exception e) {
@ -167,13 +158,12 @@ public class IDSUManager {
}
}
//@SideOnly(Side.SERVER)
public void save(){
if(idsuValues.isEmpty()){
public void save() {
if (idsuValues.isEmpty()) {
return;
}
if(!file.exists()){
if(!folder.exists()){
if (!file.exists()) {
if (!folder.exists()) {
folder.mkdirs();
}
try {

View file

@ -1,6 +1,5 @@
package techreborn.tiles.idsu;
import ic2.api.energy.EnergyNet;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySink;
@ -9,10 +8,6 @@ import ic2.api.network.INetworkClientTileEntityEventListener;
import ic2.api.tile.IEnergyStorage;
import ic2.core.IC2;
import ic2.core.block.TileEntityBlock;
import ic2.core.init.MainConfig;
import ic2.core.util.ConfigUtil;
import ic2.core.util.StackUtil;
import ic2.core.util.Util;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@ -20,6 +15,8 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import techreborn.config.ConfigTechReborn;
import techreborn.init.ModBlocks;
import techreborn.packets.PacketHandler;
public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySource, INetworkClientTileEntityEventListener, IEnergyStorage {
@ -27,21 +24,21 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
public int channelID = 0;
public void handleGuiInputFromClient(int buttonID, int channel, EntityPlayer player, String name) {
if(buttonID == 4){
if (buttonID == 4) {
channelID = channel;
IDSUManager.INSTANCE.getSaveDataForWorld(worldObj, channel).name = name;
IDSUManager.INSTANCE.getWorldDataFormWorld(worldObj).save();
if(worldObj.isRemote){
if (worldObj.isRemote) {
PacketHandler.sendPacketToPlayer(IDSUManager.INSTANCE.getPacket(worldObj, player), player);
}
}
}
public double getEnergy(){
public double getEnergy() {
return IDSUManager.INSTANCE.getSaveDataForWorld(worldObj, channelID).storedPower;
}
public void setEnergy(double energy){
public void setEnergy(double energy) {
IDSUManager.INSTANCE.getSaveDataForWorld(worldObj, channelID).storedPower = energy;
}
@ -54,6 +51,9 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
private boolean isEmittingRedstone = false;
private int redstoneUpdateInhibit = 5;
public boolean addedToEnergyNet = false;
private double euLastTick = 0;
private double euChange;
private int ticks;
public TileIDSU(int tier1, int output1, int maxStorage1) {
this.tier = tier1;
@ -62,8 +62,8 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
}
public float getChargeLevel() {
float ret = (float)this.getEnergy() / (float)this.maxStorage;
if(ret > 1.0F) {
float ret = (float) this.getEnergy() / (float) this.maxStorage;
if (ret > 1.0F) {
ret = 1.0F;
}
@ -72,19 +72,19 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
setEnergy(Util.limit(nbttagcompound.getDouble("energy"), 0.0D, (double) this.maxStorage + EnergyNet.instance.getPowerFromTier(this.tier)));
this.redstoneMode = nbttagcompound.getByte("redstoneMode");
this.channelID = nbttagcompound.getInteger("channel");
}
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
nbttagcompound.setDouble("energy", this.getEnergy());
nbttagcompound.setByte("redstoneMode", this.redstoneMode);
nbttagcompound.setInteger("channel", this.channelID);
}
public void onLoaded() {
super.onLoaded();
if(IC2.platform.isSimulating()) {
if (IC2.platform.isSimulating()) {
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
this.addedToEnergyNet = true;
}
@ -92,7 +92,7 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
}
public void onUnloaded() {
if(IC2.platform.isSimulating() && this.addedToEnergyNet) {
if (IC2.platform.isSimulating() && this.addedToEnergyNet) {
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
this.addedToEnergyNet = false;
}
@ -106,20 +106,35 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
public void updateEntity() {
super.updateEntity();
if (ticks == ConfigTechReborn.aveargeEuOutTickTime) {
euChange = -1;
ticks = 0;
} else {
ticks++;
euChange += getStored() - euLastTick;
if (euLastTick == getStored()) {
euChange = 0;
}
}
euLastTick = getStored();
boolean needsInvUpdate = false;
if(this.redstoneMode == 5 || this.redstoneMode == 6) {
if (this.redstoneMode == 5 || this.redstoneMode == 6) {
this.hasRedstone = this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord);
}
boolean shouldEmitRedstone1 = this.shouldEmitRedstone();
if(shouldEmitRedstone1 != this.isEmittingRedstone) {
if (shouldEmitRedstone1 != this.isEmittingRedstone) {
this.isEmittingRedstone = shouldEmitRedstone1;
this.setActive(this.isEmittingRedstone);
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord));
}
if(needsInvUpdate) {
if (needsInvUpdate) {
this.markDirty();
}
@ -138,7 +153,7 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
}
public double getOfferedEnergy() {
return this.getEnergy() >= (double)this.output && (this.redstoneMode != 5 || !this.hasRedstone) && (this.redstoneMode != 6 || !this.hasRedstone || this.getEnergy() >= (double)this.maxStorage)?Math.min(this.getEnergy(), (double)this.output):0.0D;
return this.getEnergy() >= (double) this.output && (this.redstoneMode != 5 || !this.hasRedstone) && (this.redstoneMode != 6 || !this.hasRedstone || this.getEnergy() >= (double) this.maxStorage) ? Math.min(this.getEnergy(), (double) this.output) : 0.0D;
}
public void drawEnergy(double amount) {
@ -146,11 +161,11 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
}
public double getDemandedEnergy() {
return (double)this.maxStorage - this.getEnergy();
return (double) this.maxStorage - this.getEnergy();
}
public double injectEnergy(ForgeDirection directionFrom, double amount, double voltage) {
if(this.getEnergy() >= (double)this.maxStorage) {
if (this.getEnergy() >= (double) this.maxStorage) {
return amount;
} else {
setEnergy(this.getEnergy() + amount);
@ -167,7 +182,6 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
}
public void onGuiClosed(EntityPlayer entityPlayer) {
}
@ -176,12 +190,12 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
}
public void setFacing(short facing) {
if(this.addedToEnergyNet) {
if (this.addedToEnergyNet) {
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
}
super.setFacing(facing);
if(this.addedToEnergyNet) {
if (this.addedToEnergyNet) {
this.addedToEnergyNet = false;
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
this.addedToEnergyNet = true;
@ -191,21 +205,21 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
public boolean shouldEmitRedstone() {
boolean shouldEmitRedstone = false;
switch(this.redstoneMode) {
switch (this.redstoneMode) {
case 1:
shouldEmitRedstone = this.getEnergy() >= (double)(this.maxStorage - this.output * 20);
shouldEmitRedstone = this.getEnergy() >= (double) (this.maxStorage - this.output * 20);
break;
case 2:
shouldEmitRedstone = this.getEnergy() > (double)this.output && this.getEnergy() < (double)this.maxStorage;
shouldEmitRedstone = this.getEnergy() > (double) this.output && this.getEnergy() < (double) this.maxStorage;
break;
case 3:
shouldEmitRedstone = this.getEnergy() > (double)this.output && this.getEnergy() < (double)this.maxStorage || this.getEnergy() < (double)this.output;
shouldEmitRedstone = this.getEnergy() > (double) this.output && this.getEnergy() < (double) this.maxStorage || this.getEnergy() < (double) this.output;
break;
case 4:
shouldEmitRedstone = this.getEnergy() < (double)this.output;
shouldEmitRedstone = this.getEnergy() < (double) this.output;
}
if(this.isEmittingRedstone != shouldEmitRedstone && this.redstoneUpdateInhibit != 0) {
if (this.isEmittingRedstone != shouldEmitRedstone && this.redstoneUpdateInhibit != 0) {
--this.redstoneUpdateInhibit;
return this.isEmittingRedstone;
} else {
@ -216,7 +230,7 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
public void onNetworkEvent(EntityPlayer player, int event) {
++this.redstoneMode;
if(this.redstoneMode >= redstoneModes) {
if (this.redstoneMode >= redstoneModes) {
this.redstoneMode = 0;
}
@ -224,22 +238,20 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
}
public String getredstoneMode() {
return this.redstoneMode <= 6 && this.redstoneMode >= 0? StatCollector.translateToLocal("ic2.EUStorage.gui.mod.redstone" + this.redstoneMode):"";
return this.redstoneMode <= 6 && this.redstoneMode >= 0 ? StatCollector.translateToLocal("ic2.EUStorage.gui.mod.redstone" + this.redstoneMode) : "";
}
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
ItemStack ret = super.getWrenchDrop(entityPlayer);
float energyRetainedInStorageBlockDrops = ConfigUtil.getFloat(MainConfig.get(), "balance/energyRetainedInStorageBlockDrops");
if(energyRetainedInStorageBlockDrops > 0.0F) {
NBTTagCompound nbttagcompound = StackUtil.getOrCreateNbtData(ret);
nbttagcompound.setDouble("energy", this.getEnergy() * (double)energyRetainedInStorageBlockDrops);
}
return ret;
NBTTagCompound tileEntity = new NBTTagCompound();
ItemStack dropStack = new ItemStack(ModBlocks.Idsu, 1);
writeToNBT(tileEntity);
dropStack.setTagCompound(new NBTTagCompound());
dropStack.stackTagCompound.setTag("tileEntity", tileEntity);
return dropStack;
}
public int getStored() {
return (int)this.getEnergy();
return (int) this.getEnergy();
}
public int getCapacity() {
@ -251,7 +263,7 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
}
public double getOutputEnergyUnitsPerTick() {
return (double)this.output;
return (double) this.output;
}
public void setStored(int energy1) {
@ -266,4 +278,11 @@ public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySou
public boolean isTeleporterCompatible(ForgeDirection side) {
return true;
}
public double getEuChange() {
if (euChange == -1) {
return -1;
}
return (euChange / ticks);
}
}