Advertisement
GeradesoLukas

Untitled

Nov 15th, 2022
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 KB | None | 0 0
  1. public class LightsaberItem extends SwordItem implements IAnimatable, ISyncable{
  2.  
  3.     public AnimationFactory factory = GeckoLibUtil.createFactory(this);
  4.     public String controllerName = "controller";
  5.     private static final int OFF = 0;
  6.     private static final int ON = 1;
  7.  
  8.  
  9.  
  10.     public LightsaberItem(ToolMaterial tier, int attackDamage, float attackSpeed, Settings properties) {
  11.         super(tier, attackDamage, attackSpeed, properties);
  12.         GeckoLibNetwork.registerSyncable(this);
  13.     }
  14.  
  15.  
  16.     @Override
  17.     public void registerControllers(AnimationData data) {
  18.         data.addAnimationController(new AnimationController(this, controllerName,0,this::predicate));
  19.     }
  20.  
  21.     private <E extends IAnimatable> PlayState predicate(AnimationEvent<E> event) {
  22.         return PlayState.CONTINUE;
  23.     }
  24.  
  25.     @Override
  26.     public TypedActionResult<ItemStack> use(World level, PlayerEntity player, Hand interactionHand) {
  27.         Hand otherHand = Hand.MAIN_HAND;
  28.         if(interactionHand == Hand.MAIN_HAND) otherHand = Hand.OFF_HAND;
  29.  
  30.         if(!level.isClient()) {
  31.             ItemStack stack = player.getStackInHand(interactionHand);
  32.             ItemStack otherStack = player.getStackInHand(otherHand);
  33.             final int id = GeckoLibUtil.guaranteeIDForStack(player.getStackInHand(interactionHand), (ServerWorld) level);
  34.  
  35.             player.getItemCooldownManager().set(this,20);
  36.             if(stack.getNbt() != null) {
  37.                 if (stack.getNbt().getBoolean("ignited")){
  38.  
  39.                     stack.getNbt().putBoolean("ignited", false);
  40.                     level.playSound(null, player.getBlockPos(), ModSoundRegistry.GENERIC_LIGHTSABER_RETRACT.get(), SoundCategory.PLAYERS, 0.7f, 0.8f + (RandomUtils.nextFloat(0.1f, 0.5f)));
  41.  
  42.                     GeckoLibNetwork.syncAnimation(player, this, id, OFF);
  43.                     for (PlayerEntity otherPlayer : PlayerLookup.tracking(player)) {
  44.                         GeckoLibNetwork.syncAnimation(otherPlayer, this, id, OFF);
  45.                     }
  46.                 } else{
  47.                     stack.getNbt().putBoolean("ignited", true);
  48.                     level.playSound(null, player.getBlockPos(), ModSoundRegistry.GENERIC_LIGHTSABER_IGNITE.get(), SoundCategory.PLAYERS, 0.7f, 0.8f + (RandomUtils.nextFloat(0.1f, 0.5f)));
  49.                     GeckoLibNetwork.syncAnimation(player, this, id, ON);
  50.                     for (PlayerEntity otherPlayer : PlayerLookup.tracking(player)) {
  51.                         GeckoLibNetwork.syncAnimation(otherPlayer, this, id, ON);
  52.                     }
  53.                 }          
  54.             }
  55.              }
  56.         return TypedActionResult.pass(player.getStackInHand(interactionHand));
  57.     }
  58.  
  59.     @Override
  60.     public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) {
  61.         if(stack.getNbt() != null && stack.getNbt().getBoolean("ignited")) {
  62.             attacker.getWorld().playSound(null, target.getBlockPos(), ModSoundRegistry.GENERIC_LIGHTSABER_HIT.get(), SoundCategory.PLAYERS, 1.0f, 1.0f);
  63.         }
  64.         return super.postHit(stack, target,attacker);
  65.     }
  66.  
  67.     @Override
  68.     public AnimationFactory getFactory() {
  69.         return this.factory;
  70.     }
  71.  
  72.     @Override
  73.     public void onAnimationSync(int id, int state) {
  74.         final AnimationController controller = GeckoLibUtil.getControllerForID(this.factory, id, controllerName);
  75.         if(state == ON) {
  76.             if (controller.getAnimationState() == AnimationState.Stopped) {
  77.                 controller.markNeedsReload();
  78.                 controller.setAnimation(new AnimationBuilder().addAnimation("animation.ignite", ILoopType.EDefaultLoopTypes.HOLD_ON_LAST_FRAME));
  79.             controller.setAnimation(new AnimationBuilder().addAnimation("animation.ignite"));
  80.             }
  81.         }
  82.          else if(state == OFF) {
  83.             if (controller.getAnimationState() == AnimationState.Stopped) {
  84.                 controller.markNeedsReload();
  85.                 controller.setAnimation(new AnimationBuilder().addAnimation("animation.retract", ILoopType.EDefaultLoopTypes.PLAY_ONCE).addAnimation("animation.hilt", ILoopType.EDefaultLoopTypes.PLAY_ONCE));
  86.             }
  87.         }
  88.     }
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement