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.
This commit is contained in:
aumgn 2012-04-10 13:09:47 +02:00
parent 2ed92dafad
commit e86dc2c90b
6 changed files with 79 additions and 56 deletions

View File

@ -1,43 +1,17 @@
package com.sk89q.worldedit; package com.sk89q.worldedit;
public class BiomeType { public interface BiomeType {
private String name; public static final BiomeType UNKNOWN = new BiomeType() {
public String getName() {
public BiomeType(String name) { return "Unknown";
this.name = name;
} }
};
/** /**
* Get the name of the player. * Get the name of this biome type.
* *
* @return String * @return String
*/ */
public String getName() { 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;
}
} }

View File

@ -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;
}
}

View File

@ -1,11 +1,9 @@
package com.sk89q.worldedit.bukkit; package com.sk89q.worldedit.bukkit;
import java.util.ArrayList; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import org.bukkit.block.Biome;
import com.sk89q.worldedit.BiomeType; import com.sk89q.worldedit.BiomeType;
import com.sk89q.worldedit.BiomeTypes; import com.sk89q.worldedit.BiomeTypes;
import com.sk89q.worldedit.UnknownBiomeTypeException; import com.sk89q.worldedit.UnknownBiomeTypeException;
@ -18,7 +16,7 @@ public class BukkitBiomeTypes implements BiomeTypes {
@Override @Override
public boolean has(String name) { public boolean has(String name) {
try { try {
Biome.valueOf(name.toUpperCase(Locale.ENGLISH)); BukkitBiomeType.valueOf(name.toUpperCase(Locale.ENGLISH));
return true; return true;
} catch (IllegalArgumentException exc) { } catch (IllegalArgumentException exc) {
return false; return false;
@ -28,8 +26,7 @@ public class BukkitBiomeTypes implements BiomeTypes {
@Override @Override
public BiomeType get(String name) throws UnknownBiomeTypeException { public BiomeType get(String name) throws UnknownBiomeTypeException {
try { try {
Biome biome = Biome.valueOf(name.toUpperCase(Locale.ENGLISH)); return BukkitBiomeType.valueOf(name.toUpperCase(Locale.ENGLISH));
return new BiomeType(biome.name());
} catch (IllegalArgumentException exc) { } catch (IllegalArgumentException exc) {
throw new UnknownBiomeTypeException(name); throw new UnknownBiomeTypeException(name);
} }
@ -37,11 +34,7 @@ public class BukkitBiomeTypes implements BiomeTypes {
@Override @Override
public List<BiomeType> all() { public List<BiomeType> all() {
List<BiomeType> biomes = new ArrayList<BiomeType>(); return Arrays.<BiomeType>asList(BukkitBiomeType.values());
for (Biome biome : Biome.values()) {
biomes.add(new BiomeType(biome.name()));
}
return biomes;
} }
} }

View File

@ -214,19 +214,21 @@ public class BukkitWorld extends LocalWorld {
*/ */
public BiomeType getBiome(Vector2D pt) { public BiomeType getBiome(Vector2D pt) {
Biome bukkitBiome = world.getBiome(pt.getBlockX(), pt.getBlockZ()); 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 @Override
public void setBiome(Vector2D pt, BiomeType biome) { public void setBiome(Vector2D pt, BiomeType biome) {
if (biome instanceof BukkitBiomeType) {
Biome bukkitBiome; Biome bukkitBiome;
try { bukkitBiome = ((BukkitBiomeType) biome).getBukkitBiome();
bukkitBiome = Biome.valueOf(biome.getName().toUpperCase());
} catch (IllegalArgumentException e) {
return;
}
world.setBiome(pt.getBlockX(), pt.getBlockZ(), bukkitBiome); world.setBiome(pt.getBlockX(), pt.getBlockZ(), bukkitBiome);
} }
}
/** /**
* Regenerate an area. * Regenerate an area.

View File

@ -23,14 +23,18 @@ import org.spout.api.generator.biome.BiomeType;
/** /**
* @author zml2008 * @author zml2008
*/ */
public class SpoutBiomeType extends com.sk89q.worldedit.BiomeType { public class SpoutBiomeType implements com.sk89q.worldedit.BiomeType {
private final BiomeType type; private final BiomeType type;
public SpoutBiomeType(BiomeType type) { public SpoutBiomeType(BiomeType type) {
super(type.getName().toLowerCase().replace(" ", ""));
this.type = type; this.type = type;
} }
@Override
public String getName() {
return type.getName().toLowerCase().replace(" ", "");
}
public BiomeType getSpoutBiome() { public BiomeType getSpoutBiome() {
return type; return type;
} }

View File

@ -206,11 +206,12 @@ public class SpoutWorld extends LocalWorld {
BiomeGenerator gen = (BiomeGenerator) world.getGenerator(); BiomeGenerator gen = (BiomeGenerator) world.getGenerator();
return new SpoutBiomeType(gen.getBiome(pt.getBlockX(), pt.getBlockZ(), world.getSeed())); 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) { 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(); BiomeGenerator gen = (BiomeGenerator) world.getGenerator();
gen.setBiome(new Vector3(pt.getBlockX(), 0, pt.getBlockZ()), ((SpoutBiomeType) biome).getSpoutBiome()); gen.setBiome(new Vector3(pt.getBlockX(), 0, pt.getBlockZ()), ((SpoutBiomeType) biome).getSpoutBiome());
} }