Improve performance of some ExtendedBlockState methods (#4698)
This commit is contained in:
parent
dda7bd5d42
commit
fff660918a
1 changed files with 29 additions and 18 deletions
|
@ -22,15 +22,12 @@ package net.minecraftforge.common.property;
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import com.google.common.base.Predicates;
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.common.collect.ImmutableTable;
|
import com.google.common.collect.ImmutableTable;
|
||||||
import com.google.common.collect.Iterables;
|
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
import net.minecraft.block.properties.IProperty;
|
import net.minecraft.block.properties.IProperty;
|
||||||
import net.minecraft.block.state.BlockStateContainer;
|
import net.minecraft.block.state.BlockStateContainer;
|
||||||
|
@ -95,7 +92,7 @@ public class ExtendedBlockState extends BlockStateContainer
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlistedProperties.values().stream().noneMatch(Optional::isPresent))
|
if (this == this.cleanState)
|
||||||
{ // no dynamic properties present, looking up in the normal table
|
{ // no dynamic properties present, looking up in the normal table
|
||||||
return clean;
|
return clean;
|
||||||
}
|
}
|
||||||
|
@ -104,39 +101,53 @@ public class ExtendedBlockState extends BlockStateContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> IExtendedBlockState withProperty(IUnlistedProperty<V> property, V value)
|
public <V> IExtendedBlockState withProperty(IUnlistedProperty<V> property, @Nullable V value)
|
||||||
{
|
{
|
||||||
if(!this.unlistedProperties.containsKey(property))
|
Optional<?> oldValue = unlistedProperties.get(property);
|
||||||
|
if (oldValue == null)
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException("Cannot set unlisted property " + property + " as it does not exist in " + getBlock().getBlockState());
|
throw new IllegalArgumentException("Cannot set unlisted property " + property + " as it does not exist in " + getBlock().getBlockState());
|
||||||
}
|
}
|
||||||
if(!property.isValid(value))
|
if (Objects.equals(oldValue.orElse(null), value))
|
||||||
|
{
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
if (!property.isValid(value))
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException("Cannot set unlisted property " + property + " to " + value + " on block " + Block.REGISTRY.getNameForObject(getBlock()) + ", it is not an allowed value");
|
throw new IllegalArgumentException("Cannot set unlisted property " + property + " to " + value + " on block " + Block.REGISTRY.getNameForObject(getBlock()) + ", it is not an allowed value");
|
||||||
}
|
}
|
||||||
Map<IUnlistedProperty<?>, Optional<?>> newMap = new HashMap<IUnlistedProperty<?>, Optional<?>>(unlistedProperties);
|
boolean clean = true;
|
||||||
newMap.put(property, Optional.ofNullable(value));
|
ImmutableMap.Builder<IUnlistedProperty<?>, Optional<?>> builder = ImmutableMap.builder();
|
||||||
if(Iterables.all(newMap.values(), Predicates.<Optional<?>>equalTo(Optional.empty())))
|
for (Map.Entry<IUnlistedProperty<?>, Optional<?>> entry : unlistedProperties.entrySet())
|
||||||
{ // no dynamic properties, lookup normal state
|
{
|
||||||
return (IExtendedBlockState)cleanState;
|
IUnlistedProperty<?> key = entry.getKey();
|
||||||
|
Optional<?> newValue = key.equals(property) ? Optional.ofNullable(value) : entry.getValue();
|
||||||
|
if (newValue.isPresent()) clean = false;
|
||||||
|
builder.put(key, newValue);
|
||||||
}
|
}
|
||||||
return new ExtendedStateImplementation(getBlock(), getProperties(), ImmutableMap.copyOf(newMap), propertyValueTable, this.cleanState);
|
if (clean)
|
||||||
|
{ // no dynamic properties, lookup normal state
|
||||||
|
return (IExtendedBlockState) cleanState;
|
||||||
|
}
|
||||||
|
return new ExtendedStateImplementation(getBlock(), getProperties(), builder.build(), propertyValueTable, this.cleanState);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<IUnlistedProperty<?>> getUnlistedNames()
|
public Collection<IUnlistedProperty<?>> getUnlistedNames()
|
||||||
{
|
{
|
||||||
return Collections.unmodifiableCollection(unlistedProperties.keySet());
|
return unlistedProperties.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V>V getValue(IUnlistedProperty<V> property)
|
@Nullable
|
||||||
|
public <V> V getValue(IUnlistedProperty<V> property)
|
||||||
{
|
{
|
||||||
if(!this.unlistedProperties.containsKey(property))
|
Optional<?> value = unlistedProperties.get(property);
|
||||||
|
if (value == null)
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException("Cannot get unlisted property " + property + " as it does not exist in " + getBlock().getBlockState());
|
throw new IllegalArgumentException("Cannot get unlisted property " + property + " as it does not exist in " + getBlock().getBlockState());
|
||||||
}
|
}
|
||||||
return property.getType().cast(this.unlistedProperties.get(property).orElse(null));
|
return property.getType().cast(value.orElse(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImmutableMap<IUnlistedProperty<?>, Optional<?>> getUnlistedProperties()
|
public ImmutableMap<IUnlistedProperty<?>, Optional<?>> getUnlistedProperties()
|
||||||
|
|
Loading…
Reference in a new issue