Skip to content

Commit

Permalink
Improve Sorting of Invalid Setups + Display of Freq
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Dec 28, 2024
1 parent bf69fc0 commit 7563d3b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ private static Comparator<InfoWrapper> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Pair<PartP2PTunnel<?>, PartP2PTunnel<?>>> cir,
@Local(ordinal = 0) short frequency) {
CallbackInfoReturnable<Pair<PartP2PTunnel<?>, 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() : "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -71,10 +71,57 @@ public abstract class InfoListMixin implements AccessibleInfoList {
scrollBar.setCurrentScroll(0);
}

@Unique
private void labs$calcDistFor(Iterable<InfoWrapper> 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<InfoWrapper> 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<InfoWrapper> updateList, WidgetScrollBar scrollbar, int numEntries,
CallbackInfo ci) {
labs$checkInfo();
}

@Inject(method = "rebuild",
Expand All @@ -91,7 +138,15 @@ private void replaceOldRebuildScrollbar(Collection<InfoWrapper> updateList, Widg
@Inject(method = "update", at = @At("HEAD"))
private void calcDistanceInUpdate(Collection<InfoWrapper> 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<InfoWrapper> updateList, WidgetScrollBar scrollbar, int numEntries,
CallbackInfo ci) {
labs$checkInfo();
}

@Inject(method = "update",
Expand All @@ -105,13 +160,6 @@ private void replaceOldUpdateScrollbar(Collection<InfoWrapper> updateList, Widge
labs$properlyResetScrollbar(scrollbar, numEntries);
}

@Unique
private void calcDistFor(Iterable<InfoWrapper> 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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,6 +28,9 @@ public class InfoWrapperMixin implements AccessibleInfoWrapper {
@Final
private P2PLocation loc;

@Shadow
private short frequency;

@Unique
private double labs$distanceToPlayer = 0.0;

Expand Down Expand Up @@ -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<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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<InfoWrapper> 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<InfoWrapper> 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());
Expand Down

0 comments on commit 7563d3b

Please sign in to comment.