2018-01-03 05:35:51 +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-01-03 05:35:51 +00:00
|
|
|
|
2018-10-05 00:18:01 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkArgument;
|
|
|
|
|
2018-07-17 14:42:09 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
|
|
|
import com.sk89q.worldedit.function.mask.Mask;
|
|
|
|
import com.sk89q.worldedit.function.mask.SingleBlockTypeMask;
|
|
|
|
import com.sk89q.worldedit.function.pattern.FawePattern;
|
2019-01-09 07:13:44 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
2018-08-10 10:29:06 +00:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
|
|
|
import com.sk89q.worldedit.world.registry.BlockMaterial;
|
|
|
|
import com.sk89q.worldedit.extension.platform.Capability;
|
|
|
|
import com.sk89q.worldedit.registry.NamespacedRegistry;
|
2019-01-31 15:08:58 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.AbstractProperty;
|
2018-07-17 14:42:09 +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-07-05 08:15:51 +00:00
|
|
|
import com.sk89q.worldedit.world.item.ItemType;
|
2018-08-27 16:56:28 +00:00
|
|
|
import com.sk89q.worldedit.world.item.ItemTypes;
|
2018-01-03 05:35:51 +00:00
|
|
|
import com.sk89q.worldedit.world.registry.BundledBlockData;
|
2018-07-01 12:03:22 +00:00
|
|
|
import com.sk89q.worldedit.world.registry.LegacyMapper;
|
2018-01-03 05:35:51 +00:00
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
import javax.annotation.Nonnull;
|
2018-08-12 14:03:07 +00:00
|
|
|
import javax.annotation.Nullable;
|
2018-08-27 16:56:28 +00:00
|
|
|
import java.util.*;
|
2019-01-31 15:08:58 +00:00
|
|
|
import java.util.function.Function;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
public class BlockType implements FawePattern {
|
|
|
|
|
|
|
|
public static final NamespacedRegistry<BlockType> REGISTRY = new NamespacedRegistry<>("block type");
|
|
|
|
|
|
|
|
private final @Nonnull String id;
|
|
|
|
private ArrayList<BlockState> states;
|
|
|
|
public final Function<BlockState, BlockState> defaultValue;
|
|
|
|
private BlockTypes.Settings settings;
|
|
|
|
private BlockMaterial material;
|
|
|
|
|
|
|
|
public BlockType(@Nonnull String id) {
|
|
|
|
this(id, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlockType(@Nonnull String id, Function<BlockState, BlockState> defaultValue) {
|
|
|
|
this.id = id;
|
|
|
|
this.defaultValue = defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setStates(ArrayList<BlockState> states) {
|
|
|
|
this.states = states;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSettings(BlockTypes.Settings settings) {
|
|
|
|
this.settings = settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlockTypes.Settings getSettings(){
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<BlockState> updateStates(){
|
|
|
|
if(settings != null) {
|
|
|
|
return settings.localStates = new ArrayList<BlockState>(settings.localStates.stream().map(state -> new BlockStateImpl(this, state.getInternalId(), state.getOrdinal())).collect(Collectors.toList()));
|
|
|
|
}else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Deprecated
|
2019-01-31 15:08:58 +00:00
|
|
|
public int getMaxStateId() {
|
|
|
|
return settings.permutations;
|
2018-01-03 05:35:51 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 05:42:47 +00:00
|
|
|
/**
|
|
|
|
* Gets the ID of this block.
|
|
|
|
*
|
|
|
|
* @return The id
|
|
|
|
*/
|
2019-01-31 15:08:58 +00:00
|
|
|
public String getId() {
|
|
|
|
return this.id;
|
|
|
|
}
|
2018-01-03 05:35:51 +00:00
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
public String getNamespace() {
|
2018-08-27 16:56:28 +00:00
|
|
|
String id = getId();
|
|
|
|
int i = id.indexOf(':');
|
|
|
|
return i == -1 ? "minecraft" : id.substring(0, i);
|
|
|
|
}
|
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
public String getResource() {
|
2018-08-27 16:56:28 +00:00
|
|
|
String id = getId();
|
|
|
|
return id.substring(id.indexOf(':') + 1);
|
|
|
|
}
|
|
|
|
|
2018-06-17 05:42:47 +00:00
|
|
|
/**
|
|
|
|
* Gets the name of this block, or the ID if the name cannot be found.
|
|
|
|
*
|
|
|
|
* @return The name, or ID
|
|
|
|
*/
|
2019-01-31 15:08:58 +00:00
|
|
|
public String getName() {
|
2018-12-23 11:43:20 +00:00
|
|
|
String name = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getName(this);
|
|
|
|
if (name == null) {
|
2018-06-17 05:42:47 +00:00
|
|
|
return getId();
|
|
|
|
} else {
|
2018-12-23 11:43:20 +00:00
|
|
|
return name;
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Deprecated
|
2019-01-31 15:08:58 +00:00
|
|
|
public BlockState withPropertyId(int propertyId) {
|
|
|
|
if (settings.stateOrdinals == null) return settings.defaultState;
|
|
|
|
return states.get(settings.stateOrdinals[propertyId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
public BlockState withStateId(int internalStateId) {
|
|
|
|
return this.withPropertyId(internalStateId >> BlockTypes.BIT_OFFSET);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 14:42:09 +00:00
|
|
|
/**
|
2019-01-31 15:08:58 +00:00
|
|
|
* Properties string in the form property1=foo,prop2=bar
|
|
|
|
* @param properties
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public BlockState withProperties(String properties) {
|
|
|
|
int id = getInternalId();
|
|
|
|
for (String keyPair : properties.split(",")) {
|
|
|
|
String[] split = keyPair.split("=");
|
|
|
|
String name = split[0];
|
|
|
|
String value = split[1];
|
|
|
|
AbstractProperty btp = settings.propertiesMap.get(name);
|
|
|
|
id = btp.modify(id, btp.getValueFor(value));
|
|
|
|
}
|
|
|
|
return withStateId(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the properties of this BlockType in a key->property mapping.
|
2018-07-17 14:42:09 +00:00
|
|
|
*
|
|
|
|
* @return The properties map
|
|
|
|
*/
|
2018-08-12 14:03:07 +00:00
|
|
|
@Deprecated
|
2019-01-31 15:08:58 +00:00
|
|
|
public Map<String, ? extends Property<?>> getPropertyMap() {
|
|
|
|
return this.settings.propertiesMap;
|
2018-08-27 16:56:28 +00:00
|
|
|
}
|
2018-07-17 14:42:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the properties of this BlockType.
|
|
|
|
*
|
|
|
|
* @return the properties
|
|
|
|
*/
|
2018-08-12 14:03:07 +00:00
|
|
|
@Deprecated
|
2019-01-31 15:08:58 +00:00
|
|
|
public List<? extends Property<?>> getProperties() {
|
|
|
|
return this.settings.propertiesList;
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
@Deprecated
|
2019-01-31 15:08:58 +00:00
|
|
|
public Set<? extends Property<?>> getPropertiesSet() {
|
|
|
|
return this.settings.propertiesSet;
|
2018-08-27 16:56:28 +00:00
|
|
|
}
|
2018-07-17 14:42:09 +00:00
|
|
|
|
2018-07-23 01:05:33 +00:00
|
|
|
/**
|
|
|
|
* Gets a property by name.
|
|
|
|
*
|
|
|
|
* @param name The name
|
|
|
|
* @return The property
|
|
|
|
*/
|
2018-08-12 14:03:07 +00:00
|
|
|
@Deprecated
|
2019-01-31 15:08:58 +00:00
|
|
|
public <V> Property<V> getProperty(String name) {
|
|
|
|
checkArgument(this.settings.propertiesMap.get(name) != null, "%s has no property named %s", this, name);
|
|
|
|
return (Property<V>) this.settings.propertiesMap.get(name);
|
2018-08-27 16:56:28 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
public boolean hasProperty(PropertyKey key) {
|
|
|
|
int ordinal = key.ordinal();
|
|
|
|
return this.settings.propertiesMapArr.length > ordinal ? this.settings.propertiesMapArr[ordinal] != null : false;
|
2018-08-27 16:56:28 +00:00
|
|
|
}
|
2019-01-31 15:08:58 +00:00
|
|
|
|
|
|
|
public <V> Property<V> getProperty(PropertyKey key) {
|
|
|
|
try {
|
|
|
|
return (Property<V>) this.settings.propertiesMapArr[key.ordinal()];
|
|
|
|
} catch (IndexOutOfBoundsException ignore) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-27 16:56:28 +00:00
|
|
|
}
|
2018-07-23 01:05:33 +00:00
|
|
|
|
2018-06-17 05:42:47 +00:00
|
|
|
/**
|
|
|
|
* Gets the default state of this block type.
|
|
|
|
*
|
|
|
|
* @return The default state
|
|
|
|
*/
|
2019-01-31 15:08:58 +00:00
|
|
|
public BlockState getDefaultState() {
|
|
|
|
BlockState defaultState = this.settings.defaultState;
|
|
|
|
if (defaultValue != null) {
|
|
|
|
defaultState = defaultValue.apply(defaultState);
|
|
|
|
}
|
|
|
|
return defaultState;
|
|
|
|
}
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-09-20 06:56:46 +00:00
|
|
|
/**
|
2019-01-31 15:08:58 +00:00
|
|
|
* Slow
|
|
|
|
* @return collection of states
|
2018-09-20 06:56:46 +00:00
|
|
|
*/
|
2019-01-31 15:08:58 +00:00
|
|
|
@Deprecated
|
|
|
|
public List<BlockState> getAllStates() {
|
|
|
|
if (settings.stateOrdinals == null) return Collections.singletonList(getDefaultState());
|
|
|
|
return IntStream.of(settings.stateOrdinals).filter(i -> i != -1).mapToObj(i -> states.get(i)).collect(Collectors.toList());
|
|
|
|
}
|
2018-09-20 06:56:46 +00:00
|
|
|
|
2018-10-05 00:18:43 +00:00
|
|
|
/**
|
|
|
|
* Gets a state of this BlockType with the given properties.
|
|
|
|
*
|
|
|
|
* @return The state, if it exists
|
|
|
|
*/
|
2019-01-31 15:08:58 +00:00
|
|
|
public BlockState getState(Map<Property<?>, Object> key) {
|
|
|
|
int id = getInternalId();
|
|
|
|
for (Map.Entry<Property<?>, Object> iter : key.entrySet()) {
|
|
|
|
Property<?> prop = iter.getKey();
|
|
|
|
Object value = iter.getValue();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
* This is likely wrong. The only place this seems to currently (Dec 23 2018)
|
|
|
|
* be invoked is via ForgeWorld, and value is a String when invoked there...
|
|
|
|
*/
|
|
|
|
AbstractProperty btp = this.settings.propertiesMap.get(prop.getName());
|
|
|
|
checkArgument(btp != null, "%s has no property named %s", this, prop.getName());
|
|
|
|
id = btp.modify(id, btp.getValueFor((String)value));
|
|
|
|
}
|
|
|
|
return withStateId(id);
|
|
|
|
}
|
|
|
|
|
2018-10-05 00:18:43 +00:00
|
|
|
|
2018-07-01 13:20:07 +00:00
|
|
|
/**
|
|
|
|
* Gets whether this block type has an item representation.
|
|
|
|
*
|
|
|
|
* @return If it has an item
|
|
|
|
*/
|
2019-01-31 15:08:58 +00:00
|
|
|
public boolean hasItemType() {
|
2018-07-01 13:20:07 +00:00
|
|
|
return getItemType() != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the item representation of this block type, if it exists.
|
|
|
|
*
|
|
|
|
* @return The item representation
|
|
|
|
*/
|
|
|
|
@Nullable
|
2019-01-31 15:08:58 +00:00
|
|
|
public ItemType getItemType() {
|
|
|
|
return ItemTypes.get(this);
|
2018-08-27 16:56:28 +00:00
|
|
|
}
|
2018-07-01 13:20:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the material for this BlockType.
|
|
|
|
*
|
|
|
|
* @return The material
|
|
|
|
*/
|
2019-01-31 15:08:58 +00:00
|
|
|
public BlockMaterial getMaterial() {
|
|
|
|
return this.material == null ?
|
|
|
|
WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getMaterial(this)
|
|
|
|
: this.material;
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2018-08-14 07:18:49 +00:00
|
|
|
/**
|
|
|
|
* Gets the legacy ID. Needed for legacy reasons.
|
|
|
|
*
|
|
|
|
* DO NOT USE THIS.
|
|
|
|
*
|
|
|
|
* @return legacy id or 0, if unknown
|
|
|
|
*/
|
2019-01-31 15:08:58 +00:00
|
|
|
public int getLegacyCombinedId() {
|
2018-08-14 07:18:49 +00:00
|
|
|
Integer combinedId = LegacyMapper.getInstance().getLegacyCombined(this);
|
2018-08-12 14:03:07 +00:00
|
|
|
return combinedId == null ? 0 : combinedId;
|
2018-07-01 13:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 05:35:51 +00:00
|
|
|
/**
|
2018-08-12 14:03:07 +00:00
|
|
|
* The internal index of this type.
|
2018-01-03 05:35:51 +00:00
|
|
|
*
|
2018-08-12 14:03:07 +00:00
|
|
|
* This number is not necessarily consistent across restarts.
|
2018-01-03 05:35:51 +00:00
|
|
|
*
|
2018-08-12 14:03:07 +00:00
|
|
|
* @return internal id
|
2018-01-03 05:35:51 +00:00
|
|
|
*/
|
2019-01-31 15:08:58 +00:00
|
|
|
public int getInternalId() {
|
|
|
|
return this.settings.internalId;
|
|
|
|
}
|
2018-01-03 05:35:51 +00:00
|
|
|
|
|
|
|
@Override
|
2019-01-31 15:08:58 +00:00
|
|
|
public int hashCode() {
|
|
|
|
return this.id.hashCode();
|
|
|
|
}
|
2018-01-03 05:35:51 +00:00
|
|
|
|
|
|
|
@Override
|
2019-01-31 15:08:58 +00:00
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof BlockType && this.id.equals(((BlockType) obj).id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
|
|
|
|
return extent.setBlock(set, this.getDefaultState());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockStateHolder apply(BlockVector3 position) {
|
|
|
|
return this.getDefaultState();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Mask toMask(Extent extent) {
|
|
|
|
return new SingleBlockTypeMask(extent, this);
|
|
|
|
}
|
|
|
|
|
2018-08-14 07:18:49 +00:00
|
|
|
|
|
|
|
@Deprecated
|
2019-01-31 15:08:58 +00:00
|
|
|
public int getLegacyId() {
|
2018-08-14 07:18:49 +00:00
|
|
|
Integer id = LegacyMapper.getInstance().getLegacyCombined(this.getDefaultState());
|
|
|
|
if (id != null) {
|
|
|
|
return id >> 4;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 05:35:51 +00:00
|
|
|
}
|