made player preferences client-sided

This commit is contained in:
Ellpeck 2020-05-08 20:56:27 +02:00
parent b65a73c77b
commit 5632727d04
6 changed files with 77 additions and 39 deletions

View file

@ -14,4 +14,8 @@ public enum ItemOrder {
ItemOrder(Comparator<ItemStack> comparator) { ItemOrder(Comparator<ItemStack> comparator) {
this.comparator = comparator; this.comparator = comparator;
} }
public ItemOrder next() {
return values()[(this.ordinal() + 1) % values().length];
}
} }

View file

@ -0,0 +1,51 @@
package de.ellpeck.prettypipes.misc;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonWriter;
import de.ellpeck.prettypipes.PrettyPipes;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity;
import java.io.*;
public class PlayerPrefs {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static PlayerPrefs instance;
public ItemOrder terminalItemOrder = ItemOrder.AMOUNT;
public boolean terminalAscending;
public void save() {
File file = getFile();
if (file.exists())
file.delete();
try (FileWriter writer = new FileWriter(file)) {
GSON.toJson(this, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
public static PlayerPrefs get() {
if (instance == null) {
File file = getFile();
if (file.exists()) {
try (FileReader reader = new FileReader(file)) {
instance = GSON.fromJson(reader, PlayerPrefs.class);
return instance;
} catch (IOException e) {
e.printStackTrace();
}
}
instance = new PlayerPrefs();
}
return instance;
}
private static File getFile() {
File location = Minecraft.getInstance().gameDir;
return new File(location, PrettyPipes.ID + "prefs");
}
}

View file

@ -109,14 +109,6 @@ public class PacketButton {
STACK_SIZE_AMOUNT((pos, data, player) -> { STACK_SIZE_AMOUNT((pos, data, player) -> {
AbstractPipeContainer<?> container = (AbstractPipeContainer<?>) player.openContainer; AbstractPipeContainer<?> container = (AbstractPipeContainer<?>) player.openContainer;
StackSizeModuleItem.setMaxStackSize(container.moduleStack, data[0]); StackSizeModuleItem.setMaxStackSize(container.moduleStack, data[0]);
}),
TERMINAL_ORDER((pos, data, player) -> {
player.getPersistentData().putInt(PrettyPipes.ID + ":item_order", data[0]);
Utility.getTileEntity(ItemTerminalTileEntity.class, player.world, pos).updateItems(player);
}),
TERMINAL_ASCENDING((pos, data, player) -> {
player.getPersistentData().putBoolean(PrettyPipes.ID + ":ascending", data[0] > 0);
Utility.getTileEntity(ItemTerminalTileEntity.class, player.world, pos).updateItems(player);
}); });
public final TriConsumer<BlockPos, int[], PlayerEntity> action; public final TriConsumer<BlockPos, int[], PlayerEntity> action;

View file

@ -22,13 +22,9 @@ import java.util.function.Supplier;
public class PacketNetworkItems { public class PacketNetworkItems {
private List<ItemStack> items; private List<ItemStack> items;
private ItemOrder order;
private boolean ascending;
public PacketNetworkItems(List<ItemStack> items, ItemOrder order, boolean ascending) { public PacketNetworkItems(List<ItemStack> items) {
this.items = items; this.items = items;
this.order = order;
this.ascending = ascending;
} }
private PacketNetworkItems() { private PacketNetworkItems() {
@ -43,8 +39,6 @@ public class PacketNetworkItems {
stack.setCount(buf.readVarInt()); stack.setCount(buf.readVarInt());
client.items.add(stack); client.items.add(stack);
} }
client.order = ItemOrder.values()[buf.readByte()];
client.ascending = buf.readBoolean();
return client; return client;
} }
@ -56,8 +50,6 @@ public class PacketNetworkItems {
buf.writeItemStack(copy); buf.writeItemStack(copy);
buf.writeVarInt(stack.getCount()); buf.writeVarInt(stack.getCount());
} }
buf.writeByte(packet.order.ordinal());
buf.writeBoolean(packet.ascending);
} }
@SuppressWarnings("Convert2Lambda") @SuppressWarnings("Convert2Lambda")
@ -67,7 +59,7 @@ public class PacketNetworkItems {
public void run() { public void run() {
Minecraft mc = Minecraft.getInstance(); Minecraft mc = Minecraft.getInstance();
if (mc.currentScreen instanceof ItemTerminalGui) if (mc.currentScreen instanceof ItemTerminalGui)
((ItemTerminalGui) mc.currentScreen).updateItemList(message.items, message.order, message.ascending); ((ItemTerminalGui) mc.currentScreen).updateItemList(message.items);
} }
}); });
ctx.get().setPacketHandled(true); ctx.get().setPacketHandled(true);

View file

@ -119,11 +119,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
ItemTerminalTileEntity tile = ((ItemTerminalContainer) player.openContainer).tile; ItemTerminalTileEntity tile = ((ItemTerminalContainer) player.openContainer).tile;
if (tile != this) if (tile != this)
continue; continue;
PacketHandler.sendTo(player, new PacketNetworkItems(clientItems));
CompoundNBT nbt = player.getPersistentData();
ItemOrder order = ItemOrder.values()[nbt.getInt(PrettyPipes.ID + ":item_order")];
boolean ascending = nbt.getBoolean(PrettyPipes.ID + ":ascending");
PacketHandler.sendTo(player, new PacketNetworkItems(clientItems, order, ascending));
} }
} }
} }

View file

@ -3,6 +3,7 @@ package de.ellpeck.prettypipes.terminal.containers;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.misc.ItemOrder; import de.ellpeck.prettypipes.misc.ItemOrder;
import de.ellpeck.prettypipes.misc.ItemTerminalWidget; import de.ellpeck.prettypipes.misc.ItemTerminalWidget;
import de.ellpeck.prettypipes.misc.PlayerPrefs;
import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler; import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketRequest; import de.ellpeck.prettypipes.packets.PacketRequest;
@ -36,8 +37,6 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
private String lastSearchText; private String lastSearchText;
private int requestAmount = 1; private int requestAmount = 1;
private int scrollOffset; private int scrollOffset;
private ItemOrder order;
private boolean ascending;
public ItemTerminalGui(ItemTerminalContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) { public ItemTerminalGui(ItemTerminalContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
@ -77,14 +76,18 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
this.orderButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop, 20, 20, "", button -> { this.orderButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop, 20, 20, "", button -> {
if (this.sortedItems == null) if (this.sortedItems == null)
return; return;
int order = (this.order.ordinal() + 1) % ItemOrder.values().length; PlayerPrefs prefs = PlayerPrefs.get();
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.TERMINAL_ORDER, order)); prefs.terminalItemOrder = prefs.terminalItemOrder.next();
prefs.save();
this.updateWidgets();
})); }));
this.ascendingButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop + 22, 20, 20, "", button -> { this.ascendingButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop + 22, 20, 20, "", button -> {
if (this.sortedItems == null) if (this.sortedItems == null)
return; return;
int asc = !this.ascending ? 1 : 0; PlayerPrefs prefs = PlayerPrefs.get();
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.TERMINAL_ASCENDING, asc)); prefs.terminalAscending = !prefs.terminalAscending;
prefs.save();
this.updateWidgets();
})); }));
for (int y = 0; y < 4; y++) { for (int y = 0; y < 4; y++) {
for (int x = 0; x < 9; x++) for (int x = 0; x < 9; x++)
@ -121,19 +124,18 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
return super.keyPressed(x, y, z); return super.keyPressed(x, y, z);
} }
public void updateItemList(List<ItemStack> items, ItemOrder order, boolean ascending) { public void updateItemList(List<ItemStack> items) {
this.order = order;
this.ascending = ascending;
this.items = items; this.items = items;
this.updateWidgets(); this.updateWidgets();
this.ascendingButton.setMessage(this.ascending ? "^" : "v");
this.orderButton.setMessage(this.order.name().substring(0, 1));
} }
private void updateWidgets() { private void updateWidgets() {
Comparator<ItemStack> comparator = this.order.comparator; PlayerPrefs prefs = PlayerPrefs.get();
if (!this.ascending) this.ascendingButton.setMessage(prefs.terminalAscending ? "^" : "v");
this.orderButton.setMessage(prefs.terminalItemOrder.name().substring(0, 1));
Comparator<ItemStack> comparator = prefs.terminalItemOrder.comparator;
if (!prefs.terminalAscending)
comparator = comparator.reversed(); comparator = comparator.reversed();
this.sortedItems = new ArrayList<>(this.items); this.sortedItems = new ArrayList<>(this.items);
@ -181,10 +183,11 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
widget.renderToolTip(mouseX, mouseY); widget.renderToolTip(mouseX, mouseY);
} }
if (this.sortedItems != null) { if (this.sortedItems != null) {
PlayerPrefs prefs = PlayerPrefs.get();
if (this.orderButton.isHovered()) if (this.orderButton.isHovered())
this.renderTooltip(I18n.format("info." + PrettyPipes.ID + ".order", I18n.format("info." + PrettyPipes.ID + ".order." + this.order.name().toLowerCase(Locale.ROOT))), mouseX, mouseY); this.renderTooltip(I18n.format("info." + PrettyPipes.ID + ".order", I18n.format("info." + PrettyPipes.ID + ".order." + prefs.terminalItemOrder.name().toLowerCase(Locale.ROOT))), mouseX, mouseY);
if (this.ascendingButton.isHovered()) if (this.ascendingButton.isHovered())
this.renderTooltip(I18n.format("info." + PrettyPipes.ID + "." + (this.ascending ? "ascending" : "descending")), mouseX, mouseY); this.renderTooltip(I18n.format("info." + PrettyPipes.ID + "." + (prefs.terminalAscending ? "ascending" : "descending")), mouseX, mouseY);
} }
this.renderHoveredToolTip(mouseX, mouseY); this.renderHoveredToolTip(mouseX, mouseY);
} }