Jump to content

[1.12.2] intestack capability not sync to remote world


perromercenary00

Recommended Posts

Goo days and sorry for the broken English

 

im doing a custome capability to manage some weapons and tools actually still in develop stage

when i was doing mi code i notice that the values i set in item stack in the server side are not reaching the client side

so i made a test item to check it out,  but still the values i change remain even after i close the world , lets explain it better

 

i have a test item "prueba00"

and a costume capability "animacionMercenaria"

 

in the item left click:  i set a value for the variable action only in the server

in the item righ click:  only display the value of action in both worlds

 

	    // ############################################################################################3
    //@Override
    public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
        
        boolean sePudo = true;
        ItemStack stack = player.getHeldItem(hand);
        player.setActiveHand(hand);
	        animacionMercenaria alm = getAlmacenamiento(stack);
        byte action = alm.getAccion();
        
        chat.chatm((EntityPlayer)player, world.isRemote + "RIGHTCLICK, accion=" + alm.getAccion() );
        
        if (stack.getMetadata() < 1) {
            player.setActiveHand(hand);
            return new ActionResult(EnumActionResult.SUCCESS, stack);
        } else {
            return new ActionResult(EnumActionResult.FAIL, stack);
        }
	    }
    
    // ############################################################################################3
	    @Override
    public boolean onEntitySwing(EntityLivingBase player, ItemStack stack) {
	        World world = player.world;
        animacionMercenaria alm = getAlmacenamiento(stack);
        
        if( !world.isRemote ) {
	            byte action = alm.getAccion();
            alm.setAccion(action + 1);
            
            chat.chatm((EntityPlayer)player, world.isRemote + "LEFTCLICK, accion=" + alm.getAccion() );
        }
	        return true;
    }
    
    // ############################################################################################3
	

 

 

this is the output

	//i press three times left click to set action to '4'
	[07:56:07] [Client thread/INFO] [minecraft/GuiNewChat]: [CHAT] falseLEFTCLICK, accion=2
[07:56:09] [Client thread/INFO] [minecraft/GuiNewChat]: [CHAT] falseLEFTCLICK, accion=3
[07:56:10] [Client thread/INFO] [minecraft/GuiNewChat]: [CHAT] falseLEFTCLICK, accion=4
	 
	//but it only change in the server side, the local side remains in 1
[07:56:14] [Client thread/INFO] [minecraft/GuiNewChat]: [CHAT] trueRIGHTCLICK, accion=1
[07:56:14] [Client thread/INFO] [minecraft/GuiNewChat]: [CHAT] falseRIGHTCLICK, accion=4
[07:56:20] [Client thread/INFO] [minecraft/GuiNewChat]: [CHAT] trueRIGHTCLICK, accion=1
[07:56:20] [Client thread/INFO] [minecraft/GuiNewChat]: [CHAT] falseRIGHTCLICK, accion=4
	

 

if  i close minecraf and open again and just rightclick the item

 

	//the value was saved and is now set in bot worlds
	[08:03:55] [Client thread/INFO] [minecraft/GuiNewChat]: [CHAT] trueRIGHTCLICK, accion=4
[08:03:57] [Client thread/INFO] [minecraft/GuiNewChat]: [CHAT] falseRIGHTCLICK, accion=4
[08:04:02] [Client thread/INFO] [minecraft/GuiNewChat]: [CHAT] trueRIGHTCLICK, accion=4
[08:04:02] [Client thread/INFO] [minecraft/GuiNewChat]: [CHAT] falseRIGHTCLICK, accion=4
	

 

 

#################################################################

jummmmm

what could  be wrong here ??

 

Prueba00

	package modmercenario.items.pruebas;
	import java.util.List;
	import net.minecraft.block.Block;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Enchantments;
import net.minecraft.init.Items;
import net.minecraft.item.EnumAction;
import net.minecraft.item.IItemPropertyGetter;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import modmercenario.modmercenario;
import modmercenario.init.Mitems;
import modmercenario.items.materiales.materialBasico;
import modmercenario.poderes.animacionMercenaria;
import modmercenario.poderes.cdalm;
import modmercenario.utilidades.chat;
import modmercenario.utilidades.objetoobjetivo;
import modmercenario.utilidades.util;
	//############################################################################################3
