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



×
×
  • Create New...

Important Information

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