Skip to content

Commit

Permalink
Basically finish the state system. Just 1 more bug that I'm encounter…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
me4502 committed Jul 17, 2018
1 parent 9e3c082 commit d42f36f
Show file tree
Hide file tree
Showing 21 changed files with 158 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.bukkit.Material;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;
Expand All @@ -39,17 +38,9 @@ public class BukkitBlockRegistry extends BundledBlockRegistry {

@Nullable
@Override
public BlockMaterial getMaterial(String id) {
return materialMap.computeIfAbsent(BukkitUtil.toMaterial(BlockTypes.get(id)),
material -> new BukkitBlockMaterial(BukkitBlockRegistry.super.getMaterial(id), material));
}

@Override
public List<Object> getPropertyValues(BlockType blockType, Property<?> property) {
if (WorldEditPlugin.getInstance().getBukkitImplAdapter() != null) {
return WorldEditPlugin.getInstance().getBukkitImplAdapter().getPropertyValues(blockType, property);
}
return super.getPropertyValues(blockType, property);
public BlockMaterial getMaterial(BlockType blockType) {
return materialMap.computeIfAbsent(BukkitUtil.toMaterial(blockType),
material -> new BukkitBlockMaterial(BukkitBlockRegistry.super.getMaterial(blockType), material));
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ public void onEnable() {

WorldEdit worldEdit = WorldEdit.getInstance();

loadConfig(); // Load configuration
PermissionsResolverManager.initialize(this); // Setup permission resolver

// Setup platform
server = new BukkitServerInterface(this, getServer());
worldEdit.getPlatformManager().register(server);
worldEdit.loadMappings();

loadConfig(); // Load configuration
PermissionsResolverManager.initialize(this); // Setup permission resolver

// Register CUI
getServer().getMessenger().registerIncomingPluginChannel(this, CUI_PLUGIN_CHANNEL, new CUIChannelListener(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.bukkit.block.Biome;
import org.bukkit.entity.Entity;

import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -95,15 +94,6 @@ public interface BukkitImplAdapter {
@Nullable
Entity createEntity(Location location, BaseEntity state);


/**
* Get a list of values for a property.
*
* @param property The property
* @return The list of values
*/
List<Object> getPropertyValues(BlockType blockType, Property<?> property);

/**
* Get a map of string -> properties
*
Expand Down
Binary file not shown.
12 changes: 9 additions & 3 deletions worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ public class WorldEdit {
static {
WorldEditPrefixHandler.register("com.sk89q.worldedit");
getVersion();
BundledBlockData.getInstance(); // Load block registry
BundledItemData.getInstance(); // Load item registry
LegacyMapper.getInstance(); // Load item registry
}

private WorldEdit() {
Expand Down Expand Up @@ -283,6 +280,15 @@ private File getSafeFile(Player player, File dir, String filename, String defaul
}
}

/**
* Load the bundled mappings.
*/
public void loadMappings() {
BundledBlockData.getInstance(); // Load block registry
BundledItemData.getInstance(); // Load item registry
LegacyMapper.getInstance(); // Load item registry
}

/**
* Checks to see if the specified radius is within bounds.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -78,7 +77,6 @@ private static BaseBlock getBlockInHand(Actor actor, HandSide handSide) throws I
public BlockStateHolder parseFromInput(String input, ParserContext context)
throws InputParseException {
String originalInput = input;
input = input.replace("_", " ");
input = input.replace(";", "|");
Exception suppressed = null;
try {
Expand Down Expand Up @@ -162,8 +160,7 @@ private static BlockState applyProperties(BlockState state, String[] stateProper
throw new NoMatchException("Bad state format in " + parseableData);
}

Property propertyKey = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS)
.getRegistries().getBlockRegistry().getProperties(state.getBlockType()).get(parts[0]);
Property propertyKey = state.getBlockType().getPropertyMap().get(parts[0]);
if (propertyKey == null) {
throw new NoMatchException("Unknown state " + parts[0] + " for block " + state.getBlockType().getName());
}
Expand All @@ -176,6 +173,7 @@ private static BlockState applyProperties(BlockState state, String[] stateProper
} catch (NoMatchException e) {
throw e; // Pass-through
} catch (Exception e) {
e.printStackTrace();
throw new NoMatchException("Unknown state '" + parseableData + "'");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ public synchronized Platform queryCapability(Capability capability) throws NoCap
if (platform != null) {
return platform;
} else {
if (preferences.isEmpty()) {
return platforms.get(0); // Use the first available if preferences have not been decided yet.
}
throw new NoCapablePlatformException("No platform was found supporting " + capability.name());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,17 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.LazyBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.registry.state.DirectionalProperty;
import com.sk89q.worldedit.registry.state.Property;

import java.util.Map;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -127,14 +124,7 @@ private static <T extends BlockStateHolder> T transform(T block, Transform trans
checkNotNull(block);
checkNotNull(transform);

Map<String, ? extends Property> states = WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getProperties(block.getBlockType());

if (states == null) {
return changedBlock;
}

for (Property property : states.values()) {
for (Property property : block.getBlockType().getProperties()) {
if (property instanceof DirectionalProperty) {
Vector value = (Vector) block.getState(property);
if (value != null) {
Expand Down Expand Up @@ -164,11 +154,11 @@ private static Vector getNewStateValue(DirectionalProperty state, Transform tran
double closest = -2;
boolean found = false;

for (Vector v : state.getValues()) {
double dot = v.normalize().dot(newDirection);
for (Direction v : state.getValues()) {
double dot = v.toVector().normalize().dot(newDirection);
if (dot >= closest) {
closest = dot;
newValue = v;
newValue = v.toVector();
found = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@

import static com.google.common.base.Preconditions.checkState;

import java.util.List;

public abstract class AbstractProperty<T> implements Property<T> {

private String name;
private List<T> values;

public AbstractProperty() {
public AbstractProperty(final String name, final List<T> values) {
this.name = name;
this.values = values;
}

public AbstractProperty(final String name) {
this.name = name;
@Override
public List<T> getValues() {
return this.values;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@

public class BooleanProperty extends AbstractProperty<Boolean> {

@Override
public List<Boolean> getValues() {
return null;
public BooleanProperty(final String name, final List<Boolean> values) {
super(name, values);
}

@Nullable
@Override
public Boolean getValueFor(String string) {
return null;
boolean val = Boolean.parseBoolean(string);
if (!getValues().contains(val)) {
throw new IllegalArgumentException("Invalid boolean value: " + string + ". Must be in " + getValues().toString());
}
return val;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@

package com.sk89q.worldedit.registry.state;

import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.util.Direction;

import java.util.List;

import javax.annotation.Nullable;

public class DirectionalProperty extends AbstractProperty<Vector> {
@Override
public List<Vector> getValues() {
return null;
public class DirectionalProperty extends AbstractProperty<Direction> {

public DirectionalProperty(final String name, final List<Direction> values) {
super(name, values);
}

@Nullable
@Override
public Vector getValueFor(final String string) {
return null;
public Direction getValueFor(final String string) {
Direction direction = Direction.valueOf(string);
if (!getValues().contains(direction)) {
throw new IllegalArgumentException("Invalid direction value: " + string + ". Must be in " + getValues().toString());
}
return direction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@

public class EnumProperty extends AbstractProperty<String> {

@Override
public List<String> getValues() {
return null;
public EnumProperty(final String name, final List<String> values) {
super(name, values);
}

@Nullable
@Override
public String getValueFor(String string) {
return null;
if (!getValues().contains(string)) {
throw new IllegalArgumentException("Invalid value: " + string + ". Must be in " + getValues().toString());
}
return string;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@

public class IntegerProperty extends AbstractProperty<Integer> {

@Override
public List<Integer> getValues() {
return null;
public IntegerProperty(final String name, final List<Integer> values) {
super(name, values);
}

@Nullable
@Override
public Integer getValueFor(String string) {
return null;
try {
int val = Integer.parseInt(string);
if (!getValues().contains(val)) {
throw new IllegalArgumentException("Invalid int value: " + string + ". Must be in " + getValues().toString());
}
return val;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid int value: " + string + ". Not an int.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public interface Property<T> {
*
* @param string The string
* @return The value, or null
* @throws IllegalArgumentException When the value is invalid.
*/
@Nullable
T getValueFor(String string);
T getValueFor(String string) throws IllegalArgumentException;
}
Loading

0 comments on commit d42f36f

Please sign in to comment.