diff --git a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortModes.java b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortModes.java index bf22233..81da0f9 100644 --- a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortModes.java +++ b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortModes.java @@ -54,6 +54,11 @@ private static Comparator compDefault(InfoWrapper selected) { if (a.getFrequency() == 0) return -1; if (b.getFrequency() == 0) return 1; + // Errors next + if (a.getError()) return -1; + if (b.getError()) return 1; + + // Else, sort by frequency return b.getFrequency() - a.getFrequency(); } diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GridServerCacheMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GridServerCacheMixin.java index e755fec..f9d4aab 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GridServerCacheMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GridServerCacheMixin.java @@ -15,9 +15,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; -import com.llamalad7.mixinextras.sugar.Local; import com.nomiceu.nomilabs.integration.betterp2p.AccessibleGridServerCache; import com.projecturanus.betterp2p.network.data.GridServerCache; import com.projecturanus.betterp2p.network.data.P2PLocation; @@ -109,17 +107,26 @@ private void properlyChangeAllP2PTypes(P2PLocation p2p, TunnelInfo newType, Call @Inject(method = "linkP2P", at = @At(value = "INVOKE", - target = "Lappeng/me/cache/P2PCache;getInputs(SLjava/lang/Class;)Lappeng/me/cache/helpers/TunnelCollection;", - ordinal = 0), - locals = LocalCapture.CAPTURE_FAILEXCEPTION, + target = "Lappeng/parts/p2p/PartP2PTunnel;getFrequency()S"), require = 1, cancellable = true) private void properlyLinkP2P(P2PLocation inputIndex, P2PLocation outputIndex, - CallbackInfoReturnable, PartP2PTunnel>> cir, - @Local(ordinal = 0) short frequency) { + CallbackInfoReturnable, PartP2PTunnel>> cir) { var input = listP2P.get(inputIndex); var output = listP2P.get(outputIndex); + var frequency = input.getFrequency(); + try { + if (frequency == 0 || input.isOutput()) { + // Generate new frequency + // Use AE2's way instead of Better P2P's way + frequency = input.getProxy().getP2P().newFrequency(); + } + } catch (GridAccessException e) { + cir.setReturnValue(null); + return; + } + // Perform the link var inputResult = updateP2P(inputIndex, input, frequency, false, input.hasCustomInventoryName() ? input.getCustomInventoryName() : ""); diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java index bbd20a1..bc82aad 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java @@ -9,6 +9,7 @@ import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.nomiceu.nomilabs.integration.betterp2p.AccessibleGuiAdvancedMemoryCard; @@ -26,7 +27,8 @@ /** * Allows accessing needed functions and fields, and initializes playerPos field in InfoList. Also fills up - * LabsClientCache, calls proper resetting of scrollbar in custom places, and allows arrows to scroll. + * LabsClientCache, cancels existing checks for invalid setups (moved to infoList, right before sorting), + * calls proper resetting of scrollbar in custom places, and allows arrows to scroll. */ @Mixin(value = GuiAdvancedMemoryCard.class, remap = false) public abstract class GuiAdvancedMemoryCardMixin extends GuiScreen implements AccessibleGuiAdvancedMemoryCard { @@ -89,6 +91,26 @@ private void properlySetScrollbarInit(CallbackInfo ci) { labs$properlyResetScrollbar(); } + @Redirect(method = "initGui", + at = @At(value = "INVOKE", + target = "Lcom/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard;checkInfo()V", + remap = false), + require = 1, + remap = true) + private void cancelExistingChecksInit(GuiAdvancedMemoryCard instance) {} + + @Redirect(method = "refreshInfo", + at = @At(value = "INVOKE", + target = "Lcom/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard;checkInfo()V"), + require = 1) + private void cancelExistingChecksRefresh(GuiAdvancedMemoryCard instance) {} + + @Redirect(method = "updateInfo", + at = @At(value = "INVOKE", + target = "Lcom/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard;checkInfo()V"), + require = 1) + private void cancelExistingChecksUpdate(GuiAdvancedMemoryCard instance) {} + @Inject(method = "mouseClicked", at = @At(value = "INVOKE", target = "Lcom/projecturanus/betterp2p/client/gui/InfoList;refilter()V", diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java index 5e157f3..a879e19 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java @@ -27,7 +27,7 @@ import com.projecturanus.betterp2p.network.data.P2PLocation; /** - * Handles updating infos' distance to player, and allows for custom sorting. + * Handles updating infos' distance to player, and allows for custom sorting, including with error info. * Improves handling of scrollbar after refresh, reload, or refilter. */ @Mixin(value = InfoList.class, remap = false) @@ -55,7 +55,7 @@ public abstract class InfoListMixin implements AccessibleInfoList { @Override public void labs$setPlayerPos(Vec3d pos) { labs$playerPos = pos; - calcDistFor(masterMap.values()); + labs$calcDistFor(masterMap.values()); } @Unique @@ -71,10 +71,57 @@ public abstract class InfoListMixin implements AccessibleInfoList { scrollBar.setCurrentScroll(0); } + @Unique + private void labs$calcDistFor(Iterable infos) { + for (InfoWrapper info : infos) { + ((AccessibleInfoWrapper) (Object) info).labs$calculateDistance(labs$playerPos); + } + } + + @Unique + private void labs$checkInfo() { + // Do for all, as this is pre-filtering + // We can use sorted list here, as values have been added, but not sorted yet + for (InfoWrapper info : labs$getThis().getSorted()) { + // Ignore unbound, classified as different error type + if (info.getFrequency() == 0) { + info.setError(false); + continue; + } + + // Search for same freq, and opposite output state (output search for input, etc.) + info.setError(!labs$has(info.getFrequency(), !info.getOutput())); + } + } + + @Unique + private boolean labs$has(int freq, boolean isOutput) { + for (InfoWrapper info : labs$getThis().getSorted()) { + if (info.getFrequency() == freq && info.getOutput() == isOutput) + return true; + } + return false; + } + + @Inject(method = "refresh", + at = @At(value = "INVOKE", target = "Lcom/projecturanus/betterp2p/client/gui/InfoList;resort()V"), + require = 1) + private void checkInfoBeforeSort1(CallbackInfo ci) { + labs$checkInfo(); + } + @Inject(method = "rebuild", at = @At("HEAD")) private void calcDistanceInRebuild(Collection updateList, WidgetScrollBar scrollbar, int numEntries, CallbackInfo ci) { - calcDistFor(updateList); + labs$calcDistFor(updateList); + } + + @Inject(method = "rebuild", + at = @At(value = "INVOKE", target = "Lcom/projecturanus/betterp2p/client/gui/InfoList;resort()V"), + require = 1) + private void checkInfoBeforeSort2(Collection updateList, WidgetScrollBar scrollbar, int numEntries, + CallbackInfo ci) { + labs$checkInfo(); } @Inject(method = "rebuild", @@ -91,7 +138,15 @@ private void replaceOldRebuildScrollbar(Collection updateList, Widg @Inject(method = "update", at = @At("HEAD")) private void calcDistanceInUpdate(Collection updateList, WidgetScrollBar scrollbar, int numEntries, CallbackInfo ci) { - calcDistFor(updateList); + labs$calcDistFor(updateList); + } + + @Inject(method = "update", + at = @At(value = "INVOKE", target = "Lcom/projecturanus/betterp2p/client/gui/InfoList;resort()V"), + require = 1) + private void checkInfoBeforeSort3(Collection updateList, WidgetScrollBar scrollbar, int numEntries, + CallbackInfo ci) { + labs$checkInfo(); } @Inject(method = "update", @@ -105,13 +160,6 @@ private void replaceOldUpdateScrollbar(Collection updateList, Widge labs$properlyResetScrollbar(scrollbar, numEntries); } - @Unique - private void calcDistFor(Iterable infos) { - for (InfoWrapper info : infos) { - ((AccessibleInfoWrapper) (Object) info).labs$calculateDistance(labs$playerPos); - } - } - @Inject(method = "resort", at = @At("HEAD"), cancellable = true) private void customSortLogic(CallbackInfo ci) { labs$getThis().getSorted().sort(SortModes.DEFAULT.getComp(getSelectedInfo())); diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoWrapperMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoWrapperMixin.java index da02e49..c0c6997 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoWrapperMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoWrapperMixin.java @@ -10,14 +10,16 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import com.nomiceu.nomilabs.integration.betterp2p.AccessibleInfoWrapper; +import com.nomiceu.nomilabs.util.LabsTranslate; import com.projecturanus.betterp2p.client.gui.InfoWrapper; import com.projecturanus.betterp2p.network.data.P2PInfo; import com.projecturanus.betterp2p.network.data.P2PLocation; /** - * Allows saving of each P2P's distance to the player. + * Allows saving of each P2P's distance to the player, and improves display of frequencies. */ @Mixin(value = InfoWrapper.class, remap = false) public class InfoWrapperMixin implements AccessibleInfoWrapper { @@ -26,6 +28,9 @@ public class InfoWrapperMixin implements AccessibleInfoWrapper { @Final private P2PLocation loc; + @Shadow + private short frequency; + @Unique private double labs$distanceToPlayer = 0.0; @@ -72,6 +77,21 @@ private void provideChannelInfo(P2PInfo info, CallbackInfo ci) { labs$getThis().getHoverInfo().add(4, TextFormatting.LIGHT_PURPLE + channels); } + @Inject(method = "getFreqDisplay", at = @At("HEAD"), cancellable = true) + private void getLabsFreqDisplay(CallbackInfoReturnable cir) { + // Use AE2's Method + StringBuilder builder = new StringBuilder(); + builder.append(LabsTranslate.translate("item.advanced_memory_card.selected")) + .append(" "); + + if (frequency == 0) + builder.append(LabsTranslate.translate("gui.advanced_memory_card.desc.not_set")); + else + builder.append(String.format("%04X", frequency)); + + cir.setReturnValue(builder.toString()); + } + @Unique private InfoWrapper labs$getThis() { return ((InfoWrapper) (Object) this); diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/WidgetP2PColumnMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/WidgetP2PColumnMixin.java index aa86dc1..4bc3327 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/WidgetP2PColumnMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/WidgetP2PColumnMixin.java @@ -6,7 +6,6 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import com.nomiceu.nomilabs.integration.betterp2p.LabsBetterMemoryCardModes; import com.nomiceu.nomilabs.network.LabsNetworkHandler; @@ -21,8 +20,7 @@ import kotlin.jvm.functions.Function0; /** - * Trims text before renaming, handles add as input/output, properly refreshes renames in gui, - * properly searches for inputs/outputs. + * Trims text before renaming, handles add as input/output, properly refreshes renames in gui. */ @Mixin(value = WidgetP2PColumn.class, remap = false) public class WidgetP2PColumnMixin { @@ -39,28 +37,6 @@ public class WidgetP2PColumnMixin { @Final private IGuiTextField renameBar; - @Inject(method = "findInput", at = @At("HEAD"), cancellable = true) - private void findInputInAll(Short frequency, CallbackInfoReturnable cir) { - for (InfoWrapper info : infos.getSorted()) { - if (info.getFrequency() == frequency && !info.getOutput()) { - cir.setReturnValue(info); - return; - } - } - cir.setReturnValue(null); - } - - @Inject(method = "findOutput", at = @At("HEAD"), cancellable = true) - private void findOutputInAll(Short frequency, CallbackInfoReturnable cir) { - for (InfoWrapper info : infos.getSorted()) { - if (info.getFrequency() == frequency && info.getOutput()) { - cir.setReturnValue(info); - return; - } - } - cir.setReturnValue(null); - } - @Inject(method = "finishRename", at = @At("HEAD")) private void trimText(CallbackInfo ci) { renameBar.setText(renameBar.getText().trim());