Skip to content

Commit

Permalink
Barrel stuff part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
FakeDomi committed Nov 12, 2018
1 parent 6def05a commit 0ad7b46
Show file tree
Hide file tree
Showing 9 changed files with 765 additions and 1 deletion.
Expand Up @@ -197,6 +197,7 @@ public ModelResourceLocation getModelLocation(@Nonnull ItemStack stack)
ClientRegistry.bindTileEntitySpecialRenderer(TEWorldItem.class, new TESRWorldItem());
ClientRegistry.bindTileEntitySpecialRenderer(TEIngotPile.class, new TESRIngotPile());
ClientRegistry.bindTileEntitySpecialRenderer(TEBellows.class, new TESRBellows());
ClientRegistry.bindTileEntitySpecialRenderer(TEBarrel.class, new TESRBarrel());
}

@SubscribeEvent
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/net/dries007/tfc/client/render/TESRBarrel.java
@@ -0,0 +1,63 @@
package net.dries007.tfc.client.render;

import org.lwjgl.opengl.GL11;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraftforge.fluids.FluidStack;

import net.dries007.tfc.objects.te.TEBarrel;

public class TESRBarrel extends TileEntitySpecialRenderer<TEBarrel>
{
@Override
public void render(TEBarrel te, double x, double y, double z, float partialTicks, int destroyStage, float alpha)
{
FluidStack fluid = te.tank.getFluid();

if (fluid == null)
{
return;
}

TextureAtlasSprite texture = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(fluid.getFluid().getStill().toString());

GlStateManager.pushMatrix();
GlStateManager.translate(x, y, z);

GlStateManager.enableAlpha();
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);

int color = fluid.getFluid().getColor();

float r = ((color >> 16) & 0xFF) / 255f; // red
float g = ((color >> 8) & 0xFF) / 255f; // green
float b = (color & 0xFF) / 255f; // blue
float a = ((color >> 24) & 0xFF) / 255f; // alpha

GlStateManager.color(r, g, b, a);

//rendererDispatcher.renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);

BufferBuilder buffer = Tessellator.getInstance().getBuffer();

buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX_NORMAL);

double height = te.fillHeightForRender;

buffer.pos(0.1875, height, 0.1875).tex(texture.getInterpolatedU(3), texture.getInterpolatedV(3)).normal(0, 0, 1).endVertex();
buffer.pos(0.1875, height, 0.8125).tex(texture.getInterpolatedU(3), texture.getInterpolatedV(13)).normal(0, 0, 1).endVertex();
buffer.pos(0.8125, height, 0.8125).tex(texture.getInterpolatedU(13), texture.getInterpolatedV(13)).normal(0, 0, 1).endVertex();
buffer.pos(0.8125, height, 0.1875).tex(texture.getInterpolatedU(13), texture.getInterpolatedV(3)).normal(0, 0, 1).endVertex();

Tessellator.getInstance().draw();

GlStateManager.popMatrix();
}
}
5 changes: 4 additions & 1 deletion src/main/java/net/dries007/tfc/objects/blocks/BlocksTFC.java
Expand Up @@ -106,6 +106,7 @@ public final class BlocksTFC
public static final BlockLogPile LOG_PILE = null;
public static final BlockIngotPile INGOT_PILE = null;
public static final BlockTorchTFC TORCH = null;
public static final BlockBarrel BARREL = null;

// All these are for use in model registration. Do not use for block lookups.
// Use the static get methods in the classes instead.
Expand Down Expand Up @@ -238,6 +239,8 @@ public static void registerBlocks(RegistryEvent.Register<Block> event)

normalItemBlocks.add(new ItemBlock(register(r, "thatch", new BlockThatch(Material.PLANTS), CT_DECORATIONS)));

normalItemBlocks.add(new ItemBlock(register(r, "barrel", new BlockBarrel(), CT_WOOD)));

register(r, "firepit", new BlockFirePit()); // No item or creative tab.

{
Expand Down Expand Up @@ -440,7 +443,7 @@ public static void registerBlocks(RegistryEvent.Register<Block> event)
register(TEFirePit.class, "fire_pit");
register(TEToolRack.class, "tool_rack");
register(TEBellows.class, "bellows");

register(TEBarrel.class, "barrel");
}

public static boolean isWater(IBlockState current)
Expand Down
135 changes: 135 additions & 0 deletions src/main/java/net/dries007/tfc/objects/blocks/wood/BlockBarrel.java
@@ -0,0 +1,135 @@
package net.dries007.tfc.objects.blocks.wood;

import javax.annotation.Nullable;

import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import net.dries007.tfc.objects.te.TEBarrel;

