Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
1package net.lerariemann.infinity.util;
2
3import net.lerariemann.infinity.access.GameRendererAccess;
4import net.lerariemann.infinity.registry.core.ModComponentTypes;
5import net.minecraft.client.Minecraft;
6//? if >1.21 {
7import dev.architectury.registry.registries.RegistrySupplier;
8import net.minecraft.core.*;
9import net.minecraft.core.component.DataComponentMap;
10import net.minecraft.core.component.DataComponentType;
11import net.minecraft.resources.ResourceKey;
12import net.minecraft.server.level.ServerLevel;
13import net.minecraft.world.InteractionResult;
14import net.minecraft.world.item.Item;
15import net.minecraft.world.item.JukeboxSong;
16import net.minecraft.world.item.alchemy.PotionContents;
17import net.minecraft.core.component.DataComponents;
18import net.minecraft.world.item.component.CustomModelData;
19//?} else {
20/*import net.minecraft.nbt.CompoundTag;
21import net.minecraft.world.item.alchemy.PotionUtils;
22*///?}
23//? if >1.21.2 {
24//?} else {
25/*import net.minecraft.world.InteractionResultHolder;
26*///?}
27import net.minecraft.resources.ResourceLocation;
28import net.minecraft.world.effect.MobEffect;
29import net.minecraft.world.entity.player.Player;
30import net.minecraft.world.item.ItemStack;
31import net.minecraft.world.item.Items;
32import net.minecraft.world.item.alchemy.Potion;
33import net.minecraft.core.registries.BuiltInRegistries;
34import net.minecraft.core.*;
35import net.minecraft.resources.ResourceKey;
36import net.minecraft.server.level.ServerLevel;
37import net.minecraft.world.item.Item;
38import net.minecraft.world.InteractionResult;
39import net.minecraft.world.level.WorldGenLevel;
40import net.minecraft.world.level.block.Block;
41import net.minecraft.world.level.block.state.BlockBehaviour;
42import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;
43import net.minecraft.world.level.storage.loot.LootTable;
44import net.minecraft.world.phys.Vec3;
45import org.apache.logging.log4j.LogManager;
46import org.jetbrains.annotations.Nullable;
47
48import java.util.List;
49import java.util.Optional;
50import java.util.Set;
51
52public interface VersionMethods {
53 static void loadPP(Minecraft client, String sid) {
54 //? if >1.21.2 {
55 client.gameRenderer.setPostEffect(InfinityMethods.getId(sid));
56 LogManager.getLogger().info(client.gameRenderer.currentPostEffect());
57 //?} else {
58 /*((GameRendererAccess)(client.gameRenderer)).infinity$loadPP(InfinityMethods.getId("shaders/" + sid + ".json"));
59 *///?}
60 }
61
62 @SuppressWarnings("for removal")
63 static ResourceLocation id(String s) {
64 //? if >1.21 {
65 return ResourceLocation.parse(s);
66 //?} else {
67 /*return new ResourceLocation(s);
68 *///?}
69 }
70
71 @SuppressWarnings("for removal")
72 static ResourceLocation id(String namespace, String path) {
73 //? if >=1.21 {
74 return ResourceLocation.fromNamespaceAndPath(namespace, path);
75 //?} else {
76 /*return new ResourceLocation(namespace, path);
77 *///?}
78 }
79
80 //? if <1.21 {
81 /*static int getOrDefaultInt(ItemStack stack, String key, int i) {
82 if (stack.hasTag()) {
83 assert stack.getTag() != null;
84 if (stack.getTag().contains(key))
85 return stack.getTag().getInt(key);
86 }
87 return i;
88 }
89
90 static String getOrDefaultString(ItemStack stack, String key, String i) {
91 if (stack.hasTag()) {
92 assert stack.getTag() != null;
93 if (stack.getTag().contains(key))
94 return stack.getTag().getString(key);
95 }
96 return i;
97 }
98
99 static String getOrNullString(ItemStack stack, String key) {
100 return getOrDefaultString(stack, key, null);
101 }
102
103 static boolean contains(ItemStack stack, String key) {
104 if (stack.hasTag()) {
105 assert stack.getTag() != null;
106 return stack.getTag().contains(key);
107 }
108 return false;
109 }
110
111 static ItemStack apply(ItemStack stack, String key, int i) {
112 if (stack.hasTag()) {
113 assert stack.getTag() != null;
114 stack.getTag().putInt(key, i);
115 return stack;
116 } else {
117 CompoundTag nbtCompound = new CompoundTag();
118 nbtCompound.putInt(key, i);
119 stack.setTag(nbtCompound);
120 }
121 return stack;
122 }
123
124 static ItemStack apply(ItemStack stack, String key, String i) {
125 if (stack.hasTag()) {
126 assert stack.getTag() != null;
127 stack.getTag().putString(key, i);
128 return stack;
129 } else {
130 CompoundTag nbtCompound = new CompoundTag();
131 nbtCompound.putString(key, i);
132 stack.setTag(nbtCompound);
133 }
134 return stack;
135 }
136
137 static @Nullable String getDimensionComponents(ItemStack stack) {
138 if (stack.getTag() != null && stack.getTag().contains(ModComponentTypes.DESTINATION)) {
139 return stack.getTag().getString(ModComponentTypes.DESTINATION);
140 }
141 return null;
142 }
143
144 static @Nullable ResourceLocation getDimensionIdentifier(ItemStack stack) {
145 String dimension = getDimensionComponents(stack);
146 if (dimension != null) return ResourceLocation.tryParse(dimension);
147 else return null;
148 }
149
150 static void remove(ItemStack result, String destination) {
151 if (result.hasTag()) {
152 result.removeTagKey(destination);
153 }
154 }
155
156 static Optional<Potion> getOrEmptyPotion(ItemStack input) {
157 return Optional.of(PotionUtils.getPotion(input));
158 }
159
160 static boolean isPotion(Potion input, Potion awkward) {
161 return input.equals(awkward);
162 }
163
164 *///?} else {
165 static int getOrDefaultInt(ItemStack stack, RegistrySupplier<DataComponentType<Integer>> component, int i) {
166 return stack.getOrDefault(component.get(), i);
167 }
168 static String getOrDefaultString(ItemStack stack, RegistrySupplier<DataComponentType<String>> component, String s) {
169 return stack.getOrDefault(component.get(), s);
170 }
171
172 static String getOrNullString(ItemStack stack, RegistrySupplier<DataComponentType<String>> component) {
173 return stack.get(component.get());
174 }
175
176 static @Nullable ResourceLocation getDimensionIdentifier(ItemStack stack) {
177 return stack.getComponents().get(ModComponentTypes.DESTINATION.get());
178 }
179
180 static ItemStack apply(ItemStack stack, RegistrySupplier<DataComponentType<Integer>> charge, int i) {
181 stack.applyComponents(DataComponentMap.builder()
182 .set(charge.get(), i).build());
183 return stack;
184 }
185
186 static Optional<Holder<Potion>> getOrEmptyPotion(ItemStack input) {
187 return input.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY).potion();
188 }
189
190 static boolean isPotion(Holder<Potion> input, Holder<Potion> awkward) {
191 return input.value().equals(awkward.value());
192 }
193
194 static boolean contains(ItemStack currStack, RegistrySupplier<DataComponentType<String>> dyeColor) {
195 return currStack.has(dyeColor.get());
196 }
197
198 static <T> T remove(ItemStack stack, RegistrySupplier<DataComponentType<T>> component) {
199 return stack.remove(component.get());
200 }
201
202 //?}
203
204 static BlockPos clampToBounds(WorldGenLevel structureWorldAccess, BlockPos destination) {
205 return structureWorldAccess.getWorldBorder().clampToBounds(
206 //? if >1.21 {
207 destination
208 //?} else {
209 /*destination.getX(), destination.getY(), destination.getZ()
210 *///?}
211 );
212 }
213
214 static BlockBehaviour.Properties copyBlockProperties(Block b) {
215 //? if >1.21 {
216 return BlockBehaviour.Properties.ofFullCopy(b);
217 //?} else {
218 /*return BlockBehaviour.Properties.copy(b);
219 *///?}
220 }
221
222 static <T> String getNameAsString(Registry<T> reg, T obj) {
223 //? if >1.21 {
224 return reg.wrapAsHolder(obj).getRegisteredName();
225 //?} else {
226 /*return reg.getKey(obj).toString();
227 *///?}
228 }
229
230 static <T> Holder<T> getFromId(Registry<T> reg, ResourceLocation id) {
231 //? if >1.21.2 {
232 Optional<Holder.Reference<T>> entry = reg.get(id);
233 return entry.orElse(null);
234 //?} else if >1.21 {
235 /*Optional<Holder.Reference<T>> entry = reg.getHolder(id);
236 return entry.orElse(null);
237 *///?} else {
238 /*T obj = reg.get(id);
239 return reg.wrapAsHolder(obj);
240 *///?}
241 }
242
243 static <T> String getRegisteredName(Holder<T> holder) {
244 //? if >1.21 {
245 return holder.getRegisteredName();
246 //?} else {
247 /*return holder.unwrapKey().map(key -> key.location().toString()).orElse("[unregistered]");
248 *///?}
249 }
250
251 static @Nullable ResourceLocation getBiomeComponents(ItemStack stack) {
252 //? if >1.21 {
253 return stack.getComponents().get(ModComponentTypes.BIOME_CONTENTS.get());
254 //?} else {
255 /*if (stack.getTag() != null) {
256 return InfinityMethods.getId(stack.getTag().getCompound("BlockEntityTag").getString("Biome"));
257 }
258 return null;
259 *///?}
260 }
261
262 //? if forge {
263 /*static MobEffect effectOrHolder(Holder<? extends MobEffect> holder) {
264 return holder.value();
265 }
266 *///?} else {
267 static
268 //? if >1.21 {
269 Holder<MobEffect>
270 //?} else {
271 /*MobEffect
272 *///?}
273 effectOrHolder(Holder<
274 MobEffect> holder) {
275 //? if >1.21 {
276 return holder;
277 //?} else {
278 /*return holder.value();
279 *///?}
280 }
281 //?}
282
283 static MobEffect unholdEffect(
284 //? if >1.21 {
285 Holder<MobEffect>
286 //?} else {
287 /*MobEffect
288 *///?}
289 holder) {
290 //? if >1.21 {
291 return holder.value();
292 //?} else {
293 /*return holder;
294 *///?}
295 }
296
297 static int opaque(int color) {
298 return color | -16777216;
299 }
300
301 static void consumeWithRemainder(Player player, ItemStack stack, ItemStack defaultInstance) {
302 //? if >1.21 {
303 boolean creative = player.hasInfiniteMaterials();
304 //?} else {
305 /*boolean creative = player.isCreative();
306 *///?}
307 if (!creative) {
308 stack.setCount(stack.getCount()-1);
309 player.getInventory().add(new ItemStack(Items.GLASS_BOTTLE));
310 }
311 }
312
313 static Item getItem(String name) {
314 return getFromRegistry(BuiltInRegistries.ITEM, id(name));
315 }
316 static Block getBlock(String name) {
317 return getFromRegistry(BuiltInRegistries.BLOCK, id(name));
318 }
319 static Block getBlock(ResourceLocation name) {
320 return getFromRegistry(BuiltInRegistries.BLOCK, name);
321 }
322 static Block getBlock(ResourceKey<Block> name) {
323 return getFromRegistry(BuiltInRegistries.BLOCK, name);
324 }
325
326 static void teleportTo(Player player, ServerLevel level, Vec3 pos, float yaw, float pitch) {
327 //? if >1.21.4 {
328 player.teleportTo(level, pos.x, pos.y, pos.z, Set.of(), yaw, pitch, true);
329 //?} else {
330 /*player.teleportTo(level, pos.x, pos.y, pos.z, Set.of(), yaw, pitch);
331 *///?}
332 }
333
334 //? if >1.21.2 {
335 static InteractionResult sidedSuccess(ItemStack itemStack, boolean clientSide) {
336 return InteractionResult.SUCCESS_SERVER.heldItemTransformedTo(itemStack);
337 }
338 static InteractionResult sidedSuccess(boolean clientSide) {
339 return InteractionResult.SUCCESS_SERVER;
340 }
341 static InteractionResult success(ItemStack itemInHand) {
342 return InteractionResult.SUCCESS.heldItemTransformedTo(itemInHand);
343 }
344 static InteractionResult pass(ItemStack stack) {
345 return InteractionResult.PASS;
346 }
347 static InteractionResult consume(ItemStack stack) {
348 return InteractionResult.CONSUME;
349 }
350 static ItemStack getInteractionHolderResult(InteractionResult result) {
351 if (result instanceof InteractionResult.Success success) {
352 return success.heldItemTransformedTo();
353 }
354 return null;
355 }
356
357 static CustomModelData getColoredModel(int color) {
358 return new CustomModelData(List.of(), List.of(), List.of(), List.of(color));
359 }
360
361 static <T> T getFromRegistry(Registry<T> registry, ResourceLocation name) {
362 return registry.getValue(name);
363 }
364 static <T> T getFromRegistry(Registry<T> registry, ResourceKey<T> name) {
365 return registry.getValue(name);
366 }
367
368 static <T> Registry<T> getRegistry(RegistryAccess access, ResourceKey<Registry<T>> jukeboxSong) {
369 return access.lookupOrThrow(jukeboxSong);
370 }
371
372 //?} else {
373 /*static InteractionResultHolder<ItemStack> sidedSuccess(ItemStack itemStack, boolean clientSide) {
374 return InteractionResultHolder.sidedSuccess(itemStack, clientSide);
375 }
376 static InteractionResult sidedSuccess(boolean clientSide) {
377 return InteractionResult.sidedSuccess(clientSide);
378 }
379 static InteractionResultHolder<ItemStack> success(ItemStack itemInHand) {
380 return InteractionResultHolder.success(itemInHand);
381 }
382 static InteractionResultHolder<ItemStack> pass(ItemStack stack) {
383 return InteractionResultHolder.pass(stack);
384 }
385 static InteractionResultHolder<ItemStack> consume(ItemStack stack) {
386 return InteractionResultHolder.consume(stack);
387 }
388 //? if >1.21 {
389 static CustomModelData getColoredModel(int color) {
390 return new CustomModelData(color);
391 }
392 //?}
393 static ItemStack getInteractionHolderResult(InteractionResultHolder<ItemStack> result) {
394 return result.getObject();
395 }
396 static <T> T getFromRegistry(Registry<T> registry, ResourceLocation name) {
397 return registry.get(name);
398 }
399 static <T> T getFromRegistry(Registry<T> registry, ResourceKey<T> name) {
400 return registry.get(name);
401 }
402
403 static <T> Registry<T> getRegistry(RegistryAccess access, ResourceKey<Registry<T>> jukeboxSong) {
404 return access.registryOrThrow(jukeboxSong);
405 }
406 *///?}
407 static <T> HolderLookup.RegistryLookup<T> getRegistry(HolderLookup.Provider lookup, ResourceKey<Registry<T>> lootTable) {
408 return lookup.lookupOrThrow(lootTable);
409 }
410}
411