Jump to content

Recommended Posts

Posted

I was kind of following this tutorial: https://www.youtube.com/watch?v=NNclUS5edcY&t=1230s

I have a custom item saved inside my TileEntity but it only shows up on world join not when changing the Item. Here is the Code:

 

The update get and set have the same Code as read and write so it should be working.

 

This is what is not working:

@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		NBTTagCompound tag = pkt.getNbtCompound();
		readUpdateTag(tag);
		this.readFromNBT(tag);
	}
	
	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		NBTTagCompound tag = new NBTTagCompound();
		this.writeUpdateTag(tag);
		return new SPacketUpdateTileEntity(pos, getBlockMetadata(), tag);
	}
	
	@Override
	public NBTTagCompound getUpdateTag() {
		NBTTagCompound tag = super.getUpdateTag();
		writeUpdateTag(tag);
		return tag;
	}
	
	public void writeUpdateTag(NBTTagCompound tag) {
		tag.setBoolean("infusing", infusing);
		tag.setInteger("infusiontimer", infusionTimer);
		NBTTagCompound compoundItem = new NBTTagCompound();
		compoundItem = item.serializeNBT();
		tag.setTag("item", compoundItem);
	}
	
	public void readUpdateTag(NBTTagCompound tag) {
		infusing = tag.getBoolean("infusing");
		infusionTimer = tag.getInteger("infusiontimer");
		
		NBTTagCompound compoundItem = tag.getCompoundTag("item");
		item = new ItemStack(compoundItem);
	}

 

 

.

First = TileEntity. Second = renderer

package net.mysticsouls.wandustrie.tileentity;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

public class TileEntityManaInfuser extends TileEntity {
public ItemStack item;

boolean infusing;

int infusionTimer;

public void toggleItem(ItemStack item) {
if(world.isRemote) return;
ItemStack stack = removeItem();
if(stack != null) {
world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack));
}
ItemStack newItem = item.copy();
newItem.setCount(1);
setItem(newItem);

resetInfuse();

markDirty();
IBlockState state = world.getBlockState(pos);
world.notifyBlockUpdate(pos, state, state, 3);
}

ItemStack removeItem() {
ItemStack stack = this.item;
this.item = null;
return stack;
}

void setItem(ItemStack item) {
this.item = item; 
}

void resetInfuse() {
infusing = false;
infusionTimer = 0;
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
this.writeUpdateTag(compound);
return compound;
}

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

@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
NBTTagCompound tag = pkt.getNbtCompound();
readUpdateTag(tag);
this.readFromNBT(tag);
}

@Override
public SPacketUpdateTileEntity getUpdatePacket() {
NBTTagCompound tag = new NBTTagCompound();
this.writeUpdateTag(tag);
return new SPacketUpdateTileEntity(pos, getBlockMetadata(), tag);
}

@Override
public NBTTagCompound getUpdateTag() {
NBTTagCompound tag = super.getUpdateTag();
writeUpdateTag(tag);
return tag;
}

public void writeUpdateTag(NBTTagCompound tag) {
tag.setBoolean("infusing", infusing);
tag.setInteger("infusiontimer", infusionTimer);
NBTTagCompound compoundItem = new NBTTagCompound();
compoundItem = item.serializeNBT();
tag.setTag("item", compoundItem);
}

public void readUpdateTag(NBTTagCompound tag) {
infusing = tag.getBoolean("infusing");
infusionTimer = tag.getInteger("infusiontimer");

NBTTagCompound compoundItem = tag.getCompoundTag("item");
item = new ItemStack(compoundItem);
}
}

 

package net.mysticsouls.wandustrie.tileentity.render;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.mysticsouls.wandustrie.init.ModItems;
import net.mysticsouls.wandustrie.tileentity.TileEntityManaInfuser;

public class RendererManaInfuser extends TileEntitySpecialRenderer<TileEntityManaInfuser>{

 private EntityItem ITEM = new EntityItem(Minecraft.getMinecraft().world, 0, 0, 0, new ItemStack(ModItems.magicOreIngot));

