Removed fluidID from ItemStack.

This fixes a rather huge issue where FluidStacks on the client could be desynced if a modder was unaware of it.

This is a breaking change but can be mitigated with a transformer to the getter getFluidID().

Signed-off-by: King Lemming <kinglemming@gmail.com>
This commit is contained in:
King Lemming 2015-03-25 01:27:15 -04:00 committed by cpw
parent 38b9febfe5
commit 3fc40b955a
6 changed files with 88 additions and 82 deletions

View file

@ -1,12 +1,10 @@
package net.minecraftforge.common.network;
import java.util.Map;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import cpw.mods.fml.common.network.ByteBufUtils;
import io.netty.buffer.ByteBuf;
@ -40,15 +38,15 @@ public abstract class ForgeMessage {
}
public static class FluidIdMapMessage extends ForgeMessage {
BiMap<String, Integer> fluidIds = HashBiMap.create();
BiMap<Fluid, Integer> fluidIds = HashBiMap.create();
@Override
void toBytes(ByteBuf bytes)
{
Map<String, Integer> ids = FluidRegistry.getRegisteredFluidIDs();
Map<Fluid, Integer> ids = FluidRegistry.getRegisteredFluidIDs();
bytes.writeInt(ids.size());
for (Map.Entry<String, Integer> entry : ids.entrySet())
for (Map.Entry<Fluid, Integer> entry : ids.entrySet())
{
ByteBufUtils.writeUTF8String(bytes,entry.getKey());
ByteBufUtils.writeUTF8String(bytes,entry.getKey().getName());
bytes.writeInt(entry.getValue());
}
}
@ -60,7 +58,7 @@ public abstract class ForgeMessage {
for (int i = 0; i < listSize; i++) {
String fluidName = ByteBufUtils.readUTF8String(bytes);
int fluidId = bytes.readInt();
fluidIds.put(fluidName, fluidId);
fluidIds.put(FluidRegistry.getFluid(fluidName), fluidId);
}
}
}

View file

