Skip to content

Commit

Permalink
Update world generators to new generation model (#72)
Browse files Browse the repository at this point in the history
* Update world generators to new generation model

Makes some biome-related things typesafe as a side effect.

Resolves #68

* Copy biome list for tab completion purposes

* Fix logging of biome to island config when using the biome GUI
  • Loading branch information
minoneer authored Jan 3, 2025
1 parent 25c22b1 commit 11f78a2
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Biome;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -75,9 +77,28 @@ public interface IslandInfo {

/**
* The name of the biome.
*
* @deprecated Unsafe String value, use {@link #getIslandBiome()} or {@link #getBiomeName()} instead.
* @return The name of the biome.
*/
@Deprecated(since="3.1.0")
default String getBiome() {
return getIslandBiome().name().toUpperCase(Locale.ROOT);
}

/**
* The biome of the island.
*
* @return The iceland's biome.
*/
Biome getIslandBiome();

/**
* The name of the biome.
*
* @return The name of the biome.
*/
String getBiome();
String getBiomeName();

/**
* The current party-size of the island.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package us.talabrek.ultimateskyblock;

import dk.lockfuglsang.minecraft.po.I18nUtil;
import dk.lockfuglsang.minecraft.util.ItemStackUtil;
import org.bukkit.Registry;
import org.bukkit.block.Biome;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack;
import us.talabrek.ultimateskyblock.command.island.BiomeCommand;
import us.talabrek.ultimateskyblock.handler.WorldEditHandler;
import dk.lockfuglsang.minecraft.util.ItemStackUtil;

import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Settings {
Expand All @@ -32,7 +34,8 @@ public class Settings {
public static int general_cooldownInfo;
public static int general_cooldownRestart;
public static int general_biomeChange;
public static String general_defaultBiome;
public static Biome general_defaultBiome;
public static Biome general_defaultNetherBiome;
public static boolean extras_sendToSpawn;
public static boolean extras_respawnAtIsland;
public static boolean extras_obsidianToLava;
Expand Down Expand Up @@ -87,14 +90,9 @@ public static boolean loadPluginConfig(FileConfiguration config) {
} catch (Exception e) {
general_biomeChange = 3600;
}
try {
general_defaultBiome = config.getString("options.general.defaultBiome");
if (!BiomeCommand.biomeExists(general_defaultBiome)) {
general_defaultBiome = "OCEAN";
}
} catch (Exception e) {
general_defaultBiome = "OCEAN";
}
general_defaultBiome = loadBiome(config, "options.general.defaultBiome", Biome.OCEAN);
general_defaultNetherBiome = loadBiome(config, "options.general.defaultNetherBiome", Biome.NETHER_WASTES);

try {
general_cooldownRestart = config.getInt("options.general.cooldownRestart");
if (general_cooldownRestart < 0) {
Expand All @@ -120,9 +118,7 @@ public static boolean loadPluginConfig(FileConfiguration config) {
changed = true;
}
general_spawnSize = config.getInt("options.general.spawnSize", 50);
island_chestItems = ItemStackUtil.createItemList(
config.getStringList("options.island.chestItems")
);
island_chestItems = ItemStackUtil.createItemList(config.getStringList("options.island.chestItems"));

island_schematicName = config.getString("options.island.schematicName");
if (island_schematicName == null || "yourschematicname".equals(island_schematicName) || "uSkyBlockDefault".equals(island_schematicName)) {
Expand Down Expand Up @@ -159,10 +155,24 @@ public static boolean loadPluginConfig(FileConfiguration config) {
changed = true;
}
nether_lava_level = config.getInt("nether.lava_level", config.getInt("nether.lava-level", 32));
nether_height = config.getInt("nether.height", island_height/2);
nether_height = config.getInt("nether.height", island_height / 2);
return changed;
}

private static Biome loadBiome(FileConfiguration config, String path, Biome defaultBiome) {
try {
String biomeKey = config.getString(path, defaultBiome.getKey().getKey());
Biome parsedBiome = Registry.BIOME.match(biomeKey);
if (parsedBiome == null) {
log.log(Level.WARNING, "Invalid Biome in '{0}': {1}", new Object[]{path, biomeKey});
parsedBiome = defaultBiome;
}
return parsedBiome;
} catch (Exception e) {
return defaultBiome;
}
}

public static List<ItemStack> getIslandChestItems() {
return ItemStackUtil.clone(island_chestItems);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ public boolean execute(CommandSender sender, String alias, Map<String, Object> d
sender.sendMessage(tr("\u00a74That player has no island."));
return false;
}
setBiome(sender, playerInfo, plugin.getIslandInfo(playerInfo), args[1]);
Biome biome = plugin.getBiome(args[0]);
if (biome == null) {
return false;
}
setBiome(sender, playerInfo, plugin.getIslandInfo(playerInfo), biome);
return true;
} else if (args.length == 1 && sender instanceof Player) {
Biome biome = plugin.getBiome(args[0]);
Expand All @@ -121,7 +125,7 @@ public boolean execute(CommandSender sender, String alias, Map<String, Object> d
sender.sendMessage(tr("\u00a74No valid island at your location"));
return false;
}
setBiome(sender, islandInfo, biome.name());
setBiome(sender, islandInfo, biome);
return true;
}
return false;
Expand All @@ -132,8 +136,7 @@ public boolean execute(CommandSender sender, String alias, Map<String, Object> d
public boolean execute(CommandSender sender, String alias, Map<String, Object> data, String... args) {
String cmd = "/usb island purge";
IslandInfo islandInfo = null;
if (args.length == 0 && sender instanceof Player) {
Player player = (Player) sender;
if (args.length == 0 && sender instanceof Player player) {
String islandName = WorldGuardHandler.getIslandNameAt(player.getLocation());
islandInfo = plugin.getIslandInfo(islandName);
} else if (args.length == 1) {
Expand Down Expand Up @@ -186,29 +189,21 @@ private void removePlayerFromIsland(CommandSender sender, PlayerInfo playerInfo,
playerInfo.save();
}

private void setBiome(CommandSender sender, IslandInfo islandInfo, String biome) {
if (uSkyBlock.getInstance().setBiome(islandInfo.getIslandLocation(), biome)) {
islandInfo.setBiome(biome);
sender.sendMessage(tr("\u00a7eChanged biome of {0}s island to {1}.", islandInfo.getLeader(), biome));
} else {
islandInfo.setBiome("OCEAN");
sender.sendMessage(tr("\u00a7eChanged biome of {0}s island to OCEAN.", islandInfo.getLeader()));
}
private void setBiome(CommandSender sender, IslandInfo islandInfo, Biome biome) {
uSkyBlock.getInstance().setBiome(islandInfo.getIslandLocation(), biome);
islandInfo.setBiome(biome);
sender.sendMessage(tr("\u00a7eChanged biome of {0}s island to {1}.", islandInfo.getLeader(), biome));
sender.sendMessage(tr("\u00a7aYou may need to go to spawn, or relog, to see the changes."));
}

private void setBiome(CommandSender sender, PlayerInfo playerInfo, IslandInfo islandInfo, String biome) {
private void setBiome(CommandSender sender, PlayerInfo playerInfo, IslandInfo islandInfo, Biome biome) {
if (playerInfo == null || !playerInfo.getHasIsland()) {
sender.sendMessage(tr("\u00a74That player has no island."));
return;
}
if (uSkyBlock.getInstance().setBiome(playerInfo.getIslandLocation(), biome)) {
islandInfo.setBiome(biome);
sender.sendMessage(tr("\u00a7e{0} has had their biome changed to {1}.", playerInfo.getPlayerName(), biome));
} else {
islandInfo.setBiome("OCEAN");
sender.sendMessage(tr("\u00a7e{0} has had their biome changed to OCEAN.", playerInfo.getPlayerName()));
}
uSkyBlock.getInstance().setBiome(playerInfo.getIslandLocation(), biome);
islandInfo.setBiome(biome);
sender.sendMessage(tr("\u00a7e{0} has had their biome changed to {1}.", playerInfo.getPlayerName(), biome));
sender.sendMessage(tr("\u00a7aYou may need to go to spawn, or relog, to see the changes."));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
* TabCompleter for Biomes.
*/
public class BiomeTabCompleter extends AbstractTabCompleter {
private static final List<String> BIOMES = new ArrayList<>(BiomeCommand.BIOMES.keySet());

@Override
protected List<String> getTabList(CommandSender commandSender, String term) {
return filter(BIOMES, term);
return filter(new ArrayList<>(BiomeCommand.AVAILABLE_BIOMES), term);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Registry;
import org.bukkit.block.Biome;
import org.bukkit.entity.Player;
import us.talabrek.ultimateskyblock.Settings;
Expand All @@ -14,29 +15,22 @@
import us.talabrek.ultimateskyblock.player.PlayerInfo;
import us.talabrek.ultimateskyblock.uSkyBlock;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static dk.lockfuglsang.minecraft.po.I18nUtil.marktr;
import static dk.lockfuglsang.minecraft.po.I18nUtil.tr;

public class BiomeCommand extends RequireIslandCommand {
public static final Map<String, Biome> BIOMES = new HashMap<>() {
{
for (Biome biome : Biome.values()) {
if (!biome.name().equalsIgnoreCase("custom")) {
put(biome.name().toLowerCase(), biome);
}
}
}
};
public static final List<String> AVAILABLE_BIOMES =
Registry.BIOME.stream().map(biome -> biome.getKey().getKey()).sorted().toList();
private final SkyBlockMenu menu;

public BiomeCommand(uSkyBlock plugin, SkyBlockMenu menu) {
super(plugin, "biome|b", null, "biome ?radius", marktr("change the biome of the island"));
this.menu = menu;
addFeaturePermission("usb.exempt.cooldown.biome", tr("exempt player from biome-cooldown"));
for (String biome : BIOMES.keySet()) {
for (String biome : AVAILABLE_BIOMES) {
addFeaturePermission("usb.biome." + biome, tr("Let the player change their islands biome to {0}", biome.toUpperCase()));
}
}
Expand All @@ -51,7 +45,7 @@ protected boolean doExecute(String alias, final Player player, PlayerInfo pi, fi
}
}
if (args.length >= 1) {
final String biome = args[0].toLowerCase();
final String biomeKey = args[0].toLowerCase();
if (!island.hasPerm(player, "canChangeBiome")) {
player.sendMessage(tr("\u00a74You do not have permission to change the biome of this island!"));
return true;
Expand All @@ -62,16 +56,17 @@ protected boolean doExecute(String alias, final Player player, PlayerInfo pi, fi
player.sendMessage(tr("\u00a7eYou must be on your island to change the biome!"));
return true;
}
if (!biomeExists(biome)) {
player.sendMessage(tr("\u00a7cYou have misspelled the biome name. Must be one of {0}", BIOMES.keySet()));
Biome biome = Registry.BIOME.match(biomeKey);
if (biome == null) {
player.sendMessage(tr("\u00a7cYou have misspelled the biome name. Must be one of {0}", AVAILABLE_BIOMES));
return true;
}
int cooldown = plugin.getCooldownHandler().getCooldown(player, "biome");
if (cooldown > 0) {
player.sendMessage(tr("\u00a7eYou can change your biome again in {0,number,#} minutes.", cooldown / 60));
return true;
}
if (!player.hasPermission("usb.biome." + biome.toLowerCase())) {
if (!player.hasPermission("usb.biome." + biomeKey.toLowerCase())) {
player.sendMessage(tr("\u00a7cYou do not have permission to change your biome to that type."));
return true;
}
Expand All @@ -82,6 +77,8 @@ protected boolean doExecute(String alias, final Player player, PlayerInfo pi, fi
minP.subtract(buffer, 0, buffer);
maxP.add(buffer, 0, buffer);
}
boolean changeEntireIslandBiome;

if (args.length == 2 && args[1].matches("[0-9]+")) {
int radius = Integer.parseInt(args[1], 10);
minP = BlockVector3.at(Math.max(location.getBlockX() - radius, minP.getBlockX()),
Expand All @@ -90,39 +87,34 @@ protected boolean doExecute(String alias, final Player player, PlayerInfo pi, fi
maxP = BlockVector3.at(Math.min(location.getBlockX() + radius, maxP.getBlockX()),
Math.min(location.getBlockY() + radius, maxP.getBlockY()),
Math.min(location.getBlockZ() + radius, maxP.getBlockZ()));
player.sendMessage(tr("\u00a77The pixies are busy changing the biome near you to \u00a79{0}\u00a77, be patient.", biome));
changeEntireIslandBiome = false;
player.sendMessage(tr("\u00a77The pixies are busy changing the biome near you to \u00a79{0}\u00a77, be patient.", biomeKey));
} else if (args.length == 2 && args[1].equalsIgnoreCase("chunk")) {
Chunk chunk = location.clone().getChunk();
minP = BlockVector3.at(chunk.getX() << 4, 0, chunk.getZ() << 4);
minP = BlockVector3.at(chunk.getX() << 4, location.getWorld().getMinHeight(), chunk.getZ() << 4);
maxP = BlockVector3.at((chunk.getX() << 4) + 15, location.getWorld().getMaxHeight(), (chunk.getZ() << 4) + 15);
player.sendMessage(tr("\u00a77The pixies are busy changing the biome in your current chunk to \u00a79{0}\u00a77, be patient.", biome));
changeEntireIslandBiome = false;
player.sendMessage(tr("\u00a77The pixies are busy changing the biome in your current chunk to \u00a79{0}\u00a77, be patient.", biomeKey));
} else if (args.length < 2 || args[1].equalsIgnoreCase("all")) {
player.sendMessage(tr("\u00a77The pixies are busy changing the biome of your island to \u00a79{0}\u00a77, be patient.", biome));
}
Biome biomeEnum = BIOMES.get(biome);
if (biomeEnum == null) {
player.sendMessage(tr("\u00a7eInvalid biome {0} supplied!", biome));
changeEntireIslandBiome = true;
player.sendMessage(tr("\u00a77The pixies are busy changing the biome of your island to \u00a79{0}\u00a77, be patient.", biomeKey));
} else {
player.sendMessage(tr("\u00a7cInvalid arguments. Use /is biome <biome> [radius|chunk|all]"));
return true;
}
new SetBiomeTask(plugin, player.getWorld(), minP, maxP, biomeEnum, () -> {
if (args.length == 1) {
new SetBiomeTask(plugin, player.getWorld(), minP, maxP, biome, () -> {
String biomeName = biome.getKey().getKey();
if (changeEntireIslandBiome) {
island.setBiome(biome);
player.sendMessage(tr("\u00a7aYou have changed your island''s biome to {0}", biome.toUpperCase()));
island.sendMessageToIslandGroup(true, marktr("{0} changed the island biome to {1}"), player.getName(), biome.toUpperCase());
player.sendMessage(tr("\u00a7aYou have changed your island''s biome to {0}", biomeName));
island.sendMessageToIslandGroup(true, marktr("{0} changed the island biome to {1}"), player.getName(), biomeName);
plugin.getCooldownHandler().resetCooldown(player, "biome", Settings.general_biomeChange);
} else {
player.sendMessage(tr("\u00a7aYou have changed {0} blocks around you to the {1} biome", args[1], biome.toUpperCase()));
island.sendMessageToIslandGroup(true, marktr("{0} created an area with {1} biome"), player.getName(), biome.toUpperCase());
player.sendMessage(tr("\u00a7aYou have changed {0} blocks around you to the {1} biome", args[1], biomeName));
island.sendMessageToIslandGroup(true, marktr("{0} created an area with {1} biome"), player.getName(), biomeName);
}
}).runTask(plugin);
}
return true;
}

public static boolean biomeExists(String biomeName) {
if (biomeName == null) {
return false;
}
return BIOMES.containsKey(biomeName.toLowerCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private String lookup(IslandInfo islandInfo, String placeholder) {
case "usb_island_animals": return "" + plugin.getLimitLogic().getCreatureCount(islandInfo).get(LimitLogic.CreatureType.ANIMAL);
case "usb_island_villagers": return "" + plugin.getLimitLogic().getCreatureCount(islandInfo).get(LimitLogic.CreatureType.VILLAGER);
case "usb_island_partysize": return "" + islandInfo.getPartySize();
case "usb_island_biome": return islandInfo.getBiome();
case "usb_island_biome": return islandInfo.getBiomeName();
case "usb_island_bans": return ""+islandInfo.getBans();
case "usb_island_members": return ""+islandInfo.getMembers();
case "usb_island_trustees": return ""+islandInfo.getTrustees();
Expand Down
Loading

0 comments on commit 11f78a2

Please sign in to comment.