From c97f61769237d27c44440e609097fd431c974005 Mon Sep 17 00:00:00 2001 From: Aidan Brady Date: Fri, 1 Aug 2014 23:44:28 -0400 Subject: [PATCH] Generalized some of the multiblock code into a stricter format --- .../common/multiblock/MultiblockCache.java | 19 ++++++ .../common/multiblock/SynchronizedData.java | 63 +++++++++++++++++++ .../common/tank/DynamicTankCache.java | 11 ++-- .../common/tank/SynchronizedTankData.java | 55 +--------------- 4 files changed, 91 insertions(+), 57 deletions(-) create mode 100644 src/main/java/mekanism/common/multiblock/MultiblockCache.java create mode 100644 src/main/java/mekanism/common/multiblock/SynchronizedData.java diff --git a/src/main/java/mekanism/common/multiblock/MultiblockCache.java b/src/main/java/mekanism/common/multiblock/MultiblockCache.java new file mode 100644 index 000000000..c4b653fd0 --- /dev/null +++ b/src/main/java/mekanism/common/multiblock/MultiblockCache.java @@ -0,0 +1,19 @@ +package mekanism.common.multiblock; + +import java.util.HashSet; + +import mekanism.api.Coord4D; +import net.minecraft.nbt.NBTTagCompound; + +public abstract class MultiblockCache +{ + public HashSet locations = new HashSet(); + + public abstract void apply(T data); + + public abstract void sync(T data); + + public abstract void load(NBTTagCompound nbtTags); + + public abstract void save(NBTTagCompound nbtTags); +} diff --git a/src/main/java/mekanism/common/multiblock/SynchronizedData.java b/src/main/java/mekanism/common/multiblock/SynchronizedData.java new file mode 100644 index 000000000..3003310c4 --- /dev/null +++ b/src/main/java/mekanism/common/multiblock/SynchronizedData.java @@ -0,0 +1,63 @@ +package mekanism.common.multiblock; + +import java.util.HashSet; +import java.util.Set; + +import mekanism.api.Coord4D; +import mekanism.common.tank.SynchronizedTankData; +import net.minecraft.nbt.NBTTagCompound; + +public abstract class SynchronizedData +{ + public Set locations = new HashSet(); + + public int volLength; + + public int volWidth; + + public int volHeight; + + public int volume; + + public int inventoryID; + + @Override + public int hashCode() + { + int code = 1; + code = 31 * locations.hashCode(); + code = 31 * volLength; + code = 31 * volWidth; + code = 31 * volHeight; + code = 31 * volume; + return code; + } + + @Override + public boolean equals(Object obj) + { + if(obj == null || obj.getClass() != getClass()) + { + return false; + } + + SynchronizedData data = (SynchronizedData)obj; + + if(!data.locations.equals(locations)) + { + return false; + } + + if(data.volLength != volLength || data.volWidth != volWidth || data.volHeight != volHeight) + { + return false; + } + + if(data.volume != volume) + { + return false; + } + + return true; + } +} diff --git a/src/main/java/mekanism/common/tank/DynamicTankCache.java b/src/main/java/mekanism/common/tank/DynamicTankCache.java index cda2bbc00..578d409e3 100644 --- a/src/main/java/mekanism/common/tank/DynamicTankCache.java +++ b/src/main/java/mekanism/common/tank/DynamicTankCache.java @@ -3,6 +3,7 @@ package mekanism.common.tank; import java.util.HashSet; import mekanism.api.Coord4D; +import mekanism.common.multiblock.MultiblockCache; import mekanism.common.util.FluidContainerUtils.ContainerEditMode; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -10,14 +11,13 @@ import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.Constants.NBT; import net.minecraftforge.fluids.FluidStack; -public class DynamicTankCache -{ - public HashSet locations = new HashSet(); - +public class DynamicTankCache extends MultiblockCache +{ public ItemStack[] inventory = new ItemStack[2]; public FluidStack fluid; public ContainerEditMode editMode = ContainerEditMode.BOTH; + @Override public void apply(SynchronizedTankData data) { data.inventory = inventory; @@ -25,6 +25,7 @@ public class DynamicTankCache data.editMode = editMode; } + @Override public void sync(SynchronizedTankData data) { inventory = data.inventory; @@ -32,6 +33,7 @@ public class DynamicTankCache editMode = data.editMode; } + @Override public void load(NBTTagCompound nbtTags) { editMode = ContainerEditMode.values()[nbtTags.getInteger("editMode")]; @@ -56,6 +58,7 @@ public class DynamicTankCache } } + @Override public void save(NBTTagCompound nbtTags) { nbtTags.setInteger("editMode", editMode.ordinal()); diff --git a/src/main/java/mekanism/common/tank/SynchronizedTankData.java b/src/main/java/mekanism/common/tank/SynchronizedTankData.java index fd83028be..f2f9b32a8 100644 --- a/src/main/java/mekanism/common/tank/SynchronizedTankData.java +++ b/src/main/java/mekanism/common/tank/SynchronizedTankData.java @@ -4,25 +4,14 @@ import java.util.HashSet; import java.util.Set; import mekanism.api.Coord4D; +import mekanism.common.multiblock.SynchronizedData; import mekanism.common.util.FluidContainerUtils.ContainerEditMode; import net.minecraft.item.ItemStack; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.fluids.FluidStack; -public class SynchronizedTankData +public class SynchronizedTankData extends SynchronizedData { - public Set locations = new HashSet(); - - public int volLength; - - public int volWidth; - - public int volHeight; - - public int volume; - - public int inventoryID; - public FluidStack fluidStored; public ContainerEditMode editMode = ContainerEditMode.BOTH; @@ -37,46 +26,6 @@ public class SynchronizedTankData public Set valves = new HashSet(); - @Override - public int hashCode() - { - int code = 1; - code = 31 * locations.hashCode(); - code = 31 * volLength; - code = 31 * volWidth; - code = 31 * volHeight; - code = 31 * volume; - return code; - } - - @Override - public boolean equals(Object obj) - { - if(!(obj instanceof SynchronizedTankData)) - { - return false; - } - - SynchronizedTankData data = (SynchronizedTankData)obj; - - if(!data.locations.equals(locations)) - { - return false; - } - - if(data.volLength != volLength || data.volWidth != volWidth || data.volHeight != volHeight) - { - return false; - } - - if(data.volume != volume) - { - return false; - } - - return true; - } - public static class ValveData { public ForgeDirection side;