-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
BiomeNMSImpl.java
176 lines (155 loc) · 6.74 KB
/
BiomeNMSImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.denizenscript.denizen.nms.v1_18.impl;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.BiomeNMS;
import com.denizenscript.denizen.nms.v1_18.ReflectionMappingsInfo;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.random.WeightedRandomList;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.BiomeSpecialEffects;
import net.minecraft.world.level.biome.MobSpawnSettings;
import net.minecraft.world.level.chunk.LevelChunk;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_18_R2.CraftWorld;
import org.bukkit.entity.EntityType;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
public class BiomeNMSImpl extends BiomeNMS {
public Holder<Biome> biomeBase;
public ServerLevel world;
public BiomeNMSImpl(ServerLevel world, String name) {
super(world.getWorld(), name);
this.world = world;
biomeBase = world.registryAccess().registryOrThrow(Registry.BIOME_REGISTRY).getHolder(ResourceKey.create(Registry.BIOME_REGISTRY, new ResourceLocation(name))).orElse(null);
}
@Override
public DownfallType getDownfallType() {
Biome.Precipitation nmsType = biomeBase.value().getPrecipitation();
return switch (nmsType) {
case RAIN -> DownfallType.RAIN;
case SNOW -> DownfallType.SNOW;
case NONE -> DownfallType.NONE;
default -> throw new UnsupportedOperationException();
};
}
@Override
public float getHumidity() {
return biomeBase.value().getDownfall();
}
@Override
public float getBaseTemperature() {
return biomeBase.value().getBaseTemperature();
}
@Override
public List<EntityType> getAmbientEntities() {
return getSpawnableEntities(MobCategory.AMBIENT);
}
@Override
public List<EntityType> getCreatureEntities() {
return getSpawnableEntities(MobCategory.CREATURE);
}
@Override
public List<EntityType> getMonsterEntities() {
return getSpawnableEntities(MobCategory.MONSTER);
}
@Override
public List<EntityType> getWaterEntities() {
return getSpawnableEntities(MobCategory.WATER_CREATURE);
}
@Override
public int getFoliageColor() {
// Check if the biome already has a default color
if (biomeBase.value().getFoliageColor() != 0) {
return biomeBase.value().getFoliageColor();
}
// Based on net.minecraft.world.level.biome.Biome#getFoliageColorFromTexture()
float temperature = clampColor(getBaseTemperature());
float humidity = clampColor(getHumidity());
// Based on net.minecraft.world.level.FoliageColor#get()
humidity *= temperature;
int humidityValue = (int)((1.0f - humidity) * 255.0f);
int temperatureValue = (int)((1.0f - temperature) * 255.0f);
int index = temperatureValue << 8 | humidityValue;
return index >= 65536 ? 4764952 : getColor(index / 256, index % 256).asRGB();
}
public Object getClimate() {
return ReflectionHelper.getFieldValue(net.minecraft.world.level.biome.Biome.class, ReflectionMappingsInfo.Biome_climateSettings, biomeBase.value());
}
@Override
public void setHumidity(float humidity) {
Object climate = getClimate();
ReflectionHelper.setFieldValue(climate.getClass(), ReflectionMappingsInfo.Biome_ClimateSettings_downfall, climate, humidity);
}
@Override
public void setBaseTemperature(float temperature) {
Object climate = getClimate();
ReflectionHelper.setFieldValue(climate.getClass(), ReflectionMappingsInfo.Biome_ClimateSettings_temperature, climate, temperature);
}
@Override
public void setPrecipitation(DownfallType type) {
Biome.Precipitation nmsType = switch (type) {
case RAIN -> Biome.Precipitation.RAIN;
case SNOW -> Biome.Precipitation.SNOW;
case NONE -> Biome.Precipitation.NONE;
default -> throw new UnsupportedOperationException();
};
Object climate = getClimate();
ReflectionHelper.setFieldValue(climate.getClass(), ReflectionMappingsInfo.Biome_ClimateSettings_precipitation, climate, nmsType);
}
@Override
public void setFoliageColor(int color) {
try {
ReflectionHelper.setFieldValue(BiomeSpecialEffects.class, ReflectionMappingsInfo.BiomeSpecialEffects_foliageColorOverride, biomeBase.value().getSpecialEffects(), Optional.of(color));
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
private List<EntityType> getSpawnableEntities(MobCategory creatureType) {
MobSpawnSettings mobs = biomeBase.value().getMobSettings();
WeightedRandomList<MobSpawnSettings.SpawnerData> typeSettingList = mobs.getMobs(creatureType);
List<EntityType> entityTypes = new ArrayList<>();
if (typeSettingList == null) {
return entityTypes;
}
for (MobSpawnSettings.SpawnerData meta : typeSettingList.unwrap()) {
try {
String n = net.minecraft.world.entity.EntityType.getKey(meta.type).getPath();
EntityType et = EntityType.fromName(n);
if (et == null) {
et = EntityType.valueOf(n.toUpperCase(Locale.ENGLISH));
}
entityTypes.add(et);
}
catch (Throwable e) {
// Ignore the error. Likely from invalid entity type name output.
}
}
return entityTypes;
}
@Override
public void setTo(Block block) {
if (((CraftWorld) block.getWorld()).getHandle() != this.world) {
NMSHandler.instance.getBiomeNMS(block.getWorld(), getName()).setTo(block);
return;
}
// Based on CraftWorld source
BlockPos pos = new BlockPos(block.getX(), 0, block.getZ());
if (world.hasChunkAt(pos)) {
LevelChunk chunk = world.getChunkAt(pos);
if (chunk != null) {
chunk.setBiome(block.getX() >> 2, block.getY() >> 2, block.getZ() >> 2, biomeBase);
chunk.setUnsaved(true);
}
}
}
}