Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocrafter manager #711

Merged
merged 9 commits into from
Nov 1, 2024
Prev Previous commit
Next Next commit
feat: autocrafter manager respects activeness
raoulvdberge committed Oct 31, 2024
commit a5d55b0388a7cdfa20cfb8b9fc15576169606fde
Original file line number Diff line number Diff line change
@@ -8,7 +8,9 @@
import com.refinedmods.refinedstorage.common.support.containermenu.NetworkNodeExtendedMenuProvider;
import com.refinedmods.refinedstorage.common.support.network.AbstractBaseNetworkNodeContainerBlockEntity;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
@@ -37,12 +39,28 @@ public class AutocrafterManagerBlockEntity extends AbstractBaseNetworkNodeContai
new SimpleContainer(10)
);

private final Set<AutocrafterManagerWatcher> watchers = new HashSet<>();

public AutocrafterManagerBlockEntity(final BlockPos pos, final BlockState state) {
super(BlockEntities.INSTANCE.getAutocrafterManager(), pos, state, new SimpleNetworkNode(
Platform.INSTANCE.getConfig().getAutocrafterManager().getEnergyUsage()
));
}

void addWatcher(final AutocrafterManagerWatcher watcher) {
watchers.add(watcher);
}

void removeWatcher(final AutocrafterManagerWatcher watcher) {
watchers.remove(watcher);
}

@Override
protected void activenessChanged(final boolean newActive) {
super.activenessChanged(newActive);
watchers.forEach(watcher -> watcher.activeChanged(newActive));
}

