mirror of
https://github.com/GTNewHorizons/Applied-Energistics-2-Unofficial.git
synced 2026-10-25 20:22:23 +00:00
make saved search local saved (#1378)
Co-authored-by: Martin Robertz <dream-master@gmx.net>
This commit is contained in:
1 parent
c79b6cc8c7
commit
abb624cd96
12 files changed
+298
-184
No files matched your search
@@ -13,6 +13,10 @@
|
||||
|
||||
package appeng.api.storage;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
|
||||
import appeng.api.util.IConfigurableObject;
|
||||
import appeng.core.localization.GuiText;
|
||||
|
||||
@@ -22,4 +26,10 @@ public interface ITerminalHost extends IStorageMonitorable, IConfigurableObject
|
||||
return GuiText.Terminal;
|
||||
}
|
||||
|
||||
default void saveSearchString(final String searchString, final EntityPlayer player) {}
|
||||
|
||||
@Nullable
|
||||
default String getSearchString(EntityPlayer player) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -98,8 +98,8 @@ import appeng.integration.IntegrationRegistry;
|
||||
import appeng.integration.IntegrationType;
|
||||
import appeng.integration.modules.NEI;
|
||||
import appeng.items.storage.ItemViewCell;
|
||||
import appeng.util.AEStackTypeFilter;
|
||||
import appeng.util.IConfigManagerHost;
|
||||
import appeng.util.MonitorableTypeFilter;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.item.AEItemStack;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
|
||||
@@ -153,7 +153,7 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
protected VirtualMEPinSlot[] pinSlots = null;
|
||||
protected VirtualMEMonitorableSlot[] monitorableSlots = null;
|
||||
@Nullable
|
||||
protected Reference2BooleanMap<IAEStackType<?>> typeFilters;
|
||||
protected AEStackTypeFilter typeFilters;
|
||||
|
||||
public GuiMEMonitorable(final InventoryPlayer inventoryPlayer, final ITerminalHost te) {
|
||||
this(inventoryPlayer, te, new ContainerMEMonitorable(inventoryPlayer, te));
|
||||
@@ -194,13 +194,15 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
repo.setSearchString(text.trim());
|
||||
repo.updateView();
|
||||
setScrollBar();
|
||||
|
||||
if (AEConfig.instance.preserveSearchBar) monitorableContainer.saveSearchString(this.getText());
|
||||
}
|
||||
};
|
||||
|
||||
NEI.searchField.putFormatter(this.searchField);
|
||||
|
||||
if (te instanceof ITerminalTypeFilterProvider) {
|
||||
this.typeFilters = MonitorableTypeFilter.createDefaultMap();
|
||||
this.typeFilters = new AEStackTypeFilter();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,14 +230,14 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
final IAEStackType<?> type = this.typeToggleButtons.get(tbtn);
|
||||
if (type != null && this.typeFilters != null) {
|
||||
// Optimistic client-side feedback; server will sync authoritative state shortly.
|
||||
final boolean next = !this.typeFilters.getBoolean(type);
|
||||
this.typeFilters.put(type, next);
|
||||
final boolean next = !this.typeFilters.isEnabled(type);
|
||||
this.typeFilters.setEnabled(type, next);
|
||||
tbtn.setEnabled(next);
|
||||
this.repo.updateView();
|
||||
|
||||
try {
|
||||
NetworkHandler.instance
|
||||
.sendToServer(new PacketMonitorableTypeFilter(this.typeFilters, this.inventorySlots.windowId));
|
||||
.sendToServer(new PacketMonitorableTypeFilter(this.typeFilters.getImmutableFilters(), this.inventorySlots.windowId));
|
||||
} catch (final IOException e) {
|
||||
AELog.debug(e);
|
||||
}
|
||||
@@ -347,7 +349,7 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
}
|
||||
|
||||
private boolean checkTypeFilter(IAEStackType<?> type) {
|
||||
return this.typeFilters == null || this.typeFilters.getBoolean(type);
|
||||
return this.typeFilters == null || this.typeFilters.isEnabled(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -522,8 +524,10 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
if (this.isSubGui()) {
|
||||
this.searchField.setText(memoryText);
|
||||
} else if (AEConfig.instance.preserveSearchBar) {
|
||||
this.searchField.setText(memoryText, true);
|
||||
repo.setSearchString(memoryText);
|
||||
final String localMemoryText = this.monitorableContainer.getSavedSearchString();
|
||||
final String mem = localMemoryText == null ? memoryText : localMemoryText;
|
||||
this.searchField.setText(mem, true);
|
||||
repo.setSearchString(mem);
|
||||
}
|
||||
this.searchField.setCursorPositionEnd();
|
||||
|
||||
@@ -566,7 +570,7 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
|
||||
final TypeToggleButton btn = new TypeToggleButton(x, y, texture, icon, type.getDisplayName());
|
||||
|
||||
btn.setEnabled(this.typeFilters.getBoolean(type));
|
||||
btn.setEnabled(this.typeFilters.isEnabled(type));
|
||||
this.typeToggleButtons.put(btn, type);
|
||||
this.buttonList.add(btn);
|
||||
|
||||
@@ -672,7 +676,7 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
final boolean isLShiftDown = isShiftKeyDown();
|
||||
final boolean isLControlDown = isCtrlKeyDown();
|
||||
final boolean nonItemInteraction = isLControlDown
|
||||
|| (this.typeFilters != null && !this.typeFilters.getBoolean(ITEM_STACK_TYPE));
|
||||
|| (this.typeFilters != null && !this.typeFilters.isEnabled(ITEM_STACK_TYPE));
|
||||
|
||||
switch (mouseButton) {
|
||||
case 0 -> { // left click
|
||||
@@ -793,8 +797,8 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
@Override
|
||||
public void onGuiClosed() {
|
||||
super.onGuiClosed();
|
||||
Keyboard.enableRepeatEvents(false);
|
||||
memoryText = this.searchField.getText();
|
||||
Keyboard.enableRepeatEvents(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -944,7 +948,7 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
@Override
|
||||
@Nullable
|
||||
public Reference2BooleanMap<IAEStackType<?>> getTypeFilter() {
|
||||
return this.typeFilters;
|
||||
return this.typeFilters == null ? null : this.typeFilters.getFiltersMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -973,12 +977,12 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
if (this.typeFilters == null) return;
|
||||
|
||||
for (Reference2BooleanMap.Entry<IAEStackType<?>> entry : map.reference2BooleanEntrySet()) {
|
||||
this.typeFilters.put(entry.getKey(), entry.getBooleanValue());
|
||||
this.typeFilters.setEnabled(entry.getKey(), entry.getBooleanValue());
|
||||
}
|
||||
|
||||
// Update Buttons
|
||||
for (final Map.Entry<TypeToggleButton, IAEStackType<?>> entry : this.typeToggleButtons.entrySet()) {
|
||||
final boolean enabled = this.typeFilters.getBoolean(entry.getValue());
|
||||
final boolean enabled = this.typeFilters.isEnabled(entry.getValue());
|
||||
entry.getKey().setEnabled(enabled);
|
||||
}
|
||||
|
||||
@@ -1126,7 +1130,7 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
|
||||
final boolean isRealItem = this.mc.thePlayer.inventory.getItemStack() != null;
|
||||
final boolean nonItemInteraction = isCtrlKeyDown()
|
||||
|| (this.typeFilters != null && !this.typeFilters.getBoolean(ITEM_STACK_TYPE));
|
||||
|| (this.typeFilters != null && !this.typeFilters.isEnabled(ITEM_STACK_TYPE));
|
||||
|
||||
if (nonItemInteraction) {
|
||||
for (IAEStackType<?> type : AEStackTypeRegistry.getAllTypes()) {
|
||||
@@ -1156,4 +1160,8 @@ public class GuiMEMonitorable extends AEBaseGui
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void memoryTextUpdated() {
|
||||
this.reinitalize();
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import appeng.api.config.SortDir;
|
||||
import appeng.api.config.ViewItems;
|
||||
import appeng.api.storage.data.IAEStackType;
|
||||
import appeng.util.MonitorableTypeFilter;
|
||||
import appeng.util.AEStackTypeFilter;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
|
||||
|
||||
public interface ISortSource {
|
||||
@@ -37,6 +37,6 @@ public interface ISortSource {
|
||||
|
||||
@Nullable
|
||||
default Reference2BooleanMap<IAEStackType<?>> getTypeFilter() {
|
||||
return MonitorableTypeFilter.createDefaultMap();
|
||||
return new AEStackTypeFilter().getFiltersMap();
|
||||
}
|
||||
}
|
||||
@@ -72,6 +72,7 @@ import appeng.api.storage.data.IAEStackType;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
import appeng.api.util.IConfigManager;
|
||||
import appeng.api.util.IConfigurableObject;
|
||||
import appeng.client.gui.implementations.GuiMEMonitorable;
|
||||
import appeng.container.AEBaseContainer;
|
||||
import appeng.container.guisync.GuiSync;
|
||||
import appeng.container.slot.AppEngSlot;
|
||||
@@ -79,7 +80,9 @@ import appeng.container.slot.SlotRestrictedInput;
|
||||
import appeng.container.slot.SlotRestrictedInput.PlacableItemType;
|
||||
import appeng.container.sync.ActionHandler;
|
||||
import appeng.container.sync.StreamCodecs;
|
||||
import appeng.container.sync.SyncCodecs;
|
||||
import appeng.container.sync.SyncRegistrar;
|
||||
import appeng.container.sync.handlers.ObjectSyncHandler;
|
||||
import appeng.core.AELog;
|
||||
import appeng.core.sync.network.NetworkHandler;
|
||||
import appeng.core.sync.packets.PacketMEInventoryUpdate;
|
||||
@@ -98,6 +101,7 @@ import appeng.util.InventoryAdaptor;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.inv.AdaptorPlayerHand;
|
||||
import appeng.util.item.AEItemStack;
|
||||
import cpw.mods.fml.common.network.ByteBufUtils;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectLongPair;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap.Entry;
|
||||
@@ -130,6 +134,8 @@ public class ContainerMEMonitorable extends AEBaseContainer
|
||||
|
||||
public final ActionHandler<Integer> toggleViewCellAction;
|
||||
|
||||
private final ObjectSyncHandler<String> savedSearchSync;
|
||||
|
||||
public ContainerMEMonitorable(final InventoryPlayer ip, final ITerminalHost monitorable) {
|
||||
this(ip, monitorable, true);
|
||||
}
|
||||
@@ -203,6 +209,23 @@ public class ContainerMEMonitorable extends AEBaseContainer
|
||||
final SyncRegistrar sync = this.syncRegistrar();
|
||||
this.toggleViewCellAction = sync.actionC2S("toggleViewCell", StreamCodecs.intValue())
|
||||
.onServerAction(this::toggleViewCell);
|
||||
this.savedSearchSync = sync
|
||||
.object(
|
||||
"savedSearch",
|
||||
SyncCodecs.of(
|
||||
"String",
|
||||
ByteBufUtils::writeUTF8String,
|
||||
ByteBufUtils::readUTF8String,
|
||||
String::new,
|
||||
String::equals),
|
||||
|
||||
this.host.getSearchString(ip.player))
|
||||
.onServerChange(
|
||||
(oldValue, newValue) -> this.host.saveSearchString(newValue, this.getPlayerInv().player))
|
||||
.onClientChange(
|
||||
(oldValue, newValue) -> {
|
||||
if (this.gui instanceof GuiMEMonitorable gm) gm.memoryTextUpdated();
|
||||
});
|
||||
}
|
||||
|
||||
public IGridNode getNetworkNode() {
|
||||
@@ -1309,4 +1332,13 @@ public class ContainerMEMonitorable extends AEBaseContainer
|
||||
}
|
||||
return ais.getItemStack();
|
||||
}
|
||||
|
||||
public void saveSearchString(final String searchString) {
|
||||
this.savedSearchSync.set(searchString);
|
||||
this.host.saveSearchString(searchString, getInventoryPlayer().player);
|
||||
}
|
||||
|
||||
public String getSavedSearchString() {
|
||||
return this.savedSearchSync.get();
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ import appeng.items.contents.PinsHandler;
|
||||
import appeng.items.contents.PinsHolder;
|
||||
import appeng.items.contents.WirelessTerminalViewCells;
|
||||
import appeng.tile.networking.TileWireless;
|
||||
import appeng.util.MonitorableTypeFilter;
|
||||
import appeng.util.TerminalSettings;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
|
||||
@@ -73,8 +73,7 @@ public class WirelessTerminalGuiObject implements IPortableCell, IActionHost, II
|
||||
private final PinsHolder pinsInv;
|
||||
private final boolean infinityRange;
|
||||
private final boolean infinityEnergy;
|
||||
@NotNull
|
||||
private final MonitorableTypeFilter typeFilters = new MonitorableTypeFilter();
|
||||
private final TerminalSettings terminalSettings = new TerminalSettings();
|
||||
|
||||
public WirelessTerminalGuiObject(final IWirelessTermHandler wh, final ItemStack is, final EntityPlayer ep,
|
||||
final World w, final int x, final int y, final int z) {
|
||||
@@ -87,7 +86,7 @@ public class WirelessTerminalGuiObject implements IPortableCell, IActionHost, II
|
||||
this.inventorySlot = x;
|
||||
this.viewCells = new WirelessTerminalViewCells(is);
|
||||
this.pinsInv = new PinsHolder(is);
|
||||
this.typeFilters.readFromNBT(is.getTagCompound());
|
||||
this.terminalSettings.readFromNBT(is.getTagCompound());
|
||||
|
||||
ILocatable obj = null;
|
||||
|
||||
@@ -310,11 +309,21 @@ public class WirelessTerminalGuiObject implements IPortableCell, IActionHost, II
|
||||
|
||||
@Override
|
||||
public @NotNull Reference2BooleanMap<IAEStackType<?>> getTypeFilter(EntityPlayer player) {
|
||||
return this.typeFilters.getFilters(player);
|
||||
return this.terminalSettings.getFilters(player).getFiltersMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveTypeFilter() {
|
||||
this.typeFilters.writeToNBT(this.effectiveItem);
|
||||
this.terminalSettings.writeToNBT(this.effectiveItem);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveSearchString(String searchString, EntityPlayer player) {
|
||||
this.effectiveItem.getTagCompound().setString("searchString", searchString);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getSearchString(EntityPlayer player) {
|
||||
return this.effectiveItem.getTagCompound().getString("searchString");
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ package appeng.items.contents;
|
||||
import static appeng.util.item.AEFluidStackType.FLUID_STACK_TYPE;
|
||||
import static appeng.util.item.AEItemStackType.ITEM_STACK_TYPE;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
@@ -109,4 +110,14 @@ public class PortableCellViewer<StackType extends IAEStack<?>> extends MEMonitor
|
||||
out.readFromNBT((NBTTagCompound) ItemStackNBT.get(this.target).copy());
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveSearchString(String searchString, EntityPlayer player) {
|
||||
this.target.getTagCompound().setString("searchString", searchString);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getSearchString(EntityPlayer player) {
|
||||
return this.target.getTagCompound().getString("searchString");
|
||||
}
|
||||
}
|
||||
@@ -45,8 +45,8 @@ import appeng.tile.inventory.IAEAppEngInventory;
|
||||
import appeng.tile.inventory.InvOperation;
|
||||
import appeng.util.ConfigManager;
|
||||
import appeng.util.IConfigManagerHost;
|
||||
import appeng.util.MonitorableTypeFilter;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.TerminalSettings;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
|
||||
|
||||
/**
|
||||
@@ -66,9 +66,7 @@ public abstract class AbstractPartTerminal extends AbstractPartDisplay implement
|
||||
private final IConfigManager cm = new ConfigManager(this);
|
||||
private final AppEngInternalInventory viewCell = new AppEngInternalInventory(this, 5);
|
||||
private final PinsHolder pinsInv = new PinsHolder(this);
|
||||
|
||||
@NotNull
|
||||
private final MonitorableTypeFilter typeFilters = new MonitorableTypeFilter();
|
||||
private final TerminalSettings terminalSettings = new TerminalSettings();
|
||||
|
||||
public AbstractPartTerminal(final ItemStack is) {
|
||||
super(is);
|
||||
@@ -100,7 +98,7 @@ public abstract class AbstractPartTerminal extends AbstractPartDisplay implement
|
||||
this.cm.readFromNBT(data);
|
||||
this.viewCell.readFromNBT(data, "viewCell");
|
||||
pinsInv.readFromNBT(data, "pins");
|
||||
this.typeFilters.readFromNBT(data);
|
||||
this.terminalSettings.readFromNBT(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -109,7 +107,7 @@ public abstract class AbstractPartTerminal extends AbstractPartDisplay implement
|
||||
this.cm.writeToNBT(data);
|
||||
this.viewCell.writeToNBT(data, "viewCell");
|
||||
pinsInv.writeToNBT(data, "pins");
|
||||
this.typeFilters.writeToNBT(data);
|
||||
this.terminalSettings.writeToNBT(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -191,11 +189,22 @@ public abstract class AbstractPartTerminal extends AbstractPartDisplay implement
|
||||
@Override
|
||||
@NotNull
|
||||
public Reference2BooleanMap<IAEStackType<?>> getTypeFilter(EntityPlayer player) {
|
||||
return this.typeFilters.getFilters(player);
|
||||
return this.terminalSettings.getFilters(player).getFiltersMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveTypeFilter() {
|
||||
this.saveChanges();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveSearchString(String searchString, EntityPlayer player) {
|
||||
this.terminalSettings.setSavedSearchString(searchString, player);
|
||||
this.saveChanges();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getSearchString(EntityPlayer player) {
|
||||
return this.terminalSettings.getSavedSearchString(player);
|
||||
}
|
||||
}
|
||||
@@ -98,8 +98,8 @@ import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.tile.inventory.InvOperation;
|
||||
import appeng.util.ConfigManager;
|
||||
import appeng.util.IConfigManagerHost;
|
||||
import appeng.util.MonitorableTypeFilter;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.TerminalSettings;
|
||||
import appeng.util.item.AEFluidStack;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
|
||||
@@ -130,7 +130,7 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
|
||||
private final Map<IAEStackType<?>, MEMonitorHandler> cellMap = new IdentityHashMap<>();
|
||||
private boolean displayNeedsUpdate;
|
||||
@NotNull
|
||||
private final MonitorableTypeFilter typeFilters = new MonitorableTypeFilter();
|
||||
private final TerminalSettings terminalSettings = new TerminalSettings();
|
||||
|
||||
public TileChest() {
|
||||
this.setInternalMaxPower(PowerMultiplier.CONFIG.multiply(40));
|
||||
@@ -427,7 +427,7 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
|
||||
this.paintedColor = AEColor.fromOrdinal(data.getByte("paintedColor"));
|
||||
this.getProxy().setColor(this.paintedColor);
|
||||
}
|
||||
this.typeFilters.readFromNBT(data);
|
||||
this.terminalSettings.readFromNBT(data);
|
||||
}
|
||||
|
||||
@TileEvent(TileEventType.WORLD_NBT_WRITE)
|
||||
@@ -435,7 +435,7 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
|
||||
this.config.writeToNBT(data);
|
||||
data.setInteger("priority", this.priority);
|
||||
data.setByte("paintedColor", (byte) this.paintedColor.ordinal());
|
||||
this.typeFilters.writeToNBT(data);
|
||||
this.terminalSettings.writeToNBT(data);
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
@@ -772,7 +772,7 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
|
||||
@Override
|
||||
@NotNull
|
||||
public Reference2BooleanMap<IAEStackType<?>> getTypeFilter(EntityPlayer player) {
|
||||
return this.typeFilters.getFilters(player);
|
||||
return this.terminalSettings.getFilters(player).getFiltersMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -116,6 +116,11 @@ public class AEStackTypeFilter implements IGuiPacketWritable {
|
||||
return Reference2BooleanMaps.unmodifiable(new Reference2BooleanOpenHashMap<>(this.filters));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Reference2BooleanMap<IAEStackType<?>> getFiltersMap() {
|
||||
return this.filters;
|
||||
}
|
||||
|
||||
public void setEnabled(@NotNull final IAEStackType<?> type, final boolean enabled) {
|
||||
this.filters.put(type, enabled);
|
||||
}
|
||||
|
||||
@@ -1,148 +0,0 @@
|
||||
package appeng.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.common.util.Constants.NBT;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import appeng.api.storage.data.AEStackTypeRegistry;
|
||||
import appeng.api.storage.data.IAEStackType;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanOpenHashMap;
|
||||
|
||||
public class MonitorableTypeFilter {
|
||||
|
||||
private static final String NBT_FILTERS = "typeFilters";
|
||||
private static final String NBT_UUID = "uuid";
|
||||
private static final String NBT_MAP = "map";
|
||||
private static final String NBT_TYPE_ID = "typeId";
|
||||
private static final String NBT_VALUE = "value";
|
||||
|
||||
private final Map<UUID, Reference2BooleanMap<IAEStackType<?>>> perPlayer = new HashMap<>();
|
||||
|
||||
public MonitorableTypeFilter() {}
|
||||
|
||||
@NotNull
|
||||
public static Reference2BooleanMap<IAEStackType<?>> createDefaultMap() {
|
||||
final Reference2BooleanMap<IAEStackType<?>> map = new Reference2BooleanOpenHashMap<>();
|
||||
for (IAEStackType<?> type : AEStackTypeRegistry.getAllTypes()) {
|
||||
map.put(type, true);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public void readFromNBT(@Nullable NBTTagCompound tag) {
|
||||
this.perPlayer.clear();
|
||||
|
||||
if (tag == null || !tag.hasKey(NBT_FILTERS, NBT.TAG_LIST)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final NBTTagList players = tag.getTagList(NBT_FILTERS, NBT.TAG_COMPOUND);
|
||||
for (int i = 0; i < players.tagCount(); i++) {
|
||||
final NBTTagCompound playerTag = players.getCompoundTagAt(i);
|
||||
final String uuidString = playerTag.getString(NBT_UUID);
|
||||
if (uuidString == null || uuidString.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final UUID id;
|
||||
try {
|
||||
id = UUID.fromString(uuidString);
|
||||
} catch (IllegalArgumentException e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final Reference2BooleanMap<IAEStackType<?>> map = createDefaultMap();
|
||||
|
||||
final NBTTagList list = playerTag.getTagList(NBT_MAP, NBT.TAG_COMPOUND);
|
||||
for (int j = 0; j < list.tagCount(); j++) {
|
||||
final NBTTagCompound entryTag = list.getCompoundTagAt(j);
|
||||
final String typeId = entryTag.getString(NBT_TYPE_ID);
|
||||
final boolean value = entryTag.getBoolean(NBT_VALUE);
|
||||
final IAEStackType<?> type = AEStackTypeRegistry.getType(typeId);
|
||||
if (type != null) {
|
||||
map.put(type, value);
|
||||
}
|
||||
}
|
||||
|
||||
this.perPlayer.put(id, map);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeToNBT(@NotNull ItemStack stack) {
|
||||
if (!stack.hasTagCompound()) {
|
||||
stack.setTagCompound(new NBTTagCompound());
|
||||
}
|
||||
this.writeToNBT(stack.getTagCompound());
|
||||
if (stack.getTagCompound().hasNoTags()) {
|
||||
stack.setTagCompound(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeToNBT(NBTTagCompound tag) {
|
||||
final NBTTagList players = new NBTTagList();
|
||||
|
||||
for (Map.Entry<UUID, Reference2BooleanMap<IAEStackType<?>>> entry : this.perPlayer.entrySet()) {
|
||||
final UUID id = entry.getKey();
|
||||
final Reference2BooleanMap<IAEStackType<?>> map = entry.getValue();
|
||||
|
||||
final NBTTagCompound playerTag = new NBTTagCompound();
|
||||
playerTag.setString(NBT_UUID, id.toString());
|
||||
|
||||
final NBTTagList list = new NBTTagList();
|
||||
boolean foundNonDefault = false;
|
||||
for (Reference2BooleanMap.Entry<IAEStackType<?>> filterEntry : map.reference2BooleanEntrySet()) {
|
||||
final boolean value = filterEntry.getBooleanValue();
|
||||
if (value) {
|
||||
continue; // Skip as default if true
|
||||
}
|
||||
foundNonDefault = true;
|
||||
|
||||
final NBTTagCompound entryTag = new NBTTagCompound();
|
||||
entryTag.setString(NBT_TYPE_ID, filterEntry.getKey().getId());
|
||||
entryTag.setBoolean(NBT_VALUE, value);
|
||||
list.appendTag(entryTag);
|
||||
}
|
||||
|
||||
if (!foundNonDefault) continue;
|
||||
|
||||
playerTag.setTag(NBT_MAP, list);
|
||||
players.appendTag(playerTag);
|
||||
}
|
||||
|
||||
if (players.tagCount() == 0) {
|
||||
tag.removeTag(NBT_FILTERS);
|
||||
} else {
|
||||
tag.setTag(NBT_FILTERS, players);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Reference2BooleanMap<IAEStackType<?>> getFilters(EntityPlayer player) {
|
||||
if (player == null || player.getGameProfile() == null) {
|
||||
return createDefaultMap();
|
||||
}
|
||||
|
||||
UUID id = player.getGameProfile().getId();
|
||||
if (id == null) {
|
||||
id = player.getUniqueID();
|
||||
}
|
||||
|
||||
Reference2BooleanMap<IAEStackType<?>> playerMap = this.perPlayer.get(id);
|
||||
if (playerMap == null) {
|
||||
playerMap = createDefaultMap();
|
||||
this.perPlayer.put(id, playerMap);
|
||||
}
|
||||
|
||||
return playerMap;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package appeng.util;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import appeng.api.storage.data.AEStackTypeRegistry;
|
||||
import appeng.api.storage.data.IAEStackType;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
|
||||
|
||||
public class TerminalPlayerSettings {
|
||||
|
||||
private static final String NBT_MAP = "map";
|
||||
private static final String NBT_TYPE_ID = "typeId";
|
||||
private static final String NBT_VALUE = "value";
|
||||
private static final String NBT_SAVED_STRING = "savedString";
|
||||
|
||||
private final AEStackTypeFilter typeFilters = new AEStackTypeFilter();
|
||||
private String savedString = "";
|
||||
|
||||
public void readFromNBT(NBTTagCompound tag) {
|
||||
final NBTTagList list = tag.getTagList(NBT_MAP, Constants.NBT.TAG_COMPOUND);
|
||||
for (int j = 0; j < list.tagCount(); j++) {
|
||||
final NBTTagCompound entryTag = list.getCompoundTagAt(j);
|
||||
final String typeId = entryTag.getString(NBT_TYPE_ID);
|
||||
final boolean value = entryTag.getBoolean(NBT_VALUE);
|
||||
final IAEStackType<?> type = AEStackTypeRegistry.getType(typeId);
|
||||
if (type != null) typeFilters.setEnabled(type, value);
|
||||
}
|
||||
this.savedString = tag.getString(NBT_SAVED_STRING);
|
||||
}
|
||||
|
||||
public void writeToNBT(@NotNull ItemStack stack) {
|
||||
if (!stack.hasTagCompound()) {
|
||||
stack.setTagCompound(new NBTTagCompound());
|
||||
}
|
||||
this.writeToNBT(stack.getTagCompound());
|
||||
if (stack.getTagCompound().hasNoTags()) {
|
||||
stack.setTagCompound(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeToNBT(NBTTagCompound tag) {
|
||||
final NBTTagList list = new NBTTagList();
|
||||
for (Reference2BooleanMap.Entry<IAEStackType<?>> filterEntry : this.typeFilters.getImmutableFilters()
|
||||
.reference2BooleanEntrySet()) {
|
||||
final boolean value = filterEntry.getBooleanValue();
|
||||
if (value) continue; // Skip as default if true
|
||||
|
||||
final NBTTagCompound entryTag = new NBTTagCompound();
|
||||
entryTag.setString(NBT_TYPE_ID, filterEntry.getKey().getId());
|
||||
entryTag.setBoolean(NBT_VALUE, false);
|
||||
list.appendTag(entryTag);
|
||||
}
|
||||
|
||||
tag.setTag(NBT_MAP, list);
|
||||
tag.setString(NBT_SAVED_STRING, this.savedString);
|
||||
}
|
||||
|
||||
public AEStackTypeFilter getTypeFilters() {
|
||||
return this.typeFilters;
|
||||
}
|
||||
|
||||
public void setSavedSearchString(String savedSearchString) {
|
||||
this.savedString = savedSearchString;
|
||||
}
|
||||
|
||||
public String getSavedString() {
|
||||
return this.savedString;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package appeng.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class TerminalSettings {
|
||||
|
||||
private final Map<UUID, TerminalPlayerSettings> perPlayer = new HashMap<>();
|
||||
|
||||
private static final String NBT = "terminalSettings";
|
||||
private static final String NBT_UUID_MOST = "uuid_m";
|
||||
private static final String NBT_UUID_LEAST = "uuid_l";
|
||||
|
||||
public void readFromNBT(@Nullable NBTTagCompound tag) {
|
||||
this.perPlayer.clear();
|
||||
|
||||
if (tag == null || !tag.hasKey(NBT, Constants.NBT.TAG_LIST)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final NBTTagList players = tag.getTagList(NBT, Constants.NBT.TAG_COMPOUND);
|
||||
for (int i = 0; i < players.tagCount(); i++) {
|
||||
final NBTTagCompound playerTag = players.getCompoundTagAt(i);
|
||||
|
||||
final UUID id;
|
||||
if (playerTag.hasKey(NBT_UUID_MOST)) {
|
||||
id = new UUID(playerTag.getLong(NBT_UUID_MOST), playerTag.getLong(NBT_UUID_LEAST));
|
||||
} else continue;
|
||||
|
||||
final TerminalPlayerSettings settings = new TerminalPlayerSettings();
|
||||
settings.readFromNBT(playerTag);
|
||||
this.perPlayer.put(id, settings);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeToNBT(@NotNull ItemStack stack) {
|
||||
if (!stack.hasTagCompound()) {
|
||||
stack.setTagCompound(new NBTTagCompound());
|
||||
}
|
||||
this.writeToNBT(stack.getTagCompound());
|
||||
if (stack.getTagCompound().hasNoTags()) {
|
||||
stack.setTagCompound(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeToNBT(NBTTagCompound tag) {
|
||||
final NBTTagList players = new NBTTagList();
|
||||
|
||||
for (Map.Entry<UUID, TerminalPlayerSettings> entry : this.perPlayer.entrySet()) {
|
||||
final UUID id = entry.getKey();
|
||||
final TerminalPlayerSettings settings = entry.getValue();
|
||||
|
||||
final NBTTagCompound playerTag = new NBTTagCompound();
|
||||
playerTag.setLong(NBT_UUID_MOST, id.getMostSignificantBits());
|
||||
playerTag.setLong(NBT_UUID_LEAST, id.getLeastSignificantBits());
|
||||
|
||||
settings.writeToNBT(playerTag);
|
||||
players.appendTag(playerTag);
|
||||
}
|
||||
|
||||
if (players.tagCount() == 0) {
|
||||
tag.removeTag(NBT);
|
||||
} else {
|
||||
tag.setTag(NBT, players);
|
||||
}
|
||||
}
|
||||
|
||||
public TerminalPlayerSettings getSettings(EntityPlayer player) {
|
||||
if (player == null || player.getGameProfile() == null) {
|
||||
return new TerminalPlayerSettings();
|
||||
}
|
||||
|
||||
UUID id = player.getGameProfile().getId();
|
||||
if (id == null) {
|
||||
id = player.getUniqueID();
|
||||
}
|
||||
|
||||
final TerminalPlayerSettings settings = this.perPlayer.get(id);
|
||||
if (settings == null) this.perPlayer.put(id, new TerminalPlayerSettings());
|
||||
return this.perPlayer.get(id);
|
||||
}
|
||||
|
||||
public AEStackTypeFilter getFilters(EntityPlayer player) {
|
||||
return this.getSettings(player).getTypeFilters();
|
||||
}
|
||||
|
||||
public void setSavedSearchString(String searchString, EntityPlayer player) {
|
||||
this.getSettings(player).setSavedSearchString(searchString);
|
||||
}
|
||||
|
||||
public String getSavedSearchString(EntityPlayer player) {
|
||||
return this.getSettings(player).getSavedString();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user