Jump to content

[1.19.4, SOLVED] Automatically move items around within player inventory


Recommended Posts

Posted (edited)

I want to prevent the player from holding an item in the offhand slot if the player is holding a certain item in the main hand slot. My approach so far is moving the offhand ItemStack to a free slot in the inventory. As you can see, my code is based off of Inventory#placeItemBackInInventory (if I use this method directly, the offhand item would simply stay in the same slot). 

My implementation works...only to an extent. It sometimes does not work when I open the inventory GUI and press F on an ItemStack to move it to the offhand slot; the ItemStack appears to stay in place and not move. What exactly am I missing? I am aware this is an issue regarding networking, but I was hoping the vanilla ClientboundContainerSetSlotPacket would prevent that.

// DISREGARD THIS WHOLE BLOCK OF CODE
@SubscribeEvent
public static void holdingCustomItemTick(TickEvent.PlayerTickEvent event) {
    if (event.phase == TickEvent.Phase.END) {
        if (event.player instanceof ServerPlayer serverPlayer && serverPlayer.tickCount % 20 == 0) { // I may remove the % 20 == 0 part later
            ItemStack offhandItem = serverPlayer.getOffhandItem();
            if (serverPlayer.getMainHandItem().getItem() == ModItems.CUSTOM_ITEM.get() &&
                    !offhandItem.isEmpty()) {
                // I previously tried using just these 3 lines, but it did not work (which makes sense because ItemStack#shrink and Player#addItem do not send packets to the client)
                // ItemStack offhandItem1 = offhandItem.copy();
                // offhandItem.shrink(offhandItem.getCount());
                // serverPlayer.addItem(offhandItem1);
              
                Inventory inventory = serverPlayer.getInventory();
                int i = inventory.getFreeSlot();

                if (i != -1) {
                    int j = offhandItem.getMaxStackSize() - inventory.getItem(i).getCount();
                    if (inventory.add(i, offhandItem.split(j))) {
                        serverPlayer.connection.send(
                                new ClientboundContainerSetSlotPacket(-2, 0, i, inventory.getItem(i)));
                    }
                }
                else {
                    inventory.player.drop(offhandItem, false);
                }

            }
        }
    }
}

 

EDIT as of 11/23/2023: Disregard everything above this. After giving up and then coming back a few months later, I finally found a solution! It only works in survival, but that's fine with me :)

// When the (non-creative) player is holding the item on their main hand, they are not allowed to equip any item in the offhand.
@SubscribeEvent
public static void livingEquipmentChangeEvent(LivingEquipmentChangeEvent event) {
    if (event.getEntity() instanceof Player player &&
            !player.isCreative() &&
            !player.level.isClientSide &&
            player.getMainHandItem().getItem() == ModItems.CUSTOM_ITEM.get()) {
        ItemStack offhandItem = player.getOffhandItem();
        if (!offhandItem.isEmpty()) {
            ItemEntity itemEntity = player.drop(offhandItem.copy(), true);
            offhandItem.shrink(offhandItem.getCount());
            if (itemEntity != null) {
                itemEntity.setPickUpDelay(0);
            }
        }
    }
}

 

Edited by LeeCrafts
Posted

is like many people has been this issue/idea 
but never see a vainilla/forge way to do it (the same way the bow hiddes the offhand item)

to do this i create a invisible item "hidder_item" whit an inventory from the custome bow create this "hidder_item" store the actual item in the offhand and put the "hidder_item" in the offhand slot 
this "hidder_item" must have a ticker function than after sole time or some condition meet it takes out the stored item delete itself and put the item back in offhand

Posted

Can you please clarify? Would this "hidden_item" be a copy of my offhand item--and, when I delete the offhand item after some time, the "hidden_item" would be placed in a free (non-offhand) slot in my inventory? If so, I have tried that and it did not work; explanation is in my code.

  • 2 weeks later...
Posted

this item store the offhand when left click and restore it on right clock 
the rest of the logic you must doo on inventory tick or your events

 

Spoiler

				
			package mercblk.items.classes;
		
		import net.minecraft.core.NonNullList;
		import net.minecraft.nbt.CompoundTag;
		import net.minecraft.nbt.ListTag;
		import net.minecraft.sounds.SoundEvents;
		import net.minecraft.world.InteractionHand;
		import net.minecraft.world.InteractionResultHolder;
		import net.minecraft.world.entity.Entity;
		import net.minecraft.world.entity.LivingEntity;
		import net.minecraft.world.entity.player.Player;
		import net.minecraft.world.item.Item;
		import net.minecraft.world.item.ItemStack;
		import net.minecraft.world.level.Level;
		import org.jetbrains.annotations.NotNull;
		
		import java.io.IOException;
		import java.util.HashMap;
		
		public class item_hidder extends Item {
		//########## ########## ########## ########## ########## ##########
		//########## ########## ########## ########## ########## ##########
		//########## ########## ########## ########## ########## ##########
		//########## ########## ########## ########## ########## ##########
		
		private String json = "{}";
		
		public item_hidder(Item.Properties propiedades) {
		super(propiedades);
		
		
		
		
		}
		
		
		// ########## ########## ##########
		//@Override
		public InteractionResultHolder<ItemStack> use(Level warudo, Player pe, InteractionHand hand) {
		System.out.println("\n USE ");
		ItemStack helditem = pe.getItemInHand(hand);
		ItemStack stack = ItemStack.EMPTY;
		
		if (!warudo.isClientSide()){
		stack = read_item_from(helditem);
		System.out.println( "read_item_from(" + stack.getDisplayName() + ");" );
		pe.getInventory().setItem( 40, stack);
		}
		
		return InteractionResultHolder.pass(pe.getItemInHand(hand));
		}
		
		// ########## ########## ########## ##########
		@Override
		public boolean onEntitySwing(ItemStack helditem, LivingEntity entity)
		{
		Level warudo = entity.level;
		Player pe = null;
		ItemStack stack = ItemStack.EMPTY;
		
		//Guardar el Item en la mano siniestra
		if (!warudo.isClientSide()){
		if( entity instanceof Player ){
		pe = (Player)entity;
		stack = pe.getItemInHand( InteractionHand.OFF_HAND );
		System.out.println( "write_item_to_slot(" + stack.getDisplayName() + ");" );
		write_item_to_slot(helditem, stack );
		stack = ItemStack.EMPTY;
		pe.getInventory().setItem( 40, stack);
		}
		}
		
		return false;
		}
		
		// ########## ########## ########## ##########
		// @Override
		public void inventoryTick(ItemStack stack, Level warudo, Entity en, int slot, boolean p_41408_) {
		//stufff
		
		}
		
		
		// ########## ########## ########## ##########
		// @Override
		public static ItemStack read_item_from(ItemStack container) {
		ItemStack stack = ItemStack.EMPTY;
		
		if (container.hasTag()) {
		CompoundTag compoundtag = container.getTag();
		ListTag listtag = null;
		int size = 0;
		
		if (compoundtag.contains("Items")) {
		// ListTag listtag = new ListTag();
		listtag = compoundtag.getList("Items", 10);
		size = listtag.size();
		
		if(size > 0 ) {
		CompoundTag itemstacktag = listtag.getCompound(0); //slot zero
		stack = ItemStack.of(itemstacktag);
		}
		
		}
		}
		return stack;
		}
		
		// ########## ########## ########## ##########
		// @Override
		public void write_item_to_slot(ItemStack container, ItemStack stack) {
		CompoundTag compoundtag = null;
		
		//iniziar NBT
		if (container.hasTag()) {
		compoundtag = container.getTag();
		} else {
		compoundtag = new CompoundTag();
		}
		
		//iniziar lista de ITEMS
		ListTag listtag = null;
		if (compoundtag.contains("Items")) {
		listtag = compoundtag.getList("Items", 10);
		} else {
		listtag = new ListTag();
		}
		
		CompoundTag itemstacktag = null;
		
		if( listtag.size() > 0 ) {
		itemstacktag = listtag.getCompound(0);
		} else {
		itemstacktag = new CompoundTag();
		}
		
		itemstacktag.putByte("Slot", (byte) 0);
		stack.save(itemstacktag);
		listtag.add(itemstacktag);
		
		compoundtag.put("Items", listtag);
		container.setTag(compoundtag);
		}
		}				
			

 

  • LeeCrafts changed the title to [1.19.4, SOLVED] Automatically move items around within player inventory

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

    • Hello all. I'm currently grappling with the updateShape method in a custom class extending Block.  My code currently looks like this: The conditionals in CheckState are there to switch blockstate properties, which is working fine, as it functions correctly every time in getStateForPlacement.  The problem I'm running into is that when I update a state, the blocks seem to call CheckState with the position of the block which was changed updated last.  If I build a wall I can see the same change propagate across. My question thus is this: is updateShape sending its return to the neighbouring block?  Is each block not independently executing the updateShape method, thus inserting its own current position?  The first statement appears to be true, and the second false (each block is not independently executing the method). I have tried to fix this by saving the block's own position to a variable myPos at inception, and then feeding this in as CheckState(myPos) but this causes a worse outcome, where all blocks take the update of the first modified block, rather than just their neighbour.  This raises more questions than it answers, obviously: how is a different instance's variable propagating here?  I also tried changing it so that CheckState did not take a BlockPos, but had myPos built into the body - same problem. I have previously looked at neighbourUpdate and onNeighbourUpdate, but could not find a way to get this to work at all.  One post on here about updatePostPlacement and other methods has proven itself long superceded.  All other sources on the net seem to be out of date. Many thanks in advance for any help you might offer me, it's been several days now of trying to get this work and several weeks of generally trying to get round this roadblock.  - Sandermall
    • sorry, I might be stupid, but how do I open it? because the only options I have are too X out, copy it, which doesn't work and send crash report, which doesn't show it to me, also, sorry for taking so long.
    • Can you reproduce this with version 55.0.21? A whole lot of plant placement issues were just fixed in this PR.
    • Necro'ing that thread to ask if you found a solution ? I'm encountering the same crash on loading the world. I created the world in Creative to test my MP, went into survival to test combat, died, crashed on respawn and since then crash on loading the world. Deactivating Oculus isn't fixing it either, and I don't have Optifine (Twilight forest is incompatible)
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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