Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more biome settings to Bukkit platform #477

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
import ca.solostudios.strata.version.Version;

import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.event.events.config.ConfigurationLoadEvent;
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
import com.dfsek.terra.api.world.biome.Biome;
import com.dfsek.terra.bukkit.config.PreLoadCompatibilityOptions;
import com.dfsek.terra.bukkit.config.VanillaBiomeProperties;


public class BukkitAddon implements BaseAddon {
private static final Version VERSION = Versions.getVersion(1, 0, 0);

private final PlatformImpl terraBukkitPlugin;
protected final PlatformImpl terraBukkitPlugin;

public BukkitAddon(PlatformImpl terraBukkitPlugin) {
this.terraBukkitPlugin = terraBukkitPlugin;
Expand All @@ -28,16 +25,6 @@ public void initialize() {
.register(this, ConfigPackPreLoadEvent.class)
.then(event -> event.getPack().getContext().put(event.loadTemplate(new PreLoadCompatibilityOptions())))
.global();

terraBukkitPlugin.getEventManager()
.getHandler(FunctionalEventHandler.class)
.register(this, ConfigurationLoadEvent.class)
.then(event -> {
if(event.is(Biome.class)) {
event.getLoadedObject(Biome.class).getContext().put(event.load(new VanillaBiomeProperties()));
}
})
.global();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.dfsek.tectonic.api.TypeRegistry;
import com.dfsek.tectonic.api.depth.DepthTracker;
import com.dfsek.tectonic.api.exception.LoadException;

import com.dfsek.terra.bukkit.nms.Initializer;

import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -96,7 +99,7 @@ public void runPossiblyUnsafeTask(@NotNull Runnable runnable) {

@Override
protected Iterable<BaseAddon> platformAddon() {
return List.of(new BukkitAddon(this));
return List.of(Initializer.nmsAddon(this));
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dfsek.terra.bukkit.nms;

import com.dfsek.terra.bukkit.BukkitAddon;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -13,20 +15,11 @@ public interface Initializer {

static boolean init(PlatformImpl platform) {
Logger logger = LoggerFactory.getLogger(Initializer.class);
try {
String packageVersion = NMS;
if(NMS.equals("v1_21_1")) {
packageVersion = "v1_21";
}

Class<?> initializerClass = Class.forName(TERRA_PACKAGE + "." + packageVersion + ".NMSInitializer");
try {
Initializer initializer = (Initializer) initializerClass.getConstructor().newInstance();
initializer.initialize(platform);
} catch(ReflectiveOperationException e) {
throw new RuntimeException("Error initializing NMS bindings. Report this to Terra.", e);
}
} catch(ClassNotFoundException e) {
Initializer initializer = constructInitializer();
if(initializer != null) {
initializer.initialize(platform);
} else {
logger.error("NMS bindings for version {} do not exist. Support for this version is limited.", NMS);
logger.error("This is usually due to running Terra on an unsupported Minecraft version.");
String bypassKey = "IKnowThereAreNoNMSBindingsFor" + NMS + "ButIWillProceedAnyway";
Expand All @@ -49,8 +42,34 @@ static boolean init(PlatformImpl platform) {
logger.error("Since you enabled the \"{}\" flag, we won't disable Terra. But be warned.", bypassKey);
}
}

return true;
}

static BukkitAddon nmsAddon(PlatformImpl platform) {
Initializer initializer = constructInitializer();
return initializer != null ? initializer.getNMSAddon(platform) : new BukkitAddon(platform);
}

private static Initializer constructInitializer() {
try {
String packageVersion = NMS;
if(NMS.equals("v1_21_3")) {
packageVersion = "v1_21"; // TODO: Refactor nms package to v1_21_3
}

Class<?> initializerClass = Class.forName(TERRA_PACKAGE + "." + packageVersion + ".NMSInitializer");
try {
return (Initializer) initializerClass.getConstructor().newInstance();
} catch(ReflectiveOperationException e) {
throw new RuntimeException("Error initializing NMS bindings. Report this to Terra.", e);
}
} catch(ClassNotFoundException e) {
return null;
}
}

void initialize(PlatformImpl plugin);

BukkitAddon getNMSAddon(PlatformImpl plugin);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.dfsek.terra.bukkit.nms.v1_21;

import com.dfsek.terra.api.event.events.config.ConfigurationLoadEvent;
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
import com.dfsek.terra.api.world.biome.Biome;
import com.dfsek.terra.bukkit.BukkitAddon;
import com.dfsek.terra.bukkit.PlatformImpl;
import com.dfsek.terra.bukkit.nms.v1_21.config.VanillaBiomeProperties;


public class NMSAddon extends BukkitAddon {

public NMSAddon(PlatformImpl platform) {
super(platform);
}

@Override
public void initialize() {
super.initialize();

terraBukkitPlugin.getEventManager()
.getHandler(FunctionalEventHandler.class)
.register(this, ConfigurationLoadEvent.class)
.then(event -> {
if(event.is(Biome.class)) {
event.getLoadedObject(Biome.class).getContext().put(event.load(new VanillaBiomeProperties()));
}
})
.global();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.Optional;

import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.bukkit.config.VanillaBiomeProperties;
import com.dfsek.terra.bukkit.nms.v1_21.config.VanillaBiomeProperties;


public class NMSBiomeInjector {
Expand All @@ -26,49 +26,62 @@ public static Biome createBiome(com.dfsek.terra.api.world.biome.Biome biome, Bio
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Biome.BiomeBuilder builder = new Biome.BiomeBuilder();

builder
.downfall(vanilla.climateSettings.downfall())
.temperature(vanilla.getBaseTemperature())
.mobSpawnSettings(vanilla.getMobSettings())
.generationSettings(vanilla.getGenerationSettings());


BiomeSpecialEffects.Builder effects = new BiomeSpecialEffects.Builder();

effects.grassColorModifier(vanilla.getSpecialEffects().getGrassColorModifier());

VanillaBiomeProperties vanillaBiomeProperties = biome.getContext().get(VanillaBiomeProperties.class);

effects.fogColor(Objects.requireNonNullElse(vanillaBiomeProperties.getFogColor(), vanilla.getFogColor()))

.waterColor(Objects.requireNonNullElse(vanillaBiomeProperties.getWaterColor(), vanilla.getWaterColor()))

.waterFogColor(Objects.requireNonNullElse(vanillaBiomeProperties.getWaterFogColor(), vanilla.getWaterFogColor()))
.skyColor(Objects.requireNonNullElse(vanillaBiomeProperties.getSkyColor(), vanilla.getSkyColor()))
.grassColorModifier(Objects.requireNonNullElse(vanillaBiomeProperties.getGrassColorModifier(), vanilla.getSpecialEffects().getGrassColorModifier()))
.grassColorOverride(Objects.requireNonNullElse(vanillaBiomeProperties.getGrassColor(), vanilla.getSpecialEffects().getGrassColorOverride().orElseGet(() -> Reflection.BIOME.invokeGrassColorFromTexture(vanilla))))
.foliageColorOverride(Objects.requireNonNullElse(vanillaBiomeProperties.getFoliageColor(), vanilla.getFoliageColor()));

if(vanillaBiomeProperties.getParticleConfig() == null) {
vanilla.getSpecialEffects().getAmbientParticleSettings().ifPresent(effects::ambientParticle);
} else {
effects.ambientParticle(vanillaBiomeProperties.getParticleConfig());
}

.skyColor(Objects.requireNonNullElse(vanillaBiomeProperties.getSkyColor(), vanilla.getSkyColor()));
if(vanillaBiomeProperties.getLoopSound() == null) {
vanilla.getSpecialEffects().getAmbientLoopSoundEvent().ifPresent(effects::ambientLoopSound);
} else {
RegistryFetcher.soundEventRegistry().get(vanillaBiomeProperties.getLoopSound().location()).ifPresent(effects::ambientLoopSound);
}

if(vanillaBiomeProperties.getMoodSound() == null) {
vanilla.getSpecialEffects().getAmbientMoodSettings().ifPresent(effects::ambientMoodSound);
} else {
effects.ambientMoodSound(vanillaBiomeProperties.getMoodSound());
}

if(vanillaBiomeProperties.getFoliageColor() == null) {
vanilla.getSpecialEffects().getFoliageColorOverride().ifPresent(effects::foliageColorOverride);
if(vanillaBiomeProperties.getAdditionsSound() == null) {
vanilla.getSpecialEffects().getAmbientAdditionsSettings().ifPresent(effects::ambientAdditionsSound);
} else {
effects.foliageColorOverride(vanillaBiomeProperties.getFoliageColor());
effects.ambientAdditionsSound(vanillaBiomeProperties.getAdditionsSound());
}

if(vanillaBiomeProperties.getGrassColor() == null) {
vanilla.getSpecialEffects().getGrassColorOverride().ifPresent(effects::grassColorOverride);
if(vanillaBiomeProperties.getMusic() == null) {
vanilla.getSpecialEffects().getBackgroundMusic().ifPresent(effects::backgroundMusic);
} else {
// grass
effects.grassColorOverride(vanillaBiomeProperties.getGrassColor());
effects.backgroundMusic(vanillaBiomeProperties.getMusic());
}

vanilla.getAmbientLoop().ifPresent(effects::ambientLoopSound);
vanilla.getAmbientAdditions().ifPresent(effects::ambientAdditionsSound);
vanilla.getAmbientMood().ifPresent(effects::ambientMoodSound);
vanilla.getBackgroundMusic().ifPresent(effects::backgroundMusic);
vanilla.getAmbientParticle().ifPresent(effects::ambientParticle);
builder.hasPrecipitation(Objects.requireNonNullElse(vanillaBiomeProperties.getPrecipitation(), vanilla.hasPrecipitation()));

builder.temperature(Objects.requireNonNullElse(vanillaBiomeProperties.getTemperature(), vanilla.getBaseTemperature()));

builder.downfall(Objects.requireNonNullElse(vanillaBiomeProperties.getDownfall(), vanilla.climateSettings.downfall()));

builder.temperatureAdjustment(Objects.requireNonNullElse(vanillaBiomeProperties.getTemperatureModifier(), vanilla.climateSettings.temperatureModifier()));

builder.specialEffects(effects.build());
builder.mobSpawnSettings(Objects.requireNonNullElse(vanillaBiomeProperties.getSpawnSettings(), vanilla.getMobSettings()));

return builder.build();
return builder
.specialEffects(effects.build())
.generationSettings(vanilla.getGenerationSettings())
.build();
}

public static String createBiomeID(ConfigPack pack, com.dfsek.terra.api.registry.key.RegistryKey biomeID) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dfsek.terra.bukkit.nms.v1_21;

import com.dfsek.terra.bukkit.BukkitAddon;

import org.bukkit.Bukkit;

import com.dfsek.terra.bukkit.PlatformImpl;
Expand All @@ -12,4 +14,9 @@ public void initialize(PlatformImpl platform) {
AwfulBukkitHacks.registerBiomes(platform.getRawConfigRegistry());
Bukkit.getPluginManager().registerEvents(new NMSInjectListener(), platform.getPlugin());
}

@Override
public BukkitAddon getNMSAddon(PlatformImpl plugin) {
return new NMSAddon(plugin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.server.level.ChunkMap;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.StructureManager;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.chunk.status.WorldGenContext;
import xyz.jpenilla.reflectionremapper.ReflectionRemapper;
import xyz.jpenilla.reflectionremapper.proxy.ReflectionProxyFactory;
Expand All @@ -24,8 +25,10 @@ public class Reflection {

public static final ReferenceProxy REFERENCE;


public static final ChunkMapProxy CHUNKMAP;
public static final HolderSetProxy HOLDER_SET;
public static final BiomeProxy BIOME;

static {
ReflectionRemapper reflectionRemapper = ReflectionRemapper.forReobfMappingsInPaperJar();
Expand All @@ -37,6 +40,7 @@ public class Reflection {
REFERENCE = reflectionProxyFactory.reflectionProxy(ReferenceProxy.class);
CHUNKMAP = reflectionProxyFactory.reflectionProxy(ChunkMapProxy.class);
HOLDER_SET = reflectionProxyFactory.reflectionProxy(HolderSetProxy.class);
BIOME = reflectionProxyFactory.reflectionProxy(BiomeProxy.class);
}


Expand Down Expand Up @@ -74,4 +78,10 @@ public interface HolderSetProxy {
@MethodName("contents")
<T> List<Holder<T>> invokeContents(HolderSet<T> instance);
}

@Proxies(Biome.class)
public interface BiomeProxy {
@MethodName("getGrassColorFromTexture")
int invokeGrassColorFromTexture(Biome instance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.level.biome.Biome;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.CraftServer;
Expand All @@ -23,4 +24,8 @@ private static <T> Registry<T> getRegistry(ResourceKey<Registry<T>> key) {
public static Registry<Biome> biomeRegistry() {
return getRegistry(Registries.BIOME);
}

public static Registry<SoundEvent> soundEventRegistry() {
return getRegistry(Registries.SOUND_EVENT);
}
}
Loading
Loading