Skip to content

Commit

Permalink
Re-add current room debug and add in keybind for opening upgrade screen
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed May 28, 2024
1 parent ac23278 commit 15e36a6
Show file tree
Hide file tree
Showing 18 changed files with 314 additions and 213 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.compactmods.machines.client.keybinds.room;

import com.mojang.blaze3d.platform.InputConstants;
import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.api.dimension.CompactDimension;
import dev.compactmods.machines.network.PlayerRequestedUpgradeUIPacket;
import dev.compactmods.machines.room.Rooms;
import net.minecraft.Util;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.neoforged.neoforge.client.settings.IKeyConflictContext;
import net.neoforged.neoforge.network.PacketDistributor;

public class RoomUpgradeUIMapping {

public static final String CATEGORY = Util.makeDescriptionId("key.category", CompactMachinesApi.modRL("general"));
public static final String NAME = Util.makeDescriptionId("key.mapping", CompactMachinesApi.modRL("open_upgrade_screen"));

public static final IKeyConflictContext CONFLICT_CONTEXT = new IKeyConflictContext() {
@Override
public boolean isActive() {
final var level = Minecraft.getInstance().level;
return level != null && level.dimension().equals(CompactDimension.LEVEL_KEY);
}

@Override
public boolean conflicts(IKeyConflictContext other) {
return this == other;
}
};

public static final KeyMapping MAPPING = new KeyMapping(NAME, CONFLICT_CONTEXT, InputConstants.UNKNOWN, CATEGORY);

public static void handle() {
final var level = Minecraft.getInstance().level;
final var player = Minecraft.getInstance().player;
if (level != null && level.dimension().equals(CompactDimension.LEVEL_KEY)) {
final var currentRoom = player.getData(Rooms.DataAttachments.CURRENT_ROOM_CODE);
PacketDistributor.sendToServer(new PlayerRequestedUpgradeUIPacket(currentRoom, true));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package dev.compactmods.machines.client.machine;

import dev.compactmods.machines.client.keybinds.room.RoomExitKeyMapping;
import dev.compactmods.machines.machine.Machines;
import dev.compactmods.machines.room.Rooms;
import dev.compactmods.machines.room.ui.preview.MachineRoomScreen;
import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.client.event.RegisterColorHandlersEvent;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;

Expand All @@ -14,11 +12,6 @@ public static void registerMenuScreens(final RegisterMenuScreensEvent evt) {
evt.register(Rooms.Menus.MACHINE_MENU.get(), MachineRoomScreen::new);
}

public static void handleKeybinds(final ClientTickEvent.Post clientTick) {
if (RoomExitKeyMapping.MAPPING.consumeClick())
RoomExitKeyMapping.handle();
}

public static void onItemColors(final RegisterColorHandlersEvent.Item colors) {
colors.register(MachineColors.ITEM, Machines.Items.BOUND_MACHINE.get(), Machines.Items.UNBOUND_MACHINE.get());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package dev.compactmods.machines.client.machine;

import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.common.NeoForge;

public interface MachinesClient {
static void registerEvents(IEventBus modBus) {
modBus.addListener(MachineClientEvents::registerMenuScreens);
modBus.addListener(MachineClientEvents::onBlockColors);
modBus.addListener(MachineClientEvents::onItemColors);

NeoForge.EVENT_BUS.addListener(MachineClientEvents::handleKeybinds);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.compactmods.machines.client.room;

import dev.compactmods.machines.room.Rooms;
import dev.compactmods.machines.room.ui.preview.MachineRoomScreen;
import net.minecraft.client.Minecraft;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
Expand All @@ -18,12 +19,8 @@ public static void handleBlockData(StructureTemplate blocks) {
public static void handleRoomSync(String roomCode, UUID owner) {
final var mc = Minecraft.getInstance();

// FIXME - Set client-side room data
// mc.player.getCapability(RoomHelper.CURRENT_ROOM_META).ifPresent(meta -> {
// if(owner.equals(Util.NIL_UUID))
// meta.clearCurrent();
// else
// meta.setCurrent(new ClientRoomMetadata(roomCode, owner));
// });
// FIXME - Current Room Owner
mc.player.setData(Rooms.DataAttachments.CURRENT_ROOM_CODE, roomCode);
// mc.player.setData(Rooms.DataAttachments)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package dev.compactmods.machines.client.room;

import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.client.keybinds.room.RoomExitKeyMapping;
import dev.compactmods.machines.client.keybinds.room.RoomUpgradeUIMapping;
import dev.compactmods.machines.room.Rooms;
import dev.compactmods.machines.room.ui.overlay.RoomMetadataDebugOverlay;
import dev.compactmods.machines.room.ui.upgrades.RoomUpgradeScreen;
import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.client.event.RegisterGuiLayersEvent;
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
import net.neoforged.neoforge.client.gui.VanillaGuiLayers;

public class RoomClientEvents {

Expand All @@ -15,9 +20,18 @@ public static void registerMenuScreens(final RegisterMenuScreensEvent evt) {

public static void onKeybindRegistration(final RegisterKeyMappingsEvent evt) {
evt.register(RoomExitKeyMapping.MAPPING);
evt.register(RoomUpgradeUIMapping.MAPPING);
}

public static void handleKeybinds(final ClientTickEvent.Post clientTick) {
if (RoomExitKeyMapping.MAPPING.consumeClick())
RoomExitKeyMapping.handle();

if(RoomUpgradeUIMapping.MAPPING.consumeClick())
RoomUpgradeUIMapping.handle();
}

public static void onOverlayRegistration(final RegisterGuiLayersEvent layers) {
// FIXME overlays.registerAbove(VanillaGuiOverlay.DEBUG_SCREEN.id(), new ResourceLocation(Constants.MOD_ID, "room_meta_debug"), new RoomMetadataDebugOverlay());
layers.registerAbove(VanillaGuiLayers.DEBUG_OVERLAY, CompactMachinesApi.modRL("room_meta_debug"), new RoomMetadataDebugOverlay());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package dev.compactmods.machines.client.room;

import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.common.NeoForge;

public interface RoomsClient {

static void registerEvents(IEventBus modBus) {
modBus.addListener(RoomClientEvents::registerMenuScreens);
modBus.addListener(RoomClientEvents::onKeybindRegistration);
modBus.addListener(RoomClientEvents::onOverlayRegistration);

NeoForge.EVENT_BUS.addListener(RoomClientEvents::handleKeybinds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,12 @@ interface Items {
static void prepare() {
}

Holder<Item> UNBOUND_HOLDER = BuiltInRegistries.ITEM.getHolderOrThrow(MachineConstants.UNBOUND_MACHINE_ITEM_RESKEY);
Holder<Item> BOUND_HOLDER = BuiltInRegistries.ITEM.getHolderOrThrow(MachineConstants.BOUND_MACHINE_ITEM_RESKEY);

static ItemStack unbound() {
return unboundColored(0xFFFFFFFF);
}

static ItemStack unboundColored(int color) {
final var stack = new ItemStack(UNBOUND_HOLDER, 1);
stack.set(Machines.DataComponents.ROOM_TEMPLATE_ID, RoomTemplate.NO_TEMPLATE);
final var stack = UNBOUND_MACHINE.toStack();
stack.set(Machines.DataComponents.MACHINE_COLOR, color);
return stack;
}
Expand All @@ -83,14 +79,14 @@ static ItemStack boundToRoom(String roomCode) {
}

static ItemStack boundToRoom(String roomCode, int color) {
ItemStack stack = new ItemStack(BOUND_HOLDER, 1);
ItemStack stack = BOUND_MACHINE.toStack();
stack.set(Machines.DataComponents.BOUND_ROOM_CODE, roomCode);
stack.set(Machines.DataComponents.MACHINE_COLOR, color);
return stack;
}

static ItemStack forNewRoom(ResourceLocation templateID, RoomTemplate template) {
final var stack = new ItemStack(UNBOUND_HOLDER, 1);
final var stack = UNBOUND_MACHINE.toStack();
stack.set(Machines.DataComponents.ROOM_TEMPLATE_ID, templateID);
stack.set(Machines.DataComponents.ROOM_TEMPLATE, template);
stack.set(Machines.DataComponents.MACHINE_COLOR, template.defaultMachineColor().rgb());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import dev.compactmods.machines.api.Translations;
import dev.compactmods.machines.api.machine.MachineTranslations;
import dev.compactmods.machines.api.room.RoomTemplate;
import dev.compactmods.machines.machine.Machines;
import net.minecraft.Util;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.CommonColors;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
Expand Down Expand Up @@ -35,6 +37,14 @@ public String getDescriptionId(ItemStack stack) {
return Util.makeDescriptionId("machine", getTemplateId(stack));
}

@Override
public ItemStack getDefaultInstance() {
var stack = new ItemStack(this);
stack.set(Machines.DataComponents.ROOM_TEMPLATE_ID, RoomTemplate.NO_TEMPLATE);
stack.set(Machines.DataComponents.MACHINE_COLOR, CommonColors.WHITE);
return stack;
}

private ResourceLocation getTemplateId(ItemStack stack) {
return stack.get(Machines.DataComponents.ROOM_TEMPLATE_ID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void onPacketRegistration(final RegisterPayloadHandlersEvent paylo

main.playToServer(PlayerRequestedRoomUIPacket.TYPE, PlayerRequestedRoomUIPacket.STREAM_CODEC, PlayerRequestedRoomUIPacket.HANDLER);

main.playToServer(PlayerRequestedUpgradeMenuPacket.TYPE, PlayerRequestedUpgradeMenuPacket.STREAM_CODEC, PlayerRequestedUpgradeMenuPacket.HANDLER);
main.playToServer(PlayerRequestedUpgradeUIPacket.TYPE, PlayerRequestedUpgradeUIPacket.STREAM_CODEC, PlayerRequestedUpgradeUIPacket.HANDLER);

main.playToClient(MachineColorSyncPacket.TYPE, MachineColorSyncPacket.STREAM_CODEC, MachineColorSyncPacket.HANDLER);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.compactmods.machines.network;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.api.room.RoomApi;
import dev.compactmods.machines.room.Rooms;
Expand All @@ -18,10 +16,6 @@
public record PlayerRequestedRoomUIPacket(String roomCode) implements CustomPacketPayload {
public static final Type<PlayerRequestedRoomUIPacket> TYPE = new Type<>(CompactMachinesApi.modRL("player_wants_to_open_room_ui"));

public static final Codec<PlayerRequestedRoomUIPacket> CODEC = RecordCodecBuilder.create(inst -> inst.group(
Codec.STRING.fieldOf("roomCode").forGetter(PlayerRequestedRoomUIPacket::roomCode)
).apply(inst, PlayerRequestedRoomUIPacket::new));

public static final StreamCodec<FriendlyByteBuf, PlayerRequestedRoomUIPacket> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.STRING_UTF8, PlayerRequestedRoomUIPacket::roomCode,
PlayerRequestedRoomUIPacket::new
Expand All @@ -31,9 +25,10 @@ public record PlayerRequestedRoomUIPacket(String roomCode) implements CustomPack
final var player = ctx.player();
RoomApi.room(pkt.roomCode).ifPresent(inst -> {
final var server = player.getServer();
final var pos = player.getData(Rooms.DataAttachments.OPEN_MACHINE_POS);
player.openMenu(MachineRoomMenu.provider(server, inst), buf -> {
final var pos = player.getData(Rooms.DataAttachments.OPEN_MACHINE_POS);
buf.writeJsonWithCodec(GlobalPos.CODEC, pos);

buf.writeUtf(pkt.roomCode);
buf.writeOptional(Optional.<String>empty(), FriendlyByteBuf::writeUtf);
});
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.compactmods.machines.network;

import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.api.room.RoomApi;
import dev.compactmods.machines.room.ui.upgrades.RoomUpgradeMenu;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.neoforged.neoforge.network.handling.IPayloadHandler;

public record PlayerRequestedUpgradeUIPacket(String roomCode, boolean isIsolated) implements CustomPacketPayload {

public static final Type<PlayerRequestedUpgradeUIPacket> TYPE = new Type<>(CompactMachinesApi.modRL("player_wants_to_open_room_upgrade_menu"));

public static final IPayloadHandler<PlayerRequestedUpgradeUIPacket> HANDLER = (pkt, ctx) -> {
final var player = ctx.player();
RoomApi.room(pkt.roomCode()).ifPresent(inst -> {
player.openMenu(RoomUpgradeMenu.provider(inst), buf -> {
buf.writeBoolean(pkt.isIsolated);
buf.writeUtf(pkt.roomCode());
});
});
};

public static final StreamCodec<FriendlyByteBuf, PlayerRequestedUpgradeUIPacket> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.STRING_UTF8, PlayerRequestedUpgradeUIPacket::roomCode,
ByteBufCodecs.BOOL, PlayerRequestedUpgradeUIPacket::isIsolated,
PlayerRequestedUpgradeUIPacket::new
);

@Override
public Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}
Loading

0 comments on commit 15e36a6

Please sign in to comment.