Rewrite to use new RebornCore Power API. Texture fixes.

This commit is contained in:
Dragon2488 2016-07-21 22:22:56 +07:00
parent 6f54e3801f
commit b292fdd352
77 changed files with 1342 additions and 3008 deletions

View file

@ -15,37 +15,29 @@ import java.util.HashMap;
import java.util.TreeMap;
public class IDSUManager
{
public class IDSUManager {
public static final String savename = "idsu.json";
public static IDSUManager INSTANCE;
public HashMap<World, IDSUWorldSaveData> worldData = new HashMap<>();
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void worldSave(WorldEvent.Save event)
{
public void worldSave(WorldEvent.Save event) {
if (event.getWorld() != null && event.getWorld().getSaveHandler() != null
&& event.getWorld().getSaveHandler().getWorldDirectory() != null)
{
if (worldData.containsKey(event.getWorld()))
{
&& event.getWorld().getSaveHandler().getWorldDirectory() != null) {
if (worldData.containsKey(event.getWorld())) {
worldData.get(event.getWorld()).save();
}
}
}
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void worldLoad(WorldEvent.Load event)
{
public void worldLoad(WorldEvent.Load event) {
if (event.getWorld() != null && event.getWorld().getSaveHandler() != null
&& event.getWorld().getSaveHandler().getWorldDirectory() != null)
{
if (worldData.containsKey(event.getWorld()))
{
&& event.getWorld().getSaveHandler().getWorldDirectory() != null) {
if (worldData.containsKey(event.getWorld())) {
worldData.get(event.getWorld()).load();
} else
{
} else {
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(event.getWorld());
worldData.put(event.getWorld(), worldSaveData);
worldSaveData.load();
@ -54,13 +46,10 @@ public class IDSUManager
}
@SubscribeEvent(priority = EventPriority.LOWEST)
public void worldClosed(WorldEvent.Unload event)
{
public void worldClosed(WorldEvent.Unload event) {
if (event.getWorld() != null && event.getWorld().getSaveHandler() != null
&& event.getWorld().getSaveHandler().getWorldDirectory() != null)
{
if (worldData.containsKey(event.getWorld()))
{
&& event.getWorld().getSaveHandler().getWorldDirectory() != null) {
if (worldData.containsKey(event.getWorld())) {
worldData.get(event.getWorld()).save();
}
}
@ -68,13 +57,10 @@ public class IDSUManager
worldData.clear();
}
public IDSUValueSaveData getSaveDataForWorld(World world, String channel)
{
if (worldData.containsKey(world))
{
public IDSUValueSaveData getSaveDataForWorld(World world, String channel) {
if (worldData.containsKey(world)) {
return worldData.get(world).getSaves(channel);
} else
{
} else {
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(world);
worldData.put(world, worldSaveData);
worldSaveData.load();
@ -82,13 +68,10 @@ public class IDSUManager
}
}
public IDSUWorldSaveData getWorldDataFormWorld(World world)
{
if (worldData.containsKey(world))
{
public IDSUWorldSaveData getWorldDataFormWorld(World world) {
if (worldData.containsKey(world)) {
return worldData.get(world);
} else
{
} else {
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(world);
worldData.put(world, worldSaveData);
worldSaveData.load();
@ -96,31 +79,25 @@ public class IDSUManager
}
}
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
{
} else {
worldSaveData = new IDSUWorldSaveData(world);
worldData.put(world, worldSaveData);
}
Gson gson = new Gson();
Type typeOfHashMap = new TypeToken<TreeMap<Integer, IDSUValueSaveData>>()
{
Type typeOfHashMap = new TypeToken<TreeMap<Integer, IDSUValueSaveData>>() {
}.getType();
worldSaveData.idsuValues.clear();
worldSaveData.idsuValues = gson.fromJson(json, typeOfHashMap);
}
public class IDSUWorldSaveData
{
public class IDSUWorldSaveData {
public TreeMap<String, IDSUValueSaveData> idsuValues = new TreeMap<>();
@ -132,114 +109,90 @@ public class IDSUManager
File file;
public IDSUWorldSaveData(World world)
{
public IDSUWorldSaveData(World world) {
this.world = world;
this.saveHandler = world.getSaveHandler();
folder = new File(saveHandler.getWorldDirectory(), "idsuData");
file = new File(folder, savename);
}
public IDSUValueSaveData getSaves(String udid)
{
if (udid == null)
{
public IDSUValueSaveData getSaves(String udid) {
if (udid == null) {
return null;
}
if (idsuValues.containsKey(udid))
{
if (idsuValues.containsKey(udid)) {
return idsuValues.get(udid);
} else
{
} else {
IDSUValueSaveData data = new IDSUValueSaveData();
idsuValues.put(udid, data);
return data;
}
}
public void load()
{
if (!file.exists())
{
public void load() {
if (!file.exists()) {
return;
}
try
{
try {
Gson gson = new Gson();
BufferedReader reader = new BufferedReader(new FileReader(file));
Type typeOfHashMap = new TypeToken<TreeMap<String, IDSUValueSaveData>>()
{
Type typeOfHashMap = new TypeToken<TreeMap<String, IDSUValueSaveData>>() {
}.getType();
idsuValues.clear();
idsuValues = gson.fromJson(reader, typeOfHashMap);
} catch (Exception e)
{
} catch (Exception e) {
e.printStackTrace();
}
}
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
{
try {
file.createNewFile();
} catch (IOException e)
{
} catch (IOException e) {
e.printStackTrace();
}
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(idsuValues);
try
{
try {
FileWriter writer = new FileWriter(file);
writer.write(json);
writer.close();
} catch (IOException e)
{
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class IDSUValueSaveData
{
public class IDSUValueSaveData {
public double storedPower = 0;
public IDSUValueSaveData(double storedPower)
{
public IDSUValueSaveData(double storedPower) {
this.storedPower = storedPower;
}
public IDSUValueSaveData()
{
public IDSUValueSaveData() {
}
public double getStoredPower()
{
public double getStoredPower() {
return storedPower;
}
public void setStoredPower(double storedPower)
{
public void setStoredPower(double storedPower) {
this.storedPower = storedPower;
}
public void addEnergy(double storedPower)
{
public void addEnergy(double storedPower) {
this.storedPower += storedPower;
}
}

View file

@ -1,19 +1,19 @@
package techreborn.tiles.idsu;
import reborncore.common.IWrenchable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import org.apache.commons.lang3.StringUtils;
import reborncore.api.power.EnumPowerTier;
import reborncore.common.powerSystem.TilePowerAcceptor;
import reborncore.common.IWrenchable;
import reborncore.common.tile.TilePowerAcceptorProducer;
import techreborn.config.ConfigTechReborn;
import techreborn.init.ModBlocks;
import techreborn.power.EnergyUtils;
public class TileIDSU extends TilePowerAcceptor implements IWrenchable
{
public class TileIDSU extends TilePowerAcceptorProducer implements IWrenchable {
public String ownerUdid;
public int tier;
@ -23,111 +23,67 @@ public class TileIDSU extends TilePowerAcceptor implements IWrenchable
private double euChange;
private int ticks;
public TileIDSU(int tier1, int output1, int maxStorage1)
{
super(tier1);
public TileIDSU(int tier1, int output1, int maxStorage1) {
this.tier = tier1;
this.output = output1;
this.maxStorage = maxStorage1;
}
public TileIDSU()
{
public TileIDSU() {
this(5, 2048, 100000000);
}
@Override
public double getEnergy()
{
if (ownerUdid == null && StringUtils.isBlank(ownerUdid) || StringUtils.isEmpty(ownerUdid))
{
public double getEnergy() {
if (ownerUdid == null && StringUtils.isBlank(ownerUdid) || StringUtils.isEmpty(ownerUdid)) {
return 0.0;
}
return IDSUManager.INSTANCE.getSaveDataForWorld(worldObj, ownerUdid).storedPower;
}
@Override
public void setEnergy(double energy)
{
if (ownerUdid == null && StringUtils.isBlank(ownerUdid) || StringUtils.isEmpty(ownerUdid))
{
public void setEnergy(double energy) {
if (ownerUdid == null && StringUtils.isBlank(ownerUdid) || StringUtils.isEmpty(ownerUdid)) {
return;
}
IDSUManager.INSTANCE.getSaveDataForWorld(worldObj, ownerUdid).storedPower = energy;
}
@Override
public void readFromNBTWithoutCoords(NBTTagCompound tag)
{
}
@Override
public double getMaxPower()
{
public double getMaxPower() {
return 1000000000;
}
@Override
public boolean canAcceptEnergy(EnumFacing direction)
{
return getFacingEnum() != direction;
public EnumPowerTier getTier() {
return EnumPowerTier.INSANE;
}
@Override
public boolean canProvideEnergy(EnumFacing direction)
{
return getFacingEnum() == direction;
}
@Override
public double getMaxOutput()
{
return output;
}
@Override
public double getMaxInput()
{
return maxStorage;
}
@Override
public EnumPowerTier getTier()
{
return EnumPowerTier.EXTREME;
}
public float getChargeLevel()
{
public float getChargeLevel() {
float ret = (float) this.getEnergy() / (float) this.maxStorage;
if (ret > 1.0F)
{
if (ret > 1.0F) {
ret = 1.0F;
}
return ret;
}
public void readFromNBT(NBTTagCompound nbttagcompound)
{
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
this.ownerUdid = nbttagcompound.getString("ownerUdid");
}
public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound)
{
public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
if (ownerUdid == null && StringUtils.isBlank(ownerUdid) || StringUtils.isEmpty(ownerUdid))
{
if (ownerUdid == null || StringUtils.isEmpty(ownerUdid)) {
return nbttagcompound;
}
nbttagcompound.setString("ownerUdid", this.ownerUdid);
return nbttagcompound;
}
public void updateEntity() {
super.updateEntity();
public void update() {
super.update();
if (ticks == ConfigTechReborn.AverageEuOutTickTime) {
euChange = -1;
@ -145,38 +101,55 @@ public class TileIDSU extends TilePowerAcceptor implements IWrenchable
if (!worldObj.isRemote && getEnergy() > 0) {
double maxOutput = getEnergy() > getMaxOutput() ? getMaxOutput() : getEnergy();
useEnergy(EnergyUtils.dispatchEnergyToNeighbours(worldObj, getPos(), this, maxOutput));
for(EnumFacing facing : EnumFacing.VALUES) {
double disposed = emitEnergy(facing, maxOutput);
if(disposed != 0) {
maxOutput -= disposed;
useEnergy(disposed);
if (maxOutput == 0) return;
}
}
}
}
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side)
{
//TODO move to RebornCore
public double emitEnergy(EnumFacing enumFacing, double amount) {
BlockPos pos = getPos().offset(enumFacing);
EnergyUtils.PowerNetReceiver receiver = EnergyUtils.getReceiver(
worldObj, enumFacing.getOpposite(), pos);
if(receiver != null) {
return receiver.receiveEnergy(amount, false);
}
return 0;
}
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
return false;
}
@Override public boolean wrenchCanSetFacing(EntityPlayer p0, EnumFacing p1)
{
@Override
public boolean wrenchCanSetFacing(EntityPlayer p0, EnumFacing p1) {
return true;
}
@Override public EnumFacing getFacing()
{
@Override
public EnumFacing getFacing() {
return getFacingEnum();
}
@Override public boolean wrenchCanRemove(EntityPlayer p0)
{
@Override
public boolean wrenchCanRemove(EntityPlayer p0) {
return true;
}
@Override public float getWrenchDropRate()
{
@Override
public float getWrenchDropRate() {
return 1.0F;
}
public ItemStack getWrenchDrop(EntityPlayer entityPlayer)
{
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
NBTTagCompound tileEntity = new NBTTagCompound();
ItemStack dropStack = new ItemStack(ModBlocks.Idsu, 1);
writeToNBT(tileEntity);
@ -185,40 +158,32 @@ public class TileIDSU extends TilePowerAcceptor implements IWrenchable
return dropStack;
}
public double getEuChange()
{
if (euChange == -1)
{
public double getEuChange() {
if (euChange == -1) {
return -1;
}
return (euChange / ticks);
}
public void handleGuiInputFromClient(int id)
{
if (id == 0)
{
public void handleGuiInputFromClient(int id) {
if (id == 0) {
output += 256;
}
if (id == 1)
{
if (id == 1) {
output += 64;
}
if (id == 2)
{
if (id == 2) {
output -= 64;
}
if (id == 3)
{
if (id == 3) {
output -= 256;
}
if (output > 4096)
{
if (output > 4096) {
output = 4096;
}
if (output <= -1)
{
if (output <= -1) {
output = 0;
}
}
}