 @Override
 public void renderTileEntityAt(TileEntityManaInfuser te, double x, double y, double z, float partialTicks,
 int destroyStage) {
 super.renderTileEntityAt(te, x, y, z, partialTicks, destroyStage); 

 GlStateManager.pushMatrix();
 {
 if(te.item != null) {
 ITEM = new EntityItem(Minecraft.getMinecraft().world, 0, 0, 0, te.item);
 ITEM.hoverStart = 0F;
 GlStateManager.translate(x, y, z);
 GlStateManager.translate(0.5, 0, 0.5);
 GlStateManager.rotate(45F, 0, 1, 0);
 Minecraft.getMinecraft().getRenderManager().doRenderEntity(ITEM, 0, 0, 0, 0F, 0F, false);
 }
 }
 GlStateManager.popMatrix();
 }
}

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Managed to solve the issue myself. Solved by a fresh modpack instance.
    • Hello, so straight to the problem. Today i wanted to startup Enigmatica as usual and it just wont startup. Last night everything worked fine, i didnt even turn off my pc. Crash log: https://mclo.gs/GJ6Kcy1
    • Rubidium and Embeddium are versions of the same mod, you can only have one. I believe Embeddium is the one that's actively maintained.
    • Hello, New to modding, but have a solid CS foundation. I've created multiple custom BlockEntities that all have the same issue, which is that the inventory only updates on right click (overriding the useItemOn method). I've seen multiple posts on here outlining a similar issue to mine, but I've already implemented the solution of: overriding the correct methods in the BlockEntity class and calling setChanged(). I've tried every different place for setChanged() to no success. I'm wondering if I'm missing something else or if there was some change to sending data to the client in 1.21.5? Or will I have to use a custom packet sender? Here is the code for one of my BlockEntity classes with a single inventory slot: public class MyCustomBlockEntity extends BlockEntity { public final ItemStackHandler inventory = new ItemStackHandler(1) { @Override protected int getStackLimit(int slot, @NotNull ItemStack stack) { return 1; } @Override protected void onContentsChanged(int slot) { setChanged(); if (!level.isClientSide()) { level.setBlockAndUpdate(getBlockPos(), getBlockState()); } } }; public MyCustomBlockEntity(BlockPos pPos, BlockState pBlockState) { super(ModBlockEntities.MY_CUSTOM_BE.get(), pPos, pBlockState); } public void clearContents() { inventory.setStackInSlot(0, ItemStack.EMPTY); } public void dropItem() { SimpleContainer inv = new SimpleContainer(inventory.getSlots()); inv.setItem(0, inventory.getStackInSlot(0)); Containers.dropContents(this.level, this.worldPosition, inv); } @Override public void setRemoved() { dropItem(); super.setRemoved(); } @Override protected void saveAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) { super.saveAdditional(pTag, pRegistries); pTag.put("inventory", inventory.serializeNBT(pRegistries)); } @Override protected void loadAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) { super.loadAdditional(pTag, pRegistries); inventory.deserializeNBT(pRegistries, pTag.getCompound("inventory").get()); } @Override public Packet<ClientGamePacketListener> getUpdatePacket() { return ClientboundBlockEntityDataPacket.create(this); } @Override public CompoundTag getUpdateTag(HolderLookup.Provider pRegistries) { return saveWithoutMetadata(pRegistries); } } Mostly encountering the issue when calling the clearContents() method anywhere outside of useItemOn() in the Block class. I've also tried overriding both the handleUpdateTag() and onDataPacket() methods, calling their super along with loadAdditional(), but neither changed the outcome. Thanks in advance for any replies.
    • Hi all! I’m working on a Jurassic Park-themed mod for Minecraft 1.20.1, aiming to include dinosaurs, fossils, DNA extraction, and cool machines. This is a free project, mainly passion-driven, and I’ll give full credit to everyone involved. this is the perfect opportunity for beginners of modeling and coding. This project will give you experience and a creative freedom If you love dinosaurs and Minecraft modding, hit me up! Thanks! Add Me ogfrost. <--- Discord
  • Topics

×
×
  • Create New...

Important Information

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