Skip to content

Commit

Permalink
Added a category system and refactored registries
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Aug 4, 2018
1 parent 484687a commit 282eca7
Show file tree
Hide file tree
Showing 47 changed files with 715 additions and 316 deletions.
Expand Up @@ -20,20 +20,20 @@
package com.sk89q.worldedit.bukkit;

import com.sk89q.worldedit.world.registry.BiomeRegistry;
import com.sk89q.worldedit.world.registry.BundledWorldData;
import com.sk89q.worldedit.world.registry.BundledRegistries;

/**
* World data for the Bukkit platform.
*/
class BukkitWorldData extends BundledWorldData {
class BukkitRegistries extends BundledRegistries {

private static final BukkitWorldData INSTANCE = new BukkitWorldData();
private static final BukkitRegistries INSTANCE = new BukkitRegistries();
private final BiomeRegistry biomeRegistry = new BukkitBiomeRegistry();

/**
* Create a new instance.
*/
BukkitWorldData() {
BukkitRegistries() {
}

@Override
Expand All @@ -46,7 +46,7 @@ public BiomeRegistry getBiomeRegistry() {
*
* @return an instance
*/
public static BukkitWorldData getInstance() {
public static BukkitRegistries getInstance() {
return INSTANCE;
}

Expand Down
Expand Up @@ -30,8 +30,8 @@
import com.sk89q.worldedit.util.command.CommandMapping;
import com.sk89q.worldedit.util.command.Description;
import com.sk89q.worldedit.util.command.Dispatcher;
import com.sk89q.worldedit.world.registry.Registries;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
Expand All @@ -48,13 +48,11 @@ public class BukkitServerInterface implements MultiUserPlatform {
public Server server;
public WorldEditPlugin plugin;
private CommandRegistration dynamicCommands;
private BukkitBiomeRegistry biomes;
private boolean hookingEvents;

public BukkitServerInterface(WorldEditPlugin plugin, Server server) {
this.plugin = plugin;
this.server = server;
this.biomes = new BukkitBiomeRegistry();
dynamicCommands = new CommandRegistration(plugin);
}

Expand All @@ -63,9 +61,8 @@ boolean isHookingEvents() {
}

@Override
public int resolveItem(String name) {
Material mat = Material.matchMaterial(name);
return mat == null ? 0 : mat.getId();
public Registries getRegistries() {
return BukkitRegistries.getInstance();
}

@Override
Expand Down
Expand Up @@ -40,7 +40,7 @@
import com.sk89q.worldedit.world.AbstractWorld;
import com.sk89q.worldedit.world.biome.BaseBiome;
import com.sk89q.worldedit.world.registry.BundledBlockData;
import com.sk89q.worldedit.world.registry.WorldData;
import com.sk89q.worldedit.world.registry.Registries;
import org.bukkit.Effect;
import org.bukkit.TreeType;
import org.bukkit.World;
Expand Down Expand Up @@ -354,11 +354,6 @@ public boolean playEffect(Vector position, int type, int data) {
return true;
}

@Override
public WorldData getWorldData() {
return BukkitWorldData.getInstance();
}

@Override
public void simulateBlockMine(Vector pt) {
getWorld().getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()).breakNaturally();
Expand All @@ -367,7 +362,9 @@ public void simulateBlockMine(Vector pt) {
@Override
public com.sk89q.worldedit.blocks.type.BlockState getBlock(Vector position) {
Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
BlockType blockType = BlockTypes.getBlockType(BundledBlockData.getInstance().fromLegacyId(bukkitBlock.getTypeId()));
BlockType blockType = BlockTypes.getBlockType(
BundledBlockData.getInstance().fromLegacyId(bukkitBlock.getTypeId())
);
return blockType.getDefaultState(); // TODO Data
}

Expand Down
Expand Up @@ -2165,12 +2165,12 @@ protected BaseBiome getBiome(int x, int z, BaseBiome defaultBiomeType) {
}

private static final Vector[] recurseDirections = {
PlayerDirection.NORTH.vector(),
PlayerDirection.EAST.vector(),
PlayerDirection.SOUTH.vector(),
PlayerDirection.WEST.vector(),
PlayerDirection.UP.vector(),
PlayerDirection.DOWN.vector(),
Direction.NORTH.toVector(),
Direction.EAST.toVector(),
Direction.SOUTH.toVector(),
Direction.WEST.toVector(),
Direction.UP.toVector(),
Direction.DOWN.toVector(),
};

private static double lengthSq(double x, double y, double z) {
Expand Down
Expand Up @@ -22,7 +22,6 @@
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.CuboidClipboard.FlipDirection;
import com.sk89q.worldedit.blocks.type.BlockState;
import com.sk89q.worldedit.blocks.type.BlockStateHolder;
import com.sk89q.worldedit.blocks.type.BlockType;
Expand Down Expand Up @@ -102,7 +101,7 @@ public BaseBlock(BlockType blockType) {
*/
public BaseBlock(BlockState state, @Nullable CompoundTag nbtData) {
this.blockState = state;
setNbtData(nbtData);
this.nbtData = nbtData;
}

/**
Expand All @@ -126,7 +125,7 @@ public BaseBlock(int id, int data) {
@Deprecated
public BaseBlock(int id, int data, @Nullable CompoundTag nbtData) {
this(id);
setNbtData(nbtData);
this.nbtData = nbtData;
}

/**
Expand Down Expand Up @@ -174,6 +173,7 @@ public int getData() {
*
* @return The state map
*/
@Override
public Map<State, StateValue> getStates() {
return this.blockState.getStates();
}
Expand All @@ -194,19 +194,11 @@ public BaseBlock with(State state, StateValue value) {
* @param state The state to get the value for
* @return The state value
*/
@Override
public StateValue getState(State state) {
return this.blockState.getState(state);
}

/**
* Set the block's data value.
*
* @param data block data value
*/
@Deprecated
public void setData(int data) {
}

@Override
public boolean hasNbtData() {
return getNbtData() != null;
Expand Down Expand Up @@ -237,80 +229,6 @@ public void setNbtData(@Nullable CompoundTag nbtData) {
throw new UnsupportedOperationException("This class is immutable.");
}

/**
* Returns true if it's air.
*
* @return if air
*/
public boolean isAir() {
return getBlockType() == BlockTypes.AIR;
}

/**
* Rotate this block 90 degrees.
*
* @return new data value
* @deprecated Use {@link BlockData#rotate90(int, int)}
*/
@Deprecated
public int rotate90() {
int newData = BlockData.rotate90(getBlockType().getLegacyId(), getData());
setData(newData);
return newData;
}

/**
* Rotate this block -90 degrees.
*
* @return new data value
* @deprecated Use {@link BlockData#rotate90Reverse(int, int)}
*/
@Deprecated
public int rotate90Reverse() {
int newData = BlockData.rotate90Reverse(getBlockType().getLegacyId(), getData());
setData((short) newData);
return newData;
}

/**
* Cycle the damage value of the block forward or backward
*
* @param increment 1 for forward, -1 for backward
* @return new data value
* @deprecated Use {@link BlockData#cycle(int, int, int)}
*/
@Deprecated
public int cycleData(int increment) {
int newData = BlockData.cycle(getBlockType().getLegacyId(), getData(), increment);
setData((short) newData);
return newData;
}

/**
* Flip this block.
*
* @return this block
* @deprecated Use {@link BlockData#flip(int, int)}
*/
@Deprecated
public BaseBlock flip() {
setData((short) BlockData.flip(getBlockType().getLegacyId(), getData()));
return this;
}

/**
* Flip this block.
*
* @param direction direction to flip in
* @return this block
* @deprecated Use {@link BlockData#flip(int, int, FlipDirection)}
*/
@Deprecated
public BaseBlock flip(FlipDirection direction) {
setData((short) BlockData.flip(getBlockType().getLegacyId(), getData(), direction));
return this;
}

/**
* Checks whether the type ID and data value are equal.
*/
Expand Down
@@ -0,0 +1,108 @@
/*
* 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.blocks.type;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nullable;

/**
* Stores a list of categories of Block Types.
*/
public class BlockCategories {

private BlockCategories() {
}

public static final BlockCategory ACACIA_LOGS = new BlockCategory("minecraft:acacia_logs");
public static final BlockCategory ANVIL = new BlockCategory("minecraft:anvil");
public static final BlockCategory BANNERS = new BlockCategory("minecraft:banners");
public static final BlockCategory BIRCH_LOGS = new BlockCategory("minecraft:birch_logs");
public static final BlockCategory BUTTONS = new BlockCategory("minecraft:buttons");
public static final BlockCategory CARPETS = new BlockCategory("minecraft:carpets");
public static final BlockCategory CORAL = new BlockCategory("minecraft:coral");
public static final BlockCategory CORAL_PLANTS = new BlockCategory("minecraft:coral_plants");
public static final BlockCategory DARK_OAK_LOGS = new BlockCategory("minecraft:dark_oak_logs");
public static final BlockCategory DOORS = new BlockCategory("minecraft:doors");
public static final BlockCategory ENDERMAN_HOLDABLE = new BlockCategory("minecraft:enderman_holdable");
public static final BlockCategory FLOWER_POTS = new BlockCategory("minecraft:flower_pots");
public static final BlockCategory ICE = new BlockCategory("minecraft:ice");
public static final BlockCategory JUNGLE_LOGS = new BlockCategory("minecraft:jungle_logs");
public static final BlockCategory LEAVES = new BlockCategory("minecraft:leaves");
public static final BlockCategory LOGS = new BlockCategory("minecraft:logs");
public static final BlockCategory OAK_LOGS = new BlockCategory("minecraft:oak_logs");
public static final BlockCategory PLANKS = new BlockCategory("minecraft:planks");
public static final BlockCategory RAILS = new BlockCategory("minecraft:rails");
public static final BlockCategory SAND = new BlockCategory("minecraft:sand");
public static final BlockCategory SAPLINGS = new BlockCategory("minecraft:saplings");
public static final BlockCategory SLABS = new BlockCategory("minecraft:slabs");
public static final BlockCategory SPRUCE_LOGS = new BlockCategory("minecraft:spruce_logs");
public static final BlockCategory STAIRS = new BlockCategory("minecraft:stairs");
public static final BlockCategory STONE_BRICKS = new BlockCategory("minecraft:stone_bricks");
public static final BlockCategory VALID_SPAWN = new BlockCategory("minecraft:valid_spawn");
public static final BlockCategory WOODEN_BUTTONS = new BlockCategory("minecraft:wooden_buttons");
public static final BlockCategory WOODEN_DOORS = new BlockCategory("minecraft:wooden_doors");
public static final BlockCategory WOODEN_PRESSURE_PLATES = new BlockCategory("minecraft:wooden_pressure_plates");
public static final BlockCategory WOODEN_SLABS = new BlockCategory("minecraft:wooden_slabs");
public static final BlockCategory WOODEN_STAIRS = new BlockCategory("minecraft:wooden_stairs");
public static final BlockCategory WOOL = new BlockCategory("minecraft:wool");

// Fluids
public static final BlockCategory LAVA = new BlockCategory("minecraft:lava");
public static final BlockCategory WATER = new BlockCategory("minecraft:water");

private static final Map<String, BlockCategory> categoryMapping = new HashMap<>();

static {
for (Field field : BlockCategories.class.getFields()) {
if (field.getType() == BlockCategory.class) {
try {
registerCategory((BlockCategory) field.get(null));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}

public static void registerCategory(BlockCategory blockCategory) {
if (categoryMapping.containsKey(blockCategory.getId()) && !blockCategory.getId().startsWith("minecraft:")) {
throw new IllegalArgumentException("Existing category with this ID already registered");
}

categoryMapping.put(blockCategory.getId(), blockCategory);
}

@Nullable
public static BlockCategory getBlockType(String id) {
// If it has no namespace, assume minecraft.
if (id != null && !id.contains(":")) {
id = "minecraft:" + id;
}
return categoryMapping.get(id);
}

public static Collection<BlockCategory> values() {
return categoryMapping.values();
}
}

0 comments on commit 282eca7

Please sign in to comment.