Skip to content

Commit

Permalink
add menu registry
Browse files Browse the repository at this point in the history
  • Loading branch information
ToCraft committed Oct 18, 2024
1 parent 6c43bc0 commit eb88087
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 495 deletions.
32 changes: 25 additions & 7 deletions common/src/main/java/dev/tocraft/crafted/ctgen/impl/CTGClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,46 @@
import net.fabricmc.api.Environment;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;

@Environment(EnvType.CLIENT)
@ApiStatus.Internal
public class CTGClient {
@ApiStatus.Internal
public static final AtomicReference<SyncMapPacket> LAST_SYNC_MAP_PACKET = new AtomicReference<>(null);
@ApiStatus.Internal
public static final KeyMapping OPEN_MAP_KEY = new KeyMapping("key.ctgen_map", InputConstants.Type.KEYSYM, GLFW.GLFW_KEY_G, "key.categories.ui");
private static final AtomicBoolean MENU = new AtomicBoolean(true);
private static final Map<ResourceLocation, BiFunction<Minecraft, SyncMapPacket, Screen>> MENU_REGISTRY = new ConcurrentHashMap<>();

public static void tick(Minecraft minecraft) {
@ApiStatus.Internal
public static void tick(@NotNull Minecraft minecraft) {
assert minecraft.player != null;

if (OPEN_MAP_KEY.consumeClick() && MENU.get()) {
minecraft.setScreen(new MapScreen(minecraft));
if (OPEN_MAP_KEY.consumeClick()) {
SyncMapPacket packet = CTGClient.LAST_SYNC_MAP_PACKET.get();
BiFunction<Minecraft, SyncMapPacket, Screen> screenFunc = MapScreen::new;
if (packet != null) {
ResourceLocation mapId = packet.getMapId();
if (mapId != null && MENU_REGISTRY.containsKey(mapId)) {
screenFunc = MENU_REGISTRY.get(mapId);
}
}
minecraft.setScreen(screenFunc.apply(minecraft, packet));
}
}

public static void disableMapMenu() {
MENU.set(false);
@SuppressWarnings("unused")
@ApiStatus.Internal
public static void registerMenu(ResourceLocation mapId, BiFunction<Minecraft, SyncMapPacket, Screen> entry) {
MENU_REGISTRY.put(mapId, entry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@ApiStatus.Internal
public class SyncMapPacket {
public static final ResourceLocation PACKET_ID = CTerrainGeneration.id("sync_map_id");

Expand All @@ -21,6 +20,7 @@ public class SyncMapPacket {
private final int mapWidth;
private final int mapHeight;

@ApiStatus.Internal
public SyncMapPacket(@Nullable ResourceLocation mapId, int xOffset, int yOffset, int mapWidth, int mapHeight) {
this.mapId = mapId;
this.xOffset = xOffset;
Expand All @@ -29,6 +29,7 @@ public SyncMapPacket(@Nullable ResourceLocation mapId, int xOffset, int yOffset,
this.mapHeight = mapHeight;
}

@ApiStatus.Internal
public void encode(@NotNull FriendlyByteBuf buf) {
boolean bl = this.mapId != null;
buf.writeBoolean(bl);
Expand All @@ -41,6 +42,7 @@ public void encode(@NotNull FriendlyByteBuf buf) {
}
}

@ApiStatus.Internal
public static @NotNull SyncMapPacket decode(@NotNull FriendlyByteBuf buf) {
boolean bl = buf.readBoolean();
if (bl) {
Expand All @@ -52,39 +54,41 @@ public void encode(@NotNull FriendlyByteBuf buf) {
return new SyncMapPacket(mapId, xOffset, yOffset, mapWidth, mapHeight);
}
else {
return new SyncMapPacket(null, -1, -1, -1, -1);
return empty();
}
}

@ApiStatus.Internal
public static SyncMapPacket empty() {
return new SyncMapPacket(null, -1, -1, -1, -1);
}

@ApiStatus.Internal
public void handle() {
CTGClient.LAST_SYNC_MAP_PACKET.set(this);
}

@ApiStatus.Internal
public void send(ServerPlayer to) {
ServerPlatform.INSTANCE.send(this, to);
}

@ApiStatus.Internal
public @Nullable ResourceLocation getMapId() {
return mapId;
}

@ApiStatus.Internal
public int getXOffset() {
return xOffset;
}

@ApiStatus.Internal
public int getYOffset() {
return yOffset;
}

@ApiStatus.Internal
public int getMapHeight() {
return mapHeight;
}

@ApiStatus.Internal
public int getMapWidth() {
return mapWidth;
}
Expand Down
Loading

0 comments on commit eb88087

Please sign in to comment.