Jump to content

[1.7.10] Tile Entity Losing Members On Reload


Autom

Recommended Posts

I've created a tile entity that (through the consumption of bottles-o-enchanting placed in its inventory) maintains an 'experience level' internal to itself. It seems to be working fine, until the game is closed and reloaded- then the level has been reset to zero.

 

My understanding was that overriding the ReadFromNBT and WriteToNBT methods would allow the level data to be saved, as indeed that seems to be all that is required to save the items:

@Override
	public void writeToNBT(NBTTagCompound nbt) {
	    super.writeToNBT(nbt);

	    NBTTagList list = new NBTTagList();
	    for (int i = 0; i < this.getSizeInventory(); ++i) {
	        if (this.getStackInSlot(i) != null) {
	            NBTTagCompound stackTag = new NBTTagCompound();
	            stackTag.setByte("Slot", (byte) i);
	            this.getStackInSlot(i).writeToNBT(stackTag);
	            list.appendTag(stackTag);
	        }
	    }
	    nbt.setTag("Items", list);
	    
	    NBTTagInt lev = new NBTTagInt(level);
	    nbt.setTag("level", lev);
	}


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

	    NBTTagList list = nbt.getTagList("Items", 10);
	    for (int i = 0; i < list.tagCount(); ++i) {
	        NBTTagCompound stackTag = list.getCompoundTagAt(i);
	        int slot = stackTag.getByte("Slot") & 255;
	        this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
	    }
	    this.level = nbt.getInteger("level");
	}

Is this really all that is required? More broadly, is any modification or implementation of a network manager required to keep something like this synchronized between clients and servers? There don't seem to be many good tutorials on this subject that don't point to dead source code, etc., and looking at other mods has left me more confused than enlightened by the variety of implementations used.

Link to comment
Share on other sites

Alright, I spent hours figuring this one out. There are a myriad of methods you need to implement besides writeToNBT() and readFromNBT(NBTTagCompound). A working example of a luckyblock tileentity is:

 

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class TileEntityLuckyBlock extends TileEntity {
	public static final int MAX_TYPE = 1;
	
	private int luck;
	private int type; // 0: normal, 1: well
	private boolean isPlayerEditing;
	
	public TileEntityLuckyBlock() {
		this.luck = 0;
		this.type = 0;
		this.isPlayerEditing = false;
	}
	
	public int getLuck() {
		return luck;
	}
	
	public void setLuck(int newLuck) {
		luck = newLuck;
		markDirty(); // important
	}
	
	public int getType() {
		return type;
	}
	
	public void setType(int newType) {
		type = newType;
		markDirty(); // important
	}
	
	public boolean isPlayerEditing() {
		return isPlayerEditing;
	}
	
	public void setPlayerEditing(boolean editing) {
		isPlayerEditing = editing; // not marked because this field doesn't need to be persistent
	}
	
	public void executeDrop(World world, BlockPos pos, EntityPlayer breaker) {
		switch(type) {
			case 0:
				LBDrops.executeDrop(luck, world, pos, breaker);
				break;
			case 1:
			{
				EntityPlayer player = world.getClosestPlayer(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, 16.0 * 16.0 * 32.0, false);
				if(player != null)
					LBDrops.executeWellDrop(luck, world, pos, player, false);
				break;
			}
		}
	}
	
	/** Everything below here is an absolute MUST have. **/

	@Override
	public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) {
		return false;
	}
	
	@Override
	public NBTTagCompound getTileData() {
		return serializeNBT();
	}
	
	@Override
    public SPacketUpdateTileEntity getUpdatePacket() {
        NBTTagCompound nbt = new NBTTagCompound();
        writeToNBT(nbt);
        return new SPacketUpdateTileEntity(this.pos, 1, nbt);
    }
	
	@Override
	public NBTTagCompound getUpdateTag() {
		NBTTagCompound nbt = new NBTTagCompound();
		writeToNBT(nbt);
		return nbt;
	}

    @Override
    public void onDataPacket(NetworkManager manager, SPacketUpdateTileEntity packet) {
        readFromNBT(packet.getNbtCompound());
    }
	
	/** Obviously these mehtods will be different. **/

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
		super.writeToNBT(nbt);
		nbt.setInteger("luck", luck);
		nbt.setInteger("type", type);
		return nbt;
	}
	
	@Override
	public void readFromNBT(NBTTagCompound nbt) {
		super.readFromNBT(nbt);
		luck = nbt.getInteger("luck");
		type = nbt.getInteger("type");
		isPlayerEditing = false;
	}
}

 

So basically try copying the implementations of shouldRefresh, getTileData, getUpdatePacket, getUpdateTag, and onDataPacket. You will also need to schedule a render update in onDataPacket if you're rendering your tile entity.

  • Like 1
Link to comment
Share on other sites

  • Guest locked this topic
Guest
This topic is now closed to further replies.


  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Your drivers are in a broken state, follow the guide in the FAQ: https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/#:~:text=How do I update my drivers%3F
    • A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffc3fe22b60, pid=15228, tid=5140 # # JRE version: OpenJDK Runtime Environment Microsoft-8035246 (17.0.8+7) (build 17.0.8+7-LTS) # Java VM: OpenJDK 64-Bit Server VM Microsoft-8035246 (17.0.8+7-LTS, mixed mode, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64) # Problematic frame: # C [atio6axx.dll+0x192b60] # # No core dump will be written. Minidumps are not enabled by default on client versions of Windows # # If you would like to submit a bug report, please visit: # https://aka.ms/minecraftjavacrashes # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug i have no idea why please help
    • Quick-Books is great with regards to coordinating your monetary data. You can decide to live talk with a specialist at Quick-Books to get the answer for your questions. You will actually want to get to the talk going to the landing page and call us +1855-210-1428.
    • Update: I managed to make the item not disappear after crafting, but it doesn't remove any durability. import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.Enchantments; import net.minecraft.enchantment.UnbreakingEnchantment; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; public class NuggetHammer extends Item { private boolean damage; public NuggetHammer(Properties p_i48487_1_) { super(p_i48487_1_); } @Override public int getMaxDamage(ItemStack stack) { return 54 - 1; } public boolean isBarVisible(ItemStack stack) { return false; } @Override public ItemStack getContainerItem(ItemStack stack) { ItemStack copy = stack.copy(); copy.setCount(1); if (!this.damage) return copy; int unbreaking = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.UNBREAKING, stack); for (int i = 0; i < unbreaking; i++) { if (UnbreakingEnchantment.shouldIgnoreDurabilityDrop(stack, unbreaking, random)) return copy; } copy.setDamageValue(stack.getDamageValue() + 1); if (copy.getDamageValue() > stack.getMaxDamage()) return ItemStack.EMPTY; return copy; } }  
  • Topics

×
×
  • Create New...

Important Information

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