From e86dc2c90b8252e201da2281792a4ec70fa4cf00 Mon Sep 17 00:00:00 2001 From: aumgn Date: Tue, 10 Apr 2012 13:09:47 +0200 Subject: [PATCH] Biome clean up & fixes Change BiomeType to an interface. Do not longer use invalid biomes of Bukkit Biome enum. Add a common unknown biome type. --- .../java/com/sk89q/worldedit/BiomeType.java | 42 +++------------- .../worldedit/bukkit/BukkitBiomeType.java | 49 +++++++++++++++++++ .../worldedit/bukkit/BukkitBiomeTypes.java | 15 ++---- .../sk89q/worldedit/bukkit/BukkitWorld.java | 16 +++--- .../sk89q/worldedit/spout/SpoutBiomeType.java | 8 ++- .../com/sk89q/worldedit/spout/SpoutWorld.java | 5 +- 6 files changed, 79 insertions(+), 56 deletions(-) create mode 100644 src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeType.java diff --git a/src/main/java/com/sk89q/worldedit/BiomeType.java b/src/main/java/com/sk89q/worldedit/BiomeType.java index ae92e1ba5..b684b3544 100644 --- a/src/main/java/com/sk89q/worldedit/BiomeType.java +++ b/src/main/java/com/sk89q/worldedit/BiomeType.java @@ -1,43 +1,17 @@ package com.sk89q.worldedit; -public class BiomeType { +public interface BiomeType { - private String name; - - public BiomeType(String name) { - this.name = name; - } + public static final BiomeType UNKNOWN = new BiomeType() { + public String getName() { + return "Unknown"; + } + }; /** - * Get the name of the player. + * Get the name of this biome type. * * @return String */ - public String getName() { - return name; - } - - /** - * Gets the hash code. - * - * @return hash code - */ - @Override - public int hashCode() { - return name.hashCode(); - } - - /** - * Returns true if equal. - * - * @param other - * @return whether the other object is equivalent - */ - @Override - public boolean equals(Object obj) { - if (obj instanceof BiomeType) { - return ((BiomeType) obj).name.equals(name); - } - return false; - } + public String getName(); } diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeType.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeType.java new file mode 100644 index 000000000..6028721d6 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeType.java @@ -0,0 +1,49 @@ +package com.sk89q.worldedit.bukkit; + +import java.util.Locale; + +import org.bukkit.block.Biome; + +import com.sk89q.worldedit.BiomeType; + +public enum BukkitBiomeType implements BiomeType { + + SWAMPLAND(Biome.SWAMPLAND), + FOREST(Biome.FOREST), + TAIGA(Biome.TAIGA), + DESERT(Biome.DESERT), + PLAINS(Biome.PLAINS), + HELL(Biome.HELL), + SKY(Biome.SKY), + RIVER(Biome.RIVER), + EXTREME_HILLS(Biome.EXTREME_HILLS), + OCEAN(Biome.OCEAN), + FROZEN_OCEAN(Biome.FROZEN_OCEAN), + FROZEN_RIVER(Biome.FROZEN_RIVER), + ICE_PLAINS(Biome.ICE_PLAINS), + ICE_MOUNTAINS(Biome.ICE_MOUNTAINS), + MUSHROOM_ISLAND(Biome.MUSHROOM_ISLAND), + MUSHROOM_SHORE(Biome.MUSHROOM_SHORE), + BEACH(Biome.BEACH), + DESERT_HILLS(Biome.DESERT_HILLS), + FOREST_HILLS(Biome.FOREST_HILLS), + TAIGA_HILLS(Biome.TAIGA_HILLS), + SMALL_MOUNTAINS(Biome.SMALL_MOUNTAINS), + JUNGLE(Biome.JUNGLE), + JUNGLE_HILLS(Biome.JUNGLE_HILLS); + + private Biome bukkitBiome; + + private BukkitBiomeType(Biome biome) { + this.bukkitBiome = biome; + } + + @Override + public String getName() { + return name().toLowerCase(Locale.ENGLISH); + } + + public Biome getBukkitBiome() { + return bukkitBiome; + } +} diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeTypes.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeTypes.java index 47bbc8759..44ca45771 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeTypes.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeTypes.java @@ -1,11 +1,9 @@ package com.sk89q.worldedit.bukkit; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Locale; -import org.bukkit.block.Biome; - import com.sk89q.worldedit.BiomeType; import com.sk89q.worldedit.BiomeTypes; import com.sk89q.worldedit.UnknownBiomeTypeException; @@ -18,7 +16,7 @@ public class BukkitBiomeTypes implements BiomeTypes { @Override public boolean has(String name) { try { - Biome.valueOf(name.toUpperCase(Locale.ENGLISH)); + BukkitBiomeType.valueOf(name.toUpperCase(Locale.ENGLISH)); return true; } catch (IllegalArgumentException exc) { return false; @@ -28,8 +26,7 @@ public class BukkitBiomeTypes implements BiomeTypes { @Override public BiomeType get(String name) throws UnknownBiomeTypeException { try { - Biome biome = Biome.valueOf(name.toUpperCase(Locale.ENGLISH)); - return new BiomeType(biome.name()); + return BukkitBiomeType.valueOf(name.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException exc) { throw new UnknownBiomeTypeException(name); } @@ -37,11 +34,7 @@ public class BukkitBiomeTypes implements BiomeTypes { @Override public List all() { - List biomes = new ArrayList(); - for (Biome biome : Biome.values()) { - biomes.add(new BiomeType(biome.name())); - } - return biomes; + return Arrays.asList(BukkitBiomeType.values()); } } diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java index 2d9c5e86d..5bdf2ed6c 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java @@ -214,18 +214,20 @@ public class BukkitWorld extends LocalWorld { */ public BiomeType getBiome(Vector2D pt) { Biome bukkitBiome = world.getBiome(pt.getBlockX(), pt.getBlockZ()); - return new BiomeType(bukkitBiome.name()); + try { + return BukkitBiomeType.valueOf(bukkitBiome.name()); + } catch (IllegalArgumentException exc) { + return BiomeType.UNKNOWN; + } } @Override public void setBiome(Vector2D pt, BiomeType biome) { - Biome bukkitBiome; - try { - bukkitBiome = Biome.valueOf(biome.getName().toUpperCase()); - } catch (IllegalArgumentException e) { - return; + if (biome instanceof BukkitBiomeType) { + Biome bukkitBiome; + bukkitBiome = ((BukkitBiomeType) biome).getBukkitBiome(); + world.setBiome(pt.getBlockX(), pt.getBlockZ(), bukkitBiome); } - world.setBiome(pt.getBlockX(), pt.getBlockZ(), bukkitBiome); } /** diff --git a/src/main/java/com/sk89q/worldedit/spout/SpoutBiomeType.java b/src/main/java/com/sk89q/worldedit/spout/SpoutBiomeType.java index ccde1c637..f6ca49858 100644 --- a/src/main/java/com/sk89q/worldedit/spout/SpoutBiomeType.java +++ b/src/main/java/com/sk89q/worldedit/spout/SpoutBiomeType.java @@ -23,14 +23,18 @@ import org.spout.api.generator.biome.BiomeType; /** * @author zml2008 */ -public class SpoutBiomeType extends com.sk89q.worldedit.BiomeType { +public class SpoutBiomeType implements com.sk89q.worldedit.BiomeType { private final BiomeType type; public SpoutBiomeType(BiomeType type) { - super(type.getName().toLowerCase().replace(" ", "")); this.type = type; } + @Override + public String getName() { + return type.getName().toLowerCase().replace(" ", ""); + } + public BiomeType getSpoutBiome() { return type; } diff --git a/src/main/java/com/sk89q/worldedit/spout/SpoutWorld.java b/src/main/java/com/sk89q/worldedit/spout/SpoutWorld.java index cb654a9aa..2b7429a9f 100644 --- a/src/main/java/com/sk89q/worldedit/spout/SpoutWorld.java +++ b/src/main/java/com/sk89q/worldedit/spout/SpoutWorld.java @@ -206,11 +206,12 @@ public class SpoutWorld extends LocalWorld { BiomeGenerator gen = (BiomeGenerator) world.getGenerator(); return new SpoutBiomeType(gen.getBiome(pt.getBlockX(), pt.getBlockZ(), world.getSeed())); } - return new BiomeType("Unknown"); + return BiomeType.UNKNOWN; } public void setBiome(Vector2D pt, BiomeType biome) { - if (world.getGenerator() instanceof BiomeGenerator) { + if (biome instanceof SpoutBiomeType && + world.getGenerator() instanceof BiomeGenerator) { BiomeGenerator gen = (BiomeGenerator) world.getGenerator(); gen.setBiome(new Vector3(pt.getBlockX(), 0, pt.getBlockZ()), ((SpoutBiomeType) biome).getSpoutBiome()); }