Support custom biomes in random teleport excluded biome list (#5703)

This commit is contained in:
pop4959 2024-02-26 03:47:00 -08:00 committed by GitHub
parent 52a638f18f
commit bd8c792fa4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 74 additions and 13 deletions

View File

@ -59,6 +59,7 @@ import net.ess3.nms.refl.providers.ReflServerStateProvider;
import net.ess3.nms.refl.providers.ReflSpawnEggProvider;
import net.ess3.nms.refl.providers.ReflSpawnerBlockProvider;
import net.ess3.nms.refl.providers.ReflSyncCommandsProvider;
import net.ess3.provider.BiomeKeyProvider;
import net.ess3.provider.ContainerProvider;
import net.ess3.provider.DamageEventProvider;
import net.ess3.provider.FormattedCommandAliasProvider;
@ -95,6 +96,7 @@ import net.ess3.provider.providers.ModernItemUnbreakableProvider;
import net.ess3.provider.providers.ModernPersistentDataProvider;
import net.ess3.provider.providers.ModernPlayerLocaleProvider;
import net.ess3.provider.providers.ModernSignDataProvider;
import net.ess3.provider.providers.PaperBiomeKeyProvider;
import net.ess3.provider.providers.PaperContainerProvider;
import net.ess3.provider.providers.PaperKnownCommandsProvider;
import net.ess3.provider.providers.PaperMaterialTagProvider;
@ -197,6 +199,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
private transient PlayerLocaleProvider playerLocaleProvider;
private transient SignDataProvider signDataProvider;
private transient DamageEventProvider damageEventProvider;
private transient BiomeKeyProvider biomeKeyProvider;
private transient Kits kits;
private transient RandomTeleport randomTeleport;
private transient UpdateChecker updateChecker;
@ -485,6 +488,10 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
damageEventProvider = new LegacyDamageEventProvider();
}
if (PaperLib.isPaper() && VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_19_4_R01)) {
biomeKeyProvider = new PaperBiomeKeyProvider();
}
execTimer.mark("Init(Providers)");
reload();
@ -1426,6 +1433,11 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
return damageEventProvider;
}
@Override
public BiomeKeyProvider getBiomeKeyProvider() {
return biomeKeyProvider;
}
@Override
public SignDataProvider getSignDataProvider() {
return signDataProvider;

View File

@ -9,6 +9,7 @@ import com.earth2me.essentials.perm.PermissionsHandler;
import com.earth2me.essentials.updatecheck.UpdateChecker;
import com.earth2me.essentials.userstorage.IUserMap;
import net.ess3.nms.refl.providers.ReflOnlineModeProvider;
import net.ess3.provider.BiomeKeyProvider;
import net.ess3.provider.ContainerProvider;
import net.ess3.provider.DamageEventProvider;
import net.ess3.provider.FormattedCommandAliasProvider;
@ -186,5 +187,7 @@ public interface IEssentials extends Plugin {
DamageEventProvider getDamageEventProvider();
BiomeKeyProvider getBiomeKeyProvider();
PluginCommand getPluginCommand(String cmd);
}

View File

@ -6,13 +6,12 @@ import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.VersionUtil;
import io.papermc.lib.PaperLib;
import net.ess3.api.InvalidWorldException;
import net.ess3.provider.BiomeKeyProvider;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Biome;
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
@ -76,14 +75,10 @@ public class RandomTeleport implements IConf {
config.save();
}
public Set<Biome> getExcludedBiomes() {
final List<String> biomeNames = config.getList("excluded-biomes", String.class);
final Set<Biome> excludedBiomes = new HashSet<>();
for (final String biomeName : biomeNames) {
try {
excludedBiomes.add(Biome.valueOf(biomeName.toUpperCase()));
} catch (final IllegalArgumentException ignored) {
}
public Set<String> getExcludedBiomes() {
final Set<String> excludedBiomes = new HashSet<>();
for (final String key : config.getList("excluded-biomes", String.class)) {
excludedBiomes.add(key.toLowerCase());
}
return excludedBiomes;
}
@ -204,7 +199,31 @@ public class RandomTeleport implements IConf {
}
private boolean isValidRandomLocation(final Location location) {
return location.getBlockY() > ess.getWorldInfoProvider().getMinHeight(location.getWorld()) && !this.getExcludedBiomes().contains(location.getBlock().getBiome());
return location.getBlockY() > ess.getWorldInfoProvider().getMinHeight(location.getWorld()) && !isExcludedBiome(location);
}
// Exclude biome if enum or namespaced key matches
private boolean isExcludedBiome(final Location location) {
final Set<String> excluded = getExcludedBiomes();
final String enumKey = location.getBlock().getBiome().name().toLowerCase();
// Try with good old bukkit enum
if (excluded.contains(enumKey)) {
return true;
}
if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_14_4_R01)) {
// No way to get the biome key on versions below this
return false;
}
final String biomeKey;
final BiomeKeyProvider biomeKeyProvider = ess.getBiomeKeyProvider();
if (biomeKeyProvider != null) {
// Works with custom biome keys
biomeKey = biomeKeyProvider.getBiomeKey(location.getBlock()).toString();
} else {
// Custom biome keys resolve as "minecraft:custom" which is unfortunate
biomeKey = location.getBlock().getBiome().getKey().toString();
}
return excluded.contains(biomeKey);
}
public File getFile() {

View File

@ -0,0 +1,8 @@
package net.ess3.provider;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
public interface BiomeKeyProvider extends Provider {
NamespacedKey getBiomeKey(Block block);
}

View File

@ -11,8 +11,8 @@ dependencies {
implementation(project(':providers:BaseProviders')) {
exclude(module: 'spigot-api')
}
compileOnly 'io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT'
compileOnly 'io.papermc.paper:paper-mojangapi:1.18.2-R0.1-SNAPSHOT'
compileOnly 'io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT'
compileOnly 'io.papermc.paper:paper-mojangapi:1.20.4-R0.1-SNAPSHOT'
}
essentials {

View File

@ -0,0 +1,19 @@
package net.ess3.provider.providers;
import net.ess3.provider.BiomeKeyProvider;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
@SuppressWarnings("deprecation")
public class PaperBiomeKeyProvider implements BiomeKeyProvider {
@Override
public NamespacedKey getBiomeKey(final Block block) {
return Bukkit.getUnsafe().getBiomeKey(block.getWorld(), block.getX(), block.getY(), block.getZ());
}
@Override
public String getDescription() {
return "Paper Biome Key Provider";
}
}