55import net .minecraft .core .particles .ParticleTypes ;
66import net .minecraft .network .chat .Component ;
77import net .minecraft .server .level .ServerLevel ;
8+ import net .minecraft .server .level .ServerPlayer ;
89import net .minecraft .sounds .SoundEvents ;
910import net .minecraft .sounds .SoundSource ;
1011import net .minecraft .world .InteractionResult ;
2728import javax .annotation .Nullable ;
2829
2930import java .util .List ;
31+ import java .util .Objects ;
32+ import java .util .Optional ;
3033import java .util .function .BiFunction ;
3134
3235public class EnderLinkerItem extends ItemBase {
@@ -44,12 +47,12 @@ public int getMaxStackSize(ItemStack stack) {
4447
4548 @ Override
4649 public boolean overrideStackedOnOther (ItemStack linker , Slot slot , ClickAction action , Player player ) {
47- return action == ClickAction .SECONDARY && tryLinkStack (player , linker , slot .getItem ());
50+ return action == ClickAction .SECONDARY && tryLinkItemInteractionTarget (player , linker , () -> slot .getItem (), null ). isPresent ( );
4851 }
4952
5053 @ Override
5154 public boolean overrideOtherStackedOnMe (ItemStack linker , ItemStack endpoint , Slot slot , ClickAction action , Player player , SlotAccess carriedAccess ) {
52- return action == ClickAction .SECONDARY && tryLinkStack (player , linker , endpoint );
55+ return action == ClickAction .SECONDARY && tryLinkItemInteractionTarget (player , linker , () -> endpoint , null ). isPresent ( );
5356 }
5457
5558 @ Override
@@ -73,20 +76,31 @@ private static LinkAttempt tryLinkBlock(Player player, ItemStack linker, Level l
7376 if (level .isClientSide ) {
7477 return LinkAttempt .SUCCESS ;
7578 }
76- return level instanceof ServerLevel && linkBlock (player , linker , endpoint , pos ) ? LinkAttempt .SUCCESS : LinkAttempt .FAILURE ;
79+ Optional <LinkedStorageService .LinkResult > result = tryLinkInteractionTarget (player , linker , endpoint , pos );
80+ return result .isPresent () && result .get () == LinkedStorageService .LinkResult .SUCCESS ? LinkAttempt .SUCCESS : LinkAttempt .FAILURE ;
7781 }
7882
79- private static boolean tryLinkStack (Player player , ItemStack linker , ItemStack endpoint ) {
80- if (!LinkedStorageService .isLinkCandidate (endpoint )) {
81- return false ;
82- }
83- linkWithFeedback (player , linker , null , (level , linkerToBind ) -> LinkedStorageService .linkWithResult (level , player .getUUID (), linkerToBind , endpoint ));
84- return true ;
83+ public static Optional <LinkedStorageService .LinkResult > tryLinkItemInteractionTarget (Player player , ItemStack linker ,
84+ ILinkedStorageItemInteractionTarget target , @ Nullable BlockPos feedbackPos ) {
85+ return tryLinkInteractionTarget (player , linker , target , feedbackPos );
8586 }
8687
87- private static boolean linkBlock (Player player , ItemStack linker , ILinkedStorageBlockEndpoint endpoint , BlockPos blockPos ) {
88- return linkWithFeedback (player , linker , blockPos , (level , linkerToBind ) -> LinkedStorageService .linkWithResult (level , player .getUUID (), linkerToBind ,
89- endpoint )) == LinkedStorageService .LinkResult .SUCCESS ;
88+ public static Optional <LinkedStorageService .LinkResult > tryLinkInteractionTarget (Player player , ItemStack linker , ILinkedStorageInteractionTarget target ,
89+ @ Nullable BlockPos feedbackPos ) {
90+ if (!target .isLinkedStorageLinkCandidate ()) {
91+ return Optional .empty ();
92+ }
93+
94+ LinkedStorageEndpointData previousEndpoint = target .getLinkedStorageEndpointData ();
95+ LinkedStorageService .LinkResult result = linkWithFeedback (player , linker , feedbackPos ,
96+ (level , linkerToBind ) -> target .link (level , player .getUUID (), linkerToBind ));
97+ if (result == LinkedStorageService .LinkResult .SUCCESS && player instanceof ServerPlayer serverPlayer ) {
98+ LinkedStorageEndpointData currentEndpoint = target .getLinkedStorageEndpointData ();
99+ if (!Objects .equals (previousEndpoint , currentEndpoint )) {
100+ target .onLinkedStorageEndpointChanged (serverPlayer , previousEndpoint , currentEndpoint );
101+ }
102+ }
103+ return Optional .of (result );
90104 }
91105
92106 private static LinkedStorageService .LinkResult linkWithFeedback (Player player , ItemStack linker , @ Nullable BlockPos blockPos ,
0 commit comments