Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
1package net.lerariemann.infinity.util.screen;
2
3import dev.architectury.registry.menu.ExtendedMenuProvider;
4import net.lerariemann.infinity.item.F4Item;
5import net.lerariemann.infinity.registry.core.ModComponentTypes;
6import net.lerariemann.infinity.registry.payload.c2s.F4UpdateC2SPayload;
7import net.lerariemann.infinity.registry.var.ModScreenHandlers;
8import net.lerariemann.infinity.util.VersionMethods;
9import net.minecraft.MethodsReturnNonnullByDefault;
10import net.minecraft.client.player.LocalPlayer;
11//? if <1.21 {
12/*import net.minecraft.nbt.CompoundTag;
13import net.minecraft.util.Mth;
14*///?}
15import net.minecraft.network.FriendlyByteBuf;
16import net.minecraft.network.chat.Component;
17import net.minecraft.resources.ResourceLocation;
18import net.minecraft.world.InteractionHand;
19import net.minecraft.world.entity.player.Inventory;
20import net.minecraft.world.entity.player.Player;
21import net.minecraft.world.inventory.AbstractContainerMenu;
22import net.minecraft.world.inventory.Slot;
23import net.minecraft.world.item.ItemStack;
24import org.jetbrains.annotations.Nullable;
25
26import java.util.concurrent.atomic.AtomicInteger;
27
28//? if >1.21 {
29import net.minecraft.core.component.DataComponentMap;
30//?}
31//? if neoforge {
32/*import javax.annotation.ParametersAreNonnullByDefault;
33
34@ParametersAreNonnullByDefault
35*///?}
36@MethodsReturnNonnullByDefault
37public class F4ScreenHandler extends AbstractContainerMenu {
38 public final Inventory playerInventory;
39 public final ItemStack stack;
40 public final int slot;
41 public AtomicInteger width;
42 public AtomicInteger height;
43
44 public F4ScreenHandler(int syncId, Inventory playerInventory, FriendlyByteBuf packet) {
45 this(syncId, playerInventory, packet.readUtf(), packet.readVarInt());
46 }
47 public F4ScreenHandler(int syncId, Inventory playerInventory, String destination, int slot) {
48 super(ModScreenHandlers.F4.get(), syncId);
49 this.playerInventory = playerInventory;
50 this.slot = slot;
51 stack = playerInventory.getItem(slot);
52 width = new AtomicInteger(VersionMethods.getOrDefaultInt(stack, ModComponentTypes.SIZE_X, 3));
53 height = new AtomicInteger(VersionMethods.getOrDefaultInt(stack, ModComponentTypes.SIZE_Y, 3));
54
55 for (int i = 0; i < 3; i++) {
56 for (int j = 0; j < 9; j++) {
57 this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
58 }
59 }
60 for (int i = 0; i < 9; i++) {
61 this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142));
62 }
63 }
64
65 @Override
66 public void removed(Player player) {
67 ItemStack st = stack.copy();
68 //? if >1.21 {
69 st.applyComponents(DataComponentMap.builder()
70 .set(ModComponentTypes.SIZE_X.get(), Math.clamp(width.get(), 1, 21))
71 .set(ModComponentTypes.SIZE_Y.get(), Math.clamp(height.get(), 1, 21))
72 .build());
73 //?} else {
74 /*CompoundTag nbtCompound = new CompoundTag();
75 nbtCompound.putInt(ModComponentTypes.SIZE_X, Mth.clamp(width.get(), 1, 21));
76 nbtCompound.putInt(ModComponentTypes.SIZE_Y, Mth.clamp(height.get(), 1, 21));
77 stack.setTag(nbtCompound);
78 *///?}
79 playerInventory.setItem(slot, st);
80 super.removed(player);
81 if (player instanceof LocalPlayer) {
82 new F4UpdateC2SPayload(slot, width.get(), height.get()).send();
83 }
84 }
85
86 @Override
87 public ItemStack quickMoveStack(Player player, int slot) {
88 return ItemStack.EMPTY;
89 }
90 @Override
91 public boolean stillValid(Player player) {
92 return false;
93 }
94
95 public static class Factory implements ExtendedMenuProvider {
96 ItemStack f4;
97 int slot;
98 String destination;
99
100 public Factory(Player player) {
101 f4 = player.getItemInHand(InteractionHand.MAIN_HAND);
102 slot = player.getInventory().
103 //? if >1.21.4 {
104 getSelectedSlot();
105 //?} else {
106 /*selected;
107 *///?}
108 ResourceLocation id = VersionMethods.getDimensionIdentifier(f4);
109 destination = id == null ? "" : id.toString();
110 }
111
112 @Override
113 public void saveExtraData(FriendlyByteBuf packetByteBuf) {
114 packetByteBuf.writeUtf(destination);
115 packetByteBuf.writeVarInt(slot);
116 }
117
118 @Override
119 public Component getDisplayName() {
120 return F4Item.getDimensionTooltip(destination.isEmpty() ? null : VersionMethods.id(destination));
121 }
122
123 @Override
124 public @Nullable AbstractContainerMenu createMenu(int syncId, Inventory playerInventory, Player player) {
125 return new F4ScreenHandler(syncId, playerInventory, destination, slot);
126 }
127 }
128}