Skip to content

Commit

Permalink
category changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Jul 5, 2018
1 parent 1460d5f commit b9772f4
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 166 deletions.
Expand Up @@ -41,13 +41,13 @@ private Blocks() {
*/
private static final Set<BlockType> shouldPlaceLast = new HashSet<>();
static {
shouldPlaceLast.addAll(BlockCategories.SAPLINGS.getBlockTypes());
shouldPlaceLast.addAll(BlockCategories.FLOWER_POTS.getBlockTypes());
shouldPlaceLast.addAll(BlockCategories.BUTTONS.getBlockTypes());
shouldPlaceLast.addAll(BlockCategories.ANVIL.getBlockTypes()); // becomes relevant with asynchronous placement
shouldPlaceLast.addAll(BlockCategories.WOODEN_PRESSURE_PLATES.getBlockTypes());
shouldPlaceLast.addAll(BlockCategories.CARPETS.getBlockTypes());
shouldPlaceLast.addAll(BlockCategories.RAILS.getBlockTypes());
shouldPlaceLast.addAll(BlockCategories.SAPLINGS.getAll());
shouldPlaceLast.addAll(BlockCategories.FLOWER_POTS.getAll());
shouldPlaceLast.addAll(BlockCategories.BUTTONS.getAll());
shouldPlaceLast.addAll(BlockCategories.ANVIL.getAll()); // becomes relevant with asynchronous placement
shouldPlaceLast.addAll(BlockCategories.WOODEN_PRESSURE_PLATES.getAll());
shouldPlaceLast.addAll(BlockCategories.CARPETS.getAll());
shouldPlaceLast.addAll(BlockCategories.RAILS.getAll());
shouldPlaceLast.add(BlockTypes.BLACK_BED);
shouldPlaceLast.add(BlockTypes.BLUE_BED);
shouldPlaceLast.add(BlockTypes.BROWN_BED);
Expand Down Expand Up @@ -128,8 +128,8 @@ public static boolean shouldPlaceLast(BlockType type) {
*/
private static final Set<BlockType> shouldPlaceFinal = new HashSet<>();
static {
shouldPlaceFinal.addAll(BlockCategories.DOORS.getBlockTypes());
shouldPlaceFinal.addAll(BlockCategories.BANNERS.getBlockTypes());
shouldPlaceFinal.addAll(BlockCategories.DOORS.getAll());
shouldPlaceFinal.addAll(BlockCategories.BANNERS.getAll());
shouldPlaceFinal.add(BlockTypes.SIGN);
shouldPlaceFinal.add(BlockTypes.WALL_SIGN);
shouldPlaceFinal.add(BlockTypes.CACTUS);
Expand Down
Expand Up @@ -186,7 +186,7 @@ public void searchItem(Actor actor, CommandContext args) throws WorldEditExcepti

int found = 0;

for (ItemType searchType : ItemTypes.values()) {
for (ItemType searchType : ItemType.REGISTRY) {
if (found >= 15) {
actor.print("Too many results!");
break;
Expand Down
@@ -0,0 +1,62 @@
/*
* 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.HashSet;
import java.util.Set;

public abstract class Category<T> {
private final Set<T> set = new HashSet<>();
protected final String id;
private boolean empty = true;

protected Category(final String id) {
this.id = id;
}

public final String getId() {
return this.id;
}

public final Set<T> getAll() {
if (this.empty) {
this.set.addAll(this.load());
this.empty = false;
}
return this.set;
}

protected abstract Set<T> load();

/**
* Checks if this category contains {@code object}.
*
* @param object the object
* @return {@code true} if this category contains the object
*/
public boolean contains(final T object) {
return this.getAll().contains(object);
}

public void invalidateCache() {
this.set.clear();
this.empty = true;
}
}
Expand Up @@ -22,6 +22,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

Expand All @@ -30,9 +31,14 @@
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;

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

public NamespacedRegistry(final String name) {
this.name = name;
}

public @Nullable V get(final String key) {
checkState(key.equals(key.toLowerCase()), "key must be lowercase");
Expand All @@ -44,7 +50,7 @@ public V register(final String key, final V value) {
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);
checkState(!this.map.containsKey(key), "key '%s' already has an associated %s", key, this.name);
this.map.put(key, value);
return value;
}
Expand All @@ -63,4 +69,9 @@ private String orDefaultNamespace(final String key) {
}
return key;
}

@Override
public Iterator<V> iterator() {
return this.map.values().iterator();
}
}
Expand Up @@ -19,20 +19,12 @@

package com.sk89q.worldedit.world.block;

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 final class BlockCategories {

public static final BlockCategory ACACIA_LOGS = register("minecraft:acacia_logs");
public static final BlockCategory ANVIL = register("minecraft:anvil");
Expand Down Expand Up @@ -67,6 +59,9 @@ private BlockCategories() {
public static final BlockCategory WOODEN_STAIRS = register("minecraft:wooden_stairs");
public static final BlockCategory WOOL = register("minecraft:wool");

private BlockCategories() {
}

private static BlockCategory register(final String id) {
return register(new BlockCategory(id));
}
Expand All @@ -75,12 +70,7 @@ public static BlockCategory register(final BlockCategory tag) {
return BlockCategory.REGISTRY.register(tag.getId(), tag);
}

@Nullable
public static BlockCategory get(final String id) {
public static @Nullable BlockCategory get(final String id) {
return BlockCategory.REGISTRY.get(id);
}

public static Collection<BlockCategory> values() {
return BlockCategory.REGISTRY.values();
}
}
Expand Up @@ -21,6 +21,7 @@

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

import java.util.Set;
Expand All @@ -29,35 +30,19 @@
* A category of blocks. This is due to the splitting up of
* blocks such as wool into separate ids.
*/
public class BlockCategory {
public class BlockCategory extends Category<BlockType> {

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

private final String id;

public BlockCategory(String id) {
this.id = id;
}

public String getId() {
return this.id;
public BlockCategory(final String id) {
super(id);
}

public Set<BlockType> getBlockTypes() {
@Override
protected Set<BlockType> load() {
return WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries()
.getBlockCategoryRegistry().getCategorisedByName(this.id);
}

/**
* Checks whether the BlocKType is contained within
* this category.
*
* @param blockType The blocktype
* @return If it's a part of this category
*/
public boolean contains(BlockType blockType) {
return getBlockTypes().contains(blockType);
.getBlockCategoryRegistry().getAll(this);
}

/**
Expand All @@ -68,6 +53,6 @@ public boolean contains(BlockType blockType) {
* @return If it's a part of this category
*/
public boolean contains(BlockStateHolder blockStateHolder) {
return getBlockTypes().contains(blockStateHolder.getBlockType());
return this.getAll().contains(blockStateHolder.getBlockType());
}
}
Expand Up @@ -34,7 +34,7 @@

public class BlockType {

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

private String id;
private BlockState defaultState;
Expand Down
Expand Up @@ -19,17 +19,12 @@

package com.sk89q.worldedit.world.block;

import java.util.Collection;

import javax.annotation.Nullable;

/**
* Stores a list of common Block String IDs.
*/
public class BlockTypes {

private BlockTypes() {
}
public final class BlockTypes {

public static final BlockType ACACIA_BARK = register("minecraft:acacia_bark");
public static final BlockType ACACIA_BUTTON = register("minecraft:acacia_button");
Expand Down Expand Up @@ -604,6 +599,9 @@ private BlockTypes() {
public static final BlockType ZOMBIE_HEAD = register("minecraft:zombie_head");
public static final BlockType ZOMBIE_WALL_HEAD = register("minecraft:zombie_wall_head");

private BlockTypes() {
}

private static BlockType register(final String id) {
return register(new BlockType(id));
}
Expand All @@ -612,12 +610,7 @@ public static BlockType register(final BlockType block) {
return BlockType.REGISTRY.register(block.getId(), block);
}

@Nullable
public static BlockType get(final String id) {
public static @Nullable BlockType get(final String id) {
return BlockType.REGISTRY.get(id);
}

public static Collection<BlockType> values() {
return BlockType.REGISTRY.values();
}
}
Expand Up @@ -19,21 +19,19 @@

package com.sk89q.worldedit.world.fluid;

import java.util.Collection;

import javax.annotation.Nullable;

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

private FluidCategories() {
}
public final class FluidCategories {

public static final FluidCategory LAVA = register("minecraft:lava");
public static final FluidCategory WATER = register("minecraft:water");

private FluidCategories() {
}

private static FluidCategory register(final String id) {
return register(new FluidCategory(id));
}
Expand All @@ -42,12 +40,7 @@ public static FluidCategory register(final FluidCategory tag) {
return FluidCategory.REGISTRY.register(tag.getId(), tag);
}

@Nullable
public static FluidCategory get(final String id) {
public static @Nullable FluidCategory get(final String id) {
return FluidCategory.REGISTRY.get(id);
}

public static Collection<FluidCategory> values() {
return FluidCategory.REGISTRY.values();
}
}
Expand Up @@ -19,6 +19,7 @@

package com.sk89q.worldedit.world.fluid;

import com.sk89q.worldedit.registry.Category;
import com.sk89q.worldedit.registry.NamespacedRegistry;

import java.util.Collections;
Expand All @@ -28,35 +29,19 @@
* A category of fluids. This is due to the splitting up of
* blocks such as wool into separate ids.
*/
public class FluidCategory {
public class FluidCategory extends Category<FluidType> {

public static final NamespacedRegistry<FluidCategory> REGISTRY = new NamespacedRegistry<>();
public static final NamespacedRegistry<FluidCategory> REGISTRY = new NamespacedRegistry<>("fluid tag");

private final String id;

public FluidCategory(String id) {
this.id = id;
}

public String getId() {
return this.id;
public FluidCategory(final String id) {
super(id);
}

public Set<FluidType> getFluidTypes() {
@Override
protected Set<FluidType> load() {
return Collections.emptySet(); // TODO Make this work.
// return WorldEdit.getInstance().getPlatformManager()
// .queryCapability(Capability.GAME_HOOKS).getRegistries()
// .getBlockCategoryRegistry().getCategorisedByName(this.id);
}

/**
* Checks whether the FluidType is contained within
* this category.
*
* @param fluidType The fluidType
* @return If it's a part of this category
*/
public boolean contains(FluidType fluidType) {
return getFluidTypes().contains(fluidType);
}
}

0 comments on commit b9772f4

Please sign in to comment.