Jump to content

How Can I make an Entity with a Inventory?


feldim2425

Recommended Posts

Hi

I want to make an Entity (a Minecart) with a Inventory, but the inventory is always empty on Client side. I wrote a Sync Packet for my Entity but the Minecart does not keep the data.

 

Minecart Code:

public class ComputerCart extends EntityMinecart implements ISyncEntity, IInventory{

private boolean firstUpdate = true;
private int tier = -1;
private ItemStack[] inv = new ItemStack[20];

public ComputerCart(World p_i1712_1_) {
	super(p_i1712_1_);
}

public ComputerCart(World w, double x, double y, double z, Iterable<Pair<Integer, ItemStack>> components, int tier) {
	super(w,x,y,z);
	this.tier=tier;

	Iterator<Pair<Integer, ItemStack>> list = components.iterator();
	while(list.hasNext()){
		Pair<Integer, ItemStack> pair = list.next();
		if(pair.getKey() < 20 && pair.getValue() != null){
			inv[pair.getKey()] = pair.getValue();
		}
	}

}


public void readEntityFromNBT(NBTTagCompound nbt){
	super.readEntityFromNBT(nbt);
	this.loadInventory((NBTTagList) nbt.getTag("inventory"));
}

public void writeEntityToNBT(NBTTagCompound nbt){
	super.writeEntityToNBT(nbt);
	nbt.setTag("inventory", this.storeInventory());
}

private NBTTagList storeInventory(){
	NBTTagList tag = new NBTTagList();
	for(int i=0; i < inv.length; i+=1){
		NBTTagCompound invslot = new NBTTagCompound();
		NBTTagCompound invstack = new NBTTagCompound();
		invslot.setInteger("slot", i);
		if(inv[i] != null) inv[i].writeToNBT(invstack);
		invslot.setTag("stack", invstack);
		tag.appendTag(invslot);
	}
	return tag;
}

private void loadInventory(NBTTagList list){
	for(int i=0; i < list.tagCount(); i+=1){
		NBTTagCompound invslot = list.getCompoundTagAt(i);
		NBTTagCompound invstack = invslot.getCompoundTag("stack");
		int slot = invslot.getInteger("slot");
		if(slot<20){
			inv[slot] = ItemStack.loadItemStackFromNBT(invstack);
		}
	}
}

public void onUpdate(){
	if(this.firstUpdate){
		this.firstUpdate=false;
		if(!this.worldObj.isRemote)
		{	
			ModNetwork.sendToNearPlayers(new EntitySyncData(this.worldObj.provider.dimensionId, this.getEntityId()), this.posX, this.posY, this.posZ, this.worldObj);
		}
		else if(this.worldObj.isRemote){
			ModNetwork.channel.sendToServer(new EntitySyncRequest(this.worldObj.provider.dimensionId, this.getEntityId()));
		}
	}
}

@Override
public void writeSyncData(NBTTagCompound nbt) {
	nbt.setTag("inventory", this.storeInventory());
}

@Override
public void readSyncData(NBTTagCompound nbt) {
	this.loadInventory((NBTTagList) nbt.getTag("inventory"));

	int itemcount = 0;
	for(int i=0;i<this.inv.length;i+=1){
		if(this.inv[i]!=null) itemcount+=1;
	}
	OCMinecart.logger.log(Level.INFO, "ItemCount: "+itemcount+"  Remote: "+this.worldObj.isRemote);
}



@Override
public int getMinecartType() {
	return -1;
}

public static EntityMinecart create(World w, double x, double y, double z, Iterable<Pair<Integer, ItemStack>> components, int tier) {
	return new ComputerCart(w, x, y, z, components, tier);
}

public boolean interactFirst(EntityPlayer p){
	int itemcount = 0;
	for(int i=0;i<this.inv.length;i+=1){
		if(this.inv[i]!=null) itemcount+=1;
	}
	OCMinecart.logger.log(Level.INFO, "ItemCount: "+itemcount+"  Remote: "+this.worldObj.isRemote);
	return true;
}


@Override
public int getSizeInventory() {
	return inv.length;
}

@Override
public ItemStack getStackInSlot(int slot) {
	if(slot < inv.length) return inv[slot];
	return null;
}

@Override
public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_) {
	return null;
}

@Override
public ItemStack getStackInSlotOnClosing(int p_70304_1_) {
	return null;
}

@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
	if(slot<20) this.inv[slot] = stack;
}

@Override
public String getInventoryName() {
	return null;
}

@Override
public int getInventoryStackLimit() {
	return 64;
}

@Override
public void markDirty() {
	// TODO Auto-generated method stub

}

@Override
public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
	return true;
}

@Override
public void openInventory() {

}

@Override
public void closeInventory() {

}

@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
	return true;
}

}

 

The data from the Sync Packet are correct.

Output from the readSyncData() function:

[Client thread/INFO] [OC-Minecarts]: ItemCount: 3  Remote: false

I don't know why the world is not remote, but the Thread is running on Client Side.

 

Output on RightClick:

[Client thread/INFO] [OC-Minecarts]: ItemCount: 0  Remote: true
[server thread/INFO] [OC-Minecarts]: ItemCount: 3  Remote: false

Link to comment
Share on other sites

don't do that.

 

Lookup tutorials on creating a block with inventory and guihandler.  just substitute the inventory from the block with inventory from the entity.

No, that is not good. You can use the entities ID instead of the GuiID.

In you GuiHandler, do if(world.getEntityByID(ID) instanceof yourEntityClass.class){

open GUI in CLIENT and CONTAINER in SERVER here

}

Link to comment
Share on other sites

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

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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