Add setChanged slot based, add proper grid notifications and network notifications
This commit is contained in:
parent
7ba1cca72d
commit
69a67012c4
@ -89,6 +89,11 @@ public class BlockEntityMatterReplicator extends BlockEntityMatteryPoweredWorker
|
||||
is_idling = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatternAdded(PatternState state) {
|
||||
is_idling = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRemoved() {
|
||||
if (level != null && !level.isClientSide && grid != null)
|
||||
|
@ -9,12 +9,14 @@ import net.minecraft.network.chat.TranslatableComponent;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import ru.dbotthepony.mc.otm.capability.*;
|
||||
import ru.dbotthepony.mc.otm.matter.MatterGrid;
|
||||
import ru.dbotthepony.mc.otm.Registry;
|
||||
@ -33,6 +35,21 @@ public class BlockEntityPatternStorage extends BlockEntityMattery implements IMa
|
||||
}
|
||||
|
||||
public final MatteryContainer patterns = new MatteryContainer(this::setChanged, 3 * 3) {
|
||||
@Override
|
||||
public void setChanged(int slot, ItemStack new_state, ItemStack old_state) {
|
||||
if (grid != null && !ItemStack.isSameItemSameTags(new_state, old_state)) {
|
||||
if (!old_state.isEmpty()) {
|
||||
old_state.getCapability(MatteryCapability.PATTERN).ifPresent(cap -> cap.getStoredPatterns().forEach(grid::onPatternRemoved));
|
||||
}
|
||||
|
||||
if (!new_state.isEmpty()) {
|
||||
new_state.getCapability(MatteryCapability.PATTERN).ifPresent(cap -> cap.getStoredPatterns().forEach(grid::onPatternAdded));
|
||||
}
|
||||
}
|
||||
|
||||
super.setChanged(slot, new_state, old_state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxStackSize() {
|
||||
return 1;
|
||||
|
@ -20,8 +20,17 @@ public record PatternState(@Nonnull UUID id, @Nonnull Item item, double research
|
||||
this.research_percent = Math.max(0, Math.min(1, research_percent));
|
||||
}
|
||||
|
||||
public boolean equals(PatternState state) {
|
||||
return state.id.equals(id);
|
||||
@Override
|
||||
public boolean equals(Object state) {
|
||||
if (state instanceof PatternState state1)
|
||||
return state1.id.equals(id);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id.hashCode();
|
||||
}
|
||||
|
||||
public boolean equalsHard(PatternState state) {
|
||||
|
@ -14,7 +14,15 @@ import java.util.function.Consumer;
|
||||
|
||||
public class MatteryContainer extends SimpleContainer {
|
||||
protected final Runnable watcher;
|
||||
protected boolean ignore_change_notifications = false;
|
||||
protected int ignore_change_notifications = 0;
|
||||
|
||||
public void startIgnore() {
|
||||
ignore_change_notifications++;
|
||||
}
|
||||
|
||||
public void stopIgnore() {
|
||||
ignore_change_notifications--;
|
||||
}
|
||||
|
||||
public MatteryContainer(Runnable watcher, int i) {
|
||||
super(i);
|
||||
@ -34,14 +42,14 @@ public class MatteryContainer extends SimpleContainer {
|
||||
}
|
||||
|
||||
public void deserializeNBT(@Nullable CompoundTag tag) {
|
||||
ignore_change_notifications = true;
|
||||
ignore_change_notifications++;
|
||||
|
||||
for (int i = 0; i < getContainerSize(); i++) {
|
||||
setItem(i, ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
if (tag == null) {
|
||||
ignore_change_notifications = false;
|
||||
ignore_change_notifications--;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -52,7 +60,7 @@ public class MatteryContainer extends SimpleContainer {
|
||||
if (list.get(i) instanceof CompoundTag get_tag)
|
||||
setItem(i, ItemStack.of(get_tag));
|
||||
|
||||
ignore_change_notifications = false;
|
||||
ignore_change_notifications--;
|
||||
setChanged();
|
||||
}
|
||||
|
||||
@ -98,13 +106,17 @@ public class MatteryContainer extends SimpleContainer {
|
||||
|
||||
@Override
|
||||
public void setChanged() {
|
||||
if (ignore_change_notifications)
|
||||
if (ignore_change_notifications > 0)
|
||||
return;
|
||||
|
||||
super.setChanged();
|
||||
watcher.run();
|
||||
}
|
||||
|
||||
public void setChanged(int slot, ItemStack new_state, ItemStack old_state) {
|
||||
setChanged();
|
||||
}
|
||||
|
||||
public CompoundTag serializeNBT() {
|
||||
CompoundTag tag = new CompoundTag();
|
||||
tag.putInt("size", getContainerSize());
|
||||
@ -222,7 +234,9 @@ public class MatteryContainer extends SimpleContainer {
|
||||
|
||||
if (self_stack.isEmpty()) {
|
||||
if (!simulate) {
|
||||
container.setItem(slot, stack.copy());
|
||||
var copy = stack.copy();
|
||||
container.setChanged(slot, copy, ItemStack.EMPTY);
|
||||
container.setItem(slot, copy);
|
||||
}
|
||||
|
||||
return ItemStack.EMPTY;
|
||||
@ -232,8 +246,9 @@ public class MatteryContainer extends SimpleContainer {
|
||||
|
||||
if (diff != 0) {
|
||||
if (!simulate) {
|
||||
ItemStack copy_self = self_stack.copy();
|
||||
self_stack.grow(diff);
|
||||
container.setChanged();
|
||||
container.setChanged(slot, self_stack, copy_self);
|
||||
}
|
||||
|
||||
ItemStack copy = stack.copy();
|
||||
@ -267,8 +282,9 @@ public class MatteryContainer extends SimpleContainer {
|
||||
copy.setCount(minimal);
|
||||
|
||||
if (!simulate) {
|
||||
ItemStack copy_self = self_stack.copy();
|
||||
self_stack.shrink(minimal);
|
||||
container.setChanged();
|
||||
container.setChanged(slot, self_stack, copy_self);
|
||||
}
|
||||
|
||||
return copy;
|
||||
|
@ -21,6 +21,7 @@ public class MatterGrid implements IMatterGridListener {
|
||||
public static final Set<MatterGrid> NETWORKS = new HashSet<>();
|
||||
|
||||
private final Set<IMatterGridCell> cells = new HashSet<>();
|
||||
private final Set<IMatterGridListener> listeners = new HashSet<>();
|
||||
|
||||
private static final Set<Supplier<Boolean>> discovering_neighbours = new HashSet<>();
|
||||
|
||||
@ -385,6 +386,14 @@ public class MatterGrid implements IMatterGridListener {
|
||||
NETWORKS.remove(this);
|
||||
}
|
||||
|
||||
public void attach(IMatterGridListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public void detach(IMatterGridListener listener) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
public int networkSize() {
|
||||
return cells.size();
|
||||
}
|
||||
@ -437,41 +446,48 @@ public class MatterGrid implements IMatterGridListener {
|
||||
public void onPatternAdded(PatternState state) {
|
||||
// validate();
|
||||
cells.forEach(cell -> cell.onPatternAdded(state));
|
||||
listeners.forEach(cell -> cell.onPatternAdded(state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatternRemoved(PatternState state) {
|
||||
// validate();
|
||||
cells.forEach(cell -> cell.onPatternRemoved(state));
|
||||
listeners.forEach(cell -> cell.onPatternRemoved(state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatternUpdated(PatternState new_state, PatternState old_state) {
|
||||
// validate();
|
||||
cells.forEach(cell -> cell.onPatternUpdated(new_state, old_state));
|
||||
listeners.forEach(cell -> cell.onPatternUpdated(new_state, old_state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMatterTaskCreated(MatterTask task) {
|
||||
// validate();
|
||||
cells.forEach(cell -> cell.onMatterTaskCreated(task));
|
||||
listeners.forEach(cell -> cell.onMatterTaskCreated(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMatterTaskUpdated(MatterTask new_state, MatterTask old_state) {
|
||||
// validate();
|
||||
cells.forEach(cell -> cell.onMatterTaskUpdated(new_state, old_state));
|
||||
listeners.forEach(cell -> cell.onMatterTaskUpdated(new_state, old_state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMatterTaskFinished(MatterTask state) {
|
||||
// validate();
|
||||
cells.forEach(cell -> cell.onMatterTaskFinished(state));
|
||||
listeners.forEach(cell -> cell.onMatterTaskFinished(state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMatterTaskRemoved(MatterTask state) {
|
||||
// validate();
|
||||
cells.forEach(cell -> cell.onMatterTaskRemoved(state));
|
||||
listeners.forEach(cell -> cell.onMatterTaskRemoved(state));
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import ru.dbotthepony.mc.otm.matter.MatterRegistry;
|
||||
import ru.dbotthepony.mc.otm.Registry;
|
||||
import ru.dbotthepony.mc.otm.block.entity.BlockEntityMatterDecomposer;
|
||||
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.SlotAutoRenderable;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.MatterySlot;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.MachineOutputSlot;
|
||||
import ru.dbotthepony.mc.otm.menu.widget.MatterLevelWidget;
|
||||
import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget;
|
||||
@ -25,7 +25,7 @@ public class MatterDecomposerMenu extends PoweredMatteryMenu {
|
||||
Container container = tile != null ? tile.item_container : new SimpleContainer(2);
|
||||
|
||||
// Вход
|
||||
addSlot(new SlotAutoRenderable(container, 0, 61, 36) {
|
||||
addSlot(new MatterySlot(container, 0, 61, 36) {
|
||||
@Override
|
||||
public boolean mayPlace(ItemStack p_40231_) {
|
||||
return MatterRegistry.hasMatterValue(p_40231_);
|
||||
|
@ -2,56 +2,72 @@ package ru.dbotthepony.mc.otm.menu;
|
||||
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraftforge.fmllegacy.network.PacketDistributor;
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import ru.dbotthepony.mc.otm.Registry;
|
||||
import ru.dbotthepony.mc.otm.block.entity.BlockEntityMatterPanel;
|
||||
import ru.dbotthepony.mc.otm.capability.IMatterGridListener;
|
||||
import ru.dbotthepony.mc.otm.capability.PatternState;
|
||||
import ru.dbotthepony.mc.otm.matter.MatterGrid;
|
||||
import ru.dbotthepony.mc.otm.network.MatteryNetworking;
|
||||
import ru.dbotthepony.mc.otm.network.PatternStateSendListPacket;
|
||||
import ru.dbotthepony.mc.otm.network.PatternGridPacket;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MatterPanelMenu extends MatteryMenu {
|
||||
public class MatterPanelMenu extends MatteryMenu implements IMatterGridListener {
|
||||
public MatterPanelMenu(int p_38852_, Inventory inventory) {
|
||||
this(p_38852_, inventory, null);
|
||||
}
|
||||
|
||||
private BlockEntityMatterPanel tile;
|
||||
private MatterGrid grid;
|
||||
|
||||
public MatterPanelMenu(int p_38852_, Inventory inventory, BlockEntityMatterPanel tile) {
|
||||
super(Registry.Menus.MATTER_PANEL, p_38852_, inventory, tile);
|
||||
this.tile = tile;
|
||||
|
||||
if (tile != null) {
|
||||
grid = tile.getMatterGrid();
|
||||
|
||||
if (grid != null) {
|
||||
grid.attach(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcastFullState() {
|
||||
super.broadcastFullState();
|
||||
sendPatternsToClient();
|
||||
}
|
||||
|
||||
private boolean initial_send = false;
|
||||
|
||||
@Override
|
||||
public void broadcastChanges() {
|
||||
super.broadcastChanges();
|
||||
|
||||
if (!initial_send)
|
||||
sendPatternsToClient();
|
||||
}
|
||||
|
||||
// client code
|
||||
public ArrayList<PatternState> patterns = new ArrayList<>();
|
||||
public int changeset = 0;
|
||||
|
||||
public void networkStates(List<PatternState> list) {
|
||||
public void networkPatternsUpdated(PatternState[] patterns) {
|
||||
changeset++;
|
||||
patterns.clear();
|
||||
patterns.addAll(list);
|
||||
|
||||
for (var pattern : patterns) {
|
||||
var index_of = this.patterns.indexOf(pattern);
|
||||
|
||||
if (index_of != -1) {
|
||||
this.patterns.set(index_of, pattern);
|
||||
} else {
|
||||
this.patterns.add(pattern);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void networkPatternsRemoved(PatternState[] patterns) {
|
||||
changeset++;
|
||||
|
||||
for (var pattern : patterns) {
|
||||
this.patterns.remove(pattern);
|
||||
}
|
||||
}
|
||||
|
||||
// server code
|
||||
public void requestReplication(ServerPlayer ply, PatternState state, int how_much) {
|
||||
if (tile == null)
|
||||
return;
|
||||
|
||||
var grid = ((BlockEntityMatterPanel) tile).getMatterGrid();
|
||||
var grid = tile.getMatterGrid();
|
||||
|
||||
if (grid == null)
|
||||
return;
|
||||
@ -63,21 +79,58 @@ public class MatterPanelMenu extends MatteryMenu {
|
||||
return;
|
||||
}
|
||||
|
||||
OverdriveThatMatters.LOGGER.debug("add task: {}", ((BlockEntityMatterPanel) tile).addTask(state, how_much));
|
||||
tile.addTask(state, how_much);
|
||||
}
|
||||
|
||||
public void sendPatternsToClient() {
|
||||
@Override
|
||||
public void onPatternAdded(PatternState state) {
|
||||
sendNetwork(new PatternGridPacket(containerId, true, state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatternRemoved(PatternState state) {
|
||||
sendNetwork(new PatternGridPacket(containerId, false, state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatternUpdated(PatternState new_state, PatternState old_state) {
|
||||
sendNetwork(new PatternGridPacket(containerId, true, new_state));
|
||||
}
|
||||
|
||||
private boolean initial_send = false;
|
||||
|
||||
@Override
|
||||
public void removed(Player p_38940_) {
|
||||
super.removed(p_38940_);
|
||||
|
||||
if (grid != null)
|
||||
grid.detach(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcastChanges() {
|
||||
super.broadcastChanges();
|
||||
|
||||
if (!initial_send)
|
||||
fullPatternBroadcast();
|
||||
}
|
||||
|
||||
private void sendNetwork(Object packet) {
|
||||
MatteryNetworking.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) inventory.player), packet);
|
||||
}
|
||||
|
||||
public void fullPatternBroadcast() {
|
||||
if (inventory.player.level.isClientSide) {
|
||||
initial_send = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (tile != null) {
|
||||
var grid = ((BlockEntityMatterPanel) tile).getMatterGrid();
|
||||
var grid = tile.getMatterGrid();
|
||||
|
||||
if (grid != null) {
|
||||
initial_send = true;
|
||||
MatteryNetworking.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) inventory.player), new PatternStateSendListPacket(containerId, grid.getStoredPatterns()));
|
||||
sendNetwork(new PatternGridPacket(containerId, true, grid.getStoredPatterns().toArray(new PatternState[0])));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,21 +3,12 @@ package ru.dbotthepony.mc.otm.menu;
|
||||
import net.minecraft.world.Container;
|
||||
import net.minecraft.world.SimpleContainer;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.inventory.MenuType;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import ru.dbotthepony.mc.otm.Registry;
|
||||
import ru.dbotthepony.mc.otm.block.entity.BlockEntityMatterReplicator;
|
||||
import ru.dbotthepony.mc.otm.matter.MatterRegistry;
|
||||
import ru.dbotthepony.mc.otm.menu.MatteryMenu;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.BatterySlot;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.MachineOutputSlot;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.SlotAutoRenderable;
|
||||
import ru.dbotthepony.mc.otm.menu.widget.MatterLevelWidget;
|
||||
import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class MatterReplicatorMenu extends PoweredMatteryMenu {
|
||||
public MatterReplicatorMenu(int p_38852_, Inventory inventory) {
|
||||
this(p_38852_, inventory, null);
|
||||
|
@ -7,7 +7,7 @@ import net.minecraft.world.item.ItemStack;
|
||||
import ru.dbotthepony.mc.otm.matter.MatterRegistry;
|
||||
import ru.dbotthepony.mc.otm.Registry;
|
||||
import ru.dbotthepony.mc.otm.block.entity.BlockEntityMatterScanner;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.SlotAutoRenderable;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.MatterySlot;
|
||||
import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget;
|
||||
|
||||
public class MatterScannerMenu extends PoweredMatteryMenu {
|
||||
@ -20,7 +20,7 @@ public class MatterScannerMenu extends PoweredMatteryMenu {
|
||||
|
||||
Container container = tile != null ? tile.input_slot : new SimpleContainer(1);
|
||||
|
||||
addSlot(new SlotAutoRenderable(container, 0, 64, 38) {
|
||||
addSlot(new MatterySlot(container, 0, 64, 38) {
|
||||
@Override
|
||||
public boolean mayPlace(ItemStack p_40231_) {
|
||||
return MatterRegistry.hasMatterValue(p_40231_);
|
||||
|
@ -3,14 +3,10 @@ package ru.dbotthepony.mc.otm.menu;
|
||||
import net.minecraft.world.Container;
|
||||
import net.minecraft.world.SimpleContainer;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.inventory.MenuType;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import ru.dbotthepony.mc.otm.Registry;
|
||||
import ru.dbotthepony.mc.otm.block.entity.BlockEntityPatternStorage;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.PatternSlot;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class PatternStorageMenu extends MatteryMenu {
|
||||
public PatternStorageMenu(int p_38852_, Inventory inventory) {
|
||||
this(p_38852_, inventory, null);
|
||||
|
@ -0,0 +1,52 @@
|
||||
package ru.dbotthepony.mc.otm.menu.slot;
|
||||
|
||||
import net.minecraft.world.Container;
|
||||
import net.minecraft.world.inventory.Slot;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer;
|
||||
|
||||
// Just a sign that this slot needs auto draw
|
||||
public class MatterySlot extends Slot {
|
||||
public boolean auto_bg;
|
||||
|
||||
public MatterySlot(Container p_40223_, int index, int x, int y, boolean auto_bg) {
|
||||
super(p_40223_, index, x, y);
|
||||
this.auto_bg = auto_bg;
|
||||
}
|
||||
|
||||
public MatterySlot(Container p_40223_, int index, int x, int y) {
|
||||
this(p_40223_, index, x, y, true);
|
||||
}
|
||||
|
||||
private ItemStack last_stack;
|
||||
|
||||
@Override
|
||||
public ItemStack getItem() {
|
||||
var get = super.getItem();
|
||||
last_stack = get.copy();
|
||||
return get;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setChanged() {
|
||||
if (container instanceof MatteryContainer container1) {
|
||||
ItemStack old = last_stack.copy();
|
||||
container1.setChanged(getSlotIndex(), super.getItem(), old);
|
||||
} else {
|
||||
super.setChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(ItemStack p_40240_) {
|
||||
if (container instanceof MatteryContainer container1) {
|
||||
ItemStack old = container1.getItem(getSlotIndex()).copy();
|
||||
container1.startIgnore();
|
||||
container1.setItem(getSlotIndex(), p_40240_);
|
||||
container1.stopIgnore();
|
||||
container1.setChanged(getSlotIndex(), p_40240_, old);
|
||||
} else {
|
||||
super.set(p_40240_);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@ import net.minecraft.world.Container;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
|
||||
|
||||
public class PatternSlot extends SlotAutoRenderable {
|
||||
public class PatternSlot extends MatterySlot {
|
||||
public PatternSlot(Container p_40223_, int index, int x, int y, boolean auto_bg) {
|
||||
super(p_40223_, index, x, y, auto_bg);
|
||||
}
|
||||
|
@ -1,18 +0,0 @@
|
||||
package ru.dbotthepony.mc.otm.menu.slot;
|
||||
|
||||
import net.minecraft.world.Container;
|
||||
import net.minecraft.world.inventory.Slot;
|
||||
|
||||
// Just a sign that this slot needs auto draw
|
||||
public class SlotAutoRenderable extends Slot {
|
||||
public boolean auto_bg;
|
||||
|
||||
public SlotAutoRenderable(Container p_40223_, int index, int x, int y, boolean auto_bg) {
|
||||
super(p_40223_, index, x, y);
|
||||
this.auto_bg = auto_bg;
|
||||
}
|
||||
|
||||
public SlotAutoRenderable(Container p_40223_, int index, int x, int y) {
|
||||
this(p_40223_, index, x, y, true);
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package ru.dbotthepony.mc.otm.network;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraftforge.fmllegacy.network.NetworkEvent;
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import ru.dbotthepony.mc.otm.menu.MatterPanelMenu;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ClientPatternStateSendListPacketHandler {
|
||||
public static void handlePacket(PatternStateSendListPacket packet, Supplier<NetworkEvent.Context> context) {
|
||||
if (Minecraft.getInstance().player.containerMenu == null) {
|
||||
OverdriveThatMatters.LOGGER.error("Receive pattern state list for {}, but nothing is open!", packet.container_id);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Minecraft.getInstance().player.containerMenu.containerId != packet.container_id) {
|
||||
OverdriveThatMatters.LOGGER.error("Receive pattern state list for {}, but {} is open! ({})", packet.container_id, Minecraft.getInstance().player.containerMenu.containerId, Minecraft.getInstance().player.containerMenu);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(Minecraft.getInstance().player.containerMenu instanceof MatterPanelMenu menu)) {
|
||||
OverdriveThatMatters.LOGGER.error("Receive pattern state list for {}, but it is wrong type!", packet.container_id);
|
||||
return;
|
||||
}
|
||||
|
||||
menu.networkStates(packet.states);
|
||||
}
|
||||
}
|
@ -32,19 +32,19 @@ public class MatteryNetworking {
|
||||
|
||||
CHANNEL.registerMessage(
|
||||
next_network_id++,
|
||||
PatternStateSendListPacket.class,
|
||||
PatternStateSendListPacket::encodeNetwork,
|
||||
PatternStateSendListPacket::decodeNetwork,
|
||||
PatternStateSendListPacket::handleMessage,
|
||||
PatternGridPacket.class,
|
||||
PatternGridPacket::write,
|
||||
PatternGridPacket::read,
|
||||
PatternGridPacket::play,
|
||||
Optional.of(NetworkDirection.PLAY_TO_CLIENT)
|
||||
);
|
||||
|
||||
CHANNEL.registerMessage(
|
||||
next_network_id++,
|
||||
PatternReplicationRequestPacket.class,
|
||||
PatternReplicationRequestPacket::encodeNetwork,
|
||||
PatternReplicationRequestPacket::decodeNetwork,
|
||||
PatternReplicationRequestPacket::handleMessage,
|
||||
PatternReplicationRequestPacket::write,
|
||||
PatternReplicationRequestPacket::read,
|
||||
PatternReplicationRequestPacket::play,
|
||||
Optional.of(NetworkDirection.PLAY_TO_SERVER)
|
||||
);
|
||||
}
|
||||
|
@ -0,0 +1,61 @@
|
||||
package ru.dbotthepony.mc.otm.network;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.fml.DistExecutor;
|
||||
import net.minecraftforge.fmllegacy.network.NetworkEvent;
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import ru.dbotthepony.mc.otm.capability.PatternState;
|
||||
import ru.dbotthepony.mc.otm.menu.MatterPanelMenu;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@ParametersAreNonnullByDefault
|
||||
public record PatternGridPacket(int container_id, boolean action, PatternState ...state) {
|
||||
public void write(FriendlyByteBuf buffer) {
|
||||
buffer.writeInt(container_id);
|
||||
buffer.writeBoolean(action);
|
||||
buffer.writeInt(state.length);
|
||||
|
||||
for (var state1 : state)
|
||||
state1.write(buffer);
|
||||
}
|
||||
|
||||
public void play(Supplier<NetworkEvent.Context> context) {
|
||||
context.get().setPacketHandled(true);
|
||||
context.get().enqueueWork(() -> DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> this::playClient));
|
||||
}
|
||||
|
||||
private void playClient() {
|
||||
if (Minecraft.getInstance().player.containerMenu instanceof MatterPanelMenu menu) {
|
||||
if (menu.containerId != container_id) {
|
||||
OverdriveThatMatters.LOGGER.error("Receive pattern state list for {}, but current container has id of {}!", container_id, menu.containerId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (action)
|
||||
menu.networkPatternsUpdated(state);
|
||||
else
|
||||
menu.networkPatternsRemoved(state);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
OverdriveThatMatters.LOGGER.error("Receive pattern state list for {}, but no valid container is open!", container_id);
|
||||
}
|
||||
|
||||
public static PatternGridPacket read(FriendlyByteBuf buffer) {
|
||||
int container_id = buffer.readInt();
|
||||
boolean action = buffer.readBoolean();
|
||||
int amount = buffer.readInt();
|
||||
var list = new PatternState[amount];
|
||||
|
||||
for (int i = 0; i < amount; i++)
|
||||
list[i] = Objects.requireNonNull(PatternState.read(buffer));
|
||||
|
||||
return new PatternGridPacket(container_id, action, list);
|
||||
}
|
||||
}
|
@ -13,12 +13,12 @@ public record PatternReplicationRequestPacket(PatternState state, int how_much)
|
||||
this.how_much = Math.max(1, Math.min(99999, how_much));
|
||||
}
|
||||
|
||||
public void encodeNetwork(FriendlyByteBuf buffer) {
|
||||
public void write(FriendlyByteBuf buffer) {
|
||||
state.write(buffer);
|
||||
buffer.writeInt(how_much);
|
||||
}
|
||||
|
||||
public void handleMessage(Supplier<NetworkEvent.Context> context) {
|
||||
public void play(Supplier<NetworkEvent.Context> context) {
|
||||
context.get().setPacketHandled(true);
|
||||
|
||||
context.get().enqueueWork(() -> {
|
||||
@ -34,7 +34,7 @@ public record PatternReplicationRequestPacket(PatternState state, int how_much)
|
||||
});
|
||||
}
|
||||
|
||||
public static PatternReplicationRequestPacket decodeNetwork(FriendlyByteBuf buffer) {
|
||||
public static PatternReplicationRequestPacket read(FriendlyByteBuf buffer) {
|
||||
return new PatternReplicationRequestPacket(PatternState.read(buffer), buffer.readInt());
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
package ru.dbotthepony.mc.otm.network;
|
||||
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.fml.DistExecutor;
|
||||
import net.minecraftforge.fmllegacy.network.NetworkEvent;
|
||||
import ru.dbotthepony.mc.otm.capability.PatternState;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class PatternStateSendListPacket {
|
||||
public PatternStateSendListPacket() {
|
||||
|
||||
}
|
||||
|
||||
public PatternStateSendListPacket(Collection<PatternState> states) {
|
||||
this.states.addAll(states);
|
||||
}
|
||||
|
||||
public PatternStateSendListPacket(int container_id, Collection<PatternState> states) {
|
||||
this(states);
|
||||
this.container_id = container_id;
|
||||
}
|
||||
|
||||
public int container_id;
|
||||
public ArrayList<PatternState> states = new ArrayList<>();
|
||||
|
||||
public void encodeNetwork(FriendlyByteBuf buffer) {
|
||||
buffer.writeInt(container_id);
|
||||
buffer.writeInt(states.size());
|
||||
|
||||
for (var state : states) {
|
||||
state.write(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public void handleMessage(Supplier<NetworkEvent.Context> context) {
|
||||
context.get().setPacketHandled(true);
|
||||
|
||||
context.get().enqueueWork(() -> {
|
||||
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientPatternStateSendListPacketHandler.handlePacket(this, context));
|
||||
});
|
||||
}
|
||||
|
||||
public static PatternStateSendListPacket decodeNetwork(FriendlyByteBuf buffer) {
|
||||
var packet = new PatternStateSendListPacket();
|
||||
packet.container_id = buffer.readInt();
|
||||
int size = buffer.readInt();
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
var state = PatternState.read(buffer);
|
||||
|
||||
if (state != null) {
|
||||
packet.states.add(state);
|
||||
}
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
}
|
@ -101,7 +101,7 @@ public class MatterPanelScreen extends MatteryScreen<MatterPanelMenu> {
|
||||
top_level += 24;
|
||||
right = 5 + button_width;
|
||||
|
||||
cancel = new Button(x + modal_width - right, y + top_level, button_width, 20, new TranslatableComponent("otm.container.matter_panel.cancel"), (btn) -> onCancel());
|
||||
cancel = new Button(x + modal_width - right, y + top_level, button_width, 20, new TranslatableComponent("otm.container.matter_panel.cancel"), (btn) -> closePattern());
|
||||
right += 2 + button_width;
|
||||
|
||||
send = new Button(x + modal_width - right, y + top_level, button_width, 20, new TranslatableComponent("otm.container.matter_panel.send"), (btn) -> onSend());
|
||||
@ -153,11 +153,23 @@ public class MatterPanelScreen extends MatteryScreen<MatterPanelMenu> {
|
||||
}
|
||||
|
||||
MatteryNetworking.CHANNEL.sendToServer(new PatternReplicationRequestPacket(open_pattern, value));
|
||||
open_pattern = null;
|
||||
closePattern();
|
||||
}
|
||||
|
||||
private void onCancel() {
|
||||
private void closePattern() {
|
||||
open_pattern = null;
|
||||
input_amount.visible = false;
|
||||
input_amount.changeFocus(false);
|
||||
inc_8.visible = false;
|
||||
inc_64.visible = false;
|
||||
inc_256.visible = false;
|
||||
|
||||
send.visible = false;
|
||||
cancel.visible = false;
|
||||
|
||||
dec_8.visible = false;
|
||||
dec_64.visible = false;
|
||||
dec_256.visible = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -180,21 +192,17 @@ public class MatterPanelScreen extends MatteryScreen<MatterPanelMenu> {
|
||||
protected void renderForeground(PoseStack stack, int mouseX, int mouseY, float p_98421_) {
|
||||
if (open_pattern != null) {
|
||||
RenderSystem.disableDepthTest();
|
||||
|
||||
input_amount.render(stack, mouseX, mouseY, p_98421_);
|
||||
input_amount.visible = true;
|
||||
|
||||
RenderSystem.enableDepthTest();
|
||||
RenderSystem.depthFunc(GL_ALWAYS);
|
||||
|
||||
inc_8.render(stack, mouseX, mouseY, p_98421_);
|
||||
inc_8.visible = true;
|
||||
|
||||
inc_64.render(stack, mouseX, mouseY, p_98421_);
|
||||
inc_64.visible = true;
|
||||
|
||||
inc_256.render(stack, mouseX, mouseY, p_98421_);
|
||||
inc_256.visible = true;
|
||||
|
||||
dec_8.visible = true;
|
||||
dec_64.visible = true;
|
||||
dec_256.visible = true;
|
||||
|
||||
send.visible = true;
|
||||
cancel.visible = true;
|
||||
|
||||
dec_8.render(stack, mouseX, mouseY, p_98421_);
|
||||
dec_64.render(stack, mouseX, mouseY, p_98421_);
|
||||
@ -203,18 +211,8 @@ public class MatterPanelScreen extends MatteryScreen<MatterPanelMenu> {
|
||||
|
||||
send.render(stack, mouseX, mouseY, p_98421_);
|
||||
cancel.render(stack, mouseX, mouseY, p_98421_);
|
||||
} else {
|
||||
input_amount.visible = false;
|
||||
inc_8.visible = false;
|
||||
inc_64.visible = false;
|
||||
inc_256.visible = false;
|
||||
|
||||
send.visible = false;
|
||||
cancel.visible = false;
|
||||
|
||||
dec_8.visible = false;
|
||||
dec_64.visible = false;
|
||||
dec_256.visible = false;
|
||||
RenderSystem.depthFunc(GL_LESS);
|
||||
}
|
||||
}
|
||||
|
||||
@ -254,7 +252,20 @@ public class MatterPanelScreen extends MatteryScreen<MatterPanelMenu> {
|
||||
private void openPattern(PatternState state) {
|
||||
open_pattern = state;
|
||||
input_amount.setValue("1");
|
||||
input_amount.setFocus(true);
|
||||
scrolling = false;
|
||||
|
||||
input_amount.visible = true;
|
||||
inc_8.visible = true;
|
||||
inc_64.visible = true;
|
||||
inc_256.visible = true;
|
||||
|
||||
dec_8.visible = true;
|
||||
dec_64.visible = true;
|
||||
dec_256.visible = true;
|
||||
|
||||
send.visible = true;
|
||||
cancel.visible = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2,7 +2,6 @@ package ru.dbotthepony.mc.otm.screen;
|
||||
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.minecraft.client.gui.components.Widget;
|
||||
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
|
||||
import net.minecraft.client.renderer.GameRenderer;
|
||||
import net.minecraft.network.chat.Component;
|
||||
@ -12,7 +11,7 @@ import net.minecraft.world.inventory.Slot;
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import ru.dbotthepony.mc.otm.menu.MatteryMenu;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.BatterySlot;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.SlotAutoRenderable;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.MatterySlot;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.MachineOutputSlot;
|
||||
import ru.dbotthepony.mc.otm.menu.slot.MatterContainerInputSlot;
|
||||
import ru.dbotthepony.mc.otm.menu.widget.AbstractWidget;
|
||||
@ -98,7 +97,7 @@ public class MatteryScreen<T extends MatteryMenu> extends AbstractContainerScree
|
||||
} else {
|
||||
renderRegularSlot(pose, slot1.x, slot1.y);
|
||||
}
|
||||
} else if (slot instanceof SlotAutoRenderable slot1 && slot1.auto_bg) {
|
||||
} else if (slot instanceof MatterySlot slot1 && slot1.auto_bg) {
|
||||
renderRegularSlot(pose, slot1.x, slot1.y);
|
||||
} else if (slot instanceof MatterContainerInputSlot slot1 && slot1.auto_bg) {
|
||||
renderRegularSlot(pose, slot1.x, slot1.y);
|
||||
|
Loading…
Reference in New Issue
Block a user