Implemented new biome API.

This commit is contained in:
sk89q
2014-07-17 00:21:13 -07:00
parent d50e05480f
commit 42be110097
58 changed files with 1830 additions and 553 deletions

View File

@ -19,7 +19,11 @@
package com.sk89q.worldedit.world;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.blocks.BlockID;
@ -28,6 +32,7 @@ import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.TreeGenerator.TreeType;
import com.sk89q.worldedit.world.biome.BaseBiome;
import com.sk89q.worldedit.world.registry.LegacyWorldData;
import com.sk89q.worldedit.world.registry.WorldData;
@ -62,12 +67,13 @@ public class NullWorld extends AbstractWorld {
}
@Override
public BiomeType getBiome(Vector2D position) {
public BaseBiome getBiome(Vector2D position) {
return null;
}
@Override
public void setBiome(Vector2D position, BiomeType biome) {
public boolean setBiome(Vector2D position, BaseBiome biome) {
return false;
}
@Override

View File

@ -19,12 +19,10 @@
package com.sk89q.worldedit.world;
import com.sk89q.worldedit.BiomeType;
import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BaseItemStack;
@ -147,22 +145,6 @@ public interface World extends Extent {
*/
boolean clearContainerBlockContents(Vector position);
/**
* Get the biome type.
*
* @param position the (x, z) location to check the biome at
* @return the biome type at the location
*/
BiomeType getBiome(Vector2D position);
/**
* Set the biome type.
*
* @param position the (x, z) location to set the biome at
* @param biome the biome type to set to
*/
void setBiome(Vector2D position, BiomeType biome);
/**
* Drop an item at the given position.
*

View File

@ -0,0 +1,82 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.biome;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Basic storage object to represent a given biome.
*/
public class BaseBiome {
private int id;
/**
* Create a new biome with the given biome ID.
*
* @param id the biome ID
*/
public BaseBiome(int id) {
this.id = id;
}
/**
* Create a clone of the given biome.
*
* @param biome the biome to clone
*/
public BaseBiome(BaseBiome biome) {
checkNotNull(biome);
this.id = biome.getId();
}
/**
* Get the biome ID.
*
* @return the biome ID
*/
public int getId() {
return id;
}
/**
* Set the biome id.
*
* @param id the biome ID
*/
public void setId(int id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BaseBiome baseBiome = (BaseBiome) o;
return id == baseBiome.id;
}
@Override
public int hashCode() {
return id;
}
}

View File

@ -0,0 +1,35 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.biome;
/**
* Provides information about a biome.
*/
public interface BiomeData {
/**
* Get the name of the biome, which does not have to follow any
* particular convention.
*
* @return the biome's name
*/
String getName();
}

View File

@ -0,0 +1,57 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.biome;
import com.google.common.base.Function;
import com.sk89q.worldedit.world.registry.BiomeRegistry;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Returns the name of a biome using a given {@code BiomeRegistry}.
*/
class BiomeName implements Function<BaseBiome, String> {
private final BiomeRegistry registry;
/**
* Create a new instance.
*
* @param registry the biome registry
*/
BiomeName(BiomeRegistry registry) {
checkNotNull(registry);
this.registry = registry;
}
@Nullable
@Override
public String apply(BaseBiome input) {
BiomeData data = registry.getData(input);
if (data != null) {
return data.getName();
} else {
return null;
}
}
}

View File

@ -0,0 +1,70 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.biome;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Optional;
import com.sk89q.worldedit.util.WeightedChoice;
import com.sk89q.worldedit.util.WeightedChoice.Choice;
import com.sk89q.worldedit.util.function.LevenshteinDistance;
import com.sk89q.worldedit.world.registry.BiomeRegistry;
import javax.annotation.Nullable;
import java.util.Collection;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Utility methods related to biomes.
*/
public final class Biomes {
private Biomes() {
}
/**
* Find a biome that matches the given input name.
*
* @param biomes a list of biomes
* @param name the name to test
* @param registry a biome registry
* @return a biome or null
*/
@Nullable
public static BaseBiome findBiomeByName(Collection<BaseBiome> biomes, String name, BiomeRegistry registry) {
checkNotNull(biomes);
checkNotNull(name);
checkNotNull(registry);
Function<String, ? extends Number> compare = new LevenshteinDistance(name, false, LevenshteinDistance.STANDARD_CHARS);
WeightedChoice<BaseBiome> chooser = new WeightedChoice<BaseBiome>(Functions.compose(compare, new BiomeName(registry)), 0);
for (BaseBiome biome : biomes) {
chooser.consider(biome);
}
Optional<Choice<BaseBiome>> choice = chooser.getChoice();
if (choice.isPresent() && choice.get().getScore() <= 1) {
return choice.get().getValue();
} else {
return null;
}
}
}

View File

@ -0,0 +1,58 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.world.biome.BaseBiome;
import com.sk89q.worldedit.world.biome.BiomeData;
import javax.annotation.Nullable;
import java.util.List;
/**
* Provides information on biomes.
*/
public interface BiomeRegistry {
/**
* Create a new biome given its biome ID.
*
* @param id its biome ID
* @return a new biome or null if it can't be created
*/
@Nullable
BaseBiome createFromId(int id);
/**
* Get a list of available biomes.
*
* @return a list of biomes
*/
List<BaseBiome> getBiomes();
/**
* Get data about a biome.
*
* @param biome the biome
* @return a data object or null if information is not known
*/
@Nullable
BiomeData getData(BaseBiome biome);
}

View File

@ -23,16 +23,17 @@ package com.sk89q.worldedit.world.registry;
* An implementation of {@link WorldData} that uses legacy numeric IDs and
* a built-in block database.
*/
public final class LegacyWorldData implements WorldData {
public class LegacyWorldData implements WorldData {
private static final LegacyWorldData INSTANCE = new LegacyWorldData();
private final LegacyBlockRegistry blockRegistry = new LegacyBlockRegistry();
private final NullEntityRegistry entityRegistry = new NullEntityRegistry();
private final NullBiomeRegistry biomeRegistry = new NullBiomeRegistry();
/**
* Create a new instance.
*/
private LegacyWorldData() {
protected LegacyWorldData() {
}
@Override
@ -45,6 +46,11 @@ public final class LegacyWorldData implements WorldData {
return entityRegistry;
}
@Override
public BiomeRegistry getBiomeRegistry() {
return biomeRegistry;
}
/**
* Get a singleton instance.
*

View File

@ -0,0 +1,57 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.world.biome.BaseBiome;
import com.sk89q.worldedit.world.biome.BiomeData;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
/**
* A biome registry that knows nothing.
*/
public class NullBiomeRegistry implements BiomeRegistry {
/**
* Create a new instance.
*/
public NullBiomeRegistry() {
}
@Nullable
@Override
public BaseBiome createFromId(int id) {
return null;
}
@Override
public List<BaseBiome> getBiomes() {
return Collections.emptyList();
}
@Nullable
@Override
public BiomeData getData(BaseBiome biome) {
return null;
}
}

View File

@ -39,4 +39,11 @@ public interface WorldData {
*/
EntityRegistry getEntityRegistry();
/**
* Get the biome registry.
*
* @return the biome registry
*/
BiomeRegistry getBiomeRegistry();
}