77import net .minecraft .network .chat .Component ;
88import net .minecraft .resources .ResourceKey ;
99import net .minecraft .server .level .ServerLevel ;
10+ import net .minecraft .server .level .ServerPlayer ;
1011import net .minecraft .sounds .SoundEvents ;
1112import net .minecraft .sounds .SoundSource ;
1213import net .minecraft .world .InteractionResult ;
3233
3334import javax .annotation .Nullable ;
3435
36+ import java .util .Objects ;
37+ import java .util .Optional ;
3538import java .util .function .BiFunction ;
3639import java .util .function .Consumer ;
3740
@@ -50,12 +53,12 @@ public int getMaxStackSize(ItemStack stack) {
5053
5154 @ Override
5255 public boolean overrideStackedOnOther (ItemStack linker , Slot slot , ClickAction action , Player player ) {
53- return action == ClickAction .SECONDARY && tryLinkStack (player , linker , slot .getItem ());
56+ return action == ClickAction .SECONDARY && tryLinkItemEndpoint (player , linker , slot .getItem ()). isPresent ( );
5457 }
5558
5659 @ Override
5760 public boolean overrideOtherStackedOnMe (ItemStack linker , ItemStack endpoint , Slot slot , ClickAction action , Player player , SlotAccess carriedAccess ) {
58- return action == ClickAction .SECONDARY && tryLinkStack (player , linker , endpoint );
61+ return action == ClickAction .SECONDARY && tryLinkItemEndpoint (player , linker , endpoint ). isPresent ( );
5962 }
6063
6164 @ Override
@@ -75,23 +78,38 @@ private static InteractionResult tryLinkBlock(Player player, ItemStack linker, L
7578 if (level .isClientSide ()) {
7679 return InteractionResult .SUCCESS ;
7780 }
78- if (!(level instanceof ServerLevel ) || !linkBlock (player , linker , endpoint , pos )) {
81+ Optional <LinkedStorageService .LinkResult > result = tryLinkInteractionTarget (player , linker , endpoint , pos );
82+ if (result .isEmpty () || result .get () != LinkedStorageService .LinkResult .SUCCESS ) {
7983 return InteractionResult .FAIL ;
8084 }
8185 return InteractionResult .SUCCESS ;
8286 }
8387
84- private static boolean tryLinkStack (Player player , ItemStack linker , ItemStack endpoint ) {
85- if (!LinkedStorageService .isLinkCandidate (endpoint )) {
86- return false ;
87- }
88- linkWithFeedback (player , linker , null , (level , linkerToBind ) -> LinkedStorageService .linkWithResult (level , player .getUUID (), linkerToBind , endpoint ));
89- return true ;
88+ public static Optional <LinkedStorageService .LinkResult > tryLinkItemInteractionTarget (Player player , ItemStack linker ,
89+ ILinkedStorageItemInteractionTarget target , @ Nullable BlockPos feedbackPos ) {
90+ return tryLinkInteractionTarget (player , linker , target , feedbackPos );
91+ }
92+
93+ private static Optional <LinkedStorageService .LinkResult > tryLinkItemEndpoint (Player player , ItemStack linker , ItemStack endpoint ) {
94+ return tryLinkItemInteractionTarget (player , linker , () -> endpoint , null );
9095 }
9196
92- private static boolean linkBlock (Player player , ItemStack linker , ILinkedStorageBlockEndpoint endpoint , BlockPos blockPos ) {
93- return linkWithFeedback (player , linker , blockPos , (level , linkerToBind ) -> LinkedStorageService .linkWithResult (level , player .getUUID (), linkerToBind ,
94- endpoint )) == LinkedStorageService .LinkResult .SUCCESS ;
97+ public static Optional <LinkedStorageService .LinkResult > tryLinkInteractionTarget (Player player , ItemStack linker , ILinkedStorageInteractionTarget target ,
98+ @ Nullable BlockPos feedbackPos ) {
99+ if (!target .isLinkedStorageLinkCandidate ()) {
100+ return Optional .empty ();
101+ }
102+
103+ LinkedStorageEndpointData previousEndpoint = target .getLinkedStorageEndpointData ();
104+ LinkedStorageService .LinkResult result = linkWithFeedback (player , linker , feedbackPos ,
105+ (level , linkerToBind ) -> target .link (level , player .getUUID (), linkerToBind ));
106+ if (result == LinkedStorageService .LinkResult .SUCCESS && player instanceof ServerPlayer serverPlayer ) {
107+ LinkedStorageEndpointData currentEndpoint = target .getLinkedStorageEndpointData ();
108+ if (!Objects .equals (previousEndpoint , currentEndpoint )) {
109+ target .onLinkedStorageEndpointChanged (serverPlayer , previousEndpoint , currentEndpoint );
110+ }
111+ }
112+ return Optional .of (result );
95113 }
96114
97115 private static LinkedStorageService .LinkResult linkWithFeedback (Player player , ItemStack linker , @ Nullable BlockPos blockPos ,
0 commit comments