Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added model bake event, ISmartBlock/itemModel, Block.getExtendedState, IExtendedState and IUnlistedProperty #1518

Merged
merged 1 commit into from
Dec 12, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion patches/minecraft/net/minecraft/block/Block.java.patch
Original file line number Diff line number Diff line change
@@ -161,7 +161,7 @@
}

protected ItemStack func_180643_i(IBlockState p_180643_1_)
@@ -971,6 +989,1011 @@
@@ -971,6 +989,1019 @@
return Block.EnumOffsetType.NONE;
}

@@ -1145,6 +1145,14 @@
+ return type != null && type.equals(getHarvestTool(state));
+ }
+
+ /**
+ * Can return IExtendedBlockState
+ */
+ public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos)
+ {
+ return func_176221_a(state, world, pos);
+ }
+
+ // For Internal use only to capture droped items inside getDrops
+ protected static ThreadLocal<Boolean> captureDrops = new ThreadLocal<Boolean>()
+ {
39 changes: 39 additions & 0 deletions patches/minecraft/net/minecraft/block/state/BlockState.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--- ../src-base/minecraft/net/minecraft/block/state/BlockState.java
+++ ../src-work/minecraft/net/minecraft/block/state/BlockState.java
@@ -47,6 +47,16 @@

public BlockState(Block p_i45663_1_, IProperty ... p_i45663_2_)
{
+ this(p_i45663_1_, p_i45663_2_, null);
+ }
+
+ protected StateImplementation createState(Block block, ImmutableMap properties, ImmutableMap unlistedProperties)
+ {
+ return new StateImplementation(block, properties);
+ }
+
+ protected BlockState(Block p_i45663_1_, IProperty[] p_i45663_2_, ImmutableMap unlistedProperties)
+ {
this.field_177627_c = p_i45663_1_;
Arrays.sort(p_i45663_2_, new Comparator()
{
@@ -70,7 +80,7 @@
{
List list = (List)iterator.next();
Map map = MapPopulator.func_179400_b(this.field_177624_d, list);
- BlockState.StateImplementation stateimplementation = new BlockState.StateImplementation(p_i45663_1_, ImmutableMap.copyOf(map), null);
+ BlockState.StateImplementation stateimplementation = createState(p_i45663_1_, ImmutableMap.copyOf(map), unlistedProperties);
linkedhashmap.put(map, stateimplementation);
arraylist.add(stateimplementation);
}
@@ -231,5 +241,10 @@
{
this(p_i45661_1_, p_i45661_2_);
}
+
+ public ImmutableTable<IProperty, Comparable, IBlockState> getPropertyValueTable()
+ {
+ return field_177238_c;
+ }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--- ../src-base/minecraft/net/minecraft/client/renderer/BlockRendererDispatcher.java
+++ ../src-work/minecraft/net/minecraft/client/renderer/BlockRendererDispatcher.java
@@ -129,6 +129,12 @@
ibakedmodel = ((WeightedBakedModel)ibakedmodel).func_177564_a(MathHelper.func_180186_a(p_175022_3_));
}

+ if(ibakedmodel instanceof net.minecraftforge.client.model.ISmartBlockModel)
+ {
+ IBlockState extendedState = block.getExtendedState(p_175022_1_, p_175022_2_, p_175022_3_);
+ ibakedmodel = ((net.minecraftforge.client.model.ISmartBlockModel)ibakedmodel).handleBlockState(extendedState);
+ }
+
return ibakedmodel;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- ../src-base/minecraft/net/minecraft/client/renderer/ItemModelMesher.java
+++ ../src-work/minecraft/net/minecraft/client/renderer/ItemModelMesher.java
@@ -52,6 +52,11 @@
}
}

+ if(ibakedmodel instanceof net.minecraftforge.client.model.ISmartItemModel)
+ {
+ ibakedmodel = ((net.minecraftforge.client.model.ISmartItemModel)ibakedmodel).handleItemState(p_178089_1_);
+ }
+
if (ibakedmodel == null)
{
ibakedmodel = this.field_178090_d.func_174951_a();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- ../src-base/minecraft/net/minecraft/client/resources/model/ModelManager.java
+++ ../src-work/minecraft/net/minecraft/client/resources/model/ModelManager.java
@@ -28,6 +28,7 @@
ModelBakery modelbakery = new ModelBakery(p_110549_1_, this.field_174956_b, this.field_174957_c);
this.field_174958_a = modelbakery.func_177570_a();
this.field_174955_d = (IBakedModel)this.field_174958_a.func_82594_a(ModelBakery.field_177604_a);
+ net.minecraftforge.client.ForgeHooksClient.onModelBake(this, this.field_174958_a, modelbakery);
this.field_174957_c.func_178124_c();
}

29 changes: 10 additions & 19 deletions src/main/java/net/minecraftforge/client/ForgeHooksClient.java
Original file line number Diff line number Diff line change
@@ -2,41 +2,31 @@

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.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.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 static void onPostRenderWorld(WorldRenderer worldRenderer, int pass)
}
}
*/

public static void onModelBake(ModelManager modelManager, IRegistry modelRegistry, ModelBakery modelBakery)
{
MinecraftForge.EVENT_BUS.post(new ModelBakeEvent(modelManager, modelRegistry, modelBakery));
}
}
24 changes: 24 additions & 0 deletions src/main/java/net/minecraftforge/client/event/ModelBakeEvent.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Loading