Improve save stuff

This commit is contained in:
Raoul Van den Berge
2016-06-20 13:20:26 +02:00
parent bb05d83093
commit 336921d589
10 changed files with 97 additions and 79 deletions

View File

@@ -3,7 +3,7 @@ package com.jaquadro.minecraft.storagedrawers.api.registry;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
/** /**
* Ingredient handlers are used to get ItemStacks from ingredients in custom IRecipe implementations. If you have * Ingredient handlers are used to getOrLoad ItemStacks from ingredients in custom IRecipe implementations. If you have
* registered an IRecipeHandler that returns lists of objects that aren't ItemStacks, then you will need to * registered an IRecipeHandler that returns lists of objects that aren't ItemStacks, then you will need to
* implement an ingredient handler for those objects. * implement an ingredient handler for those objects.
*/ */

View File

@@ -12,7 +12,7 @@ public interface IRecipeHandler {
/** /**
* Get the recipe ingredient list as an array of objects (usually used for shaped recipes). * Get the recipe ingredient list as an array of objects (usually used for shaped recipes).
* If your array does not contain ItemStack objects, you will need to register an {@link IIngredientHandler} to * If your array does not contain ItemStack objects, you will need to register an {@link IIngredientHandler} to
* get an ItemStack from them. * getOrLoad an ItemStack from them.
* <p> * <p>
* If you would prefer to return a List, return null in this method and implement {@link #getInputAsList}. * If you would prefer to return a List, return null in this method and implement {@link #getInputAsList}.
* *
@@ -24,7 +24,7 @@ public interface IRecipeHandler {
/** /**
* Get the recipe ingredient list as a list of objects (usually used for shapeless recipes). * Get the recipe ingredient list as a list of objects (usually used for shapeless recipes).
* If your list does not contain ItemStack objects, you will need to register an {@link IIngredientHandler} to * If your list does not contain ItemStack objects, you will need to register an {@link IIngredientHandler} to
* get an ItemStack from them. * getOrLoad an ItemStack from them.
* <p> * <p>
* If you would prefer to return an array, return null in this method and implement {@link #getInputAsArray}. * If you would prefer to return an array, return null in this method and implement {@link #getInputAsArray}.
* *

View File

@@ -6,7 +6,7 @@ import net.minecraft.world.World;
/** /**
* Represents a slave or machine in the storage network. * Represents a slave or machine in the storage network.
* *
* Make sure you implement {@link Object#hashCode()} or the slave will not get properly removed or added by the storage master. * Make sure you implement {@link Object#hashCode()} or the slave will not getOrLoad properly removed or added by the storage master.
* Typically the hash code from {@link INetworkSlave#getPosition()} is used. * Typically the hash code from {@link INetworkSlave#getPosition()} is used.
*/ */
public interface INetworkSlave { public interface INetworkSlave {
@@ -65,7 +65,7 @@ public interface INetworkSlave {
boolean isConnected(); boolean isConnected();
/** /**
* @return If {@link INetworkSlave#canUpdate()} can get called. Typically checks for connection and redstone mode. * @return If {@link INetworkSlave#canUpdate()} can getOrLoad called. Typically checks for connection and redstone mode.
*/ */
boolean canUpdate(); boolean canUpdate();

View File

@@ -40,6 +40,10 @@ public class NetworkMaster {
public static final String NBT_CRAFTING_TASKS = "CraftingTasks"; public static final String NBT_CRAFTING_TASKS = "CraftingTasks";
public static final String NBT_ENERGY = "Energy"; public static final String NBT_ENERGY = "Energy";
public static final String NBT_SLAVES = "Slaves";
public static final String NBT_SLAVE_X = "X";
public static final String NBT_SLAVE_Y = "Y";
public static final String NBT_SLAVE_Z = "Z";
private StorageHandler storageHandler = new StorageHandler(this); private StorageHandler storageHandler = new StorageHandler(this);
private WirelessGridHandler wirelessGridHandler = new WirelessGridHandler(this); private WirelessGridHandler wirelessGridHandler = new WirelessGridHandler(this);
@@ -446,7 +450,7 @@ public class NetworkMaster {
if (RefinedStorageUtils.compareStackNoQuantity(stack, otherStack)) { if (RefinedStorageUtils.compareStackNoQuantity(stack, otherStack)) {
// We copy here so we don't modify the quantity of the ItemStack IStorage uses. // We copy here so we don't modify the quantity of the ItemStack IStorage uses.
// We re-get the ItemStack because the stack may change from a previous iteration in this loop // We re-getOrLoad the ItemStack because the stack may change from a previous iteration in this loop
ItemStack newStack = items.get(i).copy(); ItemStack newStack = items.get(i).copy();
newStack.stackSize += otherStack.stackSize; newStack.stackSize += otherStack.stackSize;
items.set(i, newStack); items.set(i, newStack);
@@ -589,13 +593,13 @@ public class NetworkMaster {
} }
} }
if (tag.hasKey("Machines")) { if (tag.hasKey(NBT_SLAVES)) {
NBTTagList machinesTag = tag.getTagList("Machines", Constants.NBT.TAG_COMPOUND); NBTTagList slavesTag = tag.getTagList(NBT_SLAVES, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < machinesTag.tagCount(); ++i) { for (int i = 0; i < slavesTag.tagCount(); ++i) {
NBTTagCompound coords = machinesTag.getCompoundTagAt(i); NBTTagCompound slave = slavesTag.getCompoundTagAt(i);
slavesToLoad.add(new BlockPos(coords.getInteger("X"), coords.getInteger("Y"), coords.getInteger("Z"))); slavesToLoad.add(new BlockPos(slave.getInteger(NBT_SLAVE_X), slave.getInteger(NBT_SLAVE_Y), slave.getInteger(NBT_SLAVE_Z)));
} }
} }
} }
@@ -615,20 +619,24 @@ public class NetworkMaster {
tag.setTag(NBT_CRAFTING_TASKS, list); tag.setTag(NBT_CRAFTING_TASKS, list);
NBTTagList machinesTag = new NBTTagList(); NBTTagList slavesTag = new NBTTagList();
for (INetworkSlave slave : slaves) { for (INetworkSlave slave : slaves) {
NBTTagCompound coords = new NBTTagCompound(); NBTTagCompound slaveTag = new NBTTagCompound();
coords.setInteger("X", slave.getPosition().getX());
coords.setInteger("Y", slave.getPosition().getY()); slaveTag.setInteger(NBT_SLAVE_X, slave.getPosition().getX());
coords.setInteger("Z", slave.getPosition().getZ()); slaveTag.setInteger(NBT_SLAVE_Y, slave.getPosition().getY());
machinesTag.appendTag(coords); slaveTag.setInteger(NBT_SLAVE_Z, slave.getPosition().getZ());
slavesTag.appendTag(slaveTag);
} }
tag.setTag("Machines", machinesTag);
tag.setTag(NBT_SLAVES, slavesTag);
return tag; return tag;
} }
public void markDirty() { public void markDirty() {
NetworkMasterSavedData.get(world).markDirty(); NetworkMasterSavedData.getOrLoad(world).markDirty();
} }
} }

