Add new system for model data, replacing extended states (#5564)

This commit is contained in:
tterrag authored and GitHub committed 2019-05-09 02:10:38 -04:00
1 parent a7204b5cd3
commit 9209c3cbfe
29 files changed
+718 -572

No files matched your search

@@ -19,6 +19,8 @@
package net.minecraftforge.common.extensions;
import javax.annotation.Nonnull;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
@@ -29,6 +31,9 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.model.ModelDataManager;
import net.minecraftforge.client.model.data.EmptyModelData;
import net.minecraftforge.client.model.data.IModelData;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
public interface IForgeTileEntity extends ICapabilitySerializable<NBTTagCompound>
@@ -86,7 +91,10 @@ public interface IForgeTileEntity extends ICapabilitySerializable<NBTTagCompound
* Called when this is first added to the world (by {@link World#addTileEntity(TileEntity)}).
* Override instead of adding {@code if (firstTick)} stuff in update.
*/
default void onLoad(){}
default void onLoad()
{
requestModelDataUpdate();
}
default boolean shouldRenderInPass(int pass)
{
@@ -168,4 +176,19 @@ public interface IForgeTileEntity extends ICapabilitySerializable<NBTTagCompound
{
return false;
}
default void requestModelDataUpdate()
{
TileEntity te = getTileEntity();
World world = te.getWorld();
if (world != null && world.isRemote)
{
ModelDataManager.requestModelDataRefresh(te);
}
}
default @Nonnull IModelData getModelData()
{
return EmptyModelData.INSTANCE;
}
}
@@ -1,178 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library 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 version 2.1
* of the License.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.property;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableTable;
import net.minecraft.block.Block;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.state.AbstractStateHolder;
import net.minecraft.state.IProperty;
import net.minecraft.state.IStateHolder;
import net.minecraft.state.StateContainer;
// TODO Extended states gotta go, I doubt this works at all
public class ExtendedStateContainer<O, S extends IExtendedState<S>> extends StateContainer<O, S>
{
private final ImmutableSet<IUnlistedProperty<?>> unlistedProperties;
public <A extends AbstractStateHolder<O, S>> ExtendedStateContainer(O blockIn, StateContainer.IFactory<O, S, A> stateFactory, net.minecraft.state.IProperty<?>[] properties, IUnlistedProperty<?>[] unlistedProperties)
{
super(blockIn, getProxyFactory(unlistedProperties, stateFactory), buildListedMap(properties));// TODO Unlisted properties?, buildUnlistedMap(unlistedProperties));
ImmutableSet.Builder<IUnlistedProperty<?>> builder = ImmutableSet.builder();
for(IUnlistedProperty<?> property : unlistedProperties)
{
builder.add(property);
}
this.unlistedProperties = builder.build();
}
private static <O, S extends IExtendedState<S>, A extends AbstractStateHolder<O, S>>
StateContainer.IFactory<O, S, AbstractStateHolder<O,S>> getProxyFactory(IUnlistedProperty<?>[] unlistedProperties, StateContainer.IFactory<O, S, A> proxy)
{
return (o, props) -> {
if (unlistedProperties == null || unlistedProperties.length == 0) return proxy.create(o, props);
return new ExtendedStateHolder<O, S>(o, props, buildUnlistedMap(unlistedProperties), null);
};
}
public Collection<IUnlistedProperty<?>> getUnlistedProperties()
{
return unlistedProperties;
}
private static Map<String, IProperty<?>> buildListedMap(IProperty<?>[] properties)
{
return Arrays.stream(properties).collect(Collectors.toMap(IProperty::getName, Function.identity()));
}
private static ImmutableMap<IUnlistedProperty<?>, Optional<?>> buildUnlistedMap(IUnlistedProperty<?>[] unlistedProperties)
{
ImmutableMap.Builder<IUnlistedProperty<?>, Optional<?>> builder = ImmutableMap.builder();
for(IUnlistedProperty<?> p : unlistedProperties)
{
builder.put(p, Optional.empty());
}
return builder.build();
}
protected static class ExtendedStateHolder<O, S extends IExtendedState<S>> extends AbstractStateHolder<O, S> implements IExtendedState<S>
{
private final ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties;
private S cleanState;
protected ExtendedStateHolder(O block, ImmutableMap<IProperty<?>, Comparable<?>> properties, ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties, S clean)
{
super(block, properties);
this.unlistedProperties = unlistedProperties;
this.cleanState = clean == null ? (S) this : clean;
}
@Override
@Nonnull
public <T extends Comparable<T>, V extends T> S with(@Nonnull IProperty<T> property, @Nonnull V value)
{
S clean = super.with(property, value);
if (clean == this.cleanState) {
return (S) this;
}
if (this == this.cleanState)
{ // no dynamic properties present, looking up in the normal table
return clean;
}
return (S) new ExtendedStateHolder(object, ((IStateHolder)clean).getValues(), unlistedProperties, this.cleanState);
}
@Override
public <V> S withProperty(IUnlistedProperty<V> property, @Nullable V value)
{
Optional<?> oldValue = unlistedProperties.get(property);
if (oldValue == null)
{
throw new IllegalArgumentException("Cannot set unlisted property " + property + " as it does not exist in " + this);
}
if (Objects.equals(oldValue.orElse(null), value))
{
return (S) this;
}
if (!property.isValid(value))
{
throw new IllegalArgumentException("Cannot set unlisted property " + property + " to " + value + " on object " + object + ", it is not an allowed value");
}
boolean clean = true;
ImmutableMap.Builder<IUnlistedProperty<?>, Optional<?>> builder = ImmutableMap.builder();
for (Map.Entry<IUnlistedProperty<?>, Optional<?>> entry : unlistedProperties.entrySet())
{
IUnlistedProperty<?> key = entry.getKey();
Optional<?> newValue = key.equals(property) ? Optional.ofNullable(value) : entry.getValue();
if (newValue.isPresent()) clean = false;
builder.put(key, newValue);
}
if (clean)
{ // no dynamic properties, lookup normal state
return (S) cleanState;
}
return (S) new ExtendedStateHolder(object, getValues(), builder.build(), this.cleanState);
}
@Override
public Collection<IUnlistedProperty<?>> getUnlistedNames()
{
return unlistedProperties.keySet();
}
@Override
@Nullable
public <V> V getValue(IUnlistedProperty<V> property)
{
Optional<?> value = unlistedProperties.get(property);
if (value == null)
{
throw new IllegalArgumentException("Cannot get unlisted property " + property + " as it does not exist in " + this);
}
return property.getType().cast(value.orElse(null));
}
public ImmutableMap<IUnlistedProperty<?>, Optional<?>> getUnlistedProperties()
{
return unlistedProperties;
}
@Override
public S getClean()
{
return cleanState;
}
}
}
@@ -1,27 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library 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 version 2.1
* of the License.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.property;
import net.minecraft.block.state.IBlockState;
public interface IExtendedBlockState extends IExtendedState<IBlockState>, IBlockState
{
}
@@ -1,41 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library 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 version 2.1
* of the License.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.property;
import java.util.Collection;
import net.minecraft.block.state.IBlockState;
import net.minecraft.state.IStateHolder;
import java.util.Optional;
import com.google.common.collect.ImmutableMap;
public interface IExtendedState<C> extends IStateHolder<C>
{
Collection<IUnlistedProperty<?>> getUnlistedNames();
<V>V getValue(IUnlistedProperty<V> property);
<V>C withProperty(IUnlistedProperty<V> property, V value);
ImmutableMap<IUnlistedProperty<?>, Optional<?>> getUnlistedProperties();
C getClean();
}
@@ -1,31 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library 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 version 2.1
* of the License.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.property;
public interface IUnlistedProperty<V>
{
String getName();
boolean isValid(V value);
Class<V> getType();
String valueToString(V value);
}
@@ -20,7 +20,7 @@
package net.minecraftforge.common.property;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.IProperty;
import net.minecraftforge.client.model.data.ModelProperty;
import net.minecraftforge.common.model.IModelState;
public class Properties
@@ -33,54 +33,5 @@ public class Properties
/**
* Property holding the IModelState used for animating the model in the TESR.
*/
public static final IUnlistedProperty<IModelState> AnimationProperty = new IUnlistedProperty<IModelState>()
{
@Override
public String getName() { return "forge_animation"; }
@Override
public boolean isValid(IModelState state) { return true; }
@Override
public Class<IModelState> getType() { return IModelState.class; }
@Override
public String valueToString(IModelState state) { return state.toString(); }
};
public static <V extends Comparable<V>> IUnlistedProperty<V> toUnlisted(IProperty<V> property)
{
return new PropertyAdapter<V>(property);
}
public static class PropertyAdapter<V extends Comparable<V>> implements IUnlistedProperty<V>
{
private final IProperty<V> parent;
public PropertyAdapter(IProperty<V> parent)
{
this.parent = parent;
}
@Override
public String getName()
{
return parent.getName();
}
@Override
public boolean isValid(V value)
{
return parent.getAllowedValues().contains(value);
}
@Override
public Class<V> getType()
{
return parent.getValueClass();
}
@Override
public String valueToString(V value)
{
return parent.getName(value);
}
}
public static final ModelProperty<IModelState> AnimationProperty = new ModelProperty<IModelState>();
}
@@ -1,70 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library 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 version 2.1
* of the License.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.property;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Range;
public class PropertyFloat implements IUnlistedProperty<Float>
{
private final String name;
private final Predicate<Float> validator;
public PropertyFloat(String name)
{
this(name, Predicates.alwaysTrue());
}
public PropertyFloat(String name, float min, float max)
{
this(name, Range.closed(min, max));
}
public PropertyFloat(String name, Predicate<Float> validator)
{
this.name = name;
this.validator = validator;
}
@Override
public String getName()
{
return name;
}
@Override
public boolean isValid(Float value)
{
return validator.apply(value);
}
@Override
public Class<Float> getType()
{
return Float.class;
}
@Override
public String valueToString(Float value)
{
return value.toString();
}
}