public class BlockBarrel extends Block implements ITileEntityProvider
{
public static final PropertyBool SEALED = PropertyBool.create("sealed");
private static final AxisAlignedBB bounds = new AxisAlignedBB(0.125D, 0.0D, 0.125D, 0.875D, 1.0D, 0.875D);

public BlockBarrel()
{
super(Material.WOOD);
setSoundType(SoundType.WOOD);
}

@Override
@SuppressWarnings("deprecation")
public boolean isFullCube(IBlockState state)
{
return false;
}

@Override
@SuppressWarnings("deprecation")
public boolean isOpaqueCube(IBlockState state)
{
return false;
}

@Override
@SideOnly(Side.CLIENT)
public BlockRenderLayer getRenderLayer()
{
return BlockRenderLayer.CUTOUT;
}

@SuppressWarnings("deprecation")
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
{
return bounds;
}

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
if (worldIn.isRemote)
{
return true;
}

ItemStack heldItem = playerIn.getHeldItem(hand);

TEBarrel te = (TEBarrel)worldIn.getTileEntity(pos);

if (heldItem != ItemStack.EMPTY)
{
FluidUtil.interactWithFluidHandler(playerIn, hand, te.tank);
te.markDirty();
worldIn.notifyBlockUpdate(pos, state, state, 3);
}
else if (playerIn.isSneaking())
{
te.sealed = !te.sealed;
te.markDirty();
worldIn.notifyBlockUpdate(pos, state, state, 3);

return false;
}
else
{
playerIn.sendMessage(new TextComponentString("Content: " + te.tank.getFluidAmount()));
}

return true;
}

@Nullable
@Override
public TileEntity createNewTileEntity(World worldIn, int meta)
{
return new TEBarrel();
}

@Override
public BlockStateContainer createBlockState()
{
return new BlockStateContainer(this, SEALED);
}

@Override
@SuppressWarnings("deprecation")
public IBlockState getStateFromMeta(int meta)
{
return this.getDefaultState();
}

@Override
public int getMetaFromState(IBlockState state)
{
return 0;
}


@Override
@SuppressWarnings("deprecation")
public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
{
return super.getActualState(state, worldIn, pos).withProperty(SEALED, ((TEBarrel)worldIn.getTileEntity(pos)).sealed);
}
}
74 changes: 74 additions & 0 deletions src/main/java/net/dries007/tfc/objects/te/TEBarrel.java
@@ -0,0 +1,74 @@
package net.dries007.tfc.objects.te;

import javax.annotation.Nullable;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;

public class TEBarrel extends TileEntity
{
public double fillHeightForRender;

public FluidTank tank = new FluidTank(10000);
public boolean sealed;

@Override
public void readFromNBT(NBTTagCompound compound)
{
super.readFromNBT(compound);

tank.readFromNBT(compound.getCompoundTag("tank"));
sealed = compound.getBoolean("sealed");
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound)
{
NBTTagCompound tankTag = new NBTTagCompound();

tank.writeToNBT(tankTag);
compound.setTag("tank", tankTag);
compound.setBoolean("sealed", sealed);

return super.writeToNBT(compound);
}

@Override
public NBTTagCompound getUpdateTag()
{
return writeToNBT(new NBTTagCompound());
}

@Override
public void handleUpdateTag(NBTTagCompound tag)
{
this.readFromNBT(tag);

if (tank.getFluid() != null)
{
fillHeightForRender = 0.140625D + (0.75D - 0.015625D) * tank.getFluidAmount() / tank.getCapacity();
}
}

@Override
@Nullable
public SPacketUpdateTileEntity getUpdatePacket()
{
if (world != null)
{
return new SPacketUpdateTileEntity(this.getPos(), 0, this.writeToNBT(new NBTTagCompound()));
}

return null;
}

@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet)
{
this.handleUpdateTag(packet.getNbtCompound());
}
}
26 changes: 26 additions & 0 deletions src/main/resources/assets/tfc/blockstates/barrel.json
@@ -0,0 +1,26 @@
{
"forge_marker": 1,
"defaults": {
"model": "tfc:barrel",
"textures": {
"particle": "tfc:blocks/wood/planks/oak",
"planks": "tfc:blocks/wood/planks/oak",
"sheet": "tfc:blocks/wood/sheets/oak",
"hoop": "tfc:blocks/barrelhoop"
}
},
"variants": {
"normal": [
{}
],
"sealed": {
"true": {
"model": "tfc:barrel_sealed"
},
"false": {}
},
"inventory": [
{}
]
}
}

0 comments on commit 0ad7b46

Please sign in to comment.