2018-06-17 05:42:47 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-07-05 08:15:51 +00:00
|
|
|
package com.sk89q.worldedit.world.block;
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-08-14 10:57:32 +00:00
|
|
|
import com.boydti.fawe.Fawe;
|
2018-08-21 17:22:37 +00:00
|
|
|
import com.boydti.fawe.command.SuggestInputParseException;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.boydti.fawe.object.string.MutableCharSequence;
|
|
|
|
import com.google.common.base.Function;
|
2018-08-21 17:22:37 +00:00
|
|
|
import com.google.common.base.Predicate;
|
|
|
|
import com.google.common.base.Supplier;
|
2018-06-17 05:42:47 +00:00
|
|
|
import com.google.common.collect.Maps;
|
2018-08-10 10:29:06 +00:00
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.Vector;
|
2018-08-27 16:56:28 +00:00
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2018-08-27 16:56:28 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BlockMaterial;
|
2018-08-21 17:22:37 +00:00
|
|
|
import com.sk89q.worldedit.extension.input.InputParseException;
|
2018-08-27 16:56:28 +00:00
|
|
|
import com.sk89q.worldedit.extension.platform.Capability;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
|
|
|
import com.sk89q.worldedit.function.mask.Mask;
|
|
|
|
import com.sk89q.worldedit.function.mask.SingleBlockStateMask;
|
|
|
|
import com.sk89q.worldedit.registry.state.AbstractProperty;
|
2018-07-05 23:16:52 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.Property;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.PropertyKey;
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import java.util.*;
|
2018-08-21 17:22:37 +00:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Stream;
|
2018-06-17 05:42:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An immutable class that represents the state a block can be in.
|
|
|
|
*/
|
2018-06-17 13:13:22 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2018-08-27 16:56:28 +00:00
|
|
|
public abstract class BlockState implements BlockStateHolder<BlockState> {
|
2018-08-12 14:03:07 +00:00
|
|
|
/**
|
|
|
|
* Returns a temporary BlockState for a given internal id
|
|
|
|
* @param combinedId
|
|
|
|
* @deprecated magic number
|
|
|
|
* @return BlockState
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2018-08-27 16:56:28 +00:00
|
|
|
public static BlockState getFromInternalId(int combinedId) throws InputParseException {
|
2018-08-12 14:03:07 +00:00
|
|
|
return BlockTypes.getFromStateId(combinedId).withStateId(combinedId);
|
|
|
|
}
|
|
|
|
|
2018-08-27 16:56:28 +00:00
|
|
|
@Deprecated
|
|
|
|
public static BlockState getFromOrdinal(int ordinal) {
|
|
|
|
return BlockTypes.states[ordinal];
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
/**
|
|
|
|
* Returns a temporary BlockState for a given type and string
|
|
|
|
* @param state String e.g. minecraft:water[level=4]
|
|
|
|
* @return BlockState
|
|
|
|
*/
|
2018-08-21 17:22:37 +00:00
|
|
|
public static BlockState get(String state) throws InputParseException {
|
2018-08-12 14:03:07 +00:00
|
|
|
return get(null, state);
|
|
|
|
}
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
/**
|
|
|
|
* Returns a temporary BlockState for a given type and string
|
|
|
|
* - It's faster if a BlockType is provided compared to parsing the string
|
|
|
|
* @param type BlockType e.g. BlockTypes.STONE (or null)
|
|
|
|
* @param state String e.g. minecraft:water[level=4]
|
|
|
|
* @return BlockState
|
|
|
|
*/
|
2018-08-21 17:22:37 +00:00
|
|
|
public static BlockState get(@Nullable BlockType type, String state) throws InputParseException {
|
2018-08-27 16:56:28 +00:00
|
|
|
return get(type, state, null);
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 13:13:22 +00:00
|
|
|
/**
|
2018-08-12 14:03:07 +00:00
|
|
|
* Returns a temporary BlockState for a given type and string
|
|
|
|
* - It's faster if a BlockType is provided compared to parsing the string
|
|
|
|
* @param type BlockType e.g. BlockTypes.STONE (or null)
|
|
|
|
* @param state String e.g. minecraft:water[level=4]
|
|
|
|
* @return BlockState
|
2018-06-17 13:13:22 +00:00
|
|
|
*/
|
2018-08-27 16:56:28 +00:00
|
|
|
public static BlockState get(@Nullable BlockType type, String state, BlockState defaultState) throws InputParseException {
|
2018-08-12 14:03:07 +00:00
|
|
|
int propStrStart = state.indexOf('[');
|
|
|
|
if (type == null) {
|
|
|
|
CharSequence key;
|
|
|
|
if (propStrStart == -1) {
|
|
|
|
key = state;
|
|
|
|
} else {
|
|
|
|
MutableCharSequence charSequence = MutableCharSequence.getTemporal();
|
|
|
|
charSequence.setString(state);
|
|
|
|
charSequence.setSubstring(0, propStrStart);
|
|
|
|
key = charSequence;
|
2018-07-17 14:42:09 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
type = BlockTypes.get(key);
|
2018-08-21 17:22:37 +00:00
|
|
|
if (type == null) {
|
|
|
|
String input = key.toString();
|
2018-10-03 23:12:29 +00:00
|
|
|
throw new SuggestInputParseException("Does not match a valid block type: " + input, input, () -> Stream.of(BlockTypes.values)
|
2018-08-21 17:22:37 +00:00
|
|
|
.filter(b -> b.getId().contains(input))
|
|
|
|
.map(e1 -> e1.getId())
|
|
|
|
.collect(Collectors.toList())
|
|
|
|
);
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
if (propStrStart == -1) {
|
|
|
|
return type.getDefaultState();
|
2018-07-17 14:42:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
List<? extends Property> propList = type.getProperties();
|
|
|
|
|
2018-08-21 17:22:37 +00:00
|
|
|
if (state.charAt(state.length() - 1) != ']') state = state + "]";
|
2018-08-12 14:03:07 +00:00
|
|
|
MutableCharSequence charSequence = MutableCharSequence.getTemporal();
|
|
|
|
charSequence.setString(state);
|
|
|
|
|
|
|
|
if (propList.size() == 1) {
|
|
|
|
AbstractProperty property = (AbstractProperty) propList.get(0);
|
|
|
|
String name = property.getName();
|
|
|
|
|
|
|
|
charSequence.setSubstring(propStrStart + name.length() + 2, state.length() - 1);
|
|
|
|
|
|
|
|
return type.withPropertyId(property.getIndexFor(charSequence));
|
2018-07-17 14:42:09 +00:00
|
|
|
}
|
2018-08-27 16:56:28 +00:00
|
|
|
int stateId;
|
|
|
|
if (defaultState != null) {
|
|
|
|
stateId = defaultState.getInternalId();
|
|
|
|
} else {
|
|
|
|
stateId = type.getInternalId();
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
int length = state.length();
|
|
|
|
AbstractProperty property = null;
|
|
|
|
|
|
|
|
int last = propStrStart + 1;
|
|
|
|
for (int i = last; i < length; i++) {
|
|
|
|
char c = state.charAt(i);
|
|
|
|
switch (c) {
|
|
|
|
case ']':
|
|
|
|
case ',': {
|
2018-08-21 17:22:37 +00:00
|
|
|
charSequence.setSubstring(last, i);
|
2018-08-14 10:57:32 +00:00
|
|
|
if (property != null) {
|
|
|
|
int index = property.getIndexFor(charSequence);
|
2018-08-21 17:22:37 +00:00
|
|
|
if (index == -1) {
|
|
|
|
String input = charSequence.toString();
|
|
|
|
List<Object> values = property.getValues();
|
|
|
|
throw new SuggestInputParseException("No value: " + input + " for " + type, input, () ->
|
|
|
|
values.stream()
|
|
|
|
.map(v -> v.toString())
|
|
|
|
.filter(v -> v.startsWith(input))
|
|
|
|
.collect(Collectors.toList()));
|
|
|
|
}
|
2018-08-14 10:57:32 +00:00
|
|
|
stateId = property.modifyIndex(stateId, index);
|
|
|
|
} else {
|
2018-08-21 17:22:37 +00:00
|
|
|
// suggest
|
|
|
|
PropertyKey key = PropertyKey.get(charSequence);
|
|
|
|
if (key == null || !type.hasProperty(key)) {
|
|
|
|
// Suggest property
|
|
|
|
String input = charSequence.toString();
|
|
|
|
BlockType finalType = type;
|
|
|
|
throw new SuggestInputParseException("Invalid property " + type + " | " + input, input, () ->
|
|
|
|
finalType.getProperties().stream()
|
|
|
|
.map(p -> p.getName())
|
|
|
|
.filter(p -> p.startsWith(input))
|
|
|
|
.collect(Collectors.toList()));
|
|
|
|
} else {
|
|
|
|
throw new SuggestInputParseException("No operator for " + state, "", () -> Arrays.asList("="));
|
|
|
|
}
|
2018-08-14 10:57:32 +00:00
|
|
|
}
|
2018-08-21 17:22:37 +00:00
|
|
|
property = null;
|
2018-08-12 14:03:07 +00:00
|
|
|
last = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '=': {
|
|
|
|
charSequence.setSubstring(last, i);
|
|
|
|
property = (AbstractProperty) type.getPropertyMap().get(charSequence);
|
|
|
|
last = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-17 14:42:09 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
return type.withPropertyId(stateId >> BlockTypes.BIT_OFFSET);
|
|
|
|
}
|
2018-07-17 14:42:09 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Override
|
|
|
|
public BlockState withPropertyId(int propertyId) {
|
|
|
|
return getBlockType().withPropertyId(propertyId);
|
2018-07-17 14:42:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Override
|
|
|
|
public Mask toMask(Extent extent) {
|
|
|
|
return new SingleBlockStateMask(extent, this);
|
|
|
|
}
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Override
|
|
|
|
public boolean apply(Extent extent, Vector get, Vector set) throws WorldEditException {
|
|
|
|
return extent.setBlock(set, this);
|
|
|
|
}
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Override
|
|
|
|
public BlockState apply(Vector position) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNbtData() {
|
|
|
|
return getNbtData() != null;
|
|
|
|
}
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Override
|
|
|
|
public String getNbtId() {
|
|
|
|
return "";
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public CompoundTag getNbtData() {
|
|
|
|
return null;
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 07:53:33 +00:00
|
|
|
@Override
|
2018-08-12 14:03:07 +00:00
|
|
|
public void setNbtData(@Nullable CompoundTag nbtData) {
|
|
|
|
throw new UnsupportedOperationException("This class is immutable.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal id with no type information
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
@Deprecated
|
|
|
|
@Override
|
|
|
|
public final int getInternalPropertiesId() {
|
|
|
|
return this.getInternalId() >> BlockTypes.BIT_OFFSET;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
@Override
|
|
|
|
public final int getInternalBlockTypeId() {
|
|
|
|
return this.getInternalId() & BlockTypes.BIT_MASK;
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 07:53:33 +00:00
|
|
|
@Override
|
2018-07-05 23:16:52 +00:00
|
|
|
public <V> BlockState with(final Property<V> property, final V value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
try {
|
2018-08-27 16:56:28 +00:00
|
|
|
BlockTypes type = getBlockType();
|
2018-08-12 14:03:07 +00:00
|
|
|
int newState = ((AbstractProperty) property).modify(this.getInternalId(), value);
|
2018-08-27 16:56:28 +00:00
|
|
|
return newState != this.getInternalId() ? type.withStateId(newState) : this;
|
2018-08-12 14:03:07 +00:00
|
|
|
} catch (ClassCastException e) {
|
|
|
|
throw new IllegalArgumentException("Property not found: " + property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <V> BlockState with(final PropertyKey property, final V value) {
|
|
|
|
try {
|
2018-08-27 16:56:28 +00:00
|
|
|
BlockTypes type = getBlockType();
|
|
|
|
int newState = ((AbstractProperty) type.getProperty(property)).modify(this.getInternalId(), value);
|
|
|
|
return newState != this.getInternalId() ? type.withStateId(newState) : this;
|
2018-08-12 14:03:07 +00:00
|
|
|
} catch (ClassCastException e) {
|
|
|
|
throw new IllegalArgumentException("Property not found: " + property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final <V> V getState(final Property<V> property) {
|
|
|
|
try {
|
|
|
|
AbstractProperty ap = (AbstractProperty) property;
|
|
|
|
return (V) ap.getValue(this.getInternalId());
|
|
|
|
} catch (ClassCastException e) {
|
|
|
|
throw new IllegalArgumentException("Property not found: " + property);
|
2018-06-17 13:13:22 +00:00
|
|
|
}
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Deprecated
|
2018-06-18 07:53:33 +00:00
|
|
|
@Override
|
2018-08-12 14:03:07 +00:00
|
|
|
public final <V> V getState(final PropertyKey key) {
|
|
|
|
return getState(getBlockType().getProperty(key));
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 07:53:33 +00:00
|
|
|
@Override
|
2018-08-12 14:03:07 +00:00
|
|
|
@Deprecated
|
|
|
|
public final Map<Property<?>, Object> getStates() {
|
|
|
|
BlockType type = this.getBlockType();
|
|
|
|
// Lazily initialize the map
|
|
|
|
Map<? extends Property, Object> map = Maps.asMap(type.getPropertiesSet(), (Function<Property, Object>) input -> getState(input));
|
|
|
|
return (Map<Property<?>, Object>) map;
|
2018-06-17 13:13:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
/**
|
|
|
|
* Deprecated, use masks - not try to this fuzzy/non fuzzy state nonsense
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2018-06-19 01:55:35 +00:00
|
|
|
public BlockState toFuzzy() {
|
2018-08-12 14:03:07 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2018-08-27 16:56:28 +00:00
|
|
|
return getOrdinal();
|
2018-06-19 01:55:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 12:51:21 +00:00
|
|
|
@Override
|
2018-08-12 14:03:07 +00:00
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return this == obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Deprecated
|
2018-06-18 12:51:21 +00:00
|
|
|
public boolean equalsFuzzy(BlockStateHolder o) {
|
2018-08-27 16:56:28 +00:00
|
|
|
return o.getOrdinal() == this.getOrdinal();
|
2018-06-18 12:51:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 12:03:22 +00:00
|
|
|
@Override
|
|
|
|
public BlockState toImmutableState() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-07-23 02:48:11 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return getAsString();
|
|
|
|
}
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|