Jump to content

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


perromercenary00

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • so i just got this crash report and i dont mod a lot so i literally have no idea what to do to fix this
    • Hello, I was trying to play a MOD in my preferred language, but I see that only some items are translated, and I go to debug and I get this information (the only thing that is translated is the bestiary):   [14sep.2024 17:14:36.415] [Render thread/WARN] [net.minecraft.client.resources.language.ClientLanguage/]: Skipped language file: mowziesmobs:lang/es_es.json (com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 394 column 2 path $.config.mowziesmobs.ice_crystal_attack_multiplier) [14sep.2024 17:14:36.421] [Render thread/WARN] [net.minecraft.client.resources.language.ClientLanguage/]: Skipped language file: iceandfire:lang/es_es.json (com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1349 column 4 path $.iceandfire.sound.subtitle.dragonflute)   Is that the reason why everything is not translated? , and is there any way to fix it? Thanks
    • I got my model to render from the models renderToBuffer method. But still not quite what I want. I want to render the model from my renderer's render method. I feel that having access to the renderer and its methods will open some doors for me later down the line. //EntityRendererProvider.Context pContext = ; I want this //ToaPlayerRenderer render = new ToaPlayerRenderer(pContext, false); // if I can get the above line to work, having the methods from the renderer class would be incredibly helpful down the line RenderType rendertype = model.renderType(p.getSkinTextureLocation()); // this should be something like render.getTextureLocation() VertexConsumer vertexconsumer = buffer.getBuffer(rendertype); model.renderToBuffer(stack, vertexconsumer, paLights, 1, 1, 1, 1, 1); // I don't want the render to happen here since it doesn't use the renderer //model.render(p, 1f, pTicks, stack, buffer, paLights); I want to render the model using this It is certainly getting closer though. Probably. I am still worried that even if pContext is initialized this new instance of the renderer class will still hit me with the classic and all too familiar "can't use static method in non-static context"
    • Hello, I am learning how to create Multipart Entities and I tried creating a PartEntity based on the EnderDragonPart code. However, when I tested summoning the entity in the game, the PartEntity appeared at position x 0, y 0, z 0 within the game. I tried to make it follow the main entity, and after testing again, the part entity followed the main entity but seemed to teleport back to x 0, y 0, z 0 every tick (I'm just guessing). I don't know how to fix this can someone help me? My github https://github.com/SteveKK666/Forge-NewWorld-1.20.1/tree/master/src/main/java/net/kk/newworldmod/entity/custom Illustration  https://drive.google.com/file/d/157SPvyQCE8GcsRXyQQkD4Dyhalz6LjBn/view?usp=drive_link Sorry for my English; I’m not very good at it. 
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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