mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-16 18:45:09 +00:00
Merge pull request #1518 from RainWarrior/model-event
Added model bake event, ISmartBlock/itemModel, Block.getExtendedState, IExtendedState and IUnlistedProperty
This commit is contained in:
commit
ca2d49b4cd
18 files changed
+720
-22
No files matched your search
@@ -2,41 +2,31 @@ package net.minecraftforge.client;
|
||||
|
||||
import static net.minecraftforge.common.ForgeVersion.Status.BETA;
|
||||
import static net.minecraftforge.common.ForgeVersion.Status.BETA_OUTDATED;
|
||||
import java.util.Random;
|
||||
import javax.imageio.ImageIO;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockBed;
|
||||
import net.minecraft.block.BlockLiquid;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.audio.ISound;
|
||||
import net.minecraft.client.audio.SoundEventAccessorComposite;
|
||||
import net.minecraft.client.audio.SoundManager;
|
||||
import net.minecraft.client.entity.EntityPlayerSP;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.gui.GuiMainMenu;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
import net.minecraft.client.model.ModelBase;
|
||||
import net.minecraft.client.model.ModelBiped;
|
||||
import net.minecraft.client.renderer.EntityRenderer;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.renderer.RenderGlobal;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.client.renderer.texture.TextureManager;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.client.resources.model.ModelBakery;
|
||||
import net.minecraft.client.resources.model.ModelManager;
|
||||
import net.minecraft.client.settings.GameSettings;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.BlockPos;
|
||||
import net.minecraft.util.ChatComponentTranslation;
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.MathHelper;
|
||||
import net.minecraft.util.IRegistry;
|
||||
import net.minecraft.util.MovingObjectPosition;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
@@ -45,6 +35,7 @@ import net.minecraftforge.client.event.DrawBlockHighlightEvent;
|
||||
import net.minecraftforge.client.event.EntityViewRenderEvent;
|
||||
import net.minecraftforge.client.event.FOVUpdateEvent;
|
||||
import net.minecraftforge.client.event.GuiScreenEvent;
|
||||
import net.minecraftforge.client.event.ModelBakeEvent;
|
||||
import net.minecraftforge.client.event.MouseEvent;
|
||||
import net.minecraftforge.client.event.RenderHandEvent;
|
||||
import net.minecraftforge.client.event.RenderWorldLastEvent;
|
||||
@@ -54,14 +45,9 @@ import net.minecraftforge.common.ForgeModContainer;
|
||||
import net.minecraftforge.common.ForgeVersion;
|
||||
import net.minecraftforge.common.ForgeVersion.Status;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
import net.minecraftforge.fml.client.FMLClientHandler;
|
||||
import net.minecraftforge.fml.client.registry.RenderingRegistry;
|
||||
import net.minecraftforge.fml.common.FMLLog;
|
||||
import org.lwjgl.LWJGLException;
|
||||
import org.lwjgl.opengl.Display;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.PixelFormat;
|
||||
//import static net.minecraftforge.client.IItemRenderer.ItemRenderType.*;
|
||||
//import static net.minecraftforge.client.IItemRenderer.ItemRendererHelper.*;
|
||||
|
||||
@@ -470,4 +456,9 @@ public class ForgeHooksClient
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public static void onModelBake(ModelManager modelManager, IRegistry modelRegistry, ModelBakery modelBakery)
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.post(new ModelBakeEvent(modelManager, modelRegistry, modelBakery));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package net.minecraftforge.client.event;
|
||||
|
||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
import net.minecraft.client.resources.model.ModelBakery;
|
||||
import net.minecraft.client.resources.model.ModelManager;
|
||||
import net.minecraft.util.IRegistry;
|
||||
|
||||
/**
|
||||
* Fired when the ModelManager is notified of the resource manager reloading.
|
||||
* Called after model registry is setup, but before it's passed to BlockModelShapes.
|
||||
*/
|
||||
public class ModelBakeEvent extends Event
|
||||
{
|
||||
public final ModelManager modelManager;
|
||||
public final IRegistry modelRegistry;
|
||||
public final ModelBakery modelBakery;
|
||||
|
||||
public ModelBakeEvent(ModelManager modelManager, IRegistry modelRegistry, ModelBakery modelBakery)
|
||||
{
|
||||
this.modelManager = modelManager;
|
||||
this.modelRegistry = modelRegistry;
|
||||
this.modelBakery = modelBakery;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package net.minecraftforge.client.model;
|
||||
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.client.resources.model.IBakedModel;
|
||||
|
||||
public interface ISmartBlockModel extends IBakedModel
|
||||
{
|
||||
IBakedModel handleBlockState(IBlockState state);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package net.minecraftforge.client.model;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.client.resources.model.IBakedModel;
|
||||
|
||||
public interface ISmartItemModel extends IBakedModel
|
||||
{
|
||||
IBakedModel handleItemState(ItemStack stack);
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package net.minecraftforge.common.property;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.properties.IProperty;
|
||||
import net.minecraft.block.state.BlockState;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.ImmutableTable;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
public class ExtendedBlockState extends BlockState
|
||||
{
|
||||
private final ImmutableSet<IUnlistedProperty<?>> unlistedProperties;
|
||||
|
||||
public ExtendedBlockState(Block blockIn, IProperty[] properties, IUnlistedProperty<?>[] unlistedProperties)
|
||||
{
|
||||
super(blockIn, properties, buildUnlistedMap(unlistedProperties));
|
||||
ImmutableSet.Builder<IUnlistedProperty<?>> builder = ImmutableSet.<IUnlistedProperty<?>>builder();
|
||||
for(IUnlistedProperty<?> property : unlistedProperties)
|
||||
{
|
||||
builder.add(property);
|
||||
}
|
||||
this.unlistedProperties = builder.build();
|
||||
}
|
||||
|
||||
private static ImmutableMap<IUnlistedProperty<?>, Optional<?>> buildUnlistedMap(IUnlistedProperty<?>[] unlistedProperties)
|
||||
{
|
||||
ImmutableMap.Builder<IUnlistedProperty<?>, Optional<?>> builder = ImmutableMap.<IUnlistedProperty<?>, Optional<?>>builder();
|
||||
for(IUnlistedProperty<?> p : unlistedProperties)
|
||||
{
|
||||
builder.put(p, Optional.absent());
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StateImplementation createState(Block block, ImmutableMap properties, ImmutableMap unlistedProperties)
|
||||
{
|
||||
return new ExtendedStateImplementation(block, properties, unlistedProperties, null);
|
||||
}
|
||||
|
||||
protected static class ExtendedStateImplementation extends StateImplementation implements IExtendedBlockState
|
||||
{
|
||||
private final ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties;
|
||||
private Map<Map<IProperty, Comparable>, IBlockState> normalMap;
|
||||
|
||||
protected ExtendedStateImplementation(Block block, ImmutableMap properties, ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties, ImmutableTable<IProperty, Comparable, IBlockState> table)
|
||||
{
|
||||
super(block, properties);
|
||||
this.unlistedProperties = unlistedProperties;
|
||||
this.propertyValueTable = table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBlockState withProperty(IProperty property, Comparable value)
|
||||
{
|
||||
if (!this.getProperties().containsKey(property))
|
||||
{
|
||||
throw new IllegalArgumentException("Cannot set property " + property + " as it does not exist in " + getBlock().getBlockState());
|
||||
}
|
||||
else if (!property.getAllowedValues().contains(value))
|
||||
{
|
||||
throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on block " + Block.blockRegistry.getNameForObject(getBlock()) + ", it is not an allowed value");
|
||||
}
|
||||
else
|
||||
{
|
||||
if(this.getProperties().get(property) == value)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
if(Iterables.all(unlistedProperties.values(), Predicates.<Optional<?>>equalTo(Optional.absent())))
|
||||
{ // no dynamic properties present, looking up in the normal table
|
||||
return super.withProperty(property, value);
|
||||
}
|
||||
Map<IProperty, Comparable> map = new HashMap<IProperty, Comparable>(getProperties());
|
||||
map.put(property, value);
|
||||
ImmutableTable<IProperty, Comparable, IBlockState> table = propertyValueTable;
|
||||
table = ((StateImplementation)table.get(property, value)).getPropertyValueTable();
|
||||
return new ExtendedStateImplementation(getBlock(), ImmutableMap.copyOf(map), unlistedProperties, table);
|
||||
}
|
||||
}
|
||||
|
||||
public <V> IExtendedBlockState withProperty(IUnlistedProperty<V> property, V value)
|
||||
{
|
||||
if(!this.unlistedProperties.containsKey(property))
|
||||
{
|
||||
throw new IllegalArgumentException("Cannot set unlisted property " + property + " as it does not exist in " + getBlock().getBlockState());
|
||||
}
|
||||
if(!property.isValid(value))
|
||||
{
|
||||
throw new IllegalArgumentException("Cannot set unlisted property " + property + " to " + value + " on block " + Block.blockRegistry.getNameForObject(getBlock()) + ", it is not an allowed value");
|
||||
}
|
||||
Map<IUnlistedProperty<?>, Optional<?>> newMap = new HashMap<IUnlistedProperty<?>, Optional<?>>(unlistedProperties);
|
||||
newMap.put(property, Optional.fromNullable(value));
|
||||
if(Iterables.all(newMap.values(), Predicates.<Optional<?>>equalTo(Optional.absent())))
|
||||
{ // no dynamic properties, lookup normal state
|
||||
return (IExtendedBlockState) normalMap.get(getProperties());
|
||||
}
|
||||
return new ExtendedStateImplementation(getBlock(), getProperties(), ImmutableMap.copyOf(newMap), propertyValueTable);
|
||||
}
|
||||
|
||||
public Collection<IUnlistedProperty<?>> getUnlistedNames()
|
||||
{
|
||||
return Collections.unmodifiableCollection(unlistedProperties.keySet());
|
||||
}
|
||||
|
||||
public <V>V getValue(IUnlistedProperty<V> property)
|
||||
{
|
||||
if(!this.unlistedProperties.containsKey(property))
|
||||
{
|
||||
throw new IllegalArgumentException("Cannot get unlisted property " + property + " as it does not exist in " + getBlock().getBlockState());
|
||||
}
|
||||
return property.getType().cast(this.unlistedProperties.get(property).orNull());
|
||||
}
|
||||
|
||||
public ImmutableMap<IUnlistedProperty<?>, Optional<?>> getUnlistedProperties()
|
||||
{
|
||||
return unlistedProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildPropertyValueTable(Map map)
|
||||
{
|
||||
this.normalMap = map;
|
||||
super.buildPropertyValueTable(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package net.minecraftforge.common.property;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
public interface IExtendedBlockState extends IBlockState
|
||||
{
|
||||
Collection<IUnlistedProperty<?>> getUnlistedNames();
|
||||
|
||||
<V>V getValue(IUnlistedProperty<V> property);
|
||||
|
||||
<V>IExtendedBlockState withProperty(IUnlistedProperty<V> property, V value);
|
||||
|
||||
ImmutableMap<IUnlistedProperty<?>, Optional<?>> getUnlistedProperties();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package net.minecraftforge.common.property;
|
||||
|
||||
public interface IUnlistedProperty<V>
|
||||
{
|
||||
String getName();
|
||||
|
||||
boolean isValid(V value);
|
||||
|
||||
Class<V> getType();
|
||||
|
||||
String valueToString(V value);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package net.minecraftforge.common.property;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import net.minecraft.block.properties.IProperty;
|
||||
|
||||
import org.apache.commons.lang3.reflect.ConstructorUtils;
|
||||
|
||||
public class Properties
|
||||
{
|
||||
public static <P extends IProperty, V>IUnlistedProperty<V> toUnlisted(P property)
|
||||
{
|
||||
return new PropertyAdapter(property);
|
||||
}
|
||||
|
||||
public static class PropertyAdapter<V extends Comparable> implements IUnlistedProperty<V>
|
||||
{
|
||||
private final IProperty parent;
|
||||
|
||||
public PropertyAdapter(IProperty parent)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return parent.getName();
|
||||
}
|
||||
|
||||
public boolean isValid(V value)
|
||||
{
|
||||
return parent.getAllowedValues().contains(value);
|
||||
}
|
||||
|
||||
public Class<V> getType()
|
||||
{
|
||||
return parent.getValueClass();
|
||||
}
|
||||
|
||||
public String valueToString(V value)
|
||||
{
|
||||
return parent.getName(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user