mirror of
https://github.com/Ellpeck/PrettyPipes.git
synced 2024-11-22 11:53:29 +01:00
made player preferences client-sided
This commit is contained in:
parent
b65a73c77b
commit
5632727d04
6 changed files with 77 additions and 39 deletions
|
@ -14,4 +14,8 @@ public enum ItemOrder {
|
|||
ItemOrder(Comparator<ItemStack> comparator) {
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
public ItemOrder next() {
|
||||
return values()[(this.ordinal() + 1) % values().length];
|
||||
}
|
||||
}
|
||||
|
|
51
src/main/java/de/ellpeck/prettypipes/misc/PlayerPrefs.java
Normal file
51
src/main/java/de/ellpeck/prettypipes/misc/PlayerPrefs.java
Normal 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");
|
||||
}
|
||||
}
|
|
@ -109,14 +109,6 @@ public class PacketButton {
|
|||
STACK_SIZE_AMOUNT((pos, data, player) -> {
|
||||
AbstractPipeContainer<?> container = (AbstractPipeContainer<?>) player.openContainer;
|
||||
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;
|
||||
|
|
|
@ -22,13 +22,9 @@ import java.util.function.Supplier;
|
|||
public class PacketNetworkItems {
|
||||
|
||||
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.order = order;
|
||||
this.ascending = ascending;
|
||||
}
|
||||
|
||||
private PacketNetworkItems() {
|
||||
|
@ -43,8 +39,6 @@ public class PacketNetworkItems {
|
|||
stack.setCount(buf.readVarInt());
|
||||
client.items.add(stack);
|
||||
}
|
||||
client.order = ItemOrder.values()[buf.readByte()];
|
||||
client.ascending = buf.readBoolean();
|
||||
return client;
|
||||
}
|
||||
|
||||
|
@ -56,8 +50,6 @@ public class PacketNetworkItems {
|
|||
buf.writeItemStack(copy);
|
||||
buf.writeVarInt(stack.getCount());
|
||||
}
|
||||
buf.writeByte(packet.order.ordinal());
|
||||
buf.writeBoolean(packet.ascending);
|
||||
}
|
||||
|
||||
@SuppressWarnings("Convert2Lambda")
|
||||
|
@ -67,7 +59,7 @@ public class PacketNetworkItems {
|
|||
public void run() {
|
||||
Minecraft mc = Minecraft.getInstance();
|
||||
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);
|
||||
|
|
|
@ -119,11 +119,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
|
|||
ItemTerminalTileEntity tile = ((ItemTerminalContainer) player.openContainer).tile;
|
||||
if (tile != this)
|
||||
continue;
|
||||
|
||||
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));
|
||||
PacketHandler.sendTo(player, new PacketNetworkItems(clientItems));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package de.ellpeck.prettypipes.terminal.containers;
|
|||
import de.ellpeck.prettypipes.PrettyPipes;
|
||||
import de.ellpeck.prettypipes.misc.ItemOrder;
|
||||
import de.ellpeck.prettypipes.misc.ItemTerminalWidget;
|
||||
import de.ellpeck.prettypipes.misc.PlayerPrefs;
|
||||
import de.ellpeck.prettypipes.packets.PacketButton;
|
||||
import de.ellpeck.prettypipes.packets.PacketHandler;
|
||||
import de.ellpeck.prettypipes.packets.PacketRequest;
|
||||
|
@ -36,8 +37,6 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
|||
private String lastSearchText;
|
||||
private int requestAmount = 1;
|
||||
private int scrollOffset;
|
||||
private ItemOrder order;
|
||||
private boolean ascending;
|
||||
|
||||
public ItemTerminalGui(ItemTerminalContainer screenContainer, PlayerInventory inv, ITextComponent 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 -> {
|
||||
if (this.sortedItems == null)
|
||||
return;
|
||||
int order = (this.order.ordinal() + 1) % ItemOrder.values().length;
|
||||
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.TERMINAL_ORDER, order));
|
||||
PlayerPrefs prefs = PlayerPrefs.get();
|
||||
prefs.terminalItemOrder = prefs.terminalItemOrder.next();
|
||||
prefs.save();
|
||||
this.updateWidgets();
|
||||
}));
|
||||
this.ascendingButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop + 22, 20, 20, "", button -> {
|
||||
if (this.sortedItems == null)
|
||||
return;
|
||||
int asc = !this.ascending ? 1 : 0;
|
||||
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.TERMINAL_ASCENDING, asc));
|
||||
PlayerPrefs prefs = PlayerPrefs.get();
|
||||
prefs.terminalAscending = !prefs.terminalAscending;
|
||||
prefs.save();
|
||||
this.updateWidgets();
|
||||
}));
|
||||
for (int y = 0; y < 4; y++) {
|
||||
for (int x = 0; x < 9; x++)
|
||||
|
@ -121,19 +124,18 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
|||
return super.keyPressed(x, y, z);
|
||||
}
|
||||
|
||||
public void updateItemList(List<ItemStack> items, ItemOrder order, boolean ascending) {
|
||||
this.order = order;
|
||||
this.ascending = ascending;
|
||||
public void updateItemList(List<ItemStack> items) {
|
||||
this.items = items;
|
||||
this.updateWidgets();
|
||||
|
||||
this.ascendingButton.setMessage(this.ascending ? "^" : "v");
|
||||
this.orderButton.setMessage(this.order.name().substring(0, 1));
|
||||
}
|
||||
|
||||
private void updateWidgets() {
|
||||
Comparator<ItemStack> comparator = this.order.comparator;
|
||||
if (!this.ascending)
|
||||
PlayerPrefs prefs = PlayerPrefs.get();
|
||||
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();
|
||||
|
||||
this.sortedItems = new ArrayList<>(this.items);
|
||||
|
@ -181,10 +183,11 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
|||
widget.renderToolTip(mouseX, mouseY);
|
||||
}
|
||||
if (this.sortedItems != null) {
|
||||
PlayerPrefs prefs = PlayerPrefs.get();
|
||||
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())
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue