Skip to content

Commit

Permalink
registry changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike authored and me4502 committed Aug 4, 2018
1 parent a48c319 commit d33e2e9
Show file tree
Hide file tree
Showing 25 changed files with 1,496 additions and 1,528 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,9 @@ public BrushTool getBrushTool(ItemType item) throws InvalidToolBindException {
public void setTool(ItemType item, @Nullable Tool tool) throws InvalidToolBindException {
if (item.hasBlockType()) {
throw new InvalidToolBindException(item, "Blocks can't be used");
} else if (item == ItemTypes.getItemType(config.wandItem)) {
} else if (item == ItemTypes.get(config.wandItem)) {
throw new InvalidToolBindException(item, "Already used for the wand");
} else if (item == ItemTypes.getItemType(config.navigationWand)) {
} else if (item == ItemTypes.get(config.navigationWand)) {
throw new InvalidToolBindException(item, "Already used for the navigation wand");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void searchItem(Actor actor, CommandContext args) throws WorldEditExcepti
boolean blocksOnly = args.hasFlag('b');
boolean itemsOnly = args.hasFlag('i');

ItemType type = ItemTypes.getItemType(query);
ItemType type = ItemTypes.get(query);

if (type != null) {
actor.print(type.getId() + " (" + type.getName() + ")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public void chunk(Player player, LocalSession session, EditSession editSession,
@CommandPermissions("worldedit.wand")
public void wand(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {

player.giveItem(new BaseItemStack(ItemTypes.getItemType(we.getConfiguration().wandItem), 1));
player.giveItem(new BaseItemStack(ItemTypes.get(we.getConfiguration().wandItem), 1));
player.print("Left click: select pos #1; Right click: select pos #2");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private BlockStateHolder parseLogic(String input, ParserContext context) throws
blockStates = blockInHand.getStates();
} else {
// Attempt to lookup a block from ID or name.
blockType = BlockTypes.getBlockType(typeString);
blockType = BlockTypes.get(typeString);

if (blockType == null) {
throw new NoMatchException("Does not match a valid block type: '" + input + "'");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.registry;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;

public final class NamespacedRegistry<V> {
private static final String MINECRAFT_NAMESPACE = "minecraft";
private final Map<String, V> map = new HashMap<>();

public @Nullable V get(final String key) {
checkState(key.equals(key.toLowerCase()), "key must be lowercase");
return this.map.get(this.orDefaultNamespace(key));
}

public V register(final String key, final V value) {
requireNonNull(key, "key");
requireNonNull(value, "value");
checkState(key.indexOf(':') > -1, "key is not namespaced");
checkState(key.equals(key.toLowerCase()), "key must be lowercase");
checkState(!this.map.containsKey(key), "key %s already has an entry", key);
this.map.put(key, value);
return value;
}

public Set<String> keySet() {
return Collections.unmodifiableSet(this.map.keySet());
}

public Collection<V> values() {
return Collections.unmodifiableCollection(this.map.values());
}

private String orDefaultNamespace(final String key) {
if (key.indexOf(':') == -1) {
return MINECRAFT_NAMESPACE + ':' + key;
}
return key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,71 +34,53 @@ 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");
public static final BlockCategory ACACIA_LOGS = register("minecraft:acacia_logs");
public static final BlockCategory ANVIL = register("minecraft:anvil");
public static final BlockCategory BANNERS = register("minecraft:banners");
public static final BlockCategory BIRCH_LOGS = register("minecraft:birch_logs");
public static final BlockCategory BUTTONS = register("minecraft:buttons");
public static final BlockCategory CARPETS = register("minecraft:carpets");
public static final BlockCategory CORAL = register("minecraft:coral");
public static final BlockCategory CORAL_PLANTS = register("minecraft:coral_plants");
public static final BlockCategory DARK_OAK_LOGS = register("minecraft:dark_oak_logs");
public static final BlockCategory DOORS = register("minecraft:doors");
public static final BlockCategory ENDERMAN_HOLDABLE = register("minecraft:enderman_holdable");
public static final BlockCategory FLOWER_POTS = register("minecraft:flower_pots");
public static final BlockCategory ICE = register("minecraft:ice");
public static final BlockCategory JUNGLE_LOGS = register("minecraft:jungle_logs");
public static final BlockCategory LEAVES = register("minecraft:leaves");
public static final BlockCategory LOGS = register("minecraft:logs");
public static final BlockCategory OAK_LOGS = register("minecraft:oak_logs");
public static final BlockCategory PLANKS = register("minecraft:planks");
public static final BlockCategory RAILS = register("minecraft:rails");
public static final BlockCategory SAND = register("minecraft:sand");
public static final BlockCategory SAPLINGS = register("minecraft:saplings");
public static final BlockCategory SLABS = register("minecraft:slabs");
public static final BlockCategory SPRUCE_LOGS = register("minecraft:spruce_logs");
public static final BlockCategory STAIRS = register("minecraft:stairs");
public static final BlockCategory STONE_BRICKS = register("minecraft:stone_bricks");
public static final BlockCategory VALID_SPAWN = register("minecraft:valid_spawn");
public static final BlockCategory WOODEN_BUTTONS = register("minecraft:wooden_buttons");
public static final BlockCategory WOODEN_DOORS = register("minecraft:wooden_doors");
public static final BlockCategory WOODEN_PRESSURE_PLATES = register("minecraft:wooden_pressure_plates");
public static final BlockCategory WOODEN_SLABS = register("minecraft:wooden_slabs");
public static final BlockCategory WOODEN_STAIRS = register("minecraft:wooden_stairs");
public static final BlockCategory WOOL = register("minecraft:wool");

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();
}
}
}
private static BlockCategory register(final String id) {
return register(new BlockCategory(id));
}

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);
public static BlockCategory register(final BlockCategory tag) {
return BlockCategory.REGISTRY.register(tag.getId(), tag);
}

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

public static Collection<BlockCategory> values() {
return categoryMapping.values();
return BlockCategory.REGISTRY.values();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.NamespacedRegistry;

import java.util.Set;

Expand All @@ -30,6 +31,8 @@
*/
public class BlockCategory {

public static final NamespacedRegistry<BlockCategory> REGISTRY = new NamespacedRegistry<>();

private final String id;

public BlockCategory(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldedit.world.registry.BundledBlockData;
Expand All @@ -33,6 +34,8 @@

public class BlockType {

public static final NamespacedRegistry<BlockType> REGISTRY = new NamespacedRegistry<>();

private String id;
private BlockState defaultState;

Expand Down Expand Up @@ -100,7 +103,7 @@ public boolean hasItemType() {
*/
@Nullable
public ItemType getItemType() {
return ItemTypes.getItemType(this.id);
return ItemTypes.get(this.id);
}

/**
Expand Down
Loading

0 comments on commit d33e2e9

Please sign in to comment.