Jump to content

ItemStack item changing


meee39

Recommended Posts

In my multiblock crafting structure the final result is placed inside the inventory of a block. It is possible to see through the TESR and System.out.println() that the item does become the final result, but when it is taken out of the inventory it changes back into what was originally put in the block.

 

package com.leo.mobsuppressors.tileentity;

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

import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.items.CapabilityItemHandler;

public class TileEntitySuppressionAltarCore extends TileEntity implements ITickable {
	public TileEntitySuppressionAltarCore() {
		
	}

	@Override
	public void update() {
		attemptCraft();
	}
	
	public boolean attemptCraft() {
		if (!(world.getTileEntity(new BlockPos(getPos().getX(), getPos().getY() + 1, getPos().getZ())) instanceof TileEntitySuppressionTower)) {
			return false;
		}
		
		TileEntitySuppressionTower tower;
		//Check structure
		BlockPos[] positions = {
			//Edges
			new BlockPos(getPos().getX() + 1, getPos().getY(), getPos().getZ()), 
			new BlockPos(getPos().getX(), getPos().getY(), getPos().getZ() + 1 ), 
			new BlockPos(getPos().getX() -1, getPos().getY(), getPos().getZ()), 
			new BlockPos(getPos().getX(), getPos().getY(), getPos().getZ() - 1), 
			//Corners
			new BlockPos(getPos().getX() + 1, getPos().getY(), getPos().getZ() + 1), 
			new BlockPos(getPos().getX() + 1, getPos().getY(), getPos().getZ() - 1), 
			new BlockPos(getPos().getX() - 1, getPos().getY(), getPos().getZ() - 1), 
			new BlockPos(getPos().getX() - 1, getPos().getY(), getPos().getZ() + 1)
		};
		
		tower = (TileEntitySuppressionTower)world.getTileEntity(new BlockPos(getPos().getX(), getPos().getY() + 1, getPos().getZ()));
		
		for (BlockPos position: positions) {
			if (!(world.getTileEntity(position) instanceof TileEntitySuppressionPedestal)) {
				return false;
			}
		}
		
		for (EnumAltarRecipes recipe: EnumAltarRecipes.values()) {
			if (tower.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).getStackInSlot(0).getItem() == recipe.inputs[4]) {
				//Check edges
				if (((TileEntitySuppressionPedestal)world.getTileEntity(positions[0])).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).getStackInSlot(0).getItem() == recipe.inputs[1] && ((TileEntitySuppressionPedestal)world.getTileEntity(positions[1])).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).getStackInSlot(0).getItem() == recipe.inputs[1] && ((TileEntitySuppressionPedestal)world.getTileEntity(positions[2])).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).getStackInSlot(0).getItem() == recipe.inputs[1] && ((TileEntitySuppressionPedestal)world.getTileEntity(positions[3])).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).getStackInSlot(0).getItem() == recipe.inputs[1]) {
					//Check corners
					if (((TileEntitySuppressionPedestal)world.getTileEntity(positions[4])).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).getStackInSlot(0).getItem() == recipe.inputs[0] && ((TileEntitySuppressionPedestal)world.getTileEntity(positions[5])).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).getStackInSlot(0).getItem() == recipe.inputs[0] && ((TileEntitySuppressionPedestal)world.getTileEntity(positions[6])).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).getStackInSlot(0).getItem() == recipe.inputs[0] && ((TileEntitySuppressionPedestal)world.getTileEntity(positions[7])).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).getStackInSlot(0).getItem() == recipe.inputs[0]) {
						if (tower.netherStarUsesLeft > 0) {
							tower.netherStarUsesLeft -= 1;
							tower.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).extractItem(0, 64, false);
							tower.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).insertItem(0, new ItemStack(recipe.output, 1), false);
							MobSuppressors.network.sendToAllAround(new PacketUpdateTower(tower), new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 64));
						
							world.addWeatherEffect(new EntityLightningBolt(world, pos.getX(), pos.getY(), pos.getZ(), false));
						
							for (BlockPos position: positions) {
								((TileEntitySuppressionPedestal)world.getTileEntity(position)).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH).extractItem(0, 1, false);
							}
						}
					}
				}
			}
		}
		
		return true;
	}
}

 

Link to comment
Share on other sites

What did you see when you stepped through these actions in the debugger? The data for " taken out of the inventory" should hit you in the face when watched in the debugger.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

20 hours ago, meee39 said:

When the craft was complete it showed that the correct item was in the inventory, but when I took it out I didn't get that item.

That's not stepping using the debugger. You're telling us only the user experience. Set some break points and step through the code so you can see, line by line, what field of what object the itemstack is moving to when you "put it in inventory", and so you can see what field of what object the "wrong" itemstack is coming from when you "take it out" (and follow it all the way into your hand to see that it's not switched).

  • Like 1

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

I stepped through with the debugger and I found that the itemstack doesn't actually change. When the success part of the function was reached it sent a packet to update the inventory and that also made the TESR think that the inventory was updated, but in reality it didn't change at all. I think there is a problem with my packet then:

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);
				System.out.println(te.itemStackHandler.getStackInSlot(0));
				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();
	}

}

 

Link to comment
Share on other sites

I just realised that the packet I was sending was a client packet, but being sent on the server side. I wrote this packet:

package com.leo.mobsuppressors.network;

import com.leo.mobsuppressors.tileentity.TileEntitySuppressionAltarCore;
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.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
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 PacketAltarCraft implements IMessage {
	public static class Handler implements IMessageHandler<PacketAltarCraft, IMessage> {
		@Override
		public PacketUpdatePedestal onMessage(PacketAltarCraft message, MessageContext ctx) {
			Minecraft.getMinecraft().addScheduledTask(() -> {
				TileEntitySuppressionTower te = (TileEntitySuppressionTower)Minecraft.getMinecraft().world.getTileEntity(message.pos);
				te.itemStackHandler.setStackInSlot(0, message.stack);
				System.out.println(te.itemStackHandler.getStackInSlot(0));
				te.lastChangeTime = message.lastChangeTime;
				System.out.println(message.stack + "handler");
			});
			return null;
		}
	}
	
	private BlockPos pos;
	private ItemStack stack;
	private long lastChangeTime;
	
	public PacketAltarCraft(BlockPos pos, ItemStack itemStack, long lastChangeTime) {
		this.pos = pos;
		this.stack = itemStack;
		this.lastChangeTime = lastChangeTime;
	}
	
	public PacketAltarCraft(TileEntitySuppressionTower te, ItemStack stack) {
		this(te.getPos(), stack, te.lastChangeTime);
		System.out.println(stack + "constructor");
	}
	
	public PacketAltarCraft() {
		
	}
	
	@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();
	}
}

hoping that it would work, but I still have the same problem as before.

Link to comment
Share on other sites

Yes, otherwise how will they know what packet to encode/decode?

The sender needs to have the packet registered in order to send it to the reciever, and the reciever needs to have it registered in order for it to be decoded back into the data you then handle.

Link to comment
Share on other sites

Um...

On 7/5/2017 at 11:00 AM, Alpvax said:

Yes, otherwise how will they know what packet to encode/decode?

The sender needs to have the packet registered in order to send it to the reciever, and the reciever needs to have it registered in order for it to be decoded back into the data you then handle.

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.

Link to comment
Share on other sites

"It hurts doctor. It hurts."

 

Please define "doesn't work" in a manner that is actually troubleshootable.

  • Like 1

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.

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

    • 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;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
  • Topics

×
×
  • Create New...

Important Information

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