From 0702a0f0acbec411824ae5cf08e3a88deeb1d079 Mon Sep 17 00:00:00 2001 From: zml2008 Date: Tue, 20 Mar 2012 22:45:48 -0700 Subject: [PATCH] Improved Spout biome handling and add LocalWorld method for setting biome --- .../java/com/sk89q/worldedit/LocalWorld.java | 11 +++++-- .../sk89q/worldedit/bukkit/BukkitWorld.java | 11 +++++++ .../sk89q/worldedit/spout/SpoutBiomeType.java | 19 ++++++++++++ .../worldedit/spout/SpoutBiomeTypes.java | 31 ++++++++++++++----- .../worldedit/spout/SpoutServerInterface.java | 4 +-- .../com/sk89q/worldedit/spout/SpoutWorld.java | 14 ++++++++- .../spout/WorldEditPlayerListener.java | 9 ++++++ .../worldedit/spout/WorldEditPlugin.java | 4 +-- 8 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/sk89q/worldedit/spout/SpoutBiomeType.java diff --git a/src/main/java/com/sk89q/worldedit/LocalWorld.java b/src/main/java/com/sk89q/worldedit/LocalWorld.java index c9318d269..2b4abdd90 100644 --- a/src/main/java/com/sk89q/worldedit/LocalWorld.java +++ b/src/main/java/com/sk89q/worldedit/LocalWorld.java @@ -105,11 +105,18 @@ public abstract class LocalWorld { /** * Get biome type * - * @param pt - * @return + * @param pt The (x, z) location to check the biome at + * @return The biome type at the location */ public abstract BiomeType getBiome(Vector2D pt); + /** + * Set the biome type + * @param pt The (x, z) location to set the biome at + * @param biome The biome type to set to + */ + public abstract void setBiome(Vector2D pt, BiomeType biome); + /** * set block type & data * @param pt diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java index 1d59e7033..99706982d 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java @@ -208,6 +208,17 @@ public class BukkitWorld extends LocalWorld { return new BiomeType(bukkitBiome.name()); } + @Override + public void setBiome(Vector2D pt, BiomeType biome) { + Biome bukkitBiome; + try { + bukkitBiome = Biome.valueOf(biome.getName().toUpperCase()); + } catch (IllegalArgumentException e) { + return; + } + world.setBiome(pt.getBlockX(), pt.getBlockZ(), bukkitBiome); + } + /** * Regenerate an area. * diff --git a/src/main/java/com/sk89q/worldedit/spout/SpoutBiomeType.java b/src/main/java/com/sk89q/worldedit/spout/SpoutBiomeType.java new file mode 100644 index 000000000..fcd8eb20e --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/spout/SpoutBiomeType.java @@ -0,0 +1,19 @@ +package com.sk89q.worldedit.spout; + +import org.spout.api.generator.biome.BiomeType; + +/** + * @author zml2008 + */ +public class SpoutBiomeType extends com.sk89q.worldedit.BiomeType { + private final BiomeType type; + + public SpoutBiomeType(BiomeType type) { + super(type.getName().toLowerCase().replace(" ", "")); + this.type = type; + } + + public BiomeType getSpoutBiome() { + return type; + } +} diff --git a/src/main/java/com/sk89q/worldedit/spout/SpoutBiomeTypes.java b/src/main/java/com/sk89q/worldedit/spout/SpoutBiomeTypes.java index 2a935e79a..35577ba5a 100644 --- a/src/main/java/com/sk89q/worldedit/spout/SpoutBiomeTypes.java +++ b/src/main/java/com/sk89q/worldedit/spout/SpoutBiomeTypes.java @@ -1,26 +1,43 @@ package com.sk89q.worldedit.spout; -import java.util.Collections; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; -import com.sk89q.worldedit.BiomeType; import com.sk89q.worldedit.BiomeTypes; import com.sk89q.worldedit.UnknownBiomeTypeException; +import org.spout.api.generator.biome.BiomeGenerator; +import org.spout.api.generator.biome.BiomeType; public class SpoutBiomeTypes implements BiomeTypes { + private final Map types = new HashMap(); @Override public boolean has(String name) { - return false; + return types.containsKey(name.toLowerCase()); } @Override - public BiomeType get(String name) throws UnknownBiomeTypeException { - throw new UnknownBiomeTypeException(name); + public SpoutBiomeType get(String name) throws UnknownBiomeTypeException { + if (!has(name)) { + throw new UnknownBiomeTypeException(name); + } else { + return types.get(name.toLowerCase()); + } + } + + public void registerBiomeTypes(BiomeGenerator generator) { + for (BiomeType type : generator.getBiomes()) { + final SpoutBiomeType weType = new SpoutBiomeType(type); + if (!types.containsKey(weType.getName())) { + types.put(weType.getName(), weType); + } + } } @Override - public List all() { - return Collections.emptyList(); + public List all() { + return new ArrayList(types.values()); } } diff --git a/src/main/java/com/sk89q/worldedit/spout/SpoutServerInterface.java b/src/main/java/com/sk89q/worldedit/spout/SpoutServerInterface.java index f04ac176c..4ab76e713 100644 --- a/src/main/java/com/sk89q/worldedit/spout/SpoutServerInterface.java +++ b/src/main/java/com/sk89q/worldedit/spout/SpoutServerInterface.java @@ -46,7 +46,7 @@ public class SpoutServerInterface extends ServerInterface { public SpoutServerInterface(WorldEditPlugin plugin, Game game) { this.plugin = plugin; this.game = game; - this.biomes = new SpoutBiomeTypes(); + this.biomes = new SpoutBiomeTypes(); this.executor = new SpoutRawCommandExecutor(plugin); } @@ -68,7 +68,7 @@ public class SpoutServerInterface extends ServerInterface { } @Override - public BiomeTypes getBiomes() { + public SpoutBiomeTypes getBiomes() { return biomes; } diff --git a/src/main/java/com/sk89q/worldedit/spout/SpoutWorld.java b/src/main/java/com/sk89q/worldedit/spout/SpoutWorld.java index 724c6442b..7dc0b7568 100644 --- a/src/main/java/com/sk89q/worldedit/spout/SpoutWorld.java +++ b/src/main/java/com/sk89q/worldedit/spout/SpoutWorld.java @@ -33,6 +33,7 @@ import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.util.TreeGenerator; import org.spout.api.entity.Entity; +import org.spout.api.generator.biome.BiomeGenerator; import org.spout.api.geo.World; import org.spout.api.geo.cuboid.Chunk; import org.spout.api.inventory.ItemStack; @@ -83,7 +84,7 @@ public class SpoutWorld extends LocalWorld { */ @Override public boolean setBlockType(Vector pt, int type) { - return world.setBlockId(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ(), (short)type, WorldEditPlugin.getInstance()); + return world.setBlockId(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ(), (short) type, WorldEditPlugin.getInstance()); } /** @@ -185,9 +186,20 @@ public class SpoutWorld extends LocalWorld { * @return */ public BiomeType getBiome(Vector2D pt) { + if (world.getGenerator() instanceof BiomeGenerator) { + BiomeGenerator gen = (BiomeGenerator) world.getGenerator(); + return new SpoutBiomeType(gen.getBiome(pt.getBlockX(), pt.getBlockZ(), world.getSeed())); + } return new BiomeType("Unknown"); } + public void setBiome(Vector2D pt, BiomeType biome) { + if (world.getGenerator() instanceof BiomeGenerator) { + BiomeGenerator gen = (BiomeGenerator) world.getGenerator(); + gen.setBiome(new Vector3(pt.getBlockX(), 0, pt.getBlockZ()), ((SpoutBiomeType) biome).getSpoutBiome()); + } + } + /** * Regenerate an area. * diff --git a/src/main/java/com/sk89q/worldedit/spout/WorldEditPlayerListener.java b/src/main/java/com/sk89q/worldedit/spout/WorldEditPlayerListener.java index 0a00975fa..056b3d531 100644 --- a/src/main/java/com/sk89q/worldedit/spout/WorldEditPlayerListener.java +++ b/src/main/java/com/sk89q/worldedit/spout/WorldEditPlayerListener.java @@ -34,6 +34,8 @@ import org.spout.api.event.player.PlayerInteractEvent.Action; import org.spout.api.event.player.PlayerJoinEvent; import org.spout.api.event.player.PlayerLeaveEvent; import org.spout.api.event.server.PreCommandEvent; +import org.spout.api.event.world.WorldLoadEvent; +import org.spout.api.generator.biome.BiomeGenerator; import org.spout.api.geo.discrete.Point; import java.util.regex.Matcher; @@ -180,4 +182,11 @@ public class WorldEditPlayerListener implements Listener { } } + + @EventHandler + public void onWorldLoad(WorldLoadEvent event) { + if (event.getWorld().getGenerator() instanceof BiomeGenerator) { + plugin.getServerInterface().getBiomes().registerBiomeTypes((BiomeGenerator) event.getWorld().getGenerator()); + } + } } diff --git a/src/main/java/com/sk89q/worldedit/spout/WorldEditPlugin.java b/src/main/java/com/sk89q/worldedit/spout/WorldEditPlugin.java index b9a44edf5..bc18eb0e7 100644 --- a/src/main/java/com/sk89q/worldedit/spout/WorldEditPlugin.java +++ b/src/main/java/com/sk89q/worldedit/spout/WorldEditPlugin.java @@ -55,7 +55,7 @@ public class WorldEditPlugin extends CommonPlugin implements Named { /** * The server interface that all server-related API goes through. */ - private ServerInterface server; + private SpoutServerInterface server; /** * Main WorldEdit instance. */ @@ -283,7 +283,7 @@ public class WorldEditPlugin extends CommonPlugin implements Named { * * @return */ - public ServerInterface getServerInterface() { + public SpoutServerInterface getServerInterface() { return server; }