/
BiomeNMSImpl.java
158 lines (139 loc) · 5.64 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
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 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.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;
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();
switch (nmsType) {
case RAIN:
return DownfallType.RAIN;
case SNOW:
return DownfallType.SNOW;
case NONE:
return DownfallType.NONE;
default:
throw new UnsupportedOperationException();
}
}
@Override
public float getHumidity() {
return biomeBase.value().getDownfall();
}
@Override
public float getTemperature() {
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);
}
public Object getClimate() {
return ReflectionHelper.getFieldValue(net.minecraft.world.level.biome.Biome.class, ReflectionMappingsInfo.Biome_climateSettings, biomeBase);
}
@Override
public void setHumidity(float humidity) {
Object climate = getClimate();
ReflectionHelper.setFieldValue(climate.getClass(), ReflectionMappingsInfo.Biome_ClimateSettings_downfall, climate, humidity);
}
@Override
public void setTemperature(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 NONE:
nmsType = Biome.Precipitation.NONE;
break;
case RAIN:
nmsType = Biome.Precipitation.RAIN;
break;
case SNOW:
nmsType = Biome.Precipitation.SNOW;
break;
default:
throw new UnsupportedOperationException();
}
Object climate = getClimate();
ReflectionHelper.setFieldValue(climate.getClass(), ReflectionMappingsInfo.Biome_ClimateSettings_precipitation, climate, nmsType);
}
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.getInstance().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);
}
}
}
}