Jump to content

Recommended Posts

Posted

In my mod I have a TESR for a block that shows a floating spinning item from its inventory. There is also a boolean that states wether the block has a Nether Star inside it. In my TESR I try to render the Nether Star by using an if condition checking if it has the Nether Star, but it always equals false. I know that the function for setting the nether star in the block executes because I have put System.out.printLine() in various places. It seems like in the TESR the TE properties aren't being accessed properly because it never executes the Nether Star rendering code even though it should.

 

package com.leo.mobsuppressors.tileentity;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.ForgeHooksClient;

public class TESRSuppressionTower extends TileEntitySpecialRenderer<TileEntitySuppressionTower> {
	@Override
	public void renderTileEntityAt(TileEntitySuppressionTower te, double x, double y, double z, float partialTicks, int destroyStage) {
		GlStateManager.enableRescaleNormal();
		GlStateManager.alphaFunc(GL11.GL_GREATER, 0.1f);
		GlStateManager.enableBlend();
		RenderHelper.enableStandardItemLighting();
		GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);
		
		ItemStack stack = te.itemStackHandler.getStackInSlot(0);
		if (!stack.isEmpty()) {
			GlStateManager.pushMatrix();
			GlStateManager.translate(x + 0.5, y + 0.15, z + 0.5);
			GlStateManager.rotate((te.getWorld().getTotalWorldTime() + partialTicks) * 4, 0, 1, 0);
	
			IBakedModel model = Minecraft.getMinecraft().getRenderItem().getItemModelWithOverrides(stack, te.getWorld(), null);
			model = ForgeHooksClient.handleCameraTransforms(model, ItemCameraTransforms.TransformType.GROUND, false);
	
			Minecraft.getMinecraft().getTextureManager().bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
			Minecraft.getMinecraft().getRenderItem().renderItem(stack, model);
			GlStateManager.popMatrix();
		}
		if (te.hasNetherStar) {
			GlStateManager.pushMatrix();
			GlStateManager.translate(x + 0.4, y + 0.5, z + 0.4);
			GlStateManager.scale(0.75, 0.75, 0.75);
			GlStateManager.rotate(90, 0, 0, 0);
			IBakedModel netherStarModel = Minecraft.getMinecraft().getRenderItem().getItemModelWithOverrides(new ItemStack(Items.NETHER_STAR),  te.getWorld(), null);
			Minecraft.getMinecraft().getTextureManager().bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
			Minecraft.getMinecraft().getRenderItem().renderItem(new ItemStack(Items.NETHER_STAR, 1), netherStarModel);
			GlStateManager.popMatrix();
		}
		
		GlStateManager.disableRescaleNormal();
		GlStateManager.disableBlend();
	}
}
package com.leo.mobsuppressors.tileentity;

import javax.annotation.Nullable;

import com.leo.mobsuppressors.MobSuppressors;
import com.leo.mobsuppressors.network.PacketRequestUpdateTower;
import com.leo.mobsuppressors.network.PacketUpdateTower;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TileEntitySuppressionTower extends TileEntity {
	public ItemStackHandler itemStackHandler = new ItemStackHandler(1) {
		@Override
		protected void onContentsChanged(int slot) {
			if (!world.isRemote) {
				lastChangeTime = world.getTotalWorldTime();
				MobSuppressors.network.sendToAllAround(new PacketUpdateTower(TileEntitySuppressionTower.this), new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 64));
			}
		}
	};
	
	public boolean hasNetherStar;
	public int netherStarUsesLeft;
	
	public long lastChangeTime;
	
	public TileEntitySuppressionTower() {
		this.hasNetherStar = false;
		this.netherStarUsesLeft = 0;
	}
	
	public void setHasNetherStar() {
		this.hasNetherStar = true;
		this.netherStarUsesLeft = 10;
	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		compound.setTag("inventory", itemStackHandler.serializeNBT());
		compound.setLong("lastChangeTime", lastChangeTime);
		compound.setBoolean("hasNetherStar", hasNetherStar);
		compound.setInteger("netherStarUsesLeft", netherStarUsesLeft);
		return super.writeToNBT(compound);
	}
	
	@Override
	public void readFromNBT(NBTTagCompound compound) {
		itemStackHandler.deserializeNBT(compound.getCompoundTag("inventory"));
		lastChangeTime = compound.getLong("lastChangeTime");
		hasNetherStar = compound.getBoolean("hasNetherStar");
		netherStarUsesLeft = compound.getInteger("netherStarUsesLeft");
		super.readFromNBT(compound);
	}
	
	@Override
	public void onLoad() {
		if (world.isRemote) {
			MobSuppressors.network.sendToServer(new PacketRequestUpdateTower(this));
		}
	}
	
	@Override
	public AxisAlignedBB getRenderBoundingBox() {
		return new AxisAlignedBB(getPos(), getPos().add(1, 2, 1));
	}
	
	@Override
	public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
		return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
	}
	
	@Nullable
	@Override
	public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
		return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? (T)itemStackHandler : super.getCapability(capability, facing);
	}
}
package com.leo.mobsuppressors.blocks;