public class prueba00 extends materialBasico {
	    private static String name = "materialesSuspensionderedstone";
    public static final PropertyInteger LEVEL = PropertyInteger.create("level", 0, 3);
	    // ############################################################################################3
    public prueba00(String name) {
        super(name);
        //setRegistryName("prueba00/" + name);
        
        this.setHasSubtypes(false);
        this.maxStackSize = 1;
        this.setCreativeTab(modmercenario.materiales);
        
        //estado
        this.addPropertyOverride(new ResourceLocation("est"), new IItemPropertyGetter() {
            @SideOnly(Side.CLIENT)
            public float apply(ItemStack stack, World worldIn, EntityLivingBase entityIn) {
	                animacionMercenaria alm = getAlmacenamiento(stack);
                return alm.getAnimation();
	            }
        });
	    }// fin de contructor
    // ############################################################################################3    
    
	    
    
    // ############################################################################################3
    @Override
    public ICapabilityProvider initCapabilities(ItemStack item, NBTTagCompound nbt) {
	        if (item.getItem() instanceof prueba00) {
            animacionMercenaria alm = new animacionMercenaria(item, nbt);
            return alm;
        }
	        return null;
    }
	    // ############################################################################################3
    public animacionMercenaria getAlmacenamiento(ItemStack stackIn) {
        //animacionMercenaria alm = getAlmacenamiento(stack);
        animacionMercenaria alm = (animacionMercenaria) stackIn.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
        return alm;
    }
    
    
    // ############################################################################################3
    @Override
    public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
        
        boolean sePudo = true;
        ItemStack stack = player.getHeldItem(hand);
        player.setActiveHand(hand);
	        animacionMercenaria alm = getAlmacenamiento(stack);
        byte action = alm.getAccion();
        
        chat.chatm((EntityPlayer)player, world.isRemote + "RIGHTCLICK, accion=" + alm.getAccion() );
        
        if (stack.getMetadata() < 1) {
            player.setActiveHand(hand);
            return new ActionResult(EnumActionResult.SUCCESS, stack);
        } else {
            return new ActionResult(EnumActionResult.FAIL, stack);
        }
	    }
    
    // ############################################################################################3
	    @Override
    public boolean onEntitySwing(EntityLivingBase player, ItemStack stack) {
	        World world = player.world;
        
        animacionMercenaria alm = getAlmacenamiento(stack);
        
        if( !world.isRemote ) {
	            byte action = alm.getAccion();
            
            alm.setAccion(action + 1);
            
            chat.chatm((EntityPlayer)player, world.isRemote + "LEFTCLICK, accion=" + alm.getAccion() );
        }
	        return true;
    }
    
    // ############################################################################################3
	 
	    // #########################################################################3
    @Override
    public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
	        return !ItemStack.areItemStacksEqual(oldStack, newStack);
	    }
	    // ############################################################################################3
}// fin de la classe
	 
	

 

capability animacionMercenaria

	package modmercenario.poderes;
	import modmercenario.utilidades.util;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
	public class animacionMercenaria implements ICapabilityProvider, ICapabilitySerializable {
    
    
    private boolean init = false;
    
    private boolean play = false;
    private boolean pause = false;
    private boolean stop = false;
    private boolean hand = true;
	    private long time = 0;
    private long accionstart = 0;
    private long accionfinis = 0;
    
    private short modelo = 0; //desde donde comienza a contar la animacion max 32,767    
    private byte accion = 0;//hasta 256 // 100
    private byte paso = 0;//hasta 256 // 1
    private byte maxpaso = 0;//hasta 256
    private byte pasos[] = {0};
	    private short municion = 0;
    private short maxmunicion = 0;
	    private short altmunicion = 0;
    private short altmaxmunicion = 0;
	    
    private int serial = 0;
    
    private long inicializaciontime = 0L;// fecha de la primera inicializacion
    private long ultrevisioninv = 0L;// ultima vez que se revizo el inventario
	    private ItemStack stack = ItemStack.EMPTY;
    private NBTTagCompound nbt = new NBTTagCompound();
    private NBTTagCompound nbtIn = null;
	    // constructor
    public animacionMercenaria(ItemStack item, NBTTagCompound nbtIn) {
        // this.entidad = entidadIn;
        this.stack = item;
        this.nbtIn = nbtIn;
    }
	    public void clearNbt() {
        nbt = new NBTTagCompound();
    }
	    // ###########################################################
    @Override
    public NBTBase serializeNBT() {
        // System.out.println("### >>> serializeNBT()");
        
        nbt.setBoolean("init",init );
        
        nbt.setBoolean("play", play);
        nbt.setBoolean("pause", pause);
        nbt.setBoolean("stop", stop);
        nbt.setBoolean("hand",hand );
	        nbt.setLong("time", time);
        nbt.setLong("accionstart",accionstart );
        nbt.setLong("accionfinis",accionfinis );
        
        nbt.setShort("modelo", modelo);
        nbt.setByte("accion", accion);
        nbt.setByte("paso",paso );
        nbt.setByte("maxpaso",maxpaso );
        
        nbt.setByteArray("pasos", pasos);
        
	        nbt.setShort("municion", municion);
        nbt.setShort("maxmunicion", maxmunicion);
	        nbt.setShort("altmunicion",altmunicion );
        nbt.setShort("altmaxmunicion", altmaxmunicion);
	        
        nbt.setInteger("serial",serial );
        
        nbt.setLong("inicializaciontime",inicializaciontime );
        nbt.setLong("ultrevisioninv", ultrevisioninv);
	        // nbtIn = nbt;
	        return nbt;
    }
	    // ###########################################################
    @Override
    public void deserializeNBT(NBTBase nbtIn) {
	        // System.out.println("### >>> deserializeNBT");
	        NBTTagCompound nbt = (NBTTagCompound) nbtIn;
        
        init = nbt.hasKey("init") ? nbt.getBoolean("init") : false;
        
        play = nbt.hasKey("play") ? nbt.getBoolean("play") : false;
        pause = nbt.hasKey("pause") ? nbt.getBoolean("pause") : false;
        stop = nbt.hasKey("stop") ? nbt.getBoolean("stop") : false;
        
        hand = nbt.hasKey("hand") ? nbt.getBoolean("hand") : false;
        
        time = nbt.hasKey("time") ? nbt.getLong("time") : 0L;
        accionstart = nbt.hasKey("accionstart") ? nbt.getLong("accionstart") : 0L;
        accionfinis = nbt.hasKey("accionfinis") ? nbt.getLong("accionfinis") : 0L;
        
        modelo = nbt.hasKey("modelo") ? nbt.getShort("modelo") : 0;
        accion = (nbt.hasKey("accion")) ? nbt.getByte("accion") : 0;
        paso = nbt.hasKey("paso") ? nbt.getByte("paso") : 0;
        maxpaso = nbt.hasKey("maxpaso") ? nbt.getByte("maxpaso") : 0;
        pasos =  nbt.hasKey("pasos") ? nbt.getByteArray("pasos") : pasos;
	        municion = nbt.hasKey("municion") ? nbt.getShort("municion") : 0;
        maxmunicion = nbt.hasKey("maxmunicion") ? nbt.getShort("maxmunicion") : 0;
	        serial = (nbt.hasKey("serial")) ? nbt.getInteger("serial") : 0;
        
        altmunicion = nbt.hasKey("altmunicion") ? nbt.getShort("altmunicion") : 0;
        altmaxmunicion = nbt.hasKey("altmaxmunicion") ? nbt.getShort("altmaxmunicion") : 0;
    
        
    }
	    // ###########################################################
    @Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return true;
        }
        return false;
    }
	    // ###########################################################
    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
	            return (T) this;
            // return (T) new ItemStackHandler(2/**the amount of slots you
            // want*/);
            // This is the default implementation by forge, but you'll likely
            // want to make your own by overriding.
        }
        return null;
    }
	    // ###########################################################
	
    public static animacionMercenaria inicializar(ItemStack stackIn, animacionMercenaria xb) {
        
        xb.setInit(true);
        
        xb.setSerial(getSerialAlHazar());
        
        xb.setStack(stackIn);
        
        return xb;
        
    }
	    // ########################################################################3
    public static int getSerialAlHazar() {
	        int R = ((int) (Math.random() * 9));
	        R = R + (((int) (Math.random() * 9)) * 10);
	        R = R + (((int) (Math.random() * 9)) * 100);
	        R = R + (((int) (Math.random() * 9)) * 1000);
	        R = R + (((int) (Math.random() * 9)) * 10000);
	        R = R + (((int) (Math.random() * 9)) * 100000);
	        R = R + (((int) (Math.random() * 9)) * 1000000);
	        R = R + (((int) (Math.random() * 8)) * 10000000) + 1;
	        return R;
    }
    
    // ###########################################################
    public int getAnimation() {
        
        if( this.isPlay() ) {
            long t = System.currentTimeMillis();
            byte[] arr = this.getPasos();
            
            
            int i = 0;
            i = (int)(( t - this.getAccionstart() ) / 50);
            i = ( i >= arr.length)? (arr.length - 1) : i;
                
            return (int)( arr[i] + this.getModelo() );
        }
        
        return this.getModelo();
    }
    
    // ###########################################################
    public void play(int accion) {
    
        this.setTime(System.currentTimeMillis());
        long t = this.getTime();
        this.setAccion(accion);
        this.setModelo(1);
        
        byte[] arr = {1,2,3,4,5,6,7,8,9,10};
        this.setPasos(arr);
        
        arr = this.getPasos();
	        this.setAccionstart(t);
        this.setAccionfinis( this.getAccionstart() + ( arr.length * 50 ));
        
        this.setPlay(true);
        this.setStop(false);
        this.setPause(false);
        
    }
    
    
    
    
    // ###########################################################
    
    public boolean isInit() {
        return init;
    }
	    public void setInit(boolean init) {
        this.init = init;
    }
	    public boolean isPlay() {
        return play;
    }
	    public void setPlay(boolean play) {
        this.play = play;
    }
	    public boolean isPause() {
        return pause;
    }
	    public void setPause(boolean pause) {
        this.pause = pause;
    }
	    public boolean isStop() {
        return stop;
    }
	    public void setStop(boolean stop) {
        this.stop = stop;
    }
	    public boolean isHand() {
        return hand;
    }
	    public void setHand(boolean hand) {
        this.hand = hand;
    }
	    public long getTime() {
        return time;
    }
	    public void setTime(long time) {
        this.time = time;
    }
	    public long getAccionstart() {
        return accionstart;
    }
	    public void setAccionstart(long accionstart) {
        this.accionstart = accionstart;
    }
	    public long getAccionfinis() {
        return accionfinis;
    }
	    public void setAccionfinis(long accionfinis) {
        this.accionfinis = accionfinis;
    }
	    public short getModelo() {
        return modelo;
    }
	    public void setModelo(int modelo) {
        this.modelo = (short)modelo;
    }
	    public byte getAccion() {
        return accion;
    }
	    public void setAccion(int accion) {
        this.accion = (byte)accion;
    }
	    public byte getPaso() {
        return paso;
    }
	    public void setPaso(byte paso) {
        this.paso = paso;
    }
	    public byte getMaxpaso() {
        return maxpaso;
    }
	    public void setMaxpaso(byte maxpaso) {
        this.maxpaso = maxpaso;
    }
    
    public byte[] getPasos() {
        return pasos;
    }
	    public void setPasos(byte[] pasos) {
        this.pasos = pasos;
    }
	    public short getMunicion() {
        return municion;
    }
	    public void setMunicion(short municion) {
        this.municion = municion;
    }
	    public short getMaxmunicion() {
        return maxmunicion;
    }
	    public void setMaxmunicion(short maxmunicion) {
        this.maxmunicion = maxmunicion;
    }
	    public short getAltmunicion() {
        return altmunicion;
    }
	    public void setAltmunicion(short altmunicion) {
        this.altmunicion = altmunicion;
    }
	    public short getAltmaxmunicion() {
        return altmaxmunicion;
    }
	    public void setAltmaxmunicion(short altmaxmunicion) {
        this.altmaxmunicion = altmaxmunicion;
    }
	    public int getSerial() {
        return serial;
    }
	    public void setSerial(int serial) {
        this.serial = serial;
    }
	    public long getInicializaciontime() {
        return inicializaciontime;
    }
	    public void setInicializaciontime(long inicializaciontime) {
        this.inicializaciontime = inicializaciontime;
    }
	    public long getUltrevisioninv() {
        return ultrevisioninv;
    }
	    public void setUltrevisioninv(long ultrevisioninv) {
        this.ultrevisioninv = ultrevisioninv;
    }
	    public ItemStack getStack() {
        return stack;
    }
	    public void setStack(ItemStack stack) {
        this.stack = stack;
    }
	    public NBTTagCompound getNbt() {
        return nbt;
    }
	    public void setNbt(NBTTagCompound nbt) {
        this.nbt = nbt;
    }
	    public NBTTagCompound getNbtIn() {
        return nbtIn;
    }
	    public void setNbtIn(NBTTagCompound nbtIn) {
        this.nbtIn = nbtIn;
    }
	    // ###########################################################
    
}
	

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

29 minutes ago, perromercenary00 said:

    public static final PropertyInteger LEVEL = PropertyInteger.create("level", 0, 3);

A property for an Item? Why?

 

30 minutes ago, perromercenary00 said:

ICapabilitySerializable

Don't use raw types. ICapabilitySerializable is generic, so you need to specify the type.

 

As for the issue:

Capabilities on items are not synced to the client by default. If you wish to sync them you need to either use packets or override Item#getNBTShareTag and write your synced data there and Item#readNBTShareTag to read the data.

I am not too sure why the values are updated on the client after relogging though, I guess forge sends the initial capability data to the client too.

If you don't want the data to persist at all don't implement ICapabilitySerializable.

Link to comment
Share on other sites

 public static final PropertyInteger LEVEL = PropertyInteger.create("level", 0, 3); 

is a remains of another test

 

"Capabilities on items are not synced to the client by default"

i don't like this part

i need it to update the nbt values to the client so it could control animations to the item

 

wait wait wait

soo

if the capability don't update to client then  what capabilities are intended for?.

 

if i have to create a custom package system just to manually sync simple values  i could just keep using directly nbttags. this  capability code i write is simple useless

 

 

arfffffffff

 

Link to comment
Share on other sites

well originally in 1.8 i use nbttags to store data in the itemstack in the server side whit it i can control animations in the remote world 

this video was  in 1.10 or 1.11 don't remember clearly and the code is missing

 

somewhere in the time nbttags stop syncing between worlds anymore

someone tell me i have to move into capabilities

i do so

move to capabilities hoping it gonna sync  i do a lot of code then it result into not syncing

 

after all capabilities are based on nbt

    public void deserializeNBT(NBTBase nbtIn) {}

    public NBTBase serializeNBT() {}

 

packages sound promising but

lets say what happens if server send data to an item when im storing it or doping it and is no more in main hand when the remote side gets the package an there is not the item in the player hand or this itemstack has change to  to this other

 

lets say  i need to change an item in a entity inventory

 

 

 

 

 

Link to comment
Share on other sites

7 minutes ago, perromercenary00 said:

lets say what happens if server send data to an item when im storing it or doping it and is no more in main hand when the remote side gets the package an there is not the item in the player hand or this itemstack has change to  to this other

 

lets say  i need to change an item in a entity inventory

These reasons are the reasons I told you to use the share tag mechanics to sync your capabilities.

Link to comment
Share on other sites

jummm

interesting

adding this two methods to the item class enable minecraft to read the nbt and sync it to remote world

this also fix mi capabilitie issue the data is also reaching remote client

 

	    // ############################################################################################3
    /**
     * Override this method to decide what to do with the NBT data received from getNBTShareTag().
     *
     * @param stack The stack that received NBT
     * @param nbt Received NBT, can be null
     */
    @Override
    public void readNBTShareTag(ItemStack stack, @Nullable NBTTagCompound nbt)
    {
        stack.setTagCompound(nbt);
    }
    
    // ############################################################################################3
    /**
     * Override this method to change the NBT data being sent to the client.
     * You should ONLY override this when you have no other choice, as this might change behavior
     * client side!
     *
     * Note that this will sometimes be applied multiple times, the following MUST be supported:
     * Item item = stack.getItem();
     * NBTTagCompound nbtShare1 = item.getNBTShareTag(stack);
     * stack.setTagCompound(nbtShare1);
     * NBTTagCompound nbtShare2 = item.getNBTShareTag(stack);
     * assert nbtShare1.equals(nbtShare2);
     *
     * @param stack The stack to send the NBT tag for
     * @return The NBT tag
     */
    @Nullable
    @Override
    public NBTTagCompound getNBTShareTag(ItemStack stack)
    {
        return stack.getTagCompound();
    }
   
	

inow tha is working  i gona stick whit the capabilitie method

 

Thanks for the help

now i gona set the post as solved

 

 

 

 

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



×
×
  • Create New...

Important Information

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