Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
1package net.lerariemann.infinity.block.entity;
2
3import net.lerariemann.infinity.block.custom.BiomeBottleBlock;
4import net.lerariemann.infinity.registry.core.ModBlockEntities;
5import net.lerariemann.infinity.registry.core.ModItems;
6import net.lerariemann.infinity.util.VersionMethods;
7import net.lerariemann.infinity.util.core.NbtUtils;
8import net.minecraft.core.BlockPos;
9import net.minecraft.core.Holder;
10//? if >1.21.4 {
11import net.minecraft.core.component.DataComponentGetter;
12import net.minecraft.world.level.storage.ValueInput;
13import net.minecraft.world.level.storage.ValueOutput;
14//?}
15import net.minecraft.nbt.CompoundTag;
16import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
17import net.minecraft.resources.ResourceLocation;
18import net.minecraft.server.level.ServerLevel;
19import net.minecraft.util.Mth;
20import net.minecraft.world.inventory.ContainerData;
21import net.minecraft.world.item.ItemStack;
22import net.minecraft.world.level.Level;
23import net.minecraft.world.level.biome.Biome;
24import net.minecraft.world.level.block.state.BlockState;
25import org.jspecify.annotations.Nullable;
26
27//? if >1.21 {
28import net.lerariemann.infinity.registry.core.ModComponentTypes;
29import net.minecraft.core.HolderLookup;
30import net.minecraft.core.component.DataComponentMap;
31//?}
32
33public class BiomeBottleBlockEntity extends SpecialBlockEntity {
34 private final ContainerData propertyDelegate;
35 private ResourceLocation biome;
36 private int color;
37 public int charge;
38 private int from_charge;
39
40 public BiomeBottleBlockEntity(BlockPos pos, BlockState state) {
41 super(ModBlockEntities.BIOME_BOTTLE.get(), pos, state);
42 this.propertyDelegate = new ContainerData() {
43 public int get(int index) {
44 if (index == 0) {
45 return BiomeBottleBlockEntity.this.color;
46 }
47 return 0;
48 }
49
50 public void set(int index, int value) {
51 if (index == 0) {
52 BiomeBottleBlockEntity.this.color = value;
53 }
54
55 }
56 public int getCount() {
57 return 1;
58 }
59 };
60 this.empty();
61 }
62
63 public void setBiome(Holder<Biome> biome) {
64 if (biome.unwrapKey().isPresent()) setBiome(biome.value().getSkyColor(), biome.unwrapKey().get().location());
65 }
66
67 public void setBiome(int c, ResourceLocation i) {
68 this.color = c;
69 this.biome = i;
70 }
71
72//? if >1.21.4 {
73 public void saveAdditional(ValueOutput tag) {
74 super.saveAdditional(tag);
75//?} else if >1.21 {
76 /*public void saveAdditional(CompoundTag tag, HolderLookup.Provider registryLookup) {
77 super.saveAdditional(tag, registryLookup);
78*///?} else {
79 /*public void saveAdditional(CompoundTag tag) {
80 super.saveAdditional(tag);
81*///?}
82 CompoundTag res = new CompoundTag();
83 res.putString("Biome", biome.toString());
84 res.putInt("Color", color);
85 res.putInt("Charge", charge);
86 if (from_charge > 0) res.putInt("from_charge", from_charge);
87 VersionMethods.saveBlockEntityData(tag, res);
88 }
89
90//? if >1.21.4 {
91 public void loadAdditional(ValueInput tag) {
92 super.loadAdditional(tag);
93//?} else if >1.21 {
94 /*public void loadAdditional(CompoundTag tag, HolderLookup.Provider registryLookup) {
95 super.loadAdditional(tag, registryLookup);
96*///?} else {
97 /*public void load(CompoundTag tag) {
98 super.load(tag);
99*///?}
100 CompoundTag res = VersionMethods.loadBlockEntityData(tag);
101 this.charge = NbtUtils.getInt(res, "Charge");
102 this.biome = VersionMethods.id(NbtUtils.getString(res, "Biome"));
103 this.color = NbtUtils.getInt(res, "Color");
104 this.from_charge = NbtUtils.getInt(res, "from_charge", 0);
105 }
106
107 //? if >1.21 {
108 @Override
109 protected void collectImplicitComponents(DataComponentMap.Builder componentMapBuilder) {
110 super.collectImplicitComponents(componentMapBuilder);
111 BiomeBottleBlock.addComponents(componentMapBuilder, biome, color, charge);
112 }
113
114 @Override
115 protected void applyImplicitComponents(
116 //? if >1.21.4 {
117 DataComponentGetter
118 //?} else {
119 /*DataComponentInput
120 *///?}
121 components) {
122 super.applyImplicitComponents(components);
123 this.biome = components.getOrDefault(ModComponentTypes.BIOME_CONTENTS.get(), BiomeBottleBlock.defaultBiome());
124 this.color = components.getOrDefault(ModComponentTypes.COLOR.get(), 0xFFFFFF);
125 this.charge = components.getOrDefault(ModComponentTypes.CHARGE.get(), 0);
126 }
127 @Override
128 public CompoundTag getUpdateTag(HolderLookup.Provider registryLookup) {
129 return saveWithoutMetadata(registryLookup);
130 }
131 //?}
132
133 @Nullable
134 @Override
135 public ClientboundBlockEntityDataPacket getUpdatePacket() {
136 return ClientboundBlockEntityDataPacket.create(this);
137 }
138
139 public int getTint() {
140 return propertyDelegate.get(0);
141 }
142
143 public ItemStack asStack() {
144 ItemStack itemStack = ModItems.BIOME_BOTTLE_ITEM.get().getDefaultInstance();
145 //? if >1.21
146 itemStack.applyComponents(this.collectComponents());
147 return itemStack;
148 }
149
150 public boolean isTicking() {
151 return from_charge > 0;
152 }
153
154 public void startTicking() {
155 from_charge = charge;
156 }
157
158 public void empty() {
159 color = 0xFFFFFF;
160 biome = BiomeBottleBlock.defaultBiome();
161 charge = 0;
162 from_charge = 0;
163 }
164
165 public static void serverTick(Level world, BlockPos pos, BlockState state, BiomeBottleBlockEntity be) {
166 if (be.isTicking() && world instanceof ServerLevel w) {
167 if (w.getGameTime() % 20 == 0) {
168 int level = be.charge/100;
169 if (level <= 0) {
170 be.empty();
171 }
172 else {
173 int diff2 = be.charge > 1500 ? 500 : 100;
174 int diff = be.charge%diff2;
175 if (diff == 0) diff = diff2;
176 int charge_new = be.charge - diff;
177 BiomeBottleBlock.spreadRing(w, pos, be.biome, be.from_charge - be.charge, be.from_charge - charge_new);
178 be.charge = charge_new;
179 world.setBlockAndUpdate(pos, state.setValue(BiomeBottleBlock.LEVEL, Mth.clamp(level - 1, 0, 10)));
180 BiomeBottleBlock.playSploosh(w, pos);
181 be.setChanged();
182 }
183 }
184 }
185 }
186}