mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-26 23:13:15 +00:00
Command work, and Added TileEntityType as a Forge Registry.
This commit is contained in:
1 parent
3f743887e7
commit
b4c23aba8e
22 files changed
+396
-2758
No files matched your search
@@ -1,806 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.fluids;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockLiquid;
|
||||
import net.minecraft.block.BlockStairs;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.properties.PropertyBool;
|
||||
import net.minecraft.block.properties.PropertyInteger;
|
||||
import net.minecraft.block.state.BlockFaceShape;
|
||||
import net.minecraft.block.state.BlockStateContainer;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.client.renderer.ActiveRenderInfo;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.util.BlockRenderLayer;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.AxisAlignedBB;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.common.property.ExtendedBlockState;
|
||||
import net.minecraftforge.common.property.IExtendedBlockState;
|
||||
import net.minecraftforge.common.property.IUnlistedProperty;
|
||||
import net.minecraftforge.common.property.Properties;
|
||||
import net.minecraftforge.common.property.PropertyFloat;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
|
||||
/**
|
||||
* This is a base implementation for Fluid blocks.
|
||||
*
|
||||
* It is highly recommended that you extend this class or one of the Forge-provided child classes.
|
||||
*
|
||||
*/
|
||||
public abstract class BlockFluidBase extends Block implements IFluidBlock
|
||||
{
|
||||
protected final static Map<Block, Boolean> defaultDisplacements = Maps.newHashMap();
|
||||
|
||||
static
|
||||
{
|
||||
defaultDisplacements.put(Blocks.OAK_DOOR, false);
|
||||
defaultDisplacements.put(Blocks.SPRUCE_DOOR, false);
|
||||
defaultDisplacements.put(Blocks.BIRCH_DOOR, false);
|
||||
defaultDisplacements.put(Blocks.JUNGLE_DOOR, false);
|
||||
defaultDisplacements.put(Blocks.ACACIA_DOOR, false);
|
||||
defaultDisplacements.put(Blocks.DARK_OAK_DOOR, false);
|
||||
defaultDisplacements.put(Blocks.TRAPDOOR, false);
|
||||
defaultDisplacements.put(Blocks.IRON_TRAPDOOR, false);
|
||||
defaultDisplacements.put(Blocks.OAK_FENCE, false);
|
||||
defaultDisplacements.put(Blocks.SPRUCE_FENCE, false);
|
||||
defaultDisplacements.put(Blocks.BIRCH_FENCE, false);
|
||||
defaultDisplacements.put(Blocks.JUNGLE_FENCE, false);
|
||||
defaultDisplacements.put(Blocks.DARK_OAK_FENCE, false);
|
||||
defaultDisplacements.put(Blocks.ACACIA_FENCE, false);
|
||||
defaultDisplacements.put(Blocks.NETHER_BRICK_FENCE, false);
|
||||
defaultDisplacements.put(Blocks.OAK_FENCE_GATE, false);
|
||||
defaultDisplacements.put(Blocks.SPRUCE_FENCE_GATE, false);
|
||||
defaultDisplacements.put(Blocks.BIRCH_FENCE_GATE, false);
|
||||
defaultDisplacements.put(Blocks.JUNGLE_FENCE_GATE, false);
|
||||
defaultDisplacements.put(Blocks.DARK_OAK_FENCE_GATE, false);
|
||||
defaultDisplacements.put(Blocks.ACACIA_FENCE_GATE, false);
|
||||
defaultDisplacements.put(Blocks.WOODEN_PRESSURE_PLATE, false);
|
||||
defaultDisplacements.put(Blocks.STONE_PRESSURE_PLATE, false);
|
||||
defaultDisplacements.put(Blocks.LIGHT_WEIGHTED_PRESSURE_PLATE, false);
|
||||
defaultDisplacements.put(Blocks.HEAVY_WEIGHTED_PRESSURE_PLATE, false);
|
||||
defaultDisplacements.put(Blocks.LADDER, false);
|
||||
defaultDisplacements.put(Blocks.IRON_BARS, false);
|
||||
defaultDisplacements.put(Blocks.GLASS_PANE, false);
|
||||
defaultDisplacements.put(Blocks.STAINED_GLASS_PANE, false);
|
||||
defaultDisplacements.put(Blocks.PORTAL, false);
|
||||
defaultDisplacements.put(Blocks.END_PORTAL, false);
|
||||
defaultDisplacements.put(Blocks.COBBLESTONE_WALL, false);
|
||||
defaultDisplacements.put(Blocks.BARRIER, false);
|
||||
defaultDisplacements.put(Blocks.STANDING_BANNER, false);
|
||||
defaultDisplacements.put(Blocks.WALL_BANNER, false);
|
||||
defaultDisplacements.put(Blocks.CAKE, false);
|
||||
|
||||
defaultDisplacements.put(Blocks.IRON_DOOR, false);
|
||||
defaultDisplacements.put(Blocks.STANDING_SIGN, false);
|
||||
defaultDisplacements.put(Blocks.WALL_SIGN, false);
|
||||
defaultDisplacements.put(Blocks.REEDS, false);
|
||||
}
|
||||
protected Map<Block, Boolean> displacements = Maps.newHashMap();
|
||||
|
||||
private static final class UnlistedPropertyBool extends Properties.PropertyAdapter<Boolean>
|
||||
{
|
||||
public UnlistedPropertyBool(String name)
|
||||
{
|
||||
super(PropertyBool.create(name));
|
||||
}
|
||||
}
|
||||
|
||||
public static final PropertyInteger LEVEL = PropertyInteger.create("level", 0, 15);
|
||||
public static final PropertyFloat[] LEVEL_CORNERS = new PropertyFloat[4];
|
||||
public static final PropertyFloat FLOW_DIRECTION = new PropertyFloat("flow_direction", -1000f, 1000f);
|
||||
public static final UnlistedPropertyBool[] SIDE_OVERLAYS = new UnlistedPropertyBool[4];
|
||||
public static final ImmutableList<IUnlistedProperty<?>> FLUID_RENDER_PROPS;
|
||||
|
||||
static
|
||||
{
|
||||
ImmutableList.Builder<IUnlistedProperty<?>> builder = ImmutableList.builder();
|
||||
builder.add(FLOW_DIRECTION);
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
LEVEL_CORNERS[i] = new PropertyFloat("level_corner_" + i, 0f, 1f);
|
||||
builder.add(LEVEL_CORNERS[i]);
|
||||
|
||||
SIDE_OVERLAYS[i] = new UnlistedPropertyBool("side_overlay_" + i);
|
||||
builder.add(SIDE_OVERLAYS[i]);
|
||||
}
|
||||
FLUID_RENDER_PROPS = builder.build();
|
||||
}
|
||||
|
||||
protected int quantaPerBlock = 8;
|
||||
protected float quantaPerBlockFloat = 8F;
|
||||
protected float quantaFraction = 8f / 9f;
|
||||
protected int density = 1;
|
||||
protected int densityDir = -1;
|
||||
protected int temperature = 295;
|
||||
|
||||
protected int tickRate = 20;
|
||||
protected BlockRenderLayer renderLayer = BlockRenderLayer.TRANSLUCENT;
|
||||
protected int maxScaledLight = 0;
|
||||
|
||||
protected final String fluidName;
|
||||
|
||||
/**
|
||||
* This is the fluid used in the constructor. Use this reference to configure things
|
||||
* like icons for your block. It might not be active in the registry, so do
|
||||
* NOT expose it.
|
||||
*/
|
||||
protected final Fluid definedFluid;
|
||||
|
||||
public BlockFluidBase(Fluid fluid, Material material)
|
||||
{
|
||||
super(material);
|
||||
this.setTickRandomly(true);
|
||||
this.disableStats();
|
||||
|
||||
this.fluidName = fluid.getName();
|
||||
this.density = fluid.density;
|
||||
this.temperature = fluid.temperature;
|
||||
this.maxScaledLight = fluid.luminosity;
|
||||
this.tickRate = fluid.viscosity / 200;
|
||||
this.densityDir = fluid.density > 0 ? -1 : 1;
|
||||
fluid.setBlock(this);
|
||||
|
||||
this.definedFluid = fluid;
|
||||
displacements.putAll(defaultDisplacements);
|
||||
this.setDefaultState(blockState.getBaseState().withProperty(LEVEL, getMaxRenderHeightMeta()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
protected BlockStateContainer createBlockState()
|
||||
{
|
||||
return new BlockStateContainer.Builder(this)
|
||||
.add(LEVEL)
|
||||
.add(FLUID_RENDER_PROPS.toArray(new IUnlistedProperty<?>[0]))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetaFromState(@Nonnull IBlockState state)
|
||||
{
|
||||
return state.getValue(LEVEL);
|
||||
}
|
||||
@Override
|
||||
@Deprecated
|
||||
@Nonnull
|
||||
public IBlockState getStateFromMeta(int meta)
|
||||
{
|
||||
return this.getDefaultState().withProperty(LEVEL, meta);
|
||||
}
|
||||
|
||||
public BlockFluidBase setQuantaPerBlock(int quantaPerBlock)
|
||||
{
|
||||
if (quantaPerBlock > 16 || quantaPerBlock < 1) quantaPerBlock = 8;
|
||||
this.quantaPerBlock = quantaPerBlock;
|
||||
this.quantaPerBlockFloat = quantaPerBlock;
|
||||
this.quantaFraction = quantaPerBlock / (quantaPerBlock + 1f);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BlockFluidBase setDensity(int density)
|
||||
{
|
||||
if (density == 0) density = 1;
|
||||
this.density = density;
|
||||
this.densityDir = density > 0 ? -1 : 1;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BlockFluidBase setTemperature(int temperature)
|
||||
{
|
||||
this.temperature = temperature;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BlockFluidBase setTickRate(int tickRate)
|
||||
{
|
||||
if (tickRate <= 0) tickRate = 20;
|
||||
this.tickRate = tickRate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BlockFluidBase setRenderLayer(BlockRenderLayer renderLayer)
|
||||
{
|
||||
this.renderLayer = renderLayer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BlockFluidBase setMaxScaledLight(int maxScaledLight)
|
||||
{
|
||||
this.maxScaledLight = maxScaledLight;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final int getDensity()
|
||||
{
|
||||
return density;
|
||||
}
|
||||
|
||||
public final int getTemperature()
|
||||
{
|
||||
return temperature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the block at (pos) is displaceable. Does not displace the block.
|
||||
*/
|
||||
public boolean canDisplace(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
IBlockState state = world.getBlockState(pos);
|
||||
Block block = state.getBlock();
|
||||
|
||||
if (block.isAir(state, world, pos))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (block == this)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (displacements.containsKey(block))
|
||||
{
|
||||
return displacements.get(block);
|
||||
}
|
||||
|
||||
Material material = state.getMaterial();
|
||||
if (material.blocksMovement() || material == Material.PORTAL || material == Material.STRUCTURE_VOID)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int density = getDensity(world, pos);
|
||||
if (density == Integer.MAX_VALUE)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.density > density;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to displace the block at (pos), return true if it was displaced.
|
||||
*/
|
||||
public boolean displaceIfPossible(World world, BlockPos pos)
|
||||
{
|
||||
boolean canDisplace = canDisplace(world, pos);
|
||||
if (canDisplace)
|
||||
{
|
||||
IBlockState state = world.getBlockState(pos);
|
||||
Block block = state.getBlock();
|
||||
|
||||
if (!block.isAir(state, world, pos) && !isFluid(state))
|
||||
{
|
||||
// Forge: Vanilla has a 'bug' where snowballs don't drop like every other block. So special case because ewww...
|
||||
if (block != Blocks.SNOW_LAYER) block.dropBlockAsItem(world, pos, state, 0);
|
||||
}
|
||||
}
|
||||
return canDisplace;
|
||||
}
|
||||
|
||||
public abstract int getQuantaValue(IBlockAccess world, BlockPos pos);
|
||||
|
||||
@Override
|
||||
public abstract boolean canCollideCheck(@Nonnull IBlockState state, boolean fullHit);
|
||||
|
||||
public abstract int getMaxRenderHeightMeta();
|
||||
|
||||
/* BLOCK FUNCTIONS */
|
||||
@Override
|
||||
public void onBlockAdded(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state)
|
||||
{
|
||||
world.scheduleUpdate(pos, this, tickRate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void neighborChanged(@Nonnull IBlockState state, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull Block neighborBlock, @Nonnull BlockPos neighbourPos)
|
||||
{
|
||||
world.scheduleUpdate(pos, this, tickRate);
|
||||
}
|
||||
|
||||
// Used to prevent updates on chunk generation
|
||||
@Override
|
||||
public boolean requiresUpdates()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPassable(@Nonnull IBlockAccess world, @Nonnull BlockPos pos)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public Item getItemDropped(@Nonnull IBlockState state, @Nonnull Random rand, int fortune)
|
||||
{
|
||||
return Items.AIR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int quantityDropped(@Nonnull Random par1Random)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int tickRate(@Nonnull World world)
|
||||
{
|
||||
return tickRate;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public Vec3d modifyAcceleration(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull Entity entity, @Nonnull Vec3d vec)
|
||||
{
|
||||
return densityDir > 0 ? vec : vec.add(getFlowVector(world, pos));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLightValue(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos)
|
||||
{
|
||||
if (maxScaledLight == 0)
|
||||
{
|
||||
return super.getLightValue(state, world, pos);
|
||||
}
|
||||
return (int) (getQuantaPercentage(world, pos) * maxScaledLight);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpaqueCube(@Nonnull IBlockState state)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFullCube(@Nonnull IBlockState state)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPackedLightmapCoords(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos)
|
||||
{
|
||||
int lightThis = world.getCombinedLight(pos, 0);
|
||||
int lightUp = world.getCombinedLight(pos.up(), 0);
|
||||
int lightThisBase = lightThis & 255;
|
||||
int lightUpBase = lightUp & 255;
|
||||
int lightThisExt = lightThis >> 16 & 255;
|
||||
int lightUpExt = lightUp >> 16 & 255;
|
||||
return (lightThisBase > lightUpBase ? lightThisBase : lightUpBase) |
|
||||
((lightThisExt > lightUpExt ? lightThisExt : lightUpExt) << 16);
|
||||
}
|
||||
|
||||
@Override
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
@Nonnull
|
||||
public BlockRenderLayer getBlockLayer()
|
||||
{
|
||||
return this.renderLayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public BlockFaceShape getBlockFaceShape(@Nonnull IBlockAccess worldIn, @Nonnull IBlockState state, @Nonnull BlockPos pos, @Nonnull EnumFacing face)
|
||||
{
|
||||
return BlockFaceShape.UNDEFINED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldSideBeRendered(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull EnumFacing side)
|
||||
{
|
||||
IBlockState neighbor = world.getBlockState(pos.offset(side));
|
||||
if (neighbor.getMaterial() == state.getMaterial())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (side == (densityDir < 0 ? EnumFacing.UP : EnumFacing.DOWN))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return super.shouldSideBeRendered(state, world, pos, side);
|
||||
}
|
||||
|
||||
private static boolean isFluid(@Nonnull IBlockState blockstate)
|
||||
{
|
||||
return blockstate.getMaterial().isLiquid() || blockstate.getBlock() instanceof IFluidBlock;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public IBlockState getExtendedState(@Nonnull IBlockState oldState, @Nonnull IBlockAccess world, @Nonnull BlockPos pos)
|
||||
{
|
||||
IExtendedBlockState state = (IExtendedBlockState)oldState;
|
||||
state = state.withProperty(FLOW_DIRECTION, (float)getFlowDirection(world, pos));
|
||||
IBlockState[][] upBlockState = new IBlockState[3][3];
|
||||
float[][] height = new float[3][3];
|
||||
float[][] corner = new float[2][2];
|
||||
upBlockState[1][1] = world.getBlockState(pos.down(densityDir));
|
||||
height[1][1] = getFluidHeightForRender(world, pos, upBlockState[1][1]);
|
||||
if (height[1][1] == 1)
|
||||
{
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
for (int j = 0; j < 2; j++)
|
||||
{
|
||||
corner[i][j] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
if (i != 1 || j != 1)
|
||||
{
|
||||
upBlockState[i][j] = world.getBlockState(pos.add(i - 1, 0, j - 1).down(densityDir));
|
||||
height[i][j] = getFluidHeightForRender(world, pos.add(i - 1, 0, j - 1), upBlockState[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
for (int j = 0; j < 2; j++)
|
||||
{
|
||||
corner[i][j] = getFluidHeightAverage(height[i][j], height[i][j + 1], height[i + 1][j], height[i + 1][j + 1]);
|
||||
}
|
||||
}
|
||||
//check for downflow above corners
|
||||
boolean n = isFluid(upBlockState[0][1]);
|
||||
boolean s = isFluid(upBlockState[2][1]);
|
||||
boolean w = isFluid(upBlockState[1][0]);
|
||||
boolean e = isFluid(upBlockState[1][2]);
|
||||
boolean nw = isFluid(upBlockState[0][0]);
|
||||
boolean ne = isFluid(upBlockState[0][2]);
|
||||
boolean sw = isFluid(upBlockState[2][0]);
|
||||
boolean se = isFluid(upBlockState[2][2]);
|
||||
if (nw || n || w)
|
||||
{
|
||||
corner[0][0] = 1;
|
||||
}
|
||||
if (ne || n || e)
|
||||
{
|
||||
corner[0][1] = 1;
|
||||
}
|
||||
if (sw || s || w)
|
||||
{
|
||||
corner[1][0] = 1;
|
||||
}
|
||||
if (se || s || e)
|
||||
{
|
||||
corner[1][1] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
EnumFacing side = EnumFacing.getHorizontal(i);
|
||||
BlockPos offset = pos.offset(side);
|
||||
boolean useOverlay = world.getBlockState(offset).getBlockFaceShape(world, offset, side.getOpposite()) == BlockFaceShape.SOLID;
|
||||
state = state.withProperty(SIDE_OVERLAYS[i], useOverlay);
|
||||
}
|
||||
|
||||
state = state.withProperty(LEVEL_CORNERS[0], corner[0][0]);
|
||||
state = state.withProperty(LEVEL_CORNERS[1], corner[0][1]);
|
||||
state = state.withProperty(LEVEL_CORNERS[2], corner[1][1]);
|
||||
state = state.withProperty(LEVEL_CORNERS[3], corner[1][0]);
|
||||
return state;
|
||||
}
|
||||
|
||||
/* FLUID FUNCTIONS */
|
||||
public static int getDensity(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
IBlockState state = world.getBlockState(pos);
|
||||
Block block = state.getBlock();
|
||||
|
||||
if (block instanceof BlockFluidBase)
|
||||
{
|
||||
return ((BlockFluidBase)block).getDensity();
|
||||
}
|
||||
|
||||
Fluid fluid = getFluid(state);
|
||||
if (fluid != null)
|
||||
{
|
||||
return fluid.getDensity();
|
||||
}
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
public static int getTemperature(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
IBlockState state = world.getBlockState(pos);
|
||||
Block block = state.getBlock();
|
||||
|
||||
if (block instanceof BlockFluidBase)
|
||||
{
|
||||
return ((BlockFluidBase)block).getTemperature();
|
||||
}
|
||||
|
||||
Fluid fluid = getFluid(state);
|
||||
if (fluid != null)
|
||||
{
|
||||
return fluid.getTemperature();
|
||||
}
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Fluid getFluid(IBlockState state)
|
||||
{
|
||||
Block block = state.getBlock();
|
||||
|
||||
if (block instanceof IFluidBlock)
|
||||
{
|
||||
return ((IFluidBlock)block).getFluid();
|
||||
}
|
||||
if (block instanceof BlockLiquid)
|
||||
{
|
||||
if (state.getMaterial() == Material.WATER)
|
||||
{
|
||||
return FluidRegistry.WATER;
|
||||
}
|
||||
if (state.getMaterial() == Material.LAVA)
|
||||
{
|
||||
return FluidRegistry.LAVA;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static double getFlowDirection(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
IBlockState state = world.getBlockState(pos);
|
||||
if (!state.getMaterial().isLiquid())
|
||||
{
|
||||
return -1000.0;
|
||||
}
|
||||
Vec3d vec = ((BlockFluidBase)state.getBlock()).getFlowVector(world, pos);
|
||||
return vec.x == 0.0D && vec.z == 0.0D ? -1000.0D : MathHelper.atan2(vec.z, vec.x) - Math.PI / 2D;
|
||||
}
|
||||
|
||||
public final int getQuantaValueBelow(IBlockAccess world, BlockPos pos, int belowThis)
|
||||
{
|
||||
int quantaRemaining = getQuantaValue(world, pos);
|
||||
if (quantaRemaining >= belowThis)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return quantaRemaining;
|
||||
}
|
||||
|
||||
public final int getQuantaValueAbove(IBlockAccess world, BlockPos pos, int aboveThis)
|
||||
{
|
||||
int quantaRemaining = getQuantaValue(world, pos);
|
||||
if (quantaRemaining <= aboveThis)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return quantaRemaining;
|
||||
}
|
||||
|
||||
public final float getQuantaPercentage(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
int quantaRemaining = getQuantaValue(world, pos);
|
||||
return quantaRemaining / quantaPerBlockFloat;
|
||||
}
|
||||
|
||||
public float getFluidHeightAverage(float... flow)
|
||||
{
|
||||
float total = 0;
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < flow.length; i++)
|
||||
{
|
||||
if (flow[i] >= quantaFraction)
|
||||
{
|
||||
total += flow[i] * 10;
|
||||
count += 10;
|
||||
}
|
||||
|
||||
if (flow[i] >= 0)
|
||||
{
|
||||
total += flow[i];
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return total / count;
|
||||
}
|
||||
|
||||
public float getFluidHeightForRender(IBlockAccess world, BlockPos pos, @Nonnull IBlockState up)
|
||||
{
|
||||
IBlockState here = world.getBlockState(pos);
|
||||
if (here.getBlock() == this)
|
||||
{
|
||||
if (isFluid(up))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (getMetaFromState(here) == getMaxRenderHeightMeta())
|
||||
{
|
||||
return quantaFraction;
|
||||
}
|
||||
}
|
||||
if (here.getBlock() instanceof BlockLiquid)
|
||||
{
|
||||
return Math.min(1 - BlockLiquid.getLiquidHeightPercent(here.getValue(BlockLiquid.LEVEL)), quantaFraction);
|
||||
}
|
||||
return !here.getMaterial().isSolid() && up.getBlock() == this ? 1 : this.getQuantaPercentage(world, pos) * quantaFraction;
|
||||
}
|
||||
|
||||
public Vec3d getFlowVector(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
Vec3d vec = new Vec3d(0.0D, 0.0D, 0.0D);
|
||||
int decay = getFlowDecay(world, pos);
|
||||
|
||||
for (EnumFacing side : EnumFacing.Plane.HORIZONTAL)
|
||||
{
|
||||
BlockPos offset = pos.offset(side);
|
||||
int otherDecay = getFlowDecay(world, offset);
|
||||
if (otherDecay >= quantaPerBlock)
|
||||
{
|
||||
if (!world.getBlockState(offset).getMaterial().blocksMovement())
|
||||
{
|
||||
otherDecay = getFlowDecay(world, offset.up(densityDir));
|
||||
if (otherDecay < quantaPerBlock)
|
||||
{
|
||||
int power = otherDecay - (decay - quantaPerBlock);
|
||||
vec = vec.addVector(side.getFrontOffsetX() * power, 0, side.getFrontOffsetZ() * power);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int power = otherDecay - decay;
|
||||
vec = vec.addVector(side.getFrontOffsetX() * power, 0, side.getFrontOffsetZ() * power);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasVerticalFlow(world, pos))
|
||||
{
|
||||
for (EnumFacing side : EnumFacing.Plane.HORIZONTAL)
|
||||
{
|
||||
BlockPos offset = pos.offset(side);
|
||||
if (causesDownwardCurrent(world, offset, side) || causesDownwardCurrent(world, offset.down(densityDir), side))
|
||||
{
|
||||
vec = vec.normalize().addVector(0.0, 6.0 * densityDir, 0.0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return vec.normalize();
|
||||
}
|
||||
|
||||
private int getFlowDecay(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
int quantaValue = getQuantaValue(world, pos);
|
||||
return quantaValue > 0 && hasVerticalFlow(world, pos) ? 0 : quantaPerBlock - quantaValue;
|
||||
}
|
||||
|
||||
private boolean hasVerticalFlow(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
return world.getBlockState(pos.down(densityDir)).getBlock() == this;
|
||||
}
|
||||
|
||||
protected boolean causesDownwardCurrent(IBlockAccess world, BlockPos pos, EnumFacing face)
|
||||
{
|
||||
IBlockState state = world.getBlockState(pos);
|
||||
Block block = state.getBlock();
|
||||
|
||||
if (block == this) return false;
|
||||
if (face == (densityDir < 0 ? EnumFacing.UP : EnumFacing.DOWN)) return true;
|
||||
if (state.getMaterial() == Material.ICE) return false;
|
||||
|
||||
boolean flag = isExceptBlockForAttachWithPiston(block) || block instanceof BlockStairs;
|
||||
return !flag && state.getBlockFaceShape(world, pos, face) == BlockFaceShape.SOLID;
|
||||
}
|
||||
|
||||
/* IFluidBlock */
|
||||
@Override
|
||||
public Fluid getFluid()
|
||||
{
|
||||
return FluidRegistry.getFluid(fluidName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFilledPercentage(World world, BlockPos pos)
|
||||
{
|
||||
return getFilledPercentage((IBlockAccess) world, pos);
|
||||
}
|
||||
|
||||
public float getFilledPercentage(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
int quantaRemaining = getQuantaValue(world, pos);
|
||||
float remaining = (quantaRemaining + 1f) / (quantaPerBlockFloat + 1f);
|
||||
return remaining * (density > 0 ? 1 : -1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AxisAlignedBB getCollisionBoundingBox(@Nonnull IBlockState blockState, @Nonnull IBlockAccess worldIn, @Nonnull BlockPos pos)
|
||||
{
|
||||
return NULL_AABB;
|
||||
}
|
||||
|
||||
@Override
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public Vec3d getFogColor(World world, BlockPos pos, IBlockState state, Entity entity, Vec3d originalColor, float partialTicks)
|
||||
{
|
||||
if (!isWithinFluid(world, pos, ActiveRenderInfo.projectViewFromEntity(entity, partialTicks)))
|
||||
{
|
||||
BlockPos otherPos = pos.down(densityDir);
|
||||
IBlockState otherState = world.getBlockState(otherPos);
|
||||
return otherState.getBlock().getFogColor(world, otherPos, otherState, entity, originalColor, partialTicks);
|
||||
}
|
||||
|
||||
if (getFluid() != null)
|
||||
{
|
||||
int color = getFluid().getColor();
|
||||
float red = (color >> 16 & 0xFF) / 255.0F;
|
||||
float green = (color >> 8 & 0xFF) / 255.0F;
|
||||
float blue = (color & 0xFF) / 255.0F;
|
||||
return new Vec3d(red, green, blue);
|
||||
}
|
||||
|
||||
return super.getFogColor(world, pos, state, entity, originalColor, partialTicks);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBlockState getStateAtViewpoint(IBlockState state, IBlockAccess world, BlockPos pos, Vec3d viewpoint)
|
||||
{
|
||||
if (!isWithinFluid(world, pos, viewpoint))
|
||||
{
|
||||
return world.getBlockState(pos.down(densityDir));
|
||||
}
|
||||
|
||||
return super.getStateAtViewpoint(state, world, pos, viewpoint);
|
||||
}
|
||||
|
||||
private boolean isWithinFluid(IBlockAccess world, BlockPos pos, Vec3d vec)
|
||||
{
|
||||
float filled = getFilledPercentage(world, pos);
|
||||
return filled < 0 ? vec.y > pos.getY() + filled + 1
|
||||
: vec.y < pos.getY() + filled;
|
||||
}
|
||||
}
|
||||
@@ -1,338 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.fluids;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.event.ForgeEventFactory;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* This is a fluid block implementation which emulates vanilla Minecraft fluid behavior.
|
||||
*
|
||||
* It is highly recommended that you use/extend this class for "classic" fluid blocks.
|
||||
*
|
||||
*/
|
||||
public class BlockFluidClassic extends BlockFluidBase
|
||||
{
|
||||
protected static final List<EnumFacing> SIDES = Collections.unmodifiableList(Arrays.asList(
|
||||
EnumFacing.WEST, EnumFacing.EAST, EnumFacing.NORTH, EnumFacing.SOUTH));
|
||||
|
||||
protected boolean[] isOptimalFlowDirection = new boolean[4];
|
||||
protected int[] flowCost = new int[4];
|
||||
|
||||
protected boolean canCreateSources = false;
|
||||
|
||||
protected FluidStack stack;
|
||||
|
||||
public BlockFluidClassic(Fluid fluid, Material material)
|
||||
{
|
||||
super(fluid, material);
|
||||
stack = new FluidStack(fluid, Fluid.BUCKET_VOLUME);
|
||||
}
|
||||
|
||||
public BlockFluidClassic setFluidStack(FluidStack stack)
|
||||
{
|
||||
this.stack = stack;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BlockFluidClassic setFluidStackAmount(int amount)
|
||||
{
|
||||
this.stack.amount = amount;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getQuantaValue(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
IBlockState state = world.getBlockState(pos);
|
||||
if (state.getBlock().isAir(state, world, pos))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (state.getBlock() != this)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return quantaPerBlock - state.getValue(LEVEL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCollideCheck(@Nonnull IBlockState state, boolean fullHit)
|
||||
{
|
||||
return fullHit && state.getValue(LEVEL) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxRenderHeightMeta()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTick(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull Random rand)
|
||||
{
|
||||
int quantaRemaining = quantaPerBlock - state.getValue(LEVEL);
|
||||
int expQuanta = -101;
|
||||
|
||||
// check adjacent block levels if non-source
|
||||
if (quantaRemaining < quantaPerBlock)
|
||||
{
|
||||
int adjacentSourceBlocks = 0;
|
||||
|
||||
if (ForgeEventFactory.canCreateFluidSource(world, pos, state, canCreateSources))
|
||||
{
|
||||
for (EnumFacing side : EnumFacing.Plane.HORIZONTAL)
|
||||
{
|
||||
if (isSourceBlock(world, pos.offset(side))) adjacentSourceBlocks++;
|
||||
}
|
||||
}
|
||||
|
||||
// new source block
|
||||
if (adjacentSourceBlocks >= 2 && (world.getBlockState(pos.up(densityDir)).getMaterial().isSolid() || isSourceBlock(world, pos.up(densityDir))))
|
||||
{
|
||||
expQuanta = quantaPerBlock;
|
||||
}
|
||||
// unobstructed flow from 'above'
|
||||
else if (world.getBlockState(pos.down(densityDir)).getBlock() == this
|
||||
|| hasDownhillFlow(world, pos, EnumFacing.EAST)
|
||||
|| hasDownhillFlow(world, pos, EnumFacing.WEST)
|
||||
|| hasDownhillFlow(world, pos, EnumFacing.NORTH)
|
||||
|| hasDownhillFlow(world, pos, EnumFacing.SOUTH))
|
||||
{
|
||||
expQuanta = quantaPerBlock - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int maxQuanta = -100;
|
||||
for (EnumFacing side : EnumFacing.Plane.HORIZONTAL)
|
||||
{
|
||||
maxQuanta = getLargerQuanta(world, pos.offset(side), maxQuanta);
|
||||
}
|
||||
expQuanta = maxQuanta - 1;
|
||||
}
|
||||
|
||||
// decay calculation
|
||||
if (expQuanta != quantaRemaining)
|
||||
{
|
||||
quantaRemaining = expQuanta;
|
||||
|
||||
if (expQuanta <= 0)
|
||||
{
|
||||
world.setBlockToAir(pos);
|
||||
}
|
||||
else
|
||||
{
|
||||
world.setBlockState(pos, state.withProperty(LEVEL, quantaPerBlock - expQuanta), 2);
|
||||
world.scheduleUpdate(pos, this, tickRate);
|
||||
world.notifyNeighborsOfStateChange(pos, this, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Flow vertically if possible
|
||||
if (canDisplace(world, pos.up(densityDir)))
|
||||
{
|
||||
flowIntoBlock(world, pos.up(densityDir), 1);
|
||||
return;
|
||||
}
|
||||
|
||||
// Flow outward if possible
|
||||
int flowMeta = quantaPerBlock - quantaRemaining + 1;
|
||||
if (flowMeta >= quantaPerBlock)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (isSourceBlock(world, pos) || !isFlowingVertically(world, pos))
|
||||
{
|
||||
if (world.getBlockState(pos.down(densityDir)).getBlock() == this)
|
||||
{
|
||||
flowMeta = 1;
|
||||
}
|
||||
boolean flowTo[] = getOptimalFlowDirections(world, pos);
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
if (flowTo[i]) flowIntoBlock(world, pos.offset(SIDES.get(i)), flowMeta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected final boolean hasDownhillFlow(IBlockAccess world, BlockPos pos, EnumFacing direction)
|
||||
{
|
||||
return world.getBlockState(pos.offset(direction).down(densityDir)).getBlock() == this
|
||||
&& (canFlowInto(world, pos.offset(direction))
|
||||
|| canFlowInto(world, pos.down(densityDir)));
|
||||
}
|
||||
|
||||
public boolean isFlowingVertically(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
return world.getBlockState(pos.up(densityDir)).getBlock() == this ||
|
||||
(world.getBlockState(pos).getBlock() == this && canFlowInto(world, pos.up(densityDir)));
|
||||
}
|
||||
|
||||
public boolean isSourceBlock(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
IBlockState state = world.getBlockState(pos);
|
||||
return state.getBlock() == this && state.getValue(LEVEL) == 0;
|
||||
}
|
||||
|
||||
protected boolean[] getOptimalFlowDirections(World world, BlockPos pos)
|
||||
{
|
||||
for (int side = 0; side < 4; side++)
|
||||
{
|
||||
flowCost[side] = 1000;
|
||||
|
||||
BlockPos pos2 = pos.offset(SIDES.get(side));
|
||||
|
||||
if (!canFlowInto(world, pos2) || isSourceBlock(world, pos2))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (canFlowInto(world, pos2.up(densityDir)))
|
||||
{
|
||||
flowCost[side] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
flowCost[side] = calculateFlowCost(world, pos2, 1, side);
|
||||
}
|
||||
}
|
||||
|
||||
int min = Ints.min(flowCost);
|
||||
for (int side = 0; side < 4; side++)
|
||||
{
|
||||
isOptimalFlowDirection[side] = flowCost[side] == min;
|
||||
}
|
||||
return isOptimalFlowDirection;
|
||||
}
|
||||
|
||||
protected int calculateFlowCost(World world, BlockPos pos, int recurseDepth, int side)
|
||||
{
|
||||
int cost = 1000;
|
||||
for (int adjSide = 0; adjSide < 4; adjSide++)
|
||||
{
|
||||
if (SIDES.get(adjSide) == SIDES.get(side).getOpposite())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
BlockPos pos2 = pos.offset(SIDES.get(adjSide));
|
||||
|
||||
if (!canFlowInto(world, pos2) || isSourceBlock(world, pos2))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (canFlowInto(world, pos2.up(densityDir)))
|
||||
{
|
||||
return recurseDepth;
|
||||
}
|
||||
|
||||
if (recurseDepth >= quantaPerBlock / 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
cost = Math.min(cost, calculateFlowCost(world, pos2, recurseDepth + 1, adjSide));
|
||||
}
|
||||
return cost;
|
||||
}
|
||||
|
||||
protected void flowIntoBlock(World world, BlockPos pos, int meta)
|
||||
{
|
||||
if (meta < 0) return;
|
||||
if (displaceIfPossible(world, pos))
|
||||
{
|
||||
world.setBlockState(pos, this.getDefaultState().withProperty(LEVEL, meta), 3);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean canFlowInto(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
return world.getBlockState(pos).getBlock() == this || canDisplace(world, pos);
|
||||
}
|
||||
|
||||
protected int getLargerQuanta(IBlockAccess world, BlockPos pos, int compare)
|
||||
{
|
||||
int quantaRemaining = getQuantaValue(world, pos);
|
||||
if (quantaRemaining <= 0)
|
||||
{
|
||||
return compare;
|
||||
}
|
||||
return quantaRemaining >= compare ? quantaRemaining : compare;
|
||||
}
|
||||
|
||||
/* IFluidBlock */
|
||||
@Override
|
||||
public int place(World world, BlockPos pos, @Nonnull FluidStack fluidStack, boolean doPlace)
|
||||
{
|
||||
if (fluidStack.amount < Fluid.BUCKET_VOLUME)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (doPlace)
|
||||
{
|
||||
FluidUtil.destroyBlockOnFluidPlacement(world, pos);
|
||||
world.setBlockState(pos, this.getDefaultState(), 11);
|
||||
}
|
||||
return Fluid.BUCKET_VOLUME;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public FluidStack drain(World world, BlockPos pos, boolean doDrain)
|
||||
{
|
||||
if (!isSourceBlock(world, pos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (doDrain)
|
||||
{
|
||||
world.setBlockToAir(pos);
|
||||
}
|
||||
|
||||
return stack.copy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDrain(World world, BlockPos pos)
|
||||
{
|
||||
return isSourceBlock(world, pos);
|
||||
}
|
||||
}
|
||||
@@ -1,300 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.fluids;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* This is a cellular-automata based finite fluid block implementation.
|
||||
*
|
||||
* It is highly recommended that you use/extend this class for finite fluid blocks.
|
||||
*
|
||||
*/
|
||||
public class BlockFluidFinite extends BlockFluidBase
|
||||
{
|
||||
public BlockFluidFinite(Fluid fluid, Material material)
|
||||
{
|
||||
super(fluid, material);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getQuantaValue(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
IBlockState state = world.getBlockState(pos);
|
||||
if (state.getBlock().isAir(state, world, pos))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (state.getBlock() != this)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return state.getValue(LEVEL) + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCollideCheck(@Nonnull IBlockState state, boolean fullHit)
|
||||
{
|
||||
return fullHit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxRenderHeightMeta()
|
||||
{
|
||||
return quantaPerBlock - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTick(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull Random rand)
|
||||
{
|
||||
boolean changed = false;
|
||||
int quantaRemaining = state.getValue(LEVEL) + 1;
|
||||
|
||||
// Flow vertically if possible
|
||||
int prevRemaining = quantaRemaining;
|
||||
quantaRemaining = tryToFlowVerticallyInto(world, pos, quantaRemaining);
|
||||
|
||||
if (quantaRemaining < 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (quantaRemaining != prevRemaining)
|
||||
{
|
||||
changed = true;
|
||||
if (quantaRemaining == 1)
|
||||
{
|
||||
world.setBlockState(pos, state.withProperty(LEVEL, quantaRemaining - 1), 2);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (quantaRemaining == 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Flow out if possible
|
||||
int lowerThan = quantaRemaining - 1;
|
||||
int total = quantaRemaining;
|
||||
int count = 1;
|
||||
|
||||
for (EnumFacing side : EnumFacing.Plane.HORIZONTAL)
|
||||
{
|
||||
BlockPos off = pos.offset(side);
|
||||
if (displaceIfPossible(world, off))
|
||||
world.setBlockToAir(off);
|
||||
|
||||
int quanta = getQuantaValueBelow(world, off, lowerThan);
|
||||
if (quanta >= 0)
|
||||
{
|
||||
count++;
|
||||
total += quanta;
|
||||
}
|
||||
}
|
||||
|
||||
if (count == 1)
|
||||
{
|
||||
if (changed)
|
||||
{
|
||||
world.setBlockState(pos, state.withProperty(LEVEL, quantaRemaining - 1), 2);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int each = total / count;
|
||||
int rem = total % count;
|
||||
|
||||
for (EnumFacing side : EnumFacing.Plane.HORIZONTAL)
|
||||
{
|
||||
BlockPos off = pos.offset(side);
|
||||
int quanta = getQuantaValueBelow(world, off, lowerThan);
|
||||
if (quanta >= 0)
|
||||
{
|
||||
int newQuanta = each;
|
||||
if (rem == count || rem > 1 && rand.nextInt(count - rem) != 0)
|
||||
{
|
||||
++newQuanta;
|
||||
--rem;
|
||||
}
|
||||
|
||||
if (newQuanta != quanta)
|
||||
{
|
||||
if (newQuanta == 0)
|
||||
{
|
||||
world.setBlockToAir(off);
|
||||
}
|
||||
else
|
||||
{
|
||||
world.setBlockState(off, getDefaultState().withProperty(LEVEL, newQuanta - 1), 2);
|
||||
}
|
||||
world.scheduleUpdate(off, this, tickRate);
|
||||
}
|
||||
--count;
|
||||
}
|
||||
}
|
||||
|
||||
if (rem > 0)
|
||||
{
|
||||
++each;
|
||||
}
|
||||
world.setBlockState(pos, state.withProperty(LEVEL, each - 1), 2);
|
||||
}
|
||||
|
||||
public int tryToFlowVerticallyInto(World world, BlockPos pos, int amtToInput)
|
||||
{
|
||||
IBlockState myState = world.getBlockState(pos);
|
||||
BlockPos other = pos.add(0, densityDir, 0);
|
||||
if (other.getY() < 0 || other.getY() >= world.getHeight())
|
||||
{
|
||||
world.setBlockToAir(pos);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int amt = getQuantaValueBelow(world, other, quantaPerBlock);
|
||||
if (amt >= 0)
|
||||
{
|
||||
amt += amtToInput;
|
||||
if (amt > quantaPerBlock)
|
||||
{
|
||||
world.setBlockState(other, myState.withProperty(LEVEL, quantaPerBlock - 1), 3);
|
||||
world.scheduleUpdate(other, this, tickRate);
|
||||
return amt - quantaPerBlock;
|
||||
}
|
||||
else if (amt > 0)
|
||||
{
|
||||
world.setBlockState(other, myState.withProperty(LEVEL, amt - 1), 3);
|
||||
world.scheduleUpdate(other, this, tickRate);
|
||||
world.setBlockToAir(pos);
|
||||
return 0;
|
||||
}
|
||||
return amtToInput;
|
||||
}
|
||||
else
|
||||
{
|
||||
int density_other = getDensity(world, other);
|
||||
if (density_other == Integer.MAX_VALUE)
|
||||
{
|
||||
if (displaceIfPossible(world, other))
|
||||
{
|
||||
world.setBlockState(other, myState.withProperty(LEVEL, amtToInput - 1), 3);
|
||||
world.scheduleUpdate(other, this, tickRate);
|
||||
world.setBlockToAir(pos);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return amtToInput;
|
||||
}
|
||||
}
|
||||
|
||||
if (densityDir < 0)
|
||||
{
|
||||
if (density_other < density) // then swap
|
||||
{
|
||||
IBlockState state = world.getBlockState(other);
|
||||
world.setBlockState(other, myState.withProperty(LEVEL, amtToInput - 1), 3);
|
||||
world.setBlockState(pos, state, 3);
|
||||
world.scheduleUpdate(other, this, tickRate);
|
||||
world.scheduleUpdate(pos, state.getBlock(), state.getBlock().tickRate(world));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (density_other > density)
|
||||
{
|
||||
IBlockState state = world.getBlockState(other);
|
||||
world.setBlockState(other, myState.withProperty(LEVEL, amtToInput - 1), 3);
|
||||
world.setBlockState(pos, state, 3);
|
||||
world.scheduleUpdate(other, this, tickRate);
|
||||
world.scheduleUpdate(pos, state.getBlock(), state.getBlock().tickRate(world));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return amtToInput;
|
||||
}
|
||||
}
|
||||
|
||||
/* IFluidBlock */
|
||||
@Override
|
||||
public int place(World world, BlockPos pos, @Nonnull FluidStack fluidStack, boolean doPlace)
|
||||
{
|
||||
IBlockState existing = world.getBlockState(pos);
|
||||
float quantaAmount = Fluid.BUCKET_VOLUME / quantaPerBlockFloat;
|
||||
// If the stack contains more available fluid than the full source block,
|
||||
// set a source block
|
||||
int closest = Fluid.BUCKET_VOLUME;
|
||||
int quanta = quantaPerBlock;
|
||||
if (fluidStack.amount < closest)
|
||||
{
|
||||
// Figure out maximum level to match stack amount
|
||||
closest = MathHelper.floor(quantaAmount * MathHelper.floor(fluidStack.amount / quantaAmount));
|
||||
quanta = MathHelper.floor(closest / quantaAmount);
|
||||
}
|
||||
if (existing.getBlock() == this)
|
||||
{
|
||||
int existingQuanta = existing.getValue(LEVEL) + 1;
|
||||
int missingQuanta = quantaPerBlock - existingQuanta;
|
||||
closest = Math.min(closest, MathHelper.floor(missingQuanta * quantaAmount));
|
||||
quanta = Math.min(quanta + existingQuanta, quantaPerBlock);
|
||||
}
|
||||
|
||||
// If too little (or too much, technically impossible) fluid is to be placed, abort
|
||||
if (quanta < 1 || quanta > 16)
|
||||
return 0;
|
||||
|
||||
if (doPlace)
|
||||
{
|
||||
FluidUtil.destroyBlockOnFluidPlacement(world, pos);
|
||||
world.setBlockState(pos, getDefaultState().withProperty(LEVEL, quanta - 1), 11);
|
||||
}
|
||||
|
||||
return closest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(World world, BlockPos pos, boolean doDrain)
|
||||
{
|
||||
final FluidStack fluidStack = new FluidStack(getFluid(), MathHelper.floor(getQuantaPercentage(world, pos) * Fluid.BUCKET_VOLUME));
|
||||
|
||||
if (doDrain)
|
||||
{
|
||||
world.setBlockToAir(pos);
|
||||
}
|
||||
|
||||
return fluidStack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDrain(World world, BlockPos pos)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -30,11 +30,9 @@ import net.minecraft.block.Block;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.init.SoundEvents;
|
||||
import net.minecraft.util.EnumParticleTypes;
|
||||
import net.minecraft.util.SoundCategory;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.text.translation.I18n;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.item.EnumRarity;
|
||||
@@ -131,7 +129,7 @@ public class Fluid
|
||||
* The default value of null should remain for any Fluid without a Block implementation.
|
||||
*/
|
||||
protected Block block = null;
|
||||
|
||||
|
||||
/**
|
||||
* Color used by universal bucket and the ModelFluid baked model.
|
||||
* Note that this int includes the alpha so converting this to RGB with alpha would be
|
||||
@@ -245,13 +243,13 @@ public class Fluid
|
||||
this.emptySound = emptySound;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Fluid setColor(Color color)
|
||||
{
|
||||
this.color = color.getRGB();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Fluid setColor(int color)
|
||||
{
|
||||
this.color = color;
|
||||
@@ -275,15 +273,10 @@ public class Fluid
|
||||
|
||||
public final boolean isLighterThanAir()
|
||||
{
|
||||
int density = this.density;
|
||||
if (block instanceof BlockFluidBase)
|
||||
{
|
||||
density = ((BlockFluidBase) block).getDensity();
|
||||
}
|
||||
return density <= 0;
|
||||
return this.density <= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Determines if this fluid should vaporize in dimensions where water vaporizes when placed.
|
||||
* To preserve the intentions of vanilla, fluids that can turn lava into obsidian should vaporize.
|
||||
* This prevents players from making the nether safe with a single bucket.
|
||||
@@ -299,7 +292,7 @@ public class Fluid
|
||||
return block.getDefaultState().getMaterial() == Material.WATER;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Called instead of placing the fluid block if {@link WorldProvider#doesWaterVaporize()} and {@link #doesVaporize(FluidStack)} are true.
|
||||
* Override this to make your explosive liquid blow up instead of the default smoke, etc.
|
||||
* Based on {@link net.minecraft.item.ItemBucket#tryPlaceContainedLiquid(EntityPlayer, World, BlockPos)}
|
||||
|
||||
@@ -19,39 +19,11 @@
|
||||
|
||||
package net.minecraftforge.fluids;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraftforge.fml.ModThreadContext;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.nbt.NBTTagString;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
|
||||
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.ImmutableSet;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import net.minecraftforge.fml.ModContainer;
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
import net.minecraftforge.registries.IRegistryDelegate;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.Marker;
|
||||
import org.apache.logging.log4j.MarkerManager;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Handles Fluid registrations. Fluids MUST be registered in order to function.
|
||||
*/
|
||||
@@ -59,196 +31,8 @@ public abstract class FluidRegistry
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static final Marker FLUIDS = MarkerManager.getMarker("FLUIDS");
|
||||
static int maxID = 0;
|
||||
|
||||
static BiMap<String, Fluid> fluids = HashBiMap.create();
|
||||
static BiMap<Fluid, Integer> fluidIDs = HashBiMap.create();
|
||||
static BiMap<Integer, String> fluidNames = HashBiMap.create(); //Caching this just makes some other calls faster
|
||||
static BiMap<Block, Fluid> fluidBlocks;
|
||||
|
||||
// the globally unique fluid map - only used to associate non-defaults during world/server loading
|
||||
static BiMap<String,Fluid> masterFluidReference = HashBiMap.create();
|
||||
static BiMap<String,String> defaultFluidName = HashBiMap.create();
|
||||
static Map<Fluid,FluidDelegate> delegates = Maps.newHashMap();
|
||||
|
||||
static boolean universalBucketEnabled = false;
|
||||
static Set<Fluid> bucketFluids = Sets.newHashSet();
|
||||
|
||||
public static final Fluid WATER = new Fluid("water", new ResourceLocation("blocks/water_still"), new ResourceLocation("blocks/water_flow"), new ResourceLocation("blocks/water_overlay")) {
|
||||
@Override
|
||||
public String getLocalizedName(FluidStack fs) {
|
||||
return I18n.translateToLocal("tile.water.name");
|
||||
}
|
||||
}.setBlock(Blocks.WATER).setUnlocalizedName(Blocks.WATER.getUnlocalizedName());
|
||||
|
||||
public static final Fluid LAVA = new Fluid("lava", new ResourceLocation("blocks/lava_still"), new ResourceLocation("blocks/lava_flow")) {
|
||||
@Override
|
||||
public String getLocalizedName(FluidStack fs) {
|
||||
return I18n.translateToLocal("tile.lava.name");
|
||||
}
|
||||
}.setBlock(Blocks.LAVA).setLuminosity(15).setDensity(3000).setViscosity(6000).setTemperature(1300).setUnlocalizedName(Blocks.LAVA.getUnlocalizedName());
|
||||
|
||||
static
|
||||
{
|
||||
registerFluid(WATER);
|
||||
registerFluid(LAVA);
|
||||
}
|
||||
|
||||
private FluidRegistry(){}
|
||||
|
||||
/**
|
||||
* Called by Forge to prepare the ID map for server -> client sync.
|
||||
* Modders, DO NOT call this.
|
||||
*/
|
||||
public static void initFluidIDs(BiMap<Fluid, Integer> newfluidIDs, Set<String> defaultNames)
|
||||
{
|
||||
maxID = newfluidIDs.size();
|
||||
loadFluidDefaults(newfluidIDs, defaultNames);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by forge to load default fluid IDs from the world or from server -> client for syncing
|
||||
* DO NOT call this and expect useful behaviour.
|
||||
* @param localFluidIDs
|
||||
* @param defaultNames
|
||||
*/
|
||||
private static void loadFluidDefaults(BiMap<Fluid, Integer> localFluidIDs, Set<String> defaultNames)
|
||||
{
|
||||
// If there's an empty set of default names, use the defaults as defined locally
|
||||
if (defaultNames.isEmpty()) {
|
||||
defaultNames.addAll(defaultFluidName.values());
|
||||
}
|
||||
BiMap<String, Fluid> localFluids = HashBiMap.create(fluids);
|
||||
for (String defaultName : defaultNames)
|
||||
{
|
||||
Fluid fluid = masterFluidReference.get(defaultName);
|
||||
if (fluid == null) {
|
||||
String derivedName = defaultName.split(":",2)[1];
|
||||
String localDefault = defaultFluidName.get(derivedName);
|
||||
if (localDefault == null) {
|
||||
LOGGER.error(FLUIDS, "The fluid {} (specified as {}) is missing from this instance - it will be removed",derivedName,defaultName);
|
||||
continue;
|
||||
}
|
||||
fluid = masterFluidReference.get(localDefault);
|
||||
LOGGER.error(FLUIDS, "The fluid {} specified as default is not present - it will be reverted to default {}",defaultName,localDefault);
|
||||
}
|
||||
LOGGER.debug(FLUIDS, "The fluid {} has been selected as the default fluid for {}",defaultName,fluid.getName());
|
||||
Fluid oldFluid = localFluids.put(fluid.getName(), fluid);
|
||||
Integer id = localFluidIDs.remove(oldFluid);
|
||||
localFluidIDs.put(fluid, id);
|
||||
}
|
||||
BiMap<Integer, String> localFluidNames = HashBiMap.create();
|
||||
for (Entry<Fluid, Integer> e : localFluidIDs.entrySet()) {
|
||||
localFluidNames.put(e.getValue(), e.getKey().getName());
|
||||
}
|
||||
fluidIDs = localFluidIDs;
|
||||
fluids = localFluids;
|
||||
fluidNames = localFluidNames;
|
||||
fluidBlocks = null;
|
||||
for (FluidDelegate fd : delegates.values())
|
||||
{
|
||||
fd.rebind();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new Fluid. If a fluid with the same name already exists, registration the alternative fluid is tracked
|
||||
* in case it is the default in another place
|
||||
*
|
||||
* @param fluid
|
||||
* The fluid to register.
|
||||
* @return True if the fluid was registered as the current default fluid, false if it was only registered as an alternative
|
||||
*/
|
||||
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;
|
||||
}
|
||||
fluids.put(fluid.getName(), fluid);
|
||||
maxID++;
|
||||
fluidIDs.put(fluid, maxID);
|
||||
fluidNames.put(maxID, fluid.getName());
|
||||
defaultFluidName.put(fluid.getName(), uniqueName(fluid));
|
||||
|
||||
MinecraftForge.EVENT_BUS.post(new FluidRegisterEvent(fluid.getName(), maxID));
|
||||
return true;
|
||||
}
|
||||
|
||||
private static String uniqueName(Fluid fluid)
|
||||
{
|
||||
return ModThreadContext.get().getActiveContainer().getPrefix() +":"+fluid.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the supplied fluid the current default fluid for it's name
|
||||
* @param fluid the fluid we're testing
|
||||
* @return if the fluid is default
|
||||
*/
|
||||
public static boolean isFluidDefault(Fluid fluid)
|
||||
{
|
||||
return fluids.containsValue(fluid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the supplied fluid have an entry for it's name (whether or not the fluid itself is default)
|
||||
* @param fluid the fluid we're testing
|
||||
* @return if the fluid's name has a registration entry
|
||||
*/
|
||||
public static boolean isFluidRegistered(Fluid fluid)
|
||||
{
|
||||
return fluid != null && fluids.containsKey(fluid.getName());
|
||||
}
|
||||
|
||||
public static boolean isFluidRegistered(String fluidName)
|
||||
{
|
||||
return fluids.containsKey(fluidName);
|
||||
}
|
||||
|
||||
public static Fluid getFluid(String fluidName)
|
||||
{
|
||||
return fluids.get(fluidName);
|
||||
}
|
||||
|
||||
public static String getFluidName(Fluid fluid)
|
||||
{
|
||||
return fluids.inverse().get(fluid);
|
||||
}
|
||||
|
||||
public static String getFluidName(FluidStack stack)
|
||||
{
|
||||
return getFluidName(stack.getFluid());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static FluidStack getFluidStack(String fluidName, int amount)
|
||||
{
|
||||
if (!fluids.containsKey(fluidName))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new FluidStack(getFluid(fluidName), amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a read-only map containing Fluid Names and their associated Fluids.
|
||||
*/
|
||||
public static Map<String, Fluid> getRegisteredFluids()
|
||||
{
|
||||
return ImmutableMap.copyOf(fluids);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a read-only map containing Fluid Names and their associated IDs.
|
||||
* Modders should never actually use this, use the String names.
|
||||
*/
|
||||
@Deprecated
|
||||
public static Map<Fluid, Integer> getRegisteredFluidIDs()
|
||||
{
|
||||
return ImmutableMap.copyOf(fluidIDs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the universal bucket in forge.
|
||||
@@ -264,205 +48,4 @@ public abstract class FluidRegistry
|
||||
{
|
||||
return universalBucketEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a fluid with the universal bucket.
|
||||
* This only has an effect if the universal bucket is enabled.
|
||||
* @param fluid The fluid that the bucket shall be able to hold
|
||||
* @return True if the fluid was added successfully, false if it already was registered or couldn't be registered with the bucket.
|
||||
*/
|
||||
public static boolean addBucketForFluid(Fluid fluid)
|
||||
{
|
||||
if(fluid == null) {
|
||||
return false;
|
||||
}
|
||||
// register unregistered fluids
|
||||
if (!isFluidRegistered(fluid))
|
||||
{
|
||||
registerFluid(fluid);
|
||||
}
|
||||
return bucketFluids.add(fluid);
|
||||
}
|
||||
|
||||
/**
|
||||
* All fluids registered with the universal bucket
|
||||
* @return An immutable set containing the fluids
|
||||
*/
|
||||
public static Set<Fluid> getBucketFluids()
|
||||
{
|
||||
return Collections.unmodifiableSet(bucketFluids);
|
||||
}
|
||||
|
||||
|
||||
public static Fluid lookupFluidForBlock(Block block)
|
||||
{
|
||||
if (fluidBlocks == null)
|
||||
{
|
||||
BiMap<Block, Fluid> tmp = HashBiMap.create();
|
||||
for (Fluid fluid : fluids.values())
|
||||
{
|
||||
if (fluid.canBePlacedInWorld() && fluid.getBlock() != null)
|
||||
{
|
||||
tmp.put(fluid.getBlock(), fluid);
|
||||
}
|
||||
}
|
||||
fluidBlocks = tmp;
|
||||
}
|
||||
if (block == Blocks.FLOWING_WATER)
|
||||
{
|
||||
block = Blocks.WATER;
|
||||
}
|
||||
else if (block == Blocks.FLOWING_LAVA)
|
||||
{
|
||||
block = Blocks.LAVA;
|
||||
}
|
||||
return fluidBlocks.get(block);
|
||||
}
|
||||
|
||||
public static class FluidRegisterEvent extends Event
|
||||
{
|
||||
private final String fluidName;
|
||||
private final int fluidID;
|
||||
|
||||
public FluidRegisterEvent(String fluidName, int fluidID)
|
||||
{
|
||||
this.fluidName = fluidName;
|
||||
this.fluidID = fluidID;
|
||||
}
|
||||
|
||||
public String getFluidName()
|
||||
{
|
||||
return fluidName;
|
||||
}
|
||||
|
||||
public int getFluidID()
|
||||
{
|
||||
return fluidID;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getMaxID()
|
||||
{
|
||||
return maxID;
|
||||
}
|
||||
|
||||
public static String getDefaultFluidName(Fluid key)
|
||||
{
|
||||
String name = masterFluidReference.inverse().get(key);
|
||||
if (Strings.isNullOrEmpty(name)) {
|
||||
LOGGER.error(FLUIDS, "The fluid registry is corrupted. A fluid {} {} is not properly registered. The mod that registered this is broken",key.getClass().getName(),key.getName());
|
||||
throw new IllegalStateException("The fluid registry is corrupted");
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getModId(@Nullable FluidStack fluidStack)
|
||||
{
|
||||
if (fluidStack != null)
|
||||
{
|
||||
String defaultFluidName = getDefaultFluidName(fluidStack.getFluid());
|
||||
if (defaultFluidName != null)
|
||||
{
|
||||
ResourceLocation fluidResourceName = new ResourceLocation(defaultFluidName);
|
||||
return fluidResourceName.getResourceDomain();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void loadFluidDefaults(NBTTagCompound tag)
|
||||
{
|
||||
Set<String> defaults = Sets.newHashSet();
|
||||
if (tag.hasKey("DefaultFluidList",9))
|
||||
{
|
||||
LOGGER.debug(FLUIDS, "Loading persistent fluid defaults from world");
|
||||
NBTTagList tl = tag.getTagList("DefaultFluidList", 8);
|
||||
for (int i = 0; i < tl.tagCount(); i++)
|
||||
{
|
||||
defaults.add(tl.getStringTagAt(i));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER.debug(FLUIDS,"World is missing persistent fluid defaults - using local defaults");
|
||||
}
|
||||
loadFluidDefaults(HashBiMap.create(fluidIDs), defaults);
|
||||
}
|
||||
|
||||
public static void writeDefaultFluidList(NBTTagCompound forgeData)
|
||||
{
|
||||
NBTTagList tagList = new NBTTagList();
|
||||
|
||||
for (Entry<String, Fluid> def : fluids.entrySet())
|
||||
{
|
||||
tagList.appendTag(new NBTTagString(getDefaultFluidName(def.getValue())));
|
||||
}
|
||||
|
||||
forgeData.setTag("DefaultFluidList", tagList);
|
||||
}
|
||||
|
||||
public static void validateFluidRegistry()
|
||||
{
|
||||
Set<Fluid> illegalFluids = Sets.newHashSet();
|
||||
for (Fluid f : fluids.values())
|
||||
{
|
||||
if (!masterFluidReference.containsValue(f))
|
||||
{
|
||||
illegalFluids.add(f);
|
||||
}
|
||||
}
|
||||
|
||||
if (!illegalFluids.isEmpty())
|
||||
{
|
||||
LOGGER.fatal(FLUIDS, "The fluid registry is corrupted. Something has inserted a fluid without registering it");
|
||||
LOGGER.fatal(FLUIDS, "There is {} unregistered fluids",illegalFluids.size());
|
||||
for (Fluid f: illegalFluids)
|
||||
{
|
||||
LOGGER.fatal(FLUIDS, " Fluid name : {}, type: {}",f.getName(),f.getClass().getName());
|
||||
}
|
||||
LOGGER.fatal(FLUIDS, "The mods that own these fluids need to register them properly");
|
||||
throw new IllegalStateException("The fluid map contains fluids unknown to the master fluid registry");
|
||||
}
|
||||
}
|
||||
|
||||
static IRegistryDelegate<Fluid> makeDelegate(Fluid fl)
|
||||
{
|
||||
return delegates.get(fl);
|
||||
}
|
||||
|
||||
|
||||
private static class FluidDelegate implements IRegistryDelegate<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 ResourceLocation name() {
|
||||
return new ResourceLocation(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Fluid> type()
|
||||
{
|
||||
return Fluid.class;
|
||||
}
|
||||
|
||||
void rebind()
|
||||
{
|
||||
fluid = fluids.get(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,6 @@ import net.minecraftforge.common.capabilities.OptionalCapabilityInstance;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
|
||||
import net.minecraftforge.fluids.capability.wrappers.BlockLiquidWrapper;
|
||||
import net.minecraftforge.fluids.capability.wrappers.BlockWrapper;
|
||||
import net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
|
||||
Reference in new issue
Block a user