use of com.sk89q.worldedit.registry.state.Property in project FastAsyncWorldEdit by IntellectualSites.
the class PaperweightAdapter method getProperties.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Map<String, ? extends Property<?>> getProperties(BlockType blockType) {
Map<String, Property<?>> properties = Maps.newTreeMap(String::compareTo);
Block block = getBlockFromType(blockType);
StateDefinition<Block, net.minecraft.world.level.block.state.BlockState> blockStateList = block.getStateDefinition();
for (net.minecraft.world.level.block.state.properties.Property state : blockStateList.getProperties()) {
Property property;
if (state instanceof net.minecraft.world.level.block.state.properties.BooleanProperty) {
property = new BooleanProperty(state.getName(), ImmutableList.copyOf(state.getPossibleValues()));
} else if (state instanceof DirectionProperty) {
property = new DirectionalProperty(state.getName(), (List<Direction>) state.getPossibleValues().stream().map(e -> Direction.valueOf(((StringRepresentable) e).getSerializedName().toUpperCase(Locale.ROOT))).collect(Collectors.toList()));
} else if (state instanceof net.minecraft.world.level.block.state.properties.EnumProperty) {
property = new EnumProperty(state.getName(), (List<String>) state.getPossibleValues().stream().map(e -> ((StringRepresentable) e).getSerializedName()).collect(Collectors.toList()));
} else if (state instanceof net.minecraft.world.level.block.state.properties.IntegerProperty) {
property = new IntegerProperty(state.getName(), ImmutableList.copyOf(state.getPossibleValues()));
} else {
throw new IllegalArgumentException("FastAsyncWorldEdit needs an update to support " + state.getClass().getSimpleName());
}
properties.put(property.getName(), property);
}
return properties;
}
use of com.sk89q.worldedit.registry.state.Property in project FastAsyncWorldEdit by IntellectualSites.
the class SnowSimulator method apply.
@Override
public boolean apply(BlockVector3 position, int depth) throws WorldEditException {
if (depth > 0) {
// We only care about the first layer.
return false;
}
BlockState block = this.extent.getBlock(position);
if (block.getBlockType() == BlockTypes.WATER) {
if (block.getState(waterLevelProperty) == 0) {
if (this.extent.setBlock(position, ice)) {
affected++;
}
}
return false;
}
// Can't put snow this far up
if (position.getBlockY() == this.extent.getMaximumPoint().getBlockY()) {
return false;
}
BlockVector3 abovePosition = position.add(0, 1, 0);
BlockState above = this.extent.getBlock(abovePosition);
// Can only replace air (or snow in stack mode)
if (!above.getBlockType().getMaterial().isAir() && (!stack || above.getBlockType() != BlockTypes.SNOW)) {
return false;
// FAWE start
} else if (!block.getBlockType().getId().toLowerCase(Locale.ROOT).contains("ice") && this.extent.getEmittedLight(abovePosition) > 10) {
return false;
} else if (!block.getBlockType().getMaterial().isFullCube()) {
Map<Property<?>, Object> states = block.getStates();
if (states.containsKey(slab) && block.getState(slab).equalsIgnoreCase("bottom")) {
return false;
} else if (states.containsKey(trapdoorOpen) && states.containsKey(trapdoor) && (block.getState(trapdoorOpen) || block.getState(trapdoor).equalsIgnoreCase("bottom"))) {
return false;
} else if (states.containsKey(stair) && block.getState(stair).equalsIgnoreCase("bottom")) {
return false;
} else {
return false;
}
// FAWE end
} else if (!block.getBlockType().getId().toLowerCase(Locale.ROOT).contains("ice") && block.getBlockType().getMaterial().isTranslucent()) {
return false;
}
if (stack && above.getBlockType() == BlockTypes.SNOW) {
int currentHeight = above.getState(snowLayersProperty);
// We've hit the highest layer (If it doesn't contain current + 2 it means it's 1 away from full)
if (!snowLayersProperty.getValues().contains(currentHeight + 2)) {
if (this.extent.setBlock(abovePosition, snowBlock)) {
if (block.getStates().containsKey(snowy)) {
this.extent.setBlock(position, block.with(snowy, true));
}
this.affected++;
}
} else {
if (this.extent.setBlock(abovePosition, above.with(snowLayersProperty, currentHeight + 1))) {
if (block.getStates().containsKey(snowy)) {
this.extent.setBlock(position, block.with(snowy, true));
}
this.affected++;
}
}
return false;
}
if (this.extent.setBlock(abovePosition, snow)) {
if (block.getStates().containsKey(snowy)) {
this.extent.setBlock(position, block.with(snowy, true));
}
this.affected++;
}
return false;
}
use of com.sk89q.worldedit.registry.state.Property in project FastAsyncWorldEdit by IntellectualSites.
the class BlockState method get.
/**
* Returns a temporary BlockState for a given type and string.
*
* <p>It's faster if a BlockType is provided compared to parsing the string.</p>
*
* @param type BlockType e.g., BlockTypes.STONE (or null)
* @param state String e.g., minecraft:water[level=4]
* @return BlockState
*/
public static BlockState get(@Nullable BlockType type, String state, BlockState defaultState) throws InputParseException {
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;
}
type = BlockTypes.get(key);
if (type == null) {
String input = key.toString();
throw new SuggestInputParseException("Does not match a valid block type: " + input, input, () -> Stream.of(BlockTypesCache.values).map(BlockType::getId).filter(id -> StringMan.blockStateMatches(input, id)).sorted(StringMan.blockStateComparator(input)).collect(Collectors.toList()));
}
}
if (propStrStart == -1) {
return type.getDefaultState();
}
List<? extends Property<?>> propList = type.getProperties();
if (state.charAt(state.length() - 1) != ']') {
state = state + "]";
}
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);
int index = charSequence.length() <= 0 ? -1 : property.getIndexFor(charSequence);
if (index != -1) {
return type.withPropertyId(index);
}
}
int stateId;
if (defaultState != null) {
stateId = defaultState.getInternalId();
} else {
stateId = type.getDefaultState().getInternalId();
}
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 ',':
{
charSequence.setSubstring(last, i);
if (property != null) {
int index = property.getIndexFor(charSequence);
if (index == -1) {
throw SuggestInputParseException.of(charSequence.toString(), (List<Object>) property.getValues());
}
stateId = property.modifyIndex(stateId, index);
} else {
// suggest
PropertyKey key = PropertyKey.getByName(charSequence);
if (key == null || !type.hasProperty(key)) {
// Suggest property
String input = charSequence.toString();
BlockType finalType = type;
throw new SuggestInputParseException("Invalid property " + key + ":" + input + " for type " + type, input, () -> finalType.getProperties().stream().map(Property::getName).filter(p -> StringMan.blockStateMatches(input, p)).sorted(StringMan.blockStateComparator(input)).collect(Collectors.toList()));
} else {
throw new SuggestInputParseException("No operator for " + state, "", () -> Collections.singletonList("="));
}
}
property = null;
last = i + 1;
break;
}
case '=':
{
charSequence.setSubstring(last, i);
property = (AbstractProperty) type.getPropertyMap().get(charSequence);
last = i + 1;
break;
}
default:
continue;
}
}
return type.withPropertyId(stateId >> BlockTypesCache.BIT_OFFSET);
}
use of com.sk89q.worldedit.registry.state.Property in project FastAsyncWorldEdit by IntellectualSites.
the class BlockType method getState.
/**
* Gets a state of this BlockType with the given properties.
*
* @return The state, if it exists
* @deprecated Not working. Not necessarily for removal, but WARNING DO NOT USE FOR NOW
*/
@Deprecated(forRemoval = true)
public BlockState getState(Map<Property<?>, Object> key) {
// FAWE start - use ids & btp (block type property)
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);
// FAWE end
}
use of com.sk89q.worldedit.registry.state.Property in project FastAsyncWorldEdit by IntellectualSites.
the class PaperweightAdapter method applyProperties.
@SuppressWarnings({ "rawtypes", "unchecked" })
private net.minecraft.world.level.block.state.BlockState applyProperties(StateDefinition<Block, net.minecraft.world.level.block.state.BlockState> stateContainer, net.minecraft.world.level.block.state.BlockState newState, Map<Property<?>, Object> states) {
for (Map.Entry<Property<?>, Object> state : states.entrySet()) {
net.minecraft.world.level.block.state.properties.Property<?> property = stateContainer.getProperty(state.getKey().getName());
Comparable<?> value = (Comparable) state.getValue();
// we may need to adapt this value, depending on the source prop
if (property instanceof DirectionProperty) {
Direction dir = (Direction) value;
value = adapt(dir);
} else if (property instanceof net.minecraft.world.level.block.state.properties.EnumProperty) {
String enumName = (String) value;
value = ((net.minecraft.world.level.block.state.properties.EnumProperty<?>) property).getValue(enumName).orElseThrow(() -> new IllegalStateException("Enum property " + property.getName() + " does not contain " + enumName));
}
newState = newState.setValue((net.minecraft.world.level.block.state.properties.Property) property, (Comparable) value);
}
return newState;
}
Aggregations