View File

@@ -25,6 +25,6 @@ public class NetworkMasterEventHandler {
@SubscribeEvent @SubscribeEvent
public void onWorldLoad(WorldEvent.Load e) { public void onWorldLoad(WorldEvent.Load e) {
NetworkMasterSavedData.get(e.getWorld()); NetworkMasterSavedData.getOrLoad(e.getWorld());
} }
} }

View File

@@ -10,7 +10,13 @@ import net.minecraftforge.common.util.Constants;
import java.util.Map; import java.util.Map;
public class NetworkMasterSavedData extends WorldSavedData { public class NetworkMasterSavedData extends WorldSavedData {
public static final String NBT_STORAGE_NETWORKS = "StorageNetworks"; public static final String ID = "RSNetworks";
public static final String NBT_NETWORKS = "Networks";
public static final String NBT_NETWORK_X = "X";
public static final String NBT_NETWORK_Y = "Y";
public static final String NBT_NETWORK_Z = "Z";
public static final String NBT_NETWORK_DIM = "Dim";
public static final String NBT_NETWORK_DATA = "Data";
public NetworkMasterSavedData(String name) { public NetworkMasterSavedData(String name) {
super(name); super(name);
@@ -18,17 +24,16 @@ public class NetworkMasterSavedData extends WorldSavedData {
@Override @Override
public void readFromNBT(NBTTagCompound tag) { public void readFromNBT(NBTTagCompound tag) {
NBTTagList networks = tag.getTagList(NBT_STORAGE_NETWORKS, Constants.NBT.TAG_COMPOUND); NBTTagList networks = tag.getTagList(NBT_NETWORKS, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < networks.tagCount(); ++i) { for (int i = 0; i < networks.tagCount(); ++i) {
NBTTagCompound networkTag = networks.getCompoundTagAt(i); NBTTagCompound networkTag = networks.getCompoundTagAt(i);
BlockPos pos = new BlockPos(networkTag.getInteger("X"), networkTag.getInteger("Y"), networkTag.getInteger("Z")); NetworkMaster network = new NetworkMaster(new BlockPos(networkTag.getInteger(NBT_NETWORK_X), networkTag.getInteger(NBT_NETWORK_Y), networkTag.getInteger(NBT_NETWORK_Z)));
NetworkMaster network = new NetworkMaster(pos); network.readFromNBT(networkTag.getCompoundTag(NBT_NETWORK_DATA));
network.readFromNBT(networkTag.getCompoundTag("Data"));
NetworkMasterRegistry.add(network, networkTag.getInteger("Dim")); NetworkMasterRegistry.add(network, networkTag.getInteger(NBT_NETWORK_DIM));
} }
} }
@@ -39,26 +44,30 @@ public class NetworkMasterSavedData extends WorldSavedData {
for (Map.Entry<Integer, Map<BlockPos, NetworkMaster>> entry : NetworkMasterRegistry.NETWORKS.entrySet()) { for (Map.Entry<Integer, Map<BlockPos, NetworkMaster>> entry : NetworkMasterRegistry.NETWORKS.entrySet()) {
for (NetworkMaster network : entry.getValue().values()) { for (NetworkMaster network : entry.getValue().values()) {
NBTTagCompound networkTag = new NBTTagCompound(); NBTTagCompound networkTag = new NBTTagCompound();
networkTag.setInteger("X", network.getPos().getX());
networkTag.setInteger("Y", network.getPos().getY()); networkTag.setInteger(NBT_NETWORK_X, network.getPos().getX());
networkTag.setInteger("Z", network.getPos().getZ()); networkTag.setInteger(NBT_NETWORK_Y, network.getPos().getY());
networkTag.setInteger("Dim", entry.getKey()); networkTag.setInteger(NBT_NETWORK_Z, network.getPos().getZ());
networkTag.setTag("Data", network.writeToNBT(new NBTTagCompound())); networkTag.setInteger(NBT_NETWORK_DIM, entry.getKey());
networkTag.setTag(NBT_NETWORK_DATA, network.writeToNBT(new NBTTagCompound()));
networks.appendTag(networkTag); networks.appendTag(networkTag);
} }
} }
tag.setTag(NBT_STORAGE_NETWORKS, networks); tag.setTag(NBT_NETWORKS, networks);
return tag; return tag;
} }
public static NetworkMasterSavedData get(World world) { public static NetworkMasterSavedData getOrLoad(World world) {
NetworkMasterSavedData instance = (NetworkMasterSavedData) world.getMapStorage().getOrLoadData(NetworkMasterSavedData.class, "RSStorageNetworks"); NetworkMasterSavedData instance = (NetworkMasterSavedData) world.getMapStorage().getOrLoadData(NetworkMasterSavedData.class, ID);
if (instance == null) { if (instance == null) {
instance = new NetworkMasterSavedData("RSStorageNetworks"); instance = new NetworkMasterSavedData(ID);
world.getMapStorage().setData("RSStorageNetworks", instance);
world.getMapStorage().setData(ID, instance);
} }
return instance; return instance;

View File

@@ -6,7 +6,7 @@ import refinedstorage.RefinedStorageUtils;
import refinedstorage.api.network.NetworkMaster; import refinedstorage.api.network.NetworkMaster;
import refinedstorage.container.ContainerController; import refinedstorage.container.ContainerController;
import refinedstorage.gui.sidebutton.SideButtonRedstoneMode; import refinedstorage.gui.sidebutton.SideButtonRedstoneMode;
import refinedstorage.tile.controller.ClientMachine; import refinedstorage.tile.controller.ClientSlave;
import refinedstorage.tile.controller.TileController; import refinedstorage.tile.controller.TileController;
import java.util.List; import java.util.List;
@@ -63,28 +63,28 @@ public class GuiController extends GuiBase {
RenderHelper.enableGUIStandardItemLighting(); RenderHelper.enableGUIStandardItemLighting();
List<ClientMachine> machines = controller.getClientMachines(); List<ClientSlave> slaves = controller.getClientSlaves();
ClientMachine machineHovering = null; ClientSlave slaveHovering = null;
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
if (slot < machines.size()) { if (slot < slaves.size()) {
ClientMachine machine = machines.get(slot); ClientSlave slave = slaves.get(slot);
drawItem(x, y + 5, machine.stack); drawItem(x, y + 5, slave.stack);
float scale = 0.5f; float scale = 0.5f;
GlStateManager.pushMatrix(); GlStateManager.pushMatrix();
GlStateManager.scale(scale, scale, 1); GlStateManager.scale(scale, scale, 1);
drawString(RefinedStorageUtils.calculateOffsetOnScale(x + 1, scale), RefinedStorageUtils.calculateOffsetOnScale(y - 2, scale), machine.stack.getDisplayName()); drawString(RefinedStorageUtils.calculateOffsetOnScale(x + 1, scale), RefinedStorageUtils.calculateOffsetOnScale(y - 2, scale), slave.stack.getDisplayName());
drawString(RefinedStorageUtils.calculateOffsetOnScale(x + 21, scale), RefinedStorageUtils.calculateOffsetOnScale(y + 10, scale), t("gui.refinedstorage:controller.machine_amount", machine.amount)); drawString(RefinedStorageUtils.calculateOffsetOnScale(x + 21, scale), RefinedStorageUtils.calculateOffsetOnScale(y + 10, scale), t("gui.refinedstorage:controller.machine_amount", slave.amount));
GlStateManager.popMatrix(); GlStateManager.popMatrix();
if (inBounds(x, y, 16, 16, mouseX, mouseY)) { if (inBounds(x, y, 16, 16, mouseX, mouseY)) {
machineHovering = machine; slaveHovering = slave;
} }
} }
@@ -98,8 +98,8 @@ public class GuiController extends GuiBase {
slot++; slot++;
} }
if (machineHovering != null) { if (slaveHovering != null) {
drawTooltip(mouseX, mouseY, t("misc.refinedstorage:energy_usage_minimal", machineHovering.energyUsage)); drawTooltip(mouseX, mouseY, t("misc.refinedstorage:energy_usage_minimal", slaveHovering.energyUsage));
} }
if (inBounds(barX, barY, barWidth, barHeight, mouseX, mouseY)) { if (inBounds(barX, barY, barWidth, barHeight, mouseX, mouseY)) {
@@ -112,7 +112,7 @@ public class GuiController extends GuiBase {
} }
private int getRows() { private int getRows() {
int max = (int) Math.ceil((float) controller.getClientMachines().size() / (float) 2); int max = (int) Math.ceil((float) controller.getClientSlaves().size() / (float) 2);
return max < 0 ? 0 : max; return max < 0 ? 0 : max;
} }

View File

@@ -62,7 +62,7 @@ public class TileDestructor extends TileSlave implements ICompareConfig, IModeCo
worldObj.setBlockToAir(front); worldObj.setBlockToAir(front);
for (ItemStack drop : drops) { for (ItemStack drop : drops) {
// We check if the controller isn't null here because when a destructor faces a machine block and removes it // We check if the controller isn't null here because when a destructor faces a slave block and removes it
// it will essentially remove this block itself from the network without knowing // it will essentially remove this block itself from the network without knowing
if (network == null) { if (network == null) {
InventoryHelper.spawnItemStack(worldObj, front.getX(), front.getY(), front.getZ(), drop); InventoryHelper.spawnItemStack(worldObj, front.getX(), front.getY(), front.getZ(), drop);

View File

@@ -3,7 +3,7 @@ package refinedstorage.tile.controller;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import refinedstorage.RefinedStorageUtils; import refinedstorage.RefinedStorageUtils;
public class ClientMachine { public class ClientSlave {
public ItemStack stack; public ItemStack stack;
public int amount; public int amount;
public int energyUsage; public int energyUsage;
@@ -14,11 +14,11 @@ public class ClientMachine {
return true; return true;
} }
if (!(other instanceof ClientMachine)) { if (!(other instanceof ClientSlave)) {
return false; return false;
} }
return energyUsage == ((ClientMachine) other).energyUsage && RefinedStorageUtils.compareStack(stack, ((ClientMachine) other).stack); return energyUsage == ((ClientSlave) other).energyUsage && RefinedStorageUtils.compareStack(stack, ((ClientSlave) other).stack);
} }
@Override @Override

View File

@@ -27,7 +27,7 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr
private NetworkMaster network; private NetworkMaster network;
// Only used client side // Only used client side
private List<ClientMachine> clientMachines = new ArrayList<ClientMachine>(); private List<ClientSlave> clientSlaves = new ArrayList<ClientSlave>();
private int energy; private int energy;
private int energyUsage; private int energyUsage;
private EnumControllerType type; private EnumControllerType type;
@@ -94,8 +94,8 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr
getNetwork().setRedstoneMode(mode); getNetwork().setRedstoneMode(mode);
} }
public List<ClientMachine> getClientMachines() { public List<ClientSlave> getClientSlaves() {
return clientMachines; return clientSlaves;
} }
public int getEnergy() { public int getEnergy() {
@@ -120,20 +120,21 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr
this.energyUsage = buf.readInt(); this.energyUsage = buf.readInt();
this.redstoneMode = RedstoneMode.getById(buf.readInt()); this.redstoneMode = RedstoneMode.getById(buf.readInt());
List<ClientMachine> machines = new ArrayList<ClientMachine>(); List<ClientSlave> slaves = new ArrayList<ClientSlave>();
int size = buf.readInt(); int size = buf.readInt();
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
ClientMachine machine = new ClientMachine(); ClientSlave slave = new ClientSlave();
machine.energyUsage = buf.readInt();
machine.amount = buf.readInt();
machine.stack = ByteBufUtils.readItemStack(buf);
machines.add(machine); slave.energyUsage = buf.readInt();
slave.amount = buf.readInt();
slave.stack = ByteBufUtils.readItemStack(buf);
slaves.add(slave);
} }
this.clientMachines = machines; this.clientSlaves = slaves;
} }
@Override @Override
@@ -143,37 +144,37 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr
buf.writeInt(getNetwork().getRedstoneMode().id); buf.writeInt(getNetwork().getRedstoneMode().id);
List<ClientMachine> m = new ArrayList<ClientMachine>(); List<ClientSlave> slaves = new ArrayList<ClientSlave>();
for (INetworkSlave machine : getNetwork().getSlaves()) { for (INetworkSlave slave : getNetwork().getSlaves()) {
if (machine.canUpdate()) { if (slave.canUpdate()) {
IBlockState state = worldObj.getBlockState(machine.getPosition()); IBlockState state = worldObj.getBlockState(slave.getPosition());
ClientMachine clientMachine = new ClientMachine(); ClientSlave clientSlave = new ClientSlave();
clientMachine.energyUsage = machine.getEnergyUsage(); clientSlave.energyUsage = slave.getEnergyUsage();
clientMachine.amount = 1; clientSlave.amount = 1;
clientMachine.stack = new ItemStack(state.getBlock(), 1, state.getBlock().getMetaFromState(state)); clientSlave.stack = new ItemStack(state.getBlock(), 1, state.getBlock().getMetaFromState(state));
if (m.contains(clientMachine)) { if (slaves.contains(clientSlave)) {
for (ClientMachine other : m) { for (ClientSlave other : slaves) {
if (other.equals(clientMachine)) { if (other.equals(clientSlave)) {
other.amount++; other.amount++;
break; break;
} }
} }
} else { } else {
m.add(clientMachine); slaves.add(clientSlave);
} }
} }
} }
buf.writeInt(m.size()); buf.writeInt(slaves.size());
for (ClientMachine machine : m) { for (ClientSlave slave : slaves) {
buf.writeInt(machine.energyUsage); buf.writeInt(slave.energyUsage);
buf.writeInt(machine.amount); buf.writeInt(slave.amount);
ByteBufUtils.writeItemStack(buf, machine.stack); ByteBufUtils.writeItemStack(buf, slave.stack);
} }
} }