Skip to content

Commit

Permalink
Refactor registries to entirely use the platform
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Feb 16, 2019
1 parent 1b10174 commit db1315e
Show file tree
Hide file tree
Showing 12 changed files with 1,695 additions and 1,666 deletions.
Expand Up @@ -33,12 +33,22 @@
import com.sk89q.worldedit.event.platform.CommandEvent;
import com.sk89q.worldedit.event.platform.CommandSuggestionEvent;
import com.sk89q.worldedit.event.platform.PlatformReadyEvent;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extent.inventory.BlockBag;

import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.FuzzyBlockState;
import com.sk89q.worldedit.world.entity.EntityType;
import com.sk89q.worldedit.world.item.ItemType;
import org.bstats.bukkit.Metrics;
import org.bukkit.Material;
import org.bukkit.block.Biome;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
Expand All @@ -51,6 +61,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -88,6 +99,7 @@ public void onEnable() {
server = new BukkitServerInterface(this, getServer());
worldEdit.getPlatformManager().register(server);
loadAdapter(); // Need an adapter to work with special blocks with NBT data
setupRegistries();
worldEdit.loadMappings();

loadConfig(); // Load configuration
Expand All @@ -109,6 +121,45 @@ public void onEnable() {
new Metrics(this);
}

public void setupRegistries() {
// Biome
for (Biome biome : Biome.values()) {
BiomeType.REGISTRY.register("minecraft:" + biome.name().toLowerCase(), new BiomeType("minecraft:" + biome.name().toLowerCase()));
}
// Block & Item
for (Material material : Material.values()) {
if (material.isBlock()) {
BlockType.REGISTRY.register(material.getKey().toString(), new BlockType(material.getKey().toString(), blockState -> {
// TODO Use something way less hacky than this.
ParserContext context = new ParserContext();
context.setPreferringWildcard(true);
context.setTryLegacy(false);
context.setRestricted(false);
try {
FuzzyBlockState state = (FuzzyBlockState) WorldEdit.getInstance().getBlockFactory().parseFromInput(
BukkitAdapter.adapt(blockState.getBlockType()).createBlockData().getAsString(), context
).toImmutableState();
BlockState defaultState = blockState.getBlockType().getAllStates().get(0);
for (Map.Entry<Property<?>, Object> propertyObjectEntry : state.getStates().entrySet()) {
defaultState = defaultState.with((Property) propertyObjectEntry.getKey(), propertyObjectEntry.getValue());
}
return defaultState;
} catch (InputParseException e) {
e.printStackTrace();
return blockState;
}
}));
}
if (material.isItem()) {
ItemType.REGISTRY.register(material.getKey().toString(), new ItemType(material.getKey().toString()));
}
}
// Entity
for (org.bukkit.entity.EntityType entityType : org.bukkit.entity.EntityType.values()) {
EntityType.REGISTRY.register("minecraft:" + entityType.name().toLowerCase(), new EntityType("minecraft:" + entityType.name().toLowerCase()));
}
}

private void loadConfig() {
createDefaultConfiguration("config.yml"); // Create the default configuration file

Expand Down
Expand Up @@ -19,86 +19,25 @@

package com.sk89q.worldedit;

import com.google.common.collect.Lists;
import com.sk89q.worldedit.util.logging.LogFormat;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldedit.world.registry.LegacyMapper;
import com.sk89q.worldedit.world.snapshot.SnapshotRepository;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.IntFunction;

/**
* Represents WorldEdit's configuration.
*/
public abstract class LocalConfiguration {

protected static final String[] defaultDisallowedBlocks = new String[] {
// dangerous stuff (physics/drops items)
BlockTypes.OAK_SAPLING.getId(),
BlockTypes.JUNGLE_SAPLING.getId(),
BlockTypes.DARK_OAK_SAPLING.getId(),
BlockTypes.SPRUCE_SAPLING.getId(),
BlockTypes.BIRCH_SAPLING.getId(),
BlockTypes.ACACIA_SAPLING.getId(),
BlockTypes.BLACK_BED.getId(),
BlockTypes.BLUE_BED.getId(),
BlockTypes.BROWN_BED.getId(),
BlockTypes.CYAN_BED.getId(),
BlockTypes.GRAY_BED.getId(),
BlockTypes.GREEN_BED.getId(),
BlockTypes.LIGHT_BLUE_BED.getId(),
BlockTypes.LIGHT_GRAY_BED.getId(),
BlockTypes.LIME_BED.getId(),
BlockTypes.MAGENTA_BED.getId(),
BlockTypes.ORANGE_BED.getId(),
BlockTypes.PINK_BED.getId(),
BlockTypes.PURPLE_BED.getId(),
BlockTypes.RED_BED.getId(),
BlockTypes.WHITE_BED.getId(),
BlockTypes.YELLOW_BED.getId(),
BlockTypes.POWERED_RAIL.getId(),
BlockTypes.DETECTOR_RAIL.getId(),
BlockTypes.GRASS.getId(),
BlockTypes.DEAD_BUSH.getId(),
BlockTypes.MOVING_PISTON.getId(),
BlockTypes.PISTON_HEAD.getId(),
BlockTypes.SUNFLOWER.getId(),
BlockTypes.ROSE_BUSH.getId(),
BlockTypes.DANDELION.getId(),
BlockTypes.POPPY.getId(),
BlockTypes.BROWN_MUSHROOM.getId(),
BlockTypes.RED_MUSHROOM.getId(),
BlockTypes.TNT.getId(),
BlockTypes.TORCH.getId(),
BlockTypes.FIRE.getId(),
BlockTypes.REDSTONE_WIRE.getId(),
BlockTypes.WHEAT.getId(),
BlockTypes.POTATOES.getId(),
BlockTypes.CARROTS.getId(),
BlockTypes.MELON_STEM.getId(),
BlockTypes.PUMPKIN_STEM.getId(),
BlockTypes.BEETROOTS.getId(),
BlockTypes.RAIL.getId(),
BlockTypes.LEVER.getId(),
BlockTypes.REDSTONE_TORCH.getId(),
BlockTypes.REDSTONE_WALL_TORCH.getId(),
BlockTypes.REPEATER.getId(),
BlockTypes.COMPARATOR.getId(),
BlockTypes.STONE_BUTTON.getId(),
BlockTypes.BIRCH_BUTTON.getId(),
BlockTypes.ACACIA_BUTTON.getId(),
BlockTypes.DARK_OAK_BUTTON.getId(),
BlockTypes.JUNGLE_BUTTON.getId(),
BlockTypes.OAK_BUTTON.getId(),
BlockTypes.SPRUCE_BUTTON.getId(),
BlockTypes.CACTUS.getId(),
BlockTypes.SUGAR_CANE.getId(),
// ores and stuff
BlockTypes.BEDROCK.getId(),
};

public boolean profile = false;
public boolean traceUnflushedSessions = false;
public Set<String> disallowedBlocks = new HashSet<>();
Expand All @@ -117,15 +56,15 @@ public abstract class LocalConfiguration {
public String logFile = "";
public String logFormat = LogFormat.DEFAULT_FORMAT;
public boolean registerHelp = true; // what is the point of this, it's not even used
public String wandItem = ItemTypes.WOODEN_AXE.getId();
public String wandItem = "minecraft:wooden_axe";
public boolean superPickaxeDrop = true;
public boolean superPickaxeManyDrop = true;
public boolean noDoubleSlash = false;
public boolean useInventory = false;
public boolean useInventoryOverride = false;
public boolean useInventoryCreativeOverride = false;
public boolean navigationUseGlass = true;
public String navigationWand = ItemTypes.COMPASS.getId();
public String navigationWand = "minecraft:compass";
public int navigationWandMaxDistance = 50;
public int scriptTimeout = 3000;
public int calculationTimeout = 100;
Expand All @@ -138,6 +77,73 @@ public abstract class LocalConfiguration {
public boolean allowSymlinks = false;
public boolean serverSideCUI = true;

protected String[] getDefaultDisallowedBlocks() {
List<BlockType> blockTypes = Lists.newArrayList(
BlockTypes.OAK_SAPLING,
BlockTypes.JUNGLE_SAPLING,
BlockTypes.DARK_OAK_SAPLING,
BlockTypes.SPRUCE_SAPLING,
BlockTypes.BIRCH_SAPLING,
BlockTypes.ACACIA_SAPLING,
BlockTypes.BLACK_BED,
BlockTypes.BLUE_BED,
BlockTypes.BROWN_BED,
BlockTypes.CYAN_BED,
BlockTypes.GRAY_BED,
BlockTypes.GREEN_BED,
BlockTypes.LIGHT_BLUE_BED,
BlockTypes.LIGHT_GRAY_BED,
BlockTypes.LIME_BED,
BlockTypes.MAGENTA_BED,
BlockTypes.ORANGE_BED,
BlockTypes.PINK_BED,
BlockTypes.PURPLE_BED,
BlockTypes.RED_BED,
BlockTypes.WHITE_BED,
BlockTypes.YELLOW_BED,
BlockTypes.POWERED_RAIL,
BlockTypes.DETECTOR_RAIL,
BlockTypes.GRASS,
BlockTypes.DEAD_BUSH,
BlockTypes.MOVING_PISTON,
BlockTypes.PISTON_HEAD,
BlockTypes.SUNFLOWER,
BlockTypes.ROSE_BUSH,
BlockTypes.DANDELION,
BlockTypes.POPPY,
BlockTypes.BROWN_MUSHROOM,
BlockTypes.RED_MUSHROOM,
BlockTypes.TNT,
BlockTypes.TORCH,
BlockTypes.FIRE,
BlockTypes.REDSTONE_WIRE,
BlockTypes.WHEAT,
BlockTypes.POTATOES,
BlockTypes.CARROTS,
BlockTypes.MELON_STEM,
BlockTypes.PUMPKIN_STEM,
BlockTypes.BEETROOTS,
BlockTypes.RAIL,
BlockTypes.LEVER,
BlockTypes.REDSTONE_TORCH,
BlockTypes.REDSTONE_WALL_TORCH,
BlockTypes.REPEATER,
BlockTypes.COMPARATOR,
BlockTypes.STONE_BUTTON,
BlockTypes.BIRCH_BUTTON,
BlockTypes.ACACIA_BUTTON,
BlockTypes.DARK_OAK_BUTTON,
BlockTypes.JUNGLE_BUTTON,
BlockTypes.OAK_BUTTON,
BlockTypes.SPRUCE_BUTTON,
BlockTypes.CACTUS,
BlockTypes.SUGAR_CANE,
// ores and stuff
BlockTypes.BEDROCK
);
return blockTypes.stream().filter(type -> !Objects.isNull(type)).map(BlockType::getId).toArray(String[]::new);
}

/**
* Load the configuration.
*/
Expand Down
Expand Up @@ -77,7 +77,7 @@ public void load() {

profile = getBool("profile", profile);
traceUnflushedSessions = getBool("trace-unflushed-sessions", traceUnflushedSessions);
disallowedBlocks = getStringSet("disallowed-blocks", defaultDisallowedBlocks);
disallowedBlocks = getStringSet("disallowed-blocks", getDefaultDisallowedBlocks());
defaultChangeLimit = getInt("default-max-changed-blocks", defaultChangeLimit);
maxChangeLimit = getInt("max-changed-blocks", maxChangeLimit);
defaultMaxPolygonalPoints = getInt("default-max-polygon-points", defaultMaxPolygonalPoints);
Expand Down
Expand Up @@ -79,7 +79,7 @@ public void load() {
butcherDefaultRadius = Math.max(-1, config.getInt("limits.butcher-radius.default", butcherDefaultRadius));
butcherMaxRadius = Math.max(-1, config.getInt("limits.butcher-radius.maximum", butcherMaxRadius));

disallowedBlocks = new HashSet<>(config.getStringList("limits.disallowed-blocks", Lists.newArrayList(defaultDisallowedBlocks)));
disallowedBlocks = new HashSet<>(config.getStringList("limits.disallowed-blocks", Lists.newArrayList(getDefaultDisallowedBlocks())));
allowedDataCycleBlocks = new HashSet<>(config.getStringList("limits.allowed-data-cycle-blocks", null));

registerHelp = config.getBoolean("register-help", true);
Expand Down

0 comments on commit db1315e

Please sign in to comment.