Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
1package net.lerariemann.infinity.item;
2
3import net.lerariemann.infinity.block.entity.InfinityPortalBlockEntity;
4import net.lerariemann.infinity.registry.core.ModComponentTypes;
5import net.lerariemann.infinity.util.VersionMethods;
6import net.lerariemann.infinity.util.InfinityMethods;
7//? if >1.21 {
8import net.minecraft.core.component.DataComponentPatch;
9//?} else {
10/*import net.minecraft.nbt.CompoundTag;
11*///?}
12import net.minecraft.resources.ResourceLocation;
13import net.minecraft.world.entity.item.ItemEntity;
14import net.minecraft.world.item.Item;
15import net.minecraft.world.item.ItemStack;
16import net.minecraft.world.level.Level;
17import org.jetbrains.annotations.Nullable;
18
19import java.util.Optional;
20
21public interface PortalDataHolder {
22 @Nullable
23 default ResourceLocation getDestination(ItemStack stack) {
24 return VersionMethods.getDimensionIdentifier(stack);
25 }
26
27 static boolean isDestinationRandom(ResourceLocation id) {
28 return (id != null && id.toString().equals(InfinityMethods.ofRandomDim));
29 }
30
31 default ResourceLocation getDestinationParsed(ItemStack stack, Level world) {
32 ResourceLocation id = getDestination(stack);
33 return (isDestinationRandom(id)) ? InfinityMethods.getRandomId(world.random) : id;
34 }
35
36 //? if >1.21 {
37 static Optional<DataComponentPatch> addPortalComponents(Item item, InfinityPortalBlockEntity ipbe) {
38 if (item instanceof PortalDataHolder pdh)
39 return Optional.of(pdh.getPortalComponents(ipbe));
40 return Optional.empty();
41 }
42 default DataComponentPatch getPortalComponents(InfinityPortalBlockEntity ipbe) {
43 return DataComponentPatch.builder()
44 .set(ModComponentTypes.COLOR.get(), ipbe.getPortalColor())
45 .build();
46 }
47
48 static Optional<DataComponentPatch> getIridComponents(Item item, ItemEntity entity) {
49 if (item instanceof PortalDataHolder pdh)
50 return pdh.getIridComponents(entity);
51 return Optional.empty();
52 }
53 default Optional<DataComponentPatch> getIridComponents(ItemEntity entity) {
54 return Optional.empty();
55 }
56
57 default ItemStack withPortalData(InfinityPortalBlockEntity ipbe) {
58 ItemStack stack = getStack();
59 stack.applyComponentsAndValidate(getPortalComponents(ipbe));
60 return stack;
61 }
62 //?} else {
63 /*static Optional<CompoundTag> addPortalComponents(Item item, InfinityPortalBlockEntity ipbe) {
64 if (item instanceof PortalDataHolder pdh)
65 return Optional.of(pdh.getPortalComponents(ipbe));
66 return Optional.empty();
67 }
68 default CompoundTag getPortalComponents(InfinityPortalBlockEntity ipbe) {
69 var compound = new CompoundTag();
70 compound.putInt(ModComponentTypes.COLOR, ipbe.getPortalColor());
71 return compound;
72 }
73
74 static Optional<CompoundTag> getIridComponents(Item item, ItemEntity entity) {
75 if (item instanceof PortalDataHolder pdh)
76 return pdh.getIridComponents(entity);
77 return Optional.empty();
78 }
79 default Optional<CompoundTag> getIridComponents(ItemEntity entity) {
80 return Optional.empty();
81 }
82
83 default ItemStack withPortalData(InfinityPortalBlockEntity ipbe) {
84 ItemStack stack = getStack();
85 CompoundTag tag = stack.getOrCreateTag();
86 var portalComponents = getPortalComponents(ipbe);
87 if (portalComponents.contains(ModComponentTypes.COLOR))
88 tag.putInt(ModComponentTypes.COLOR, portalComponents.getInt(ModComponentTypes.COLOR));
89 if (portalComponents.contains(ModComponentTypes.DESTINATION))
90 tag.putString(ModComponentTypes.DESTINATION, portalComponents.getString(ModComponentTypes.DESTINATION));
91 return stack;
92 }
93 *///?}
94
95 ItemStack getStack();
96
97 interface Destinable extends PortalDataHolder {
98 //? if >1.21 {
99 @Override
100 default DataComponentPatch getPortalComponents(InfinityPortalBlockEntity ipbe) {
101 return DataComponentPatch.builder()
102 .set(ModComponentTypes.DESTINATION.get(), ipbe.getDimension())
103 .set(ModComponentTypes.COLOR.get(), ipbe.getPortalColor())
104 .build();
105 }
106 //?} else {
107 /*@Override
108 default CompoundTag getPortalComponents(InfinityPortalBlockEntity ipbe) {
109 var compound = new CompoundTag();
110 compound.putInt(ModComponentTypes.COLOR, ipbe.getPortalColor());
111 compound.putString(ModComponentTypes.DESTINATION, ipbe.getDimension().toString());
112 return compound;
113 }
114 *///?}
115 }
116}