@ -42,7 +42,7 @@ public abstract class BlockFluidBase extends Block implements IFluidBlock
protected float quantaPerBlockFloat = 8F;
protected int density = 1;
protected int densityDir = -1;
protected int temperature = 295;
protected int temperature = 300;
protected int tickRate = 20;
protected int renderPass = 1;
@ -226,6 +226,12 @@ public abstract class BlockFluidBase extends Block implements IFluidBlock
return false;
}
@Override
public boolean canPlaceBlockAt(World world, int x, int y, int z)
{
return canDisplace(world, x, y, z);
}
@Override
public boolean getBlocksMovement(IBlockAccess world, int x, int y, int z)
{

View file

@ -33,7 +33,7 @@ import net.minecraft.item.EnumRarity;
*
*/
public class Fluid
{
{
/** The unique identification name for this fluid. */
protected final String fluidName;
@ -65,13 +65,17 @@ public class Fluid
*
* Default value is approximately the real-life room temperature of water in degrees Kelvin.
*/
protected int temperature = 295;
protected int temperature = 300;
/**
* Viscosity ("thickness") of the fluid - completely arbitrary; negative values are not
* permissible.
*
* Default value is approximately the real-life density of water in m/s^2 (x10^-3).
*
* Higher viscosity means that a fluid flows more slowly, like molasses.
* Lower viscosity means that a fluid flows more quickly, like helium.
*
*/
protected int viscosity = 1000;
@ -318,19 +322,4 @@ public class Fluid
public int getColor(World world, int x, int y, int z){ return getColor(); }
public IIcon getIcon(World world, int x, int y, int z){ return getIcon(); }
private static Map<String, String> legacyNames = Maps.newHashMap();
static String convertLegacyName(String fluidName)
{
return fluidName != null && legacyNames.containsKey(fluidName) ? legacyNames.get(fluidName) : fluidName;
}
/**
* Register a legacy liquid name with the Fluids system
* @param legacyName The legacy name to recognize
* @param canonicalName The canonical fluid name it will become
*/
public static void registerLegacyName(String legacyName, String canonicalName)
{
legacyNames.put(legacyName.toLowerCase(Locale.ENGLISH), canonicalName);
}
}

View file

@ -34,15 +34,15 @@ public abstract class FluidContainerRegistry
private static class ContainerKey
{
ItemStack container;
FluidStack fluid;
FluidStack stack;
private ContainerKey(ItemStack container)
{
this.container = container;
}
private ContainerKey(ItemStack container, FluidStack fluid)
private ContainerKey(ItemStack container, FluidStack stack)
{
this(container);
this.fluid = fluid;
this.stack = stack;
}
@Override
public int hashCode()
@ -50,8 +50,8 @@ public abstract class FluidContainerRegistry
int code = 1;
code = 31*code + container.getItem().hashCode();
code = 31*code + container.getItemDamage();
if (fluid != null)
code = 31*code + fluid.fluidID;
if (stack != null)
code = 31*code + stack.hashCode();
return code;
}
@Override
@ -61,10 +61,10 @@ public abstract class FluidContainerRegistry
ContainerKey ck = (ContainerKey)o;
if (container.getItem() != ck.container.getItem()) return false;
if (container.getItemDamage() != ck.container.getItemDamage()) return false;
if (fluid == null && ck.fluid != null) return false;
if (fluid != null && ck.fluid == null) return false;
if (fluid == null && ck.fluid == null) return true;
if (fluid.fluidID != ck.fluid.fluidID) return false;
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;
return true;
}
}
@ -361,7 +361,6 @@ public abstract class FluidContainerRegistry
public final ItemStack filledContainer;
public final ItemStack emptyContainer;
public FluidContainerData(FluidStack stack, ItemStack filledContainer, ItemStack emptyContainer)
{
this(stack, filledContainer, emptyContainer, false);

View file

@ -25,8 +25,8 @@ public abstract class FluidRegistry
{
static int maxID = 0;
static HashMap<String, Fluid> fluids = Maps.newHashMap();
static BiMap<String, Integer> fluidIDs = HashBiMap.create();
static BiMap<String, Fluid> fluids = HashBiMap.create();
static BiMap<Fluid, Integer> fluidIDs = HashBiMap.create();
static BiMap<Block, Fluid> fluidBlocks;
public static final Fluid WATER = new Fluid("water") {
@ -57,7 +57,7 @@ public abstract class FluidRegistry
* Called by Forge to prepare the ID map for server -> client sync.
* Modders, DO NOT call this.
*/
public static void initFluidIDs(BiMap<String, Integer> newfluidIDs)
public static void initFluidIDs(BiMap<Fluid, Integer> newfluidIDs)
{
maxID = newfluidIDs.size();
fluidIDs.clear();
@ -78,7 +78,7 @@ public abstract class FluidRegistry
return false;
}
fluids.put(fluid.getName(), fluid);
fluidIDs.put(fluid.getName(), ++maxID);
fluidIDs.put(fluid, ++maxID);
MinecraftForge.EVENT_BUS.post(new FluidRegisterEvent(fluid.getName(), maxID));
return true;
@ -86,12 +86,12 @@ public abstract class FluidRegistry
public static boolean isFluidRegistered(Fluid fluid)
{
return fluidIDs.containsKey(fluid.getName());
return fluids.containsKey(fluid.getName());
}
public static boolean isFluidRegistered(String fluidName)
{
return fluidIDs.containsKey(fluidName);
return fluids.containsKey(fluidName);
}
public static Fluid getFluid(String fluidName)
@ -101,31 +101,36 @@ public abstract class FluidRegistry
public static Fluid getFluid(int fluidID)
{
return fluids.get(getFluidName(fluidID));
return fluidIDs.inverse().get(fluidID);
}
public static String getFluidName(int fluidID)
public static int getFluidID(Fluid fluid)
{
return fluidIDs.inverse().get(fluidID);
}
public static String getFluidName(FluidStack stack)
{
return getFluidName(stack.fluidID);
return fluidIDs.get(fluid);
}
public static int getFluidID(String fluidName)
{
return fluidIDs.get(fluidName);
return fluidIDs.get(getFluid(fluidName));
}
public static String getFluidName(Fluid fluid)
{
return fluids.inverse().get(fluid);
}
public static String getFluidName(FluidStack stack)
{
return getFluidName(stack.fluid);
}
public static FluidStack getFluidStack(String fluidName, int amount)
{
if (!fluidIDs.containsKey(fluidName))
if (!fluids.containsKey(fluidName))
{
return null;
}
return new FluidStack(getFluidID(fluidName), amount);
return new FluidStack(getFluid(fluidName), amount);
}
/**
@ -137,9 +142,9 @@ public abstract class FluidRegistry
}
/**
* Returns a read-only map containing Fluid Names and their associated IDs.
* Returns a read-only map containing Fluid IDs and their associated Fluids.
*/
public static Map<String, Integer> getRegisteredFluidIDs()
public static Map<Fluid, Integer> getRegisteredFluidIDs()
{
return ImmutableMap.copyOf(fluidIDs);
}

View file

@ -18,25 +18,19 @@ import net.minecraft.nbt.NBTTagCompound;
*/
public class FluidStack
{
public int fluidID;
public final Fluid fluid;
public int amount;
public NBTTagCompound tag;
public FluidStack(Fluid fluid, int amount)
{
this.fluidID = fluid.getID();
this.fluid = fluid;
this.amount = amount;
}
public FluidStack(int fluidID, int amount)
public FluidStack(Fluid fluid, int amount, NBTTagCompound nbt)
{
this.fluidID = fluidID;
this.amount = amount;
}
public FluidStack(int fluidID, int amount, NBTTagCompound nbt)
{
this(fluidID, amount);
this(fluid, amount);
if (nbt != null)
{
@ -46,9 +40,23 @@ public class FluidStack
public FluidStack(FluidStack stack, int amount)
{
this(stack.fluidID, amount, stack.tag);
this(stack.fluid, amount, stack.tag);
}
// To be removed in 1.8
@Deprecated
public FluidStack(int fluidID, int amount)
{
this(FluidRegistry.getFluid(fluidID), amount);
}
// To be removed in 1.8
@Deprecated
public FluidStack(int fluidID, int amount, NBTTagCompound nbt)
{
this(FluidRegistry.getFluid(fluidID), amount, nbt);
}
/**
* This provides a safe method for retrieving a FluidStack - if the Fluid is invalid, the stack
* will return as null.
@ -60,32 +68,23 @@ public class FluidStack
return null;
}
String fluidName = nbt.getString("FluidName");
if (Strings.isNullOrEmpty(fluidName))
{
fluidName = nbt.hasKey("LiquidName") ? nbt.getString("LiquidName").toLowerCase(Locale.ENGLISH) : null;
fluidName = Fluid.convertLegacyName(fluidName);
}
if (fluidName ==null || FluidRegistry.getFluid(fluidName) == null)
if (fluidName == null || FluidRegistry.getFluid(fluidName) == null)
{
return null;
}
FluidStack stack = new FluidStack(FluidRegistry.getFluidID(fluidName), nbt.getInteger("Amount"));
FluidStack stack = new FluidStack(FluidRegistry.getFluid(fluidName), nbt.getInteger("Amount"));
if (nbt.hasKey("Tag"))
{
stack.tag = nbt.getCompoundTag("Tag");
}
else if (nbt.hasKey("extra"))
{
stack.tag = nbt.getCompoundTag("extra");
}
return stack;
}
public NBTTagCompound writeToNBT(NBTTagCompound nbt)
{
nbt.setString("FluidName", FluidRegistry.getFluidName(fluidID));
nbt.setString("FluidName", FluidRegistry.getFluidName(fluid));
nbt.setInteger("Amount", amount);
if (tag != null)
@ -97,9 +96,14 @@ public class FluidStack
public final Fluid getFluid()
{
return FluidRegistry.getFluid(fluidID);
return fluid;
}
public final int getFluidID()
{
return FluidRegistry.getFluidID(fluid);
}
public String getLocalizedName()
{
return this.getFluid().getLocalizedName(this);
@ -115,7 +119,7 @@ public class FluidStack
*/
public FluidStack copy()
{
return new FluidStack(fluidID, amount, tag);
return new FluidStack(fluid, amount, tag);
}
/**
@ -127,7 +131,7 @@ public class FluidStack
*/
public boolean isFluidEqual(FluidStack other)
{
return other != null && fluidID == other.fluidID && isFluidStackTagEqual(other);
return other != null && fluid == other.fluid && isFluidStackTagEqual(other);
}
private boolean isFluidStackTagEqual(FluidStack other)
@ -192,7 +196,12 @@ public class FluidStack
@Override
public final int hashCode()
{
return fluidID;
int code = 1;
code = 31*code + fluid.hashCode();
code = 31*code + amount;
if (tag != null)
code = 31*code + tag.hashCode();
return code;
}
/**