Fix interface terminal spamming update packets due to matching the wrong name.

This commit is contained in:
Sebastian Hartte
2020-06-18 21:32:03 +02:00
parent c6af3d9a70
commit 749742a8aa
3 changed files with 34 additions and 38 deletions
@@ -24,7 +24,6 @@ import com.google.common.collect.HashMultimap;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT; import net.minecraft.nbt.ListNBT;
@@ -46,9 +45,6 @@ public class GuiInterfaceTerminal extends AEBaseGui<ContainerInterfaceTerminal>
private static final int LINES_ON_PAGE = 6; private static final int LINES_ON_PAGE = 6;
// TODO: copied from GuiMEMonitorable. It looks not changed, maybe unneeded?
private final int offsetX = 9;
private final HashMap<Long, ClientDCInternalInv> byId = new HashMap<>(); private final HashMap<Long, ClientDCInternalInv> byId = new HashMap<>();
private final HashMultimap<String, ClientDCInternalInv> byName = HashMultimap.create(); private final HashMultimap<String, ClientDCInternalInv> byName = HashMultimap.create();
private final ArrayList<String> names = new ArrayList<>(); private final ArrayList<String> names = new ArrayList<>();
@@ -76,8 +72,7 @@ public class GuiInterfaceTerminal extends AEBaseGui<ContainerInterfaceTerminal>
this.getScrollBar().setHeight(106); this.getScrollBar().setHeight(106);
this.getScrollBar().setTop(18); this.getScrollBar().setTop(18);
this.searchField = new MEGuiTextField(this.font, this.guiLeft + Math.max(104, this.offsetX), this.guiTop + 4, this.searchField = new MEGuiTextField(this.font, this.guiLeft + 104, this.guiTop + 4, 65, 12);
65, 12);
this.searchField.setEnableBackgroundDrawing(false); this.searchField.setEnableBackgroundDrawing(false);
this.searchField.setMaxStringLength(25); this.searchField.setMaxStringLength(25);
this.searchField.setTextColor(0xFFFFFF); this.searchField.setTextColor(0xFFFFFF);
@@ -92,12 +87,7 @@ public class GuiInterfaceTerminal extends AEBaseGui<ContainerInterfaceTerminal>
final int ex = this.getScrollBar().getCurrentScroll(); final int ex = this.getScrollBar().getCurrentScroll();
final Iterator<Slot> o = this.container.inventorySlots.iterator(); this.container.inventorySlots.removeIf(slot -> slot instanceof SlotDisconnected);
while (o.hasNext()) {
if (o.next() instanceof SlotDisconnected) {
o.remove();
}
}
int offset = 17; int offset = 17;
for (int x = 0; x < LINES_ON_PAGE && ex + x < this.lines.size(); x++) { for (int x = 0; x < LINES_ON_PAGE && ex + x < this.lines.size(); x++) {
@@ -186,8 +176,8 @@ public class GuiInterfaceTerminal extends AEBaseGui<ContainerInterfaceTerminal>
try { try {
final long id = Long.parseLong(key.substring(1), Character.MAX_RADIX); final long id = Long.parseLong(key.substring(1), Character.MAX_RADIX);
final CompoundNBT invData = in.getCompound(key); final CompoundNBT invData = in.getCompound(key);
final ClientDCInternalInv current = this.getById(id, invData.getLong("sortBy"), ITextComponent un = ITextComponent.Serializer.fromJson(invData.getString("un"));
invData.getString("un")); final ClientDCInternalInv current = this.getById(id, invData.getLong("sortBy"), un);
for (int x = 0; x < current.getInventory().getSlots(); x++) { for (int x = 0; x < current.getInventory().getSlots(); x++) {
final String which = Integer.toString(x); final String which = Integer.toString(x);
@@ -243,8 +233,8 @@ public class GuiInterfaceTerminal extends AEBaseGui<ContainerInterfaceTerminal>
} }
// if found, filter skipped or machine name matching the search term, add it // if found, filter skipped or machine name matching the search term, add it
if (found || entry.getName().toLowerCase().contains(searchFilterLowerCase)) { if (found || entry.getSearchName().contains(searchFilterLowerCase)) {
this.byName.put(entry.getName(), entry); this.byName.put(entry.getFormattedName(), entry);
cachedSearch.add(entry); cachedSearch.add(entry);
} else { } else {
cachedSearch.remove(entry); cachedSearch.remove(entry);
@@ -262,8 +252,7 @@ public class GuiInterfaceTerminal extends AEBaseGui<ContainerInterfaceTerminal>
for (final String n : this.names) { for (final String n : this.names) {
this.lines.add(n); this.lines.add(n);
final ArrayList<ClientDCInternalInv> clientInventories = new ArrayList<>(); List<ClientDCInternalInv> clientInventories = new ArrayList<>(this.byName.get(n));
clientInventories.addAll(this.byName.get(n));
Collections.sort(clientInventories); Collections.sort(clientInventories);
this.lines.addAll(clientInventories); this.lines.addAll(clientInventories);
@@ -337,11 +326,11 @@ public class GuiInterfaceTerminal extends AEBaseGui<ContainerInterfaceTerminal>
return this.names.size() + this.byId.size(); return this.names.size() + this.byId.size();
} }
private ClientDCInternalInv getById(final long id, final long sortBy, final String string) { private ClientDCInternalInv getById(final long id, final long sortBy, final ITextComponent name) {
ClientDCInternalInv o = this.byId.get(id); ClientDCInternalInv o = this.byId.get(id);
if (o == null) { if (o == null) {
this.byId.put(id, o = new ClientDCInternalInv(9, id, sortBy, string)); this.byId.put(id, o = new ClientDCInternalInv(9, id, sortBy, name));
this.refreshList = true; this.refreshList = true;
} }
@@ -20,31 +20,33 @@ package appeng.client.me;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import net.minecraft.client.resources.I18n; import net.minecraft.util.text.ITextComponent;
import appeng.tile.inventory.AppEngInternalInventory; import appeng.tile.inventory.AppEngInternalInventory;
public class ClientDCInternalInv implements Comparable<ClientDCInternalInv> { public class ClientDCInternalInv implements Comparable<ClientDCInternalInv> {
private final String unlocalizedName; private final String searchName;
private final String formattedName;
private final AppEngInternalInventory inventory; private final AppEngInternalInventory inventory;
private final long id; private final long id;
private final long sortBy; private final long sortBy;
public ClientDCInternalInv(final int size, final long id, final long sortBy, final String unlocalizedName) { public ClientDCInternalInv(final int size, final long id, final long sortBy, final ITextComponent name) {
this.inventory = new AppEngInternalInventory(null, size); this.inventory = new AppEngInternalInventory(null, size);
this.unlocalizedName = unlocalizedName; this.searchName = name.getString().toLowerCase();
this.formattedName = name.getFormattedText();
this.id = id; this.id = id;
this.sortBy = sortBy; this.sortBy = sortBy;
} }
public String getName() { public String getSearchName() {
final String s = I18n.format(this.unlocalizedName + ".name"); return searchName;
if (s.equals(this.unlocalizedName + ".name")) { }
return I18n.format(this.unlocalizedName);
} public String getFormattedName() {
return s; return formattedName;
} }
@Override @Override
@@ -59,4 +61,8 @@ public class ClientDCInternalInv implements Comparable<ClientDCInternalInv> {
public long getId() { public long getId() {
return this.id; return this.id;
} }
public boolean matchesSearch(String searchFilterLowerCase) {
return this.searchName.contains(searchFilterLowerCase);
}
} }
@@ -30,6 +30,7 @@ import net.minecraft.inventory.container.ContainerType;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer; import net.minecraft.network.PacketBuffer;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import appeng.api.config.SecurityPermissions; import appeng.api.config.SecurityPermissions;
@@ -127,7 +128,7 @@ public final class ContainerInterfaceTerminal extends AEBaseContainer {
missing = true; missing = true;
} else { } else {
final DualityInterface dual = ih.getInterfaceDuality(); final DualityInterface dual = ih.getInterfaceDuality();
if (!t.unlocalizedName.equals(dual.getTermName())) { if (!t.name.equals(dual.getTermName())) {
missing = true; missing = true;
} }
} }
@@ -150,7 +151,7 @@ public final class ContainerInterfaceTerminal extends AEBaseContainer {
missing = true; missing = true;
} else { } else {
final DualityInterface dual = ih.getInterfaceDuality(); final DualityInterface dual = ih.getInterfaceDuality();
if (!t.unlocalizedName.equals(dual.getTermName())) { if (!t.name.equals(dual.getTermName())) {
missing = true; missing = true;
} }
} }
@@ -291,7 +292,7 @@ public final class ContainerInterfaceTerminal extends AEBaseContainer {
final IInterfaceHost ih = (IInterfaceHost) gn.getMachine(); final IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
final DualityInterface dual = ih.getInterfaceDuality(); final DualityInterface dual = ih.getInterfaceDuality();
if (gn.isActive() && dual.getConfigManager().getSetting(Settings.INTERFACE_TERMINAL) == YesNo.YES) { if (gn.isActive() && dual.getConfigManager().getSetting(Settings.INTERFACE_TERMINAL) == YesNo.YES) {
this.diList.put(ih, new InvTracker(dual, dual.getPatterns(), dual.getTermName().getString())); this.diList.put(ih, new InvTracker(dual, dual.getPatterns(), dual.getTermName()));
} }
} }
@@ -299,7 +300,7 @@ public final class ContainerInterfaceTerminal extends AEBaseContainer {
final IInterfaceHost ih = (IInterfaceHost) gn.getMachine(); final IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
final DualityInterface dual = ih.getInterfaceDuality(); final DualityInterface dual = ih.getInterfaceDuality();
if (gn.isActive() && dual.getConfigManager().getSetting(Settings.INTERFACE_TERMINAL) == YesNo.YES) { if (gn.isActive() && dual.getConfigManager().getSetting(Settings.INTERFACE_TERMINAL) == YesNo.YES) {
this.diList.put(ih, new InvTracker(dual, dual.getPatterns(), dual.getTermName().getString())); this.diList.put(ih, new InvTracker(dual, dual.getPatterns(), dual.getTermName()));
} }
} }
} }
@@ -332,7 +333,7 @@ public final class ContainerInterfaceTerminal extends AEBaseContainer {
if (tag.isEmpty()) { if (tag.isEmpty()) {
tag.putLong("sortBy", inv.sortBy); tag.putLong("sortBy", inv.sortBy);
tag.putString("un", inv.unlocalizedName); tag.putString("un", ITextComponent.Serializer.toJson(inv.name));
} }
for (int x = 0; x < length; x++) { for (int x = 0; x < length; x++) {
@@ -357,14 +358,14 @@ public final class ContainerInterfaceTerminal extends AEBaseContainer {
private final long sortBy; private final long sortBy;
private final long which = autoBase++; private final long which = autoBase++;
private final String unlocalizedName; private final ITextComponent name;
private final IItemHandler client; private final IItemHandler client;
private final IItemHandler server; private final IItemHandler server;
public InvTracker(final DualityInterface dual, final IItemHandler patterns, final String unlocalizedName) { public InvTracker(final DualityInterface dual, final IItemHandler patterns, final ITextComponent name) {
this.server = patterns; this.server = patterns;
this.client = new AppEngInternalInventory(null, this.server.getSlots()); this.client = new AppEngInternalInventory(null, this.server.getSlots());
this.unlocalizedName = unlocalizedName; this.name = name;
this.sortBy = dual.getSortValue(); this.sortBy = dual.getSortValue();
} }
} }