Make FluidStack hold a delegate for the fluid. This can then float based on what is "live" at present.

This commit is contained in:
cpw 2015-04-20 09:06:23 -04:00
parent 7e9d2fb509
commit 0543828603
3 changed files with 70 additions and 14 deletions

View file

@ -50,7 +50,7 @@ public abstract class FluidContainerRegistry
code = 31*code + container.getItem().hashCode();
code = 31*code + container.getItemDamage();
if (stack != null)
code = 31*code + stack.fluid.hashCode();
code = 31*code + stack.getFluid().hashCode();
return code;
}
@Override
@ -63,7 +63,7 @@ public abstract class FluidContainerRegistry
if (stack == null && ck.stack != null) return false;
if (stack != null && ck.stack == null) return false;
if (stack == null && ck.stack == null) return true;
if (stack.fluid != ck.stack.fluid) return false;
if (stack.getFluid() != ck.stack.getFluid()) return false;
return true;
}
}

View file

@ -18,6 +18,7 @@ import com.google.common.base.Strings;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import cpw.mods.fml.common.FMLLog;
@ -25,6 +26,7 @@ import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.registry.RegistryDelegate;
/**
* Handles Fluid registrations. Fluids MUST be registered in order to function.
@ -114,6 +116,10 @@ public abstract class FluidRegistry
fluidIDs.put(fluid, id);
}
fluidBlocks = null;
for (FluidDelegate fd : delegates.values())
{
fd.rebind();
}
}
/**
@ -127,7 +133,7 @@ public abstract class FluidRegistry
public static boolean registerFluid(Fluid fluid)
{
masterFluidReference.put(uniqueName(fluid), fluid);
delegates.put(fluid, new FluidDelegate(fluid, fluid.getName()));
if (fluids.containsKey(fluid.getName()))
{
return false;
@ -207,7 +213,7 @@ public abstract class FluidRegistry
public static String getFluidName(FluidStack stack)
{
return getFluidName(stack.fluid);
return getFluidName(stack.getFluid());
}
public static FluidStack getFluidStack(String fluidName, int amount)
@ -343,4 +349,46 @@ public abstract class FluidRegistry
throw new IllegalStateException("The fluid map contains fluids unknown to the master fluid registry");
}
}
}
private static Map<Fluid,FluidDelegate> delegates = Maps.newHashMap();
static RegistryDelegate<Fluid> makeDelegate(Fluid fl)
{
return delegates.get(fl);
}
private static class FluidDelegate implements RegistryDelegate<Fluid>
{
private String name;
private Fluid fluid;
FluidDelegate(Fluid fluid, String name)
{
this.fluid = fluid;
this.name = name;
}
@Override
public Fluid get()
{
return fluid;
}
@Override
public String name()
{
return name;
}
@Override
public Class<Fluid> type()
{
return Fluid.class;
}
void rebind()
{
fluid = fluids.get(name);
}
}
}

View file

@ -2,6 +2,7 @@
package net.minecraftforge.fluids;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.registry.RegistryDelegate;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@ -17,9 +18,15 @@ import net.minecraft.nbt.NBTTagCompound;
*/
public class FluidStack
{
public final Fluid fluid;
/**
* This field will be removed in 1.8. It may be incorrect after a world is loaded. Code should always
* use {@link #getFluid()} instead. That will always reflect the correct value.
*/
@Deprecated
public final Fluid fluid;
public int amount;
public NBTTagCompound tag;
private RegistryDelegate<Fluid> fluidDelegate;
public FluidStack(Fluid fluid, int amount)
{
@ -33,8 +40,9 @@ public class FluidStack
FMLLog.bigWarning("Failed attempt to create a FluidStack for an unregistered Fluid %s (type %s)", fluid.getName(), fluid.getClass().getName());
throw new IllegalArgumentException("Cannot create a fluidstack from an unregistered fluid");
}
this.fluid = fluid;
this.fluidDelegate = FluidRegistry.makeDelegate(fluid);
this.amount = amount;
this.fluid = fluid;
}
public FluidStack(Fluid fluid, int amount, NBTTagCompound nbt)
@ -49,7 +57,7 @@ public class FluidStack
public FluidStack(FluidStack stack, int amount)
{
this(stack.fluid, amount, stack.tag);
this(stack.getFluid(), amount, stack.tag);
}
// To be removed in 1.8
@ -93,7 +101,7 @@ public class FluidStack
public NBTTagCompound writeToNBT(NBTTagCompound nbt)
{
nbt.setString("FluidName", FluidRegistry.getFluidName(fluid));
nbt.setString("FluidName", FluidRegistry.getFluidName(getFluid()));
nbt.setInteger("Amount", amount);
if (tag != null)
@ -105,12 +113,12 @@ public class FluidStack
public final Fluid getFluid()
{
return fluid;
return fluidDelegate.get();
}
public final int getFluidID()
{
return FluidRegistry.getFluidID(fluid);
return FluidRegistry.getFluidID(getFluid());
}
public String getLocalizedName()
@ -128,7 +136,7 @@ public class FluidStack
*/
public FluidStack copy()
{
return new FluidStack(fluid, amount, tag);
return new FluidStack(getFluid(), amount, tag);
}
/**
@ -140,7 +148,7 @@ public class FluidStack
*/
public boolean isFluidEqual(FluidStack other)
{
return other != null && fluid == other.fluid && isFluidStackTagEqual(other);
return other != null && getFluid() == other.getFluid() && isFluidStackTagEqual(other);
}
private boolean isFluidStackTagEqual(FluidStack other)
@ -206,7 +214,7 @@ public class FluidStack
public final int hashCode()
{
int code = 1;
code = 31*code + fluid.hashCode();
code = 31*code + getFluid().hashCode();
code = 31*code + amount;
if (tag != null)
code = 31*code + tag.hashCode();