[Legacy] GeckoLib Fabric

[Legacy] GeckoLib Fabric

3M Downloads

Bug: Class variables are lost when using syncAnimation() [1.16.5]

NestorNey opened this issue · 1 comments

commented

I was working on an animation for an item which contained a variable with the animation to be executed, the problem is when I use syncNetwork, when running onAnimationSync the value of the variables is lost, I don't know why this happens, the bug doesn't happen on the side on the client, it only happens on the server side. The error is resolved if i enter any world from the client side, and then to the server side.

My item class:
`public static class GunItem extends Item implements IAnimatable, ISyncable {
private String regName;

	//Propiedades de animacion (GUN)___________________________________________________
	public boolean isShoting;
	//_________________________________________________________________________________
	
	//Variables de Animacion ___________________________________________________________
	private ItemAnimation RunningAnimation;
	private ItemAnimation QueueAnimation;
	//__________________________________________________________________________________
	
	private int ticksUsing;
	private boolean isUsing;

	//Variables de Geckolib_____________________________________________________________
	public static final String CONTROLLER = "Controller";
	public static final String AIM_CONTROLLER = "Aim_Controller";
	public static final int ANIM_OPEN = 0;
	private AnimationFactory factory = new AnimationFactory(this);
	//__________________________________________________________________________________

	public GunItem (String regName, Item.Properties prop) {
		super(prop);	
		setRegistryName(regName);
		GeckoLibNetwork.registerSyncable(this);

		this.isShoting = false;
		this.regName = regName;
		this.ticksUsing = 0;
		this.isUsing = false;
	}
	//Gun properties_______________________________________________________________________________
	@Override
    public boolean onEntitySwing(ItemStack stack, LivingEntity entity){
        return true;
    }
	
	@Override
	public boolean canPlayerBreakBlockWhileHolding(BlockState state, World worldIn, BlockPos pos, PlayerEntity player){
		return false;	
	}

	public String getGunName(){
		return this.regName;
	}
	//______________________________________________________________________________________________

	//Metodos de Geckolib___________________________________________________________________________
	private <P extends Item & IAnimatable> PlayState predicate(AnimationEvent<P> event) {
		return PlayState.CONTINUE;
	}

	private <P extends Item & IAnimatable> PlayState predicateAiming(AnimationEvent<P> event) {
		AnimationController controllerAiming = event.getController();
		if(controllerAiming.getAnimationState() == AnimationState.Stopped)
			controllerAiming.markNeedsReload();
			controllerAiming.setAnimation(new AnimationBuilder().addAnimation("aim.static", true));
		return PlayState.CONTINUE;
	}

	@Override
	public void registerControllers(AnimationData data) {
		AnimationController<GunItem> controller = new AnimationController<GunItem>(this, CONTROLLER, 1, this::predicate);
		AnimationController<GunItem> controllerAiming = new AnimationController<GunItem>(this, AIM_CONTROLLER, 0, this::predicateAiming);
		data.addAnimationController(controller);
		data.addAnimationController(controllerAiming);
	}

	@Override
	public AnimationFactory getFactory() {
		return this.factory;
	}

	public String getControllerName(){
		return this.CONTROLLER;
	}

	public ItemAnimation getRunningAnimation(){
		return this.RunningAnimation;
	}

	public ItemAnimation getQueueAnimation(){
		return this.QueueAnimation;
	}
	//________________________________________________________________________________________________

	//Ejecucion de animacion__________________________________________________________________________
	public void setAnimation(ItemAnimation animation){
		QueueAnimation = animation;
	}
           
            
	public void syncNetworkAnimation(PacketDistributor.PacketTarget target, int id){
		System.out.println("Gun anim: " + QueueAnimation.getDesc());
		GeckoLibNetwork.syncAnimation(target, this, id, this.ANIM_OPEN);
	}
	@Override
	public void onAnimationSync(int id, int state) {
		if (state == ANIM_OPEN) {
			
			final AnimationController<?> controller = GeckoLibUtil.getControllerForID(this.factory, id, CONTROLLER);
                           
                            /*Here the error happens, I understand that the method 
                               is called on the server side, but when passing the instance 
                               through syncAnimation() its values ​​are lost*/
			System.out.println("Gun desc: " + QueueAnimation.getDesc());
			
			controller.markNeedsReload();
			controller.setAnimation(new AnimationBuilder().addAnimation(QueueAnimation.getID(), QueueAnimation.isLoop()));
			RunningAnimation = QueueAnimation;
			
		}
	}
	//__________________________________________________________________________________________________

	@Override
	public int getItemEnchantability() {
		return 0;
	}

	@Override
	public float getDestroySpeed(ItemStack stack, BlockState block) {
		return 0F;
	}
	
	@Override
	public void addInformation(ItemStack itemstack, World world, List<ITextComponent> list, ITooltipFlag flag) {
		super.addInformation(itemstack, world, list, flag);
		list.add(new StringTextComponent("Hola"));
		list.add(new StringTextComponent("xd"));
	}
}`

I'm new to creating mods, is there a step I missed?

commented

1.16.5 is not supported anymore