1414import net .minecraft .util .Hand ;
1515import net .minecraft .world .World ;
1616import org .spongepowered .asm .mixin .Mixin ;
17+ import org .spongepowered .asm .mixin .Unique ;
1718import org .spongepowered .asm .mixin .injection .At ;
1819import org .spongepowered .asm .mixin .injection .Inject ;
1920import org .spongepowered .asm .mixin .injection .callback .CallbackInfoReturnable ;
@@ -29,41 +30,30 @@ protected PlayerEntityMixin(EntityType<? extends LivingEntity> entityType, World
2930 void onInteract (Entity entity , Hand hand , CallbackInfoReturnable <ActionResult > cir ) {
3031 if (entity instanceof AbstractMinecartEntity minecart ) {
3132 if (!getWorld ().isClient ()) {
32- ServerWorld world = ((ServerWorld ) entity .getWorld ());
3333 PlayerEntity player = (PlayerEntity ) (Object ) this ;
3434 ItemStack stack = player .getStackInHand (hand );
3535
3636 if (stack .isIn (Linkart .LINKERS )) {
3737 if (Linkart .UNLINKING_CARTS .containsKey (player )) {
3838 AbstractMinecartEntity unlinking = Linkart .UNLINKING_CARTS .get (player );
39- if (unlinking == null ) {
40- world .spawnParticles (ParticleTypes .ANGRY_VILLAGER , minecart .getX (), minecart .getY () + 0.2 , minecart .getZ (), 10 , 0.5 , 0.5 , 0.5 , 0.5 );
41- cir .setReturnValue (ActionResult .FAIL );
42- } else if (unlinking == minecart ) {
43- world .spawnParticles (ParticleTypes .ANGRY_VILLAGER , minecart .getX (), minecart .getY () + 0.2 , minecart .getZ (), 10 , 0.5 , 0.5 , 0.5 , 0.5 );
44- cir .setReturnValue (ActionResult .FAIL );
39+ if (unlinking == null || unlinking == minecart ) {
40+ fail (cir , minecart );
4541 } else {
4642 if (unlinking .linkart$getFollower () == minecart ) {
4743 CartUtils .unlink (minecart );
4844 cir .setReturnValue (ActionResult .SUCCESS );
4945 } else {
50- world .spawnParticles (ParticleTypes .ANGRY_VILLAGER , minecart .getX (), minecart .getY () + 0.2 , minecart .getZ (), 10 , 0.5 , 0.5 , 0.5 , 0.5 );
51- cir .setReturnValue (ActionResult .FAIL );
46+ fail (cir , minecart );
5247 }
5348 }
5449 Linkart .UNLINKING_CARTS .remove (player );
5550 } else if (Linkart .LINKING_CARTS .containsKey (player )) {
5651 AbstractMinecartEntity linkingTo = Linkart .LINKING_CARTS .get (player );
5752
58- if (linkingTo == null ) {
59- world .spawnParticles (ParticleTypes .ANGRY_VILLAGER , minecart .getX (), minecart .getY () + 0.2 , minecart .getZ (), 10 , 0.5 , 0.5 , 0.5 , 0.5 );
60- cir .setReturnValue (ActionResult .FAIL );
61- } else if (linkingTo == minecart ) {
62- world .spawnParticles (ParticleTypes .ANGRY_VILLAGER , minecart .getX (), minecart .getY () + 0.2 , minecart .getZ (), 10 , 0.5 , 0.5 , 0.5 , 0.5 );
63- cir .setReturnValue (ActionResult .FAIL );
64- } else if (Math .abs (minecart .distanceTo (linkingTo ) - 1 ) > Linkart .CONFIG .pathfindingDistance ) {
65- world .spawnParticles (ParticleTypes .ANGRY_VILLAGER , minecart .getX (), minecart .getY () + 0.2 , minecart .getZ (), 10 , 0.5 , 0.5 , 0.5 , 0.5 );
66- cir .setReturnValue (ActionResult .FAIL );
53+ if (linkingTo == null || linkingTo == minecart ||
54+ minecart .linkart$getFollower () == linkingTo ||
55+ Math .abs (minecart .distanceTo (linkingTo ) - 1 ) > Linkart .CONFIG .pathfindingDistance ) {
56+ fail (cir , minecart );
6757 } else {
6858 if (!player .isCreative ()) stack .decrement (1 );
6959 CartUtils .link (minecart , linkingTo , stack );
@@ -72,15 +62,25 @@ void onInteract(Entity entity, Hand hand, CallbackInfoReturnable<ActionResult> c
7262 Linkart .LINKING_CARTS .remove (player );
7363 } else if (minecart .linkart$getFollower () != null ) {
7464 Linkart .UNLINKING_CARTS .put (player , minecart );
75- world .spawnParticles (ParticleTypes .HAPPY_VILLAGER , minecart .getX (), minecart .getY () + 0.2 , minecart .getZ (), 10 , 0.5 , 0.5 , 0.5 , 0.5 );
76- cir .setReturnValue (ActionResult .SUCCESS );
65+ success (cir , minecart );
7766 } else {
7867 Linkart .LINKING_CARTS .put (player , minecart );
79- world .spawnParticles (ParticleTypes .HAPPY_VILLAGER , minecart .getX (), minecart .getY () + 0.2 , minecart .getZ (), 10 , 0.5 , 0.5 , 0.5 , 0.5 );
80- cir .setReturnValue (ActionResult .SUCCESS );
68+ success (cir , minecart );
8169 }
8270 }
8371 }
8472 }
8573 }
74+
75+ @ Unique
76+ private static void fail (CallbackInfoReturnable <ActionResult > cir , AbstractMinecartEntity minecart ) {
77+ ((ServerWorld ) minecart .getWorld ()).spawnParticles (ParticleTypes .ANGRY_VILLAGER , minecart .getX (), minecart .getY () + 0.2 , minecart .getZ (), 10 , 0.5 , 0.5 , 0.5 , 0.5 );
78+ cir .setReturnValue (ActionResult .FAIL );
79+ }
80+
81+ @ Unique
82+ private static void success (CallbackInfoReturnable <ActionResult > cir , AbstractMinecartEntity minecart ) {
83+ ((ServerWorld ) minecart .getWorld ()).spawnParticles (ParticleTypes .HAPPY_VILLAGER , minecart .getX (), minecart .getY () + 0.2 , minecart .getZ (), 10 , 0.5 , 0.5 , 0.5 , 0.5 );
84+ cir .setReturnValue (ActionResult .SUCCESS );
85+ }
8686}
0 commit comments