import com.leo.mobsuppressors.CreativeTab;
import com.leo.mobsuppressors.tileentity.TileEntitySuppressionTower;

import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

public class BlockSuppressionTower extends Block implements ITileEntityProvider {

	public BlockSuppressionTower(String unlocalisedName, String registryName) {
		super(Material.ROCK);
		
		this.setUnlocalizedName(unlocalisedName);
		this.setRegistryName(registryName);
		
		this.setCreativeTab(CreativeTab.mobSuppressorCreativeTab);
	}
	
	public BlockSuppressionTower(Material materialIn) {
		super(materialIn);
	}
	
	@Override
	public boolean isOpaqueCube(IBlockState state) {
		return false;
	}
	
	@Override
	public boolean isFullCube(IBlockState state) {
		return false;
	}
	
	@Override
	public boolean isFullyOpaque(IBlockState state) {
		return false;
	}

	@Override
	public TileEntity createNewTileEntity(World worldIn, int meta) {
		return new TileEntitySuppressionTower();
	}
	
	@Override
	public void breakBlock(World world, BlockPos pos, IBlockState state) {
		super.breakBlock(world, pos, state);
		world.removeTileEntity(pos);
		TileEntitySuppressionTower tile = (TileEntitySuppressionTower)world.getTileEntity(pos);
		IItemHandler itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH);
		ItemStack stack = itemHandler.getStackInSlot(0);
		if (!stack.isEmpty()) {
			EntityItem item = new EntityItem(world, pos.getX(), pos.getY(), pos.getZ(), stack);
			world.spawnEntity(item);
		}
	}
	
	@Override
	public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
		if (!world.isRemote) {
			TileEntitySuppressionTower TEST = (TileEntitySuppressionTower)world.getTileEntity(pos);
			IItemHandler itemHandler = TEST.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
			
			if (player.getHeldItem(hand).isEmpty()) {
				player.setHeldItem(hand, itemHandler.extractItem(0, 64, false));
			} else if (player.getHeldItem(hand).getItem() == Items.NETHER_STAR) {
				if (!TEST.hasNetherStar) {
					player.getHeldItem(hand).shrink(1);
					TEST.setHasNetherStar();
				}
			} else {
				player.setHeldItem(hand, itemHandler.insertItem(0, player.getHeldItem(hand), false));
			}
			
			TEST.markDirty();
		}
		
		return true;
	}
}

What I want:

2017-06-24_12_53_16.png.ff818e0c238edef77d95e22b6388f4ac.png

What I get:

2017-06-24_12_52_08.png.c2e780c72c9deadfd215ffe3e0ece061.png

 

Posted
package com.leo.mobsuppressors.network;

import com.leo.mobsuppressors.tileentity.TileEntitySuppressionTower;

import io.netty.buffer.ByteBuf;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class PacketRequestUpdateTower implements IMessage {
	public static class Handler implements IMessageHandler<PacketRequestUpdateTower, PacketUpdateTower> {
		@Override
		public PacketUpdateTower onMessage(PacketRequestUpdateTower message, MessageContext ctx) {
			World world = FMLCommonHandler.instance().getMinecraftServerInstance().worldServerForDimension(message.dimension);
			TileEntitySuppressionTower te = (TileEntitySuppressionTower)world.getTileEntity(message.pos);
			if (te != null) {
				return new PacketUpdateTower(te);
			} else {
				return null;
			}
		}
	
	}
	
	private BlockPos pos;
	private int dimension;
	
	public PacketRequestUpdateTower(BlockPos pos, int dimension) {
		this.pos = pos;
		this.dimension = dimension;
	}
	
	public PacketRequestUpdateTower(TileEntitySuppressionTower te) {
		this(te.getPos(), te.getWorld().provider.getDimension());
	}
	
	public PacketRequestUpdateTower() {
	}
	
	@Override
	public void toBytes(ByteBuf buf) {
		buf.writeLong(pos.toLong());
		buf.writeInt(dimension);
	}
	
	@Override
	public void fromBytes(ByteBuf buf) {
		pos = BlockPos.fromLong(buf.readLong());
		dimension = buf.readInt();
	}
}
package com.leo.mobsuppressors.network;

import com.leo.mobsuppressors.tileentity.TileEntitySuppressionPedestal;
import com.leo.mobsuppressors.tileentity.TileEntitySuppressionTower;

import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class PacketUpdateTower implements IMessage {
	public static class Handler implements IMessageHandler<PacketUpdateTower, IMessage> {
		@Override
		public IMessage onMessage(PacketUpdateTower message, MessageContext ctx) {
			Minecraft.getMinecraft().addScheduledTask(() -> {
				TileEntitySuppressionTower te = (TileEntitySuppressionTower)Minecraft.getMinecraft().world.getTileEntity(message.pos);
				te.itemStackHandler.setStackInSlot(0, message.stack);
				te.lastChangeTime = message.lastChangeTime;
			});
			return null;
		}
	
	}

	
	private BlockPos pos;
	private ItemStack stack;
	private long lastChangeTime;
	
	public PacketUpdateTower(BlockPos pos, ItemStack stack, long lastChangeTime) {
		this.pos = pos;
		this.stack = stack;
		this.lastChangeTime = lastChangeTime;
	}
	
	public PacketUpdateTower(TileEntitySuppressionTower te) {
		this(te.getPos(), te.itemStackHandler.getStackInSlot(0), te.lastChangeTime);
	}
	
	public PacketUpdateTower() {
	
	}
	
	@Override
	public void toBytes(ByteBuf buf) {
		buf.writeLong(pos.toLong());
		ByteBufUtils.writeItemStack(buf, stack);
		buf.writeLong(lastChangeTime);
	}
	
	@Override
	public void fromBytes(ByteBuf buf) {
		pos = BlockPos.fromLong(buf.readLong());
		stack = ByteBufUtils.readItemStack(buf);
		lastChangeTime = buf.readLong();
	}

}

 

Posted
1 hour ago, Jay Avery said:

Okay, so you're sending the information about the inventory contents to the client. How will the client know to set hasNetherStar if the inventory contains the right item?

What do you mean?

Posted
5 minutes ago, meee39 said:

Block models can't be dynamic. I need a TESR because there is no way of knowing what item will be in the pedestal.

You do it with IModels

The thread title is someone trying to render items with a TESR and diesieben07 explains how a TESR isn't needed.

Mind, the item you want to have spinning does need to be a TESR, but the everything else can be a baked model.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
4 hours ago, meee39 said:

What do you mean?

When you update the inventory contents on the client, you don't update the value of hasNetherStar. You need to set this field by checking whether the updated inventory contains a nether star or not.

Posted

But the Nether Star isn't part of the inventory, it is stored as a boolean. Are you saying that I should be storing the Star in an inventory slot?

Posted (edited)

Oh, when you asked this-

7 hours ago, meee39 said:

Do I need to use a separate packet to the one that syncs the inventory?

I assumed you meant that the nether star is in the inventory. If it's not, then yes you will have to sync the nether star independently. 

Edited by Jay Avery
Posted

I made a new packet for updating hasNetherStar, but when called it creates an error in the fromBytes() method.

Jun 25, 2017 11:20:24 AM io.netty.channel.embedded.EmbeddedChannel recordException
WARNING: More than one exception was raised. Will report only the first one and log others.
io.netty.handler.codec.EncoderException: java.lang.IndexOutOfBoundsException: readerIndex(1) + length(8) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 1, widx: 1, cap: 256)
	at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:107)
	at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116)
	at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:658)
	at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:716)
	at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:706)
	at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:741)
	at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:895)
	at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:240)
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper.sendToAllAround(SimpleNetworkWrapper.java:268)
	at com.leo.mobsuppressors.blocks.BlockSuppressionTower.onBlockActivated(BlockSuppressionTower.java:85)
	at net.minecraft.server.management.PlayerInteractionManager.processRightClickBlock(PlayerInteractionManager.java:473)
	at net.minecraft.network.NetHandlerPlayServer.processTryUseItemOnBlock(NetHandlerPlayServer.java:712)
	at net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock.processPacket(CPacketPlayerTryUseItemOnBlock.java:68)
	at net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock.processPacket(CPacketPlayerTryUseItemOnBlock.java:13)
	at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:21)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at net.minecraft.util.Util.runTask(Util.java:29)
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:754)
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:699)
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156)
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:548)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(1) + length(8) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 1, widx: 1, cap: 256)
	at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1175)
	at io.netty.buffer.AbstractByteBuf.readLong(AbstractByteBuf.java:632)
	at net.minecraft.network.PacketBuffer.readLong(PacketBuffer.java:832)
	at com.leo.mobsuppressors.network.PacketUpdateTowerHasNetherStar.toBytes(PacketUpdateTowerHasNetherStar.java:54)
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:30)
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:26)
	at net.minecraftforge.fml.common.network.FMLIndexedMessageToMessageCodec.encode(FMLIndexedMessageToMessageCodec.java:73)
	at io.netty.handler.codec.MessageToMessageCodec$1.encode(MessageToMessageCodec.java:67)
	at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:89)
	... 22 more
package com.leo.mobsuppressors.tileentity;

import javax.annotation.Nullable;

import com.leo.mobsuppressors.MobSuppressors;
import com.leo.mobsuppressors.network.PacketRequestUpdateTower;
import com.leo.mobsuppressors.network.PacketUpdateTower;
import com.leo.mobsuppressors.network.PacketUpdateTowerHasNetherStar;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TileEntitySuppressionTower extends TileEntity {
	public ItemStackHandler itemStackHandler = new ItemStackHandler(1) {
		@Override
		protected void onContentsChanged(int slot) {
			if (!world.isRemote) {
				lastChangeTime = world.getTotalWorldTime();
				MobSuppressors.network.sendToAllAround(new PacketUpdateTower(TileEntitySuppressionTower.this), new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 64));
			}
		}
	};
	
	public boolean hasNetherStar;
	public int netherStarUsesLeft;
	
	public long lastChangeTime;
	
	public TileEntitySuppressionTower() {
		this.hasNetherStar = false;
		this.netherStarUsesLeft = 0;
	}
	
	public void setHasNetherStar() {
		MobSuppressors.network.sendToAllAround(new PacketUpdateTowerHasNetherStar(this, true), new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 64));
	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		compound.setTag("inventory", itemStackHandler.serializeNBT());
		compound.setLong("lastChangeTime", lastChangeTime);
		compound.setBoolean("hasNetherStar", hasNetherStar);
		compound.setInteger("netherStarUsesLeft", netherStarUsesLeft);
		return super.writeToNBT(compound);
	}
	
	@Override
	public void readFromNBT(NBTTagCompound compound) {
		itemStackHandler.deserializeNBT(compound.getCompoundTag("inventory"));
		lastChangeTime = compound.getLong("lastChangeTime");
		hasNetherStar = compound.getBoolean("hasNetherStar");
		netherStarUsesLeft = compound.getInteger("netherStarUsesLeft");
		super.readFromNBT(compound);
	}
	
	@Override
	public void onLoad() {
		if (world.isRemote) {
			MobSuppressors.network.sendToServer(new PacketRequestUpdateTower(this));
		}
	}
	
	@Override
	public AxisAlignedBB getRenderBoundingBox() {
		return new AxisAlignedBB(getPos(), getPos().add(1, 2, 1));
	}
	
	@Override
	public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
		return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
	}
	
	@Nullable
	@Override
	public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
		return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? (T)itemStackHandler : super.getCapability(capability, facing);
	}
}
package com.leo.mobsuppressors.network;

import com.leo.mobsuppressors.tileentity.TileEntitySuppressionTower;

import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class PacketUpdateTowerHasNetherStar implements IMessage {
	public static class Handler implements IMessageHandler<PacketUpdateTowerHasNetherStar, IMessage> {
		@Override
		public IMessage onMessage(PacketUpdateTowerHasNetherStar message, MessageContext ctx) {
			Minecraft.getMinecraft().addScheduledTask(() -> {
				TileEntitySuppressionTower te = (TileEntitySuppressionTower)Minecraft.getMinecraft().world.getTileEntity(message.pos);
				te.hasNetherStar = message.hasNetherStar;
				te.lastChangeTime = message.lastChangeTime;
			});
			return null;
		}
	
	}
	
	public boolean hasNetherStar;
	public BlockPos pos;
	public long lastChangeTime;
	
	public PacketUpdateTowerHasNetherStar(boolean hasNetherStar, BlockPos pos, long lastChangeTime) {
		this.pos = pos;
		this.lastChangeTime = lastChangeTime;
	}
	
	public PacketUpdateTowerHasNetherStar(TileEntitySuppressionTower te, boolean hasNetherStar) {
		this(hasNetherStar, te.getPos(), te.lastChangeTime);
	}
	
	public PacketUpdateTowerHasNetherStar() {
	
	}
	
	@Override
	public void fromBytes(ByteBuf buf) {
		buf.writeBoolean(hasNetherStar);
		buf.writeLong(pos.toLong());
		buf.writeLong(lastChangeTime);
	}

	@Override
	public void toBytes(ByteBuf buf) {
		hasNetherStar = buf.readBoolean();
		pos = BlockPos.fromLong(buf.readLong());
		lastChangeTime = buf.readLong();
	}

}
package com.leo.mobsuppressors.blocks;

import com.leo.mobsuppressors.CreativeTab;
import com.leo.mobsuppressors.tileentity.TileEntitySuppressionTower;

import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

public class BlockSuppressionTower extends Block implements ITileEntityProvider {

	public BlockSuppressionTower(String unlocalisedName, String registryName) {
		super(Material.ROCK);
		this.setHardness(2);
		
		this.setUnlocalizedName(unlocalisedName);
		this.setRegistryName(registryName);
		
		this.setCreativeTab(CreativeTab.mobSuppressorCreativeTab);
	}
	
	public BlockSuppressionTower(Material materialIn) {
		super(materialIn);
	}
	
	@Override
	public boolean isOpaqueCube(IBlockState state) {
		return false;
	}
	
	@Override
	public boolean isFullCube(IBlockState state) {
		return false;
	}
	
	@Override
	public boolean isFullyOpaque(IBlockState state) {
		return false;
	}

	@Override
	public TileEntity createNewTileEntity(World worldIn, int meta) {
		return new TileEntitySuppressionTower();
	}
	
	@Override
	public void breakBlock(World world, BlockPos pos, IBlockState state) {
		super.breakBlock(world, pos, state);
		world.removeTileEntity(pos);
		TileEntitySuppressionTower tile = (TileEntitySuppressionTower)world.getTileEntity(pos);
		IItemHandler itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH);
		ItemStack stack = itemHandler.getStackInSlot(0);
		if (!stack.isEmpty()) {
			EntityItem item = new EntityItem(world, pos.getX(), pos.getY(), pos.getZ(), stack);
			world.spawnEntity(item);
		}
	}
	
	@Override
	public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
		if (!world.isRemote) {
			TileEntitySuppressionTower TEST = (TileEntitySuppressionTower)world.getTileEntity(pos);
			IItemHandler itemHandler = TEST.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
			
			if (player.getHeldItem(hand).isEmpty()) {
				player.setHeldItem(hand, itemHandler.extractItem(0, 64, false));
			} else if (player.getHeldItem(hand).getItem() == Items.NETHER_STAR) {
				if (!TEST.hasNetherStar) {
					player.getHeldItem(hand).shrink(1);
					TEST.setHasNetherStar();
				}
			} else {
				player.setHeldItem(hand, itemHandler.insertItem(0, player.getHeldItem(hand), false));
			}
			
			TEST.markDirty();
		}
		
		return true;
	}
}

 

Posted
6 minutes ago, meee39 said:

fromBytes(ByteBuf buf) { buf.writeBoolean(hasNetherStar);

 

6 minutes ago, meee39 said:

public void toBytes(ByteBuf buf) { hasNetherStar = buf.readBoolean();

fromBytes is supposed to convert bytes to data, not write anything.

toBytes is supposed to convert data to bytes, not read anything.

Did you accidentaly mix the methods? ;)

 

Posted

Define "doesn't work". Is it still crashing you? Are you not recieving it? Are the fields not updated? Posting your new code also helps. At this point you might want to use git for your code so it is both easier for you to update us with the changes you've made and for us to see your latest code.

Posted

The field isn't updating and so it won't show up in the TESR.

TESR:

package com.leo.mobsuppressors.tileentity;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.ForgeHooksClient;

public class TESRSuppressionTower extends TileEntitySpecialRenderer<TileEntitySuppressionTower> {
	@Override
	public void renderTileEntityAt(TileEntitySuppressionTower te, double x, double y, double z, float partialTicks, int destroyStage) {
		GlStateManager.enableRescaleNormal();
		GlStateManager.alphaFunc(GL11.GL_GREATER, 0.1f);
		GlStateManager.enableBlend();
		RenderHelper.enableStandardItemLighting();
		GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);
		
		ItemStack stack = te.itemStackHandler.getStackInSlot(0);
		if (!stack.isEmpty()) {
			GlStateManager.pushMatrix();
			GlStateManager.translate(x + 0.5, y + 0.15, z + 0.5);
			GlStateManager.rotate((te.getWorld().getTotalWorldTime() + partialTicks) * 4, 0, 1, 0);
	
			IBakedModel model = Minecraft.getMinecraft().getRenderItem().getItemModelWithOverrides(stack, te.getWorld(), null);
			model = ForgeHooksClient.handleCameraTransforms(model, ItemCameraTransforms.TransformType.GROUND, false);
	
			Minecraft.getMinecraft().getTextureManager().bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
			Minecraft.getMinecraft().getRenderItem().renderItem(stack, model);
			GlStateManager.popMatrix();
		}
		System.out.println(te.hasNetherStar);
		
		if (te.hasNetherStar) {
			GlStateManager.pushMatrix();
			GlStateManager.translate(x + 0.45, y + 0.5, z + 0.55);
			GlStateManager.scale(0.75, 0.75, 0.75);
			GlStateManager.rotate(90, 0, 0, 0);
			IBakedModel netherStarModel = Minecraft.getMinecraft().getRenderItem().getItemModelWithOverrides(new ItemStack(Items.NETHER_STAR),  te.getWorld(), null);
			Minecraft.getMinecraft().getTextureManager().bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
			Minecraft.getMinecraft().getRenderItem().renderItem(new ItemStack(Items.NETHER_STAR, 1), netherStarModel);
			GlStateManager.popMatrix();
		}
		
		GlStateManager.disableRescaleNormal();
		GlStateManager.disableBlend();
	}
}

Updated Packet:

package com.leo.mobsuppressors.network;

import com.leo.mobsuppressors.tileentity.TileEntitySuppressionTower;

import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class PacketUpdateTowerHasNetherStar implements IMessage {
	public static class Handler implements IMessageHandler<PacketUpdateTowerHasNetherStar, IMessage> {
		@Override
		public IMessage onMessage(PacketUpdateTowerHasNetherStar message, MessageContext ctx) {
			Minecraft.getMinecraft().addScheduledTask(() -> {
				TileEntitySuppressionTower te = (TileEntitySuppressionTower)Minecraft.getMinecraft().world.getTileEntity(message.pos);
				te.hasNetherStar = message.hasNetherStar;
				te.lastChangeTime = message.lastChangeTime;
			});
			return null;
		}
	
	}
	
	public boolean hasNetherStar;
	public BlockPos pos;
	public long lastChangeTime;
	
	public PacketUpdateTowerHasNetherStar(boolean hasNetherStar, BlockPos pos, long lastChangeTime) {
		this.pos = pos;
		this.lastChangeTime = lastChangeTime;
	}
	
	public PacketUpdateTowerHasNetherStar(TileEntitySuppressionTower te, boolean hasNetherStar) {
		this(hasNetherStar, te.getPos(), te.lastChangeTime);
	}
	
	public PacketUpdateTowerHasNetherStar() {
	
	}

	@Override
	public void toBytes(ByteBuf buf) {
		buf.writeBoolean(hasNetherStar);
		buf.writeLong(pos.toLong());
		buf.writeLong(lastChangeTime);	
	}
	
	@Override
	public void fromBytes(ByteBuf buf) {
		hasNetherStar = buf.readBoolean();
		pos = BlockPos.fromLong(buf.readLong());
		lastChangeTime = buf.readLong();
	}
}

 

Posted

So in your packet the

2 minutes ago, meee39 said:

public PacketUpdateTowerHasNetherStar(TileEntitySuppressionTower te, boolean hasNetherStar)

constructor that you are using in your TE reroutes to

3 minutes ago, meee39 said:

public PacketUpdateTowerHasNetherStar(boolean hasNetherStar, BlockPos pos, long lastChangeTime)

which conviniently sets every field in the packet but the 

 

3 minutes ago, meee39 said:

hasNetherStar

field.

Posted

I have been debugging the code with breakpoints and I have discovered what the problem is this time. The problem is that the handler code is never reached so the field is never actually set in the TE. I just changed the packet registry to be client side and now it works. Thanks for your help because I was being very reliant on you and not finding the solutions by myself. I'll try to be not so annoying in the future :).

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.