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 ;
2930import javax .annotation .Nullable ;
3031
3132import java .util .List ;
33+ import java .util .Objects ;
34+ import java .util .Optional ;
3235import java .util .function .BiFunction ;
3336
3437public class EnderLinkerItem extends ItemBase {
@@ -46,12 +49,12 @@ public int getMaxStackSize(ItemStack stack) {
4649
4750 @ Override
4851 public boolean overrideStackedOnOther (ItemStack linker , Slot slot , ClickAction action , Player player ) {
49- return action == ClickAction .SECONDARY && tryLinkStack (player , linker , slot .getItem ());
52+ return action == ClickAction .SECONDARY && tryLinkItemEndpoint (player , linker , slot .getItem ()). isPresent ( );
5053 }
5154
5255 @ Override
5356 public boolean overrideOtherStackedOnMe (ItemStack linker , ItemStack endpoint , Slot slot , ClickAction action , Player player , SlotAccess carriedAccess ) {
54- return action == ClickAction .SECONDARY && tryLinkStack (player , linker , endpoint );
57+ return action == ClickAction .SECONDARY && tryLinkItemEndpoint (player , linker , endpoint ). isPresent ( );
5558 }
5659
5760 @ Override
@@ -75,23 +78,38 @@ private static TriState tryLinkBlock(Player player, ItemStack linker, Level leve
7578 if (level .isClientSide ()) {
7679 return TriState .TRUE ;
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 TriState .FALSE ;
8084 }
8185 return TriState .TRUE ;
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