@Override
public Component getName() {
return ContentNames.AUTOCRAFTER_MANAGER;
@@ -56,7 +74,7 @@ protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState

@Override
public AutocrafterManagerData getMenuData() {
return new AutocrafterManagerData(groups);
return new AutocrafterManagerData(groups, mainNetworkNode.isActive());
}

@Override
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
import com.refinedmods.refinedstorage.common.support.containermenu.ClientProperty;
import com.refinedmods.refinedstorage.common.support.containermenu.PropertyTypes;
import com.refinedmods.refinedstorage.common.support.containermenu.ServerProperty;
import com.refinedmods.refinedstorage.common.support.packet.s2c.S2CPackets;
import com.refinedmods.refinedstorage.common.support.stretching.ScreenSizeListener;

import java.util.ArrayList;
@@ -19,21 +20,27 @@
import javax.annotation.Nullable;

import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

public class AutocrafterManagerContainerMenu extends AbstractBaseContainerMenu implements ScreenSizeListener {
public class AutocrafterManagerContainerMenu extends AbstractBaseContainerMenu implements ScreenSizeListener,
AutocrafterManagerWatcher {
private final Inventory playerInventory;
private final List<Group> groups;
private final List<AutocrafterManagerSlot> autocrafterSlots = new ArrayList<>();

@Nullable
private AutocrafterManagerListener listener;
@Nullable
private AutocrafterManagerBlockEntity autocrafterManager;
private String query = "";
private boolean active;

public AutocrafterManagerContainerMenu(final int syncId,
final Inventory playerInventory,
@@ -42,6 +49,7 @@ public AutocrafterManagerContainerMenu(final int syncId,
this.playerInventory = playerInventory;
registerProperty(new ClientProperty<>(PropertyTypes.REDSTONE_MODE, RedstoneMode.IGNORE));
this.groups = data.groups().stream().map(group -> Group.from(playerInventory.player.level(), group)).toList();
this.active = data.active();
resized(0, 0, 0);
}

@@ -51,6 +59,8 @@ public AutocrafterManagerContainerMenu(final int syncId,
final List<Container> containers) {
super(Menus.INSTANCE.getAutocrafterManager(), syncId);
this.playerInventory = playerInventory;
this.autocrafterManager = autocrafterManager;
this.autocrafterManager.addWatcher(this);
registerProperty(new ServerProperty<>(
PropertyTypes.REDSTONE_MODE,
autocrafterManager::getRedstoneMode,
@@ -60,6 +70,14 @@ public AutocrafterManagerContainerMenu(final int syncId,
addServerSideSlots(containers);
}

@Override
public void removed(final Player playerEntity) {
super.removed(playerEntity);
if (autocrafterManager != null) {
autocrafterManager.removeWatcher(this);
}
}

void setListener(final AutocrafterManagerListener listener) {
this.listener = listener;
}
@@ -115,7 +133,7 @@ private int addSlots(final Group group, final int rowX, final int rowY, final in
for (int i = 0; i < group.slotCount; i++) {
final int slotX = rowX + ((j % 9) * 18);
final int slotY = rowY + 18 + ((j / 9) * 18);
final boolean visible = isSlotVisible(group, i);
final boolean visible = active && isSlotVisible(group, i);
final AutocrafterManagerSlot slot = new AutocrafterManagerSlot(
group.backingInventory,
i,
@@ -172,6 +190,22 @@ void setViewType(final AutocrafterManagerViewType toggle) {
notifyListener();
}

public void setActive(final boolean active) {
this.active = active;
notifyListener();
}

boolean isActive() {
return active;
}

@Override
public void activeChanged(final boolean newActive) {
if (playerInventory.player instanceof ServerPlayer serverPlayerEntity) {
S2CPackets.sendAutocrafterManagerActive(serverPlayerEntity, newActive);
}
}

static class Group {
final Component name;
final int slotCount;
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;

public record AutocrafterManagerData(List<Group> groups) {
public record AutocrafterManagerData(List<Group> groups, boolean active) {
private static final StreamCodec<RegistryFriendlyByteBuf, Group> GROUP_STREAM_CODEC = StreamCodec.composite(
ComponentSerialization.STREAM_CODEC, Group::name,
ByteBufCodecs.INT, Group::slotCount,
@@ -18,6 +18,7 @@ public record AutocrafterManagerData(List<Group> groups) {
public static final StreamCodec<RegistryFriendlyByteBuf, AutocrafterManagerData> STREAM_CODEC = StreamCodec
.composite(
ByteBufCodecs.collection(ArrayList::new, GROUP_STREAM_CODEC), AutocrafterManagerData::groups,
ByteBufCodecs.BOOL, AutocrafterManagerData::active,
AutocrafterManagerData::new
);

Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
import javax.annotation.Nullable;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
@@ -27,6 +28,7 @@ public class AutocrafterManagerScreen extends AbstractStretchingScreen<Autocraft
private static final List<String> SEARCH_FIELD_HISTORY = new ArrayList<>();
private static final ResourceLocation AUTOCRAFTER_NAME = createIdentifier("autocrafter_manager/autocrafter_name");
private static final int COLUMNS = 9;
private static final int INACTIVE_COLOR = 0xFF5B5B5B;

@Nullable
private SearchFieldWidget searchField;
@@ -38,7 +40,10 @@ public AutocrafterManagerScreen(final AutocrafterManagerContainerMenu menu,
this.inventoryLabelY = 75;
this.imageWidth = 193;
this.imageHeight = 176;
getMenu().setListener(this::resize);
getMenu().setListener(() -> {
resize();
updateScrollbar();
});
}

@Override
@@ -121,6 +126,17 @@ protected void renderRows(final GuiGraphics graphics,
final int rows,
final int mouseX,
final int mouseY) {
if (!menu.isActive()) {
graphics.fill(
RenderType.guiOverlay(),
x + 7 + 1,
y + TOP_HEIGHT + 1,
x + 7 + (ROW_SIZE * COLUMNS) - 1,
y + TOP_HEIGHT + 1 + (ROW_SIZE * rows) - 2,
INACTIVE_COLOR
);
return;
}
renderRowTitlesAndSlots(graphics, x, y, topHeight, rows);
renderSlotContents(graphics, mouseX, mouseY, y, topHeight, rows);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.refinedmods.refinedstorage.common.autocrafting.autocraftermanager;

@FunctionalInterface
interface AutocrafterManagerWatcher {
void activeChanged(boolean active);
}
Original file line number Diff line number Diff line change
@@ -278,7 +278,7 @@ public GridView getView() {
public void onActiveChanged(final boolean newActive) {
this.active = newActive;
if (this.playerInventory.player instanceof ServerPlayer serverPlayerEntity) {
S2CPackets.sendGridActiveness(serverPlayerEntity, newActive);
S2CPackets.sendGridActive(serverPlayerEntity, newActive);
}
}

Original file line number Diff line number Diff line change
@@ -365,9 +365,7 @@ private String getAmountText(final GridResource resource,
}

private void renderDisabledSlot(final GuiGraphics graphics, final int slotX, final int slotY) {
graphics.fillGradient(
RenderType.guiOverlay(), slotX, slotY, slotX + 16, slotY + 16, DISABLED_SLOT_COLOR, DISABLED_SLOT_COLOR, 0
);
graphics.fill(RenderType.guiOverlay(), slotX, slotY, slotX + 16, slotY + 16, DISABLED_SLOT_COLOR);
}

@Override
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.refinedmods.refinedstorage.common.support.packet.s2c;

import com.refinedmods.refinedstorage.common.autocrafting.autocraftermanager.AutocrafterManagerContainerMenu;
import com.refinedmods.refinedstorage.common.support.packet.PacketContext;

import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.world.inventory.AbstractContainerMenu;

import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier;

public record AutocrafterManagerActivePacket(boolean active) implements CustomPacketPayload {
public static final Type<AutocrafterManagerActivePacket> PACKET_TYPE =
new Type<>(createIdentifier("autocrafter_manager_active"));
public static final StreamCodec<RegistryFriendlyByteBuf, AutocrafterManagerActivePacket> STREAM_CODEC =
StreamCodec.composite(
ByteBufCodecs.BOOL, AutocrafterManagerActivePacket::active,
AutocrafterManagerActivePacket::new
);

public static void handle(final AutocrafterManagerActivePacket packet, final PacketContext ctx) {
final AbstractContainerMenu menu = ctx.getPlayer().containerMenu;
if (menu instanceof AutocrafterManagerContainerMenu containerMenu) {
containerMenu.setActive(packet.active);
}
}

@Override
public Type<? extends CustomPacketPayload> type() {
return PACKET_TYPE;
}
}
Original file line number Diff line number Diff line change
@@ -29,10 +29,14 @@ public static void sendWirelessTransmitterData(final ServerPlayer player, final
Platform.INSTANCE.sendPacketToClient(player, new WirelessTransmitterDataPacket(range, active));
}

public static void sendGridActiveness(final ServerPlayer player, final boolean active) {
public static void sendGridActive(final ServerPlayer player, final boolean active) {
Platform.INSTANCE.sendPacketToClient(player, new GridActivePacket(active));
}

public static void sendAutocrafterManagerActive(final ServerPlayer player, final boolean active) {
Platform.INSTANCE.sendPacketToClient(player, new AutocrafterManagerActivePacket(active));
}

public static void sendGridUpdate(final ServerPlayer player,
final PlatformResourceKey resource,
final long change,
Original file line number Diff line number Diff line change
@@ -15,8 +15,8 @@
public abstract class AbstractStretchingScreen<T extends AbstractBaseContainerMenu & ScreenSizeListener>
extends AbstractBaseScreen<T> {
protected static final int ROW_SIZE = 18;
protected static final int TOP_HEIGHT = 19;

private static final int TOP_HEIGHT = 19;
private static final int INVENTORY_INCLUDING_TITLE_HEIGHT = 99;
private static final int COLUMNS = 9;
private static final int MIN_ROWS = 3;
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import com.refinedmods.refinedstorage.common.storagemonitor.StorageMonitorBlockEntityRenderer;
import com.refinedmods.refinedstorage.common.support.network.item.NetworkItemPropertyFunction;
import com.refinedmods.refinedstorage.common.support.packet.PacketHandler;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterManagerActivePacket;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterNameUpdatePacket;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingPreviewResponsePacket;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingResponsePacket;
@@ -266,6 +267,10 @@ private void registerPacketHandlers() {
GridActivePacket.PACKET_TYPE,
wrapHandler(GridActivePacket::handle)
);
ClientPlayNetworking.registerGlobalReceiver(
AutocrafterManagerActivePacket.PACKET_TYPE,
wrapHandler(AutocrafterManagerActivePacket::handle)
);
ClientPlayNetworking.registerGlobalReceiver(
EnergyInfoPacket.PACKET_TYPE,
wrapHandler(EnergyInfoPacket::handle)
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@
import com.refinedmods.refinedstorage.common.support.packet.c2s.SingleAmountChangePacket;
import com.refinedmods.refinedstorage.common.support.packet.c2s.StorageInfoRequestPacket;
import com.refinedmods.refinedstorage.common.support.packet.c2s.UseSlotReferencedItemPacket;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterManagerActivePacket;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterNameUpdatePacket;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingPreviewResponsePacket;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingResponsePacket;
@@ -421,6 +422,10 @@ private void registerServerToClientPackets() {
WirelessTransmitterDataPacket.STREAM_CODEC
);
PayloadTypeRegistry.playS2C().register(GridActivePacket.PACKET_TYPE, GridActivePacket.STREAM_CODEC);
PayloadTypeRegistry.playS2C().register(
AutocrafterManagerActivePacket.PACKET_TYPE,
AutocrafterManagerActivePacket.STREAM_CODEC
);
PayloadTypeRegistry.playS2C().register(GridClearPacket.PACKET_TYPE, GridClearPacket.STREAM_CODEC);
PayloadTypeRegistry.playS2C().register(GridUpdatePacket.PACKET_TYPE, GridUpdatePacket.STREAM_CODEC);
PayloadTypeRegistry.playS2C().register(
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@
import com.refinedmods.refinedstorage.common.support.packet.c2s.SingleAmountChangePacket;
import com.refinedmods.refinedstorage.common.support.packet.c2s.StorageInfoRequestPacket;
import com.refinedmods.refinedstorage.common.support.packet.c2s.UseSlotReferencedItemPacket;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterManagerActivePacket;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterNameUpdatePacket;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingPreviewResponsePacket;
import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingResponsePacket;
@@ -591,6 +592,11 @@ private static void registerServerToClientPackets(final PayloadRegistrar registr
GridActivePacket.STREAM_CODEC,
wrapHandler(GridActivePacket::handle)
);
registrar.playToClient(
AutocrafterManagerActivePacket.PACKET_TYPE,
AutocrafterManagerActivePacket.STREAM_CODEC,
wrapHandler(AutocrafterManagerActivePacket::handle)
);
registrar.playToClient(
GridClearPacket.PACKET_TYPE,
GridClearPacket.STREAM_CODEC,