Skip to content

Commit 7f794b6

Browse files
committed
chore: 🔧 Port linked storage to SB Create integration
1 parent ea0a1e8 commit 7f794b6

7 files changed

Lines changed: 111 additions & 15 deletions

File tree

‎src/main/java/net/p3pp3rf1y/sophisticatedcore/compat/create/MountedStorageBase.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,8 @@ protected void onOpen(ServerLevel level, Vec3 pos) {
118118
public void onContraptionDestroyed() {
119119

120120
}
121+
122+
public void onContraptionRemoved() {
123+
124+
}
121125
}

‎src/main/java/net/p3pp3rf1y/sophisticatedcore/compat/create/MountedStorageSettingsContainerMenuBase.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public int getContraptionEntityId() {
4040
return contraptionEntityId;
4141
}
4242

43-
private void sendStorageSettingsToClient() {
43+
protected void sendStorageSettingsToClient() {
4444
if (player.level().isClientSide()) {
4545
return;
4646
}

‎src/main/java/net/p3pp3rf1y/sophisticatedcore/linkedstorage/EnderLinkerItem.java‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.network.chat.Component;
88
import net.minecraft.resources.ResourceKey;
99
import net.minecraft.server.level.ServerLevel;
10+
import net.minecraft.server.level.ServerPlayer;
1011
import net.minecraft.sounds.SoundEvents;
1112
import net.minecraft.sounds.SoundSource;
1213
import net.minecraft.world.InteractionResult;
@@ -32,6 +33,8 @@
3233

3334
import javax.annotation.Nullable;
3435

36+
import java.util.Objects;
37+
import java.util.Optional;
3538
import java.util.function.BiFunction;
3639
import 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,
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
package net.p3pp3rf1y.sophisticatedcore.linkedstorage;
22

3+
import net.minecraft.server.level.ServerLevel;
4+
import net.minecraft.world.item.ItemStack;
5+
36
import javax.annotation.Nullable;
47

5-
public interface ILinkedStorageBlockEndpoint {
8+
import java.util.UUID;
9+
10+
public interface ILinkedStorageBlockEndpoint extends ILinkedStorageInteractionTarget {
11+
@Override
12+
default boolean isLinkedStorageLinkCandidate() {
13+
return true;
14+
}
15+
616
@Nullable
17+
@Override
718
LinkedStorageEndpointData getLinkedStorageEndpointData();
819

920
ILinkedStorageEndpointAdapter<ILinkedStorageBlockEndpoint> getLinkedStorageBlockEndpointAdapter();
21+
22+
@Override
23+
default LinkedStorageService.LinkResult link(ServerLevel level, UUID playerId, ItemStack linker) {
24+
return LinkedStorageService.linkWithResult(level, playerId, linker, this);
25+
}
1026
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.p3pp3rf1y.sophisticatedcore.linkedstorage;
2+
3+
import net.minecraft.server.level.ServerLevel;
4+
import net.minecraft.server.level.ServerPlayer;
5+
import net.minecraft.world.item.ItemStack;
6+
7+
import javax.annotation.Nullable;
8+
9+
import java.util.UUID;
10+
11+
public interface ILinkedStorageInteractionTarget {
12+
boolean isLinkedStorageLinkCandidate();
13+
14+
@Nullable
15+
LinkedStorageEndpointData getLinkedStorageEndpointData();
16+
17+
LinkedStorageService.LinkResult link(ServerLevel level, UUID playerId, ItemStack linker);
18+
19+
default void onLinkedStorageEndpointChanged(ServerPlayer player, @Nullable LinkedStorageEndpointData previousEndpoint,
20+
@Nullable LinkedStorageEndpointData currentEndpoint) {
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.p3pp3rf1y.sophisticatedcore.linkedstorage;
2+
3+
import net.minecraft.server.level.ServerLevel;
4+
import net.minecraft.world.item.ItemStack;
5+
import net.p3pp3rf1y.sophisticatedcore.init.ModCoreDataComponents;
6+
7+
import java.util.UUID;
8+
9+
public interface ILinkedStorageItemInteractionTarget extends ILinkedStorageInteractionTarget {
10+
ItemStack getLinkedStorageItem();
11+
12+
@Override
13+
default boolean isLinkedStorageLinkCandidate() {
14+
return LinkedStorageService.isLinkCandidate(getLinkedStorageItem());
15+
}
16+
17+
@Override
18+
default LinkedStorageEndpointData getLinkedStorageEndpointData() {
19+
return getLinkedStorageItem().get(ModCoreDataComponents.LINKED_STORAGE_ENDPOINT);
20+
}
21+
22+
@Override
23+
default LinkedStorageService.LinkResult link(ServerLevel level, UUID playerId, ItemStack linker) {
24+
return LinkedStorageService.linkWithResult(level, playerId, linker, getLinkedStorageItem());
25+
}
26+
}

‎src/main/java/net/p3pp3rf1y/sophisticatedcore/mixin/create/MixinAbstractContraptionEntity.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@ public class MixinAbstractContraptionEntity {
1818
@Inject(method = "remove", at = @At("HEAD"))
1919
private void cleanupMountedStoragesOnDestroy(Entity.RemovalReason removalReason, CallbackInfo ci) {
2020
AbstractContraptionEntity contraptionEntity = (AbstractContraptionEntity) (Object) this;
21-
if (contraptionEntity.level().isClientSide() || contraption == null || contraption.disassembled || removalReason != Entity.RemovalReason.KILLED) {
21+
if (contraptionEntity.level().isClientSide() || contraption == null) {
22+
return;
23+
}
24+
25+
contraption.getStorage().getAllItemStorages().values().forEach(storage -> {
26+
if (storage instanceof MountedStorageBase mountedStorage) {
27+
mountedStorage.onContraptionRemoved();
28+
}
29+
});
30+
31+
if (contraption.disassembled || removalReason != Entity.RemovalReason.KILLED) {
2232
return;
2333
}
2434

0 commit comments

Comments
 (0)