mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-17 02:45:19 +00:00
Add single property data and lazy load the instance in RetexturedTileEntity
This commit is contained in:
1 parent
623a28f673
commit
cb213db95f
3 files changed
+67
-6
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package slimeknights.mantle.client.model.data;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.minecraftforge.client.model.data.IModelData;
|
||||
import net.minecraftforge.client.model.data.ModelProperty;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* IModelData instance that holds a single model data property. Will be more efficient than the map implementation in cases without more properties.
|
||||
* If you need more than one model property, use {@link net.minecraftforge.client.model.data.ModelDataMap} instead.
|
||||
* @param <D> Property type, for data validation
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class SinglePropertyData<D> implements IModelData {
|
||||
private final ModelProperty<D> property;
|
||||
private D data = null;
|
||||
|
||||
/**
|
||||
* Creates an instance with a property and preset data
|
||||
* @param property Property for this instance
|
||||
* @param data Original data
|
||||
*/
|
||||
public SinglePropertyData(ModelProperty<D> property, D data) {
|
||||
Preconditions.checkArgument(property.test(data), "Value is invalid for this property");
|
||||
this.property = property;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasProperty(ModelProperty<?> prop) {
|
||||
return prop == this.property;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T getData(ModelProperty<T> prop) {
|
||||
if (prop == this.property) {
|
||||
return (T) data;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T setData(ModelProperty<T> prop, T data) {
|
||||
Preconditions.checkArgument(prop.test(data), "Value is invalid for this property");
|
||||
if (prop == this.property) {
|
||||
this.data = (D) data;
|
||||
return data;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import net.minecraft.block.Block;
|
||||
import net.minecraft.block.Blocks;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraftforge.client.model.data.IModelData;
|
||||
import net.minecraftforge.client.model.data.ModelDataMap;
|
||||
import slimeknights.mantle.client.model.data.SinglePropertyData;
|
||||
import slimeknights.mantle.util.RetexturedHelper;
|
||||
|
||||
/**
|
||||
@@ -45,11 +45,11 @@ public interface IRetexturedTileEntity {
|
||||
*/
|
||||
default IModelData getRetexturedModelData() {
|
||||
// texture not loaded
|
||||
ModelDataMap.Builder data = new ModelDataMap.Builder();
|
||||
Block block = getTexture();
|
||||
if (block != Blocks.AIR) {
|
||||
data = data.withInitial(RetexturedHelper.BLOCK_PROPERTY, block);
|
||||
// cannot support air, saves a conditional on usage
|
||||
if (block == Blocks.AIR) {
|
||||
block = null;
|
||||
}
|
||||
return data.build();
|
||||
return new SinglePropertyData<>(RetexturedHelper.BLOCK_PROPERTY, block);
|
||||
}
|
||||
}
|
||||
@@ -3,19 +3,23 @@ package slimeknights.mantle.tileentity;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.tileentity.TileEntityType;
|
||||
import net.minecraft.util.LazyValue;
|
||||
import net.minecraftforge.client.model.data.IModelData;
|
||||
|
||||
/**
|
||||
* Minimal implementation for {@link IRetexturedTileEntity}, use alongside {@link slimeknights.mantle.block.RetexturedBlock} and {@link slimeknights.mantle.item.RetexturedBlockItem}
|
||||
*/
|
||||
public class RetexturedTileEntity extends TileEntity implements IRetexturedTileEntity {
|
||||
|
||||
/** Lazy value of model data as it will not change after first fetch */
|
||||
private final LazyValue<IModelData> data = new LazyValue<>(this::getRetexturedModelData);
|
||||
public RetexturedTileEntity(TileEntityType<?> type) {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IModelData getModelData() {
|
||||
return getRetexturedModelData();
|
||||
return data.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user