Jump to content

[solved][1.19.4] exchange an itemstack whit other on the hotbar exactly the offhand item dont works well


Recommended Posts

Posted (edited)

im retaken and old problem 
i need to remove or hide the offhand item when pulling a bow 

well so i make a custome item invisible texture (still purple back i gonna fix that later) to replace the offhand item storing it inside and after a time it deletes itself and return the original item to the offhand in this case the offhand has two sapplings  
the problem is the item_hidder stuck's  and keeps stuck until i try to interact whit the offhand slot 

 

 


but if target any other slot it work as expected 
in this case afecthing the slot 1 whit the  briefcase works as it must 

 

···················································3

i think i need a command to force update player inventory way it reloads data client side event if the item change is only do it in the server side   

well thanks for your attention 

 

Spoiler
				
			package mercblk.items.classes;
		
		import mercblk.items.ItemInit;
		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.Inventory;
		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);
		}
		
		public static Item getHidder(){
		return ItemInit.ITEM_HIDDER.get();
		}
		
		public static int maxtime(){
		return 50;
		}
		
		public static CompoundTag get_nbt(ItemStack container){
		CompoundTag nbt = ( container.hasTag() )? container.getTag() : new CompoundTag() ;
		if(!nbt.contains("tick")) {
		nbt.putInt("tick", maxtime());
		}
		return nbt;
		}
		
		
		public static int get_tick(ItemStack container){
		CompoundTag nbt = container.getTag();
		if (nbt == null){
		nbt = new CompoundTag();
		}
		
		if (nbt.contains("tick")) {
		return nbt.getInt("tick");
		}
		return maxtime();
		}
		
		public static void set_tick(ItemStack container){
		CompoundTag nbt = container.getTag();
		if (nbt == null){
		nbt = new CompoundTag();
		}
		nbt.putInt("tick", maxtime());
		container.setTag(nbt);
		}
		
		public static boolean decrease_tick(ItemStack container){
		CompoundTag nbt = container.getTag();
		
		int tick = 0;
		tick = nbt.getInt("tick");
		tick --;
		
		if( tick < 0 ){
		tick = 0;
		}
		
		System.out.println("decrease_tick(" + tick + ")");
		nbt.putInt("tick", tick);
		container.setTag(nbt);
		
		if(tick == 0 ){
		return false;
		}
		
		return true;
		}
		
		
		
		// ########## ########## ########## ########## ########## ##########
		public static boolean hide_slot( Player pe, int slot ){
		System.out.println("hide_slot( pe, " + slot + " )");
		
		ItemStack slotstack = pe.getInventory().getItem(slot);
		CompoundTag nbt = new CompoundTag();
		
		if( slotstack == ItemStack.EMPTY ){
		System.out.println("el slot " + slot + " esta vacio");
		return false;
		}
		
		//restart timer;
		if( slotstack.getItem() instanceof item_hidder ){
		nbt = get_nbt(slotstack);
		nbt.putInt("tick", maxtime());
		slotstack.setTag(nbt);
		
		System.out.println("alredy hidde");
		return true;
		}
		
		//esconder el item
		System.out.println("hidding the item");
		ItemStack container = new ItemStack( getHidder() , 1 );
		write_item_to(container, slotstack);
		nbt = get_nbt(container);
		nbt.putInt("slot", slot);
		
		pe.getInventory().setItem(slot, container);
		container.setTag(nbt);
		
		
		return true;
		}
		
		// ########## ########## ########## ########## ########## ##########
		public static boolean unhide_slot( Entity en, int slot ){
		System.out.println("unhide_slot( en, " + slot + " )");
		
		
		if( en instanceof Player ){
		Player pe = (Player)en;
		
		ItemStack stack = ItemStack.EMPTY;
		ItemStack container = pe.getInventory().getItem(slot);
		
		if( container == ItemStack.EMPTY ){
		return false;
		}
		
		CompoundTag nbt = get_nbt(container);
		
		if( container.getItem() instanceof item_hidder ){
		stack = read_item_from( container );
		slot = nbt.getInt("slot");
		container.shrink(1);
		//pe.getInventory().setItem(slot, ItemStack.EMPTY );
		}
		
		//Inventory inventory = pe.getInventory();
		pe.getInventory().setItem(slot, stack);
		pe.getInventory().setChanged();
		}
		
		return true;
		}
		
		
		// ########## ########## ##########
		//@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(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
		if(!warudo.isClientSide())
		{
		if( !decrease_tick(stack) ) {
		unhide_slot(en, slot);
		}
		}
		}
		
		
		// ########## ########## ########## ##########
		// @Override
		public static ItemStack read_item_from(ItemStack container) {
		System.out.println( "read_item_from(container)" );
		ItemStack stack = ItemStack.EMPTY;
		
		CompoundTag nbt = get_nbt(container);
		
		//CompoundTag nbt = container.getTag();
		ListTag listtag = null;
		int size = 0;
		
		if (nbt.contains("Items")) {
		// ListTag listtag = new ListTag();
		listtag = nbt.getList("Items", 10);
		size = listtag.size();
		
		if(size > 0 ) {
		CompoundTag itemstacktag = listtag.getCompound(0); //slot zero
		stack = ItemStack.of(itemstacktag);
		}
		
		}
		
		return stack;
		}
		
		// ########## ########## ########## ##########
		// @Override
		public static void write_item_to(ItemStack container, ItemStack stack) {
		System.out.println( "write_item_to(container)" );
		CompoundTag nbt = get_nbt(container);
		
		//iniziar lista de ITEMS
		ListTag listtag = null;
		if (nbt.contains("Items")) {
		listtag = nbt.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);
		
		nbt.put("Items", listtag);
		container.setTag(nbt);
		}
		
		
		// #########################################################################3
		@Override
		public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
		return ( newStack.getItem() instanceof item_hidder )? true : false;
		}
		
		
		}				
			





 

Edited by perromercenary00
solved
Posted (edited)

yaa i found some trouble 

	// ########## ########## ########## ##########
@Override
public void inventoryTick(ItemStack container, Level warudo, Entity en, int slot, boolean p_41408_) {
//stufff
//if(!warudo.isClientSide())
{
System.out.println( "inventoryTick(slot[" + slot + "] " + warudo.isClientSide() + " )" );

if( !decrease_tick(container) ) {
unhide_slot(en, slot, container);
}
}
}
	

this piece of code i setup a sytem out on it 

inventoryTick(slot[9] false )
inventoryTick(slot[9] false )
inventoryTick(slot[9] true )
inventoryTick(slot[9] true )
inventoryTick(slot[9] false )

 

but when im using the slot 40 the offhand the funtion slot returns zeroes 

inventoryTick(slot[0] true )
inventoryTick(slot[0] true )
inventoryTick(slot[0] true )
inventoryTick(slot[0] false )
inventoryTick(slot[0] false )
inventoryTick(slot[0] true )

 

i change the method to store the slot number in the itemstack an pass the full itemstack to the unhide method 

	// ########## ########## ########## ########## ########## ##########
public static boolean unhide_slot( Entity en, int slot, ItemStack container ){
//System.out.print("unhide_slot( en, " + slot + " )");

if( en instanceof Player ){
Player pe = (Player)en;
ItemStack stack = ItemStack.EMPTY;
if( container == ItemStack.EMPTY ){
return false;
}

CompoundTag nbt = get_nbt(container);

if( container.getItem() instanceof item_hidder ){
stack = read_item_from( container );
slot = nbt.getInt("slot");
}

pe.getInventory().setItem(slot, stack);
}

return true;
}
	 

 

 

 

 

 

 

Edited by perromercenary00
  • perromercenary00 changed the title to [solved][1.19.4] exchange an itemstack whit other on the hotbar exactly the offhand item dont works well

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.