/
ModelBakeEventDebug.java
354 lines (307 loc) · 13.5 KB
/
ModelBakeEventDebug.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package net.minecraftforge.debug;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.IBakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.Vec3;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.client.model.ISmartBlockModel;
import net.minecraftforge.client.model.ISmartItemModel;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.property.ExtendedBlockState;
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;
import net.minecraftforge.common.property.Properties;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import com.google.common.primitives.Ints;
@Mod(modid = ModelBakeEventDebug.MODID, version = ModelBakeEventDebug.VERSION)
public class ModelBakeEventDebug
{
public static final String MODID = "ForgeDebugModelBakeEvent";
public static final String VERSION = "1.0";
public static final int cubeSize = 3;
private static String blockName = MODID.toLowerCase() + ":" + CustomModelBlock.name;
public static final IUnlistedProperty<Integer>[] properties = new IUnlistedProperty[6];
static
{
for(EnumFacing f : EnumFacing.values())
{
properties[f.ordinal()] = Properties.toUnlisted(PropertyInteger.create(f.getName(), 0, (1 << (cubeSize * cubeSize)) - 1));
}
}
@SidedProxy(serverSide = "net.minecraftforge.debug.ModelBakeEventDebug$CommonProxy", clientSide = "net.minecraftforge.debug.ModelBakeEventDebug$ClientProxy")
public static CommonProxy proxy;
@EventHandler
public void init(FMLInitializationEvent event) { proxy.init(event); }
@EventHandler
public void postInit(FMLPostInitializationEvent event) { proxy.postInit(event); }
public static class CommonProxy
{
public void init(FMLInitializationEvent event)
{
GameRegistry.registerBlock(CustomModelBlock.instance, CustomModelBlock.name);
GameRegistry.registerTileEntity(CustomTileEntity.class, MODID.toLowerCase() + ":custom_tile_entity");
}
public void postInit(FMLPostInitializationEvent event) {}
}
public static class ClientProxy extends CommonProxy
{
private static ModelResourceLocation modelLocation = new ModelResourceLocation(blockName, null);
@Override
public void init(FMLInitializationEvent event)
{
super.init(event);
MinecraftForge.EVENT_BUS.register(BakeEventHandler.instance);
}
@Override
public void postInit(FMLPostInitializationEvent event) {
super.postInit(event);
Item item = Item.getItemFromBlock(CustomModelBlock.instance);
RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
if(renderItem != null)
{
renderItem.getItemModelMesher().register(item, new ItemMeshDefinition() {
public ModelResourceLocation getModelLocation(ItemStack stack)
{
return modelLocation;
}
});
}
}
}
public static class BakeEventHandler
{
public static final BakeEventHandler instance = new BakeEventHandler();
private BakeEventHandler() {};
@SubscribeEvent
public void onModelBakeEvent(ModelBakeEvent event)
{
TextureAtlasSprite base = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite("minecraft:blocks/slime");
TextureAtlasSprite overlay = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite("minecraft:blocks/redstone_block");
event.modelRegistry.putObject(ClientProxy.modelLocation, new CustomModel(base, overlay));
}
}
public static class CustomModelBlock extends BlockContainer
{
public static final CustomModelBlock instance = new CustomModelBlock();
public static final String name = "custom_model_block";
private CustomModelBlock()
{
super(Material.iron);
setCreativeTab(CreativeTabs.tabBlock);
setUnlocalizedName(MODID + ":" + name);
}
@Override
public int getRenderType() { return 3; }
@Override
public boolean isOpaqueCube() { return false; }
@Override
public boolean isFullCube() { return false; }
@Override
public boolean isVisuallyOpaque() { return false; }
@Override
public TileEntity createNewTileEntity(World world, int meta)
{
return new CustomTileEntity();
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ)
{
TileEntity te = world.getTileEntity(pos);
if(te instanceof CustomTileEntity)
{
CustomTileEntity cte = (CustomTileEntity) te;
Vec3 vec = revRotate(new Vec3(hitX - .5, hitY - .5, hitZ - .5), side).addVector(.5, .5, .5);
IUnlistedProperty property = properties[side.ordinal()];
Integer value = (Integer)cte.getState().getValue(property);
if(value == null) value = 0;
value ^= (1 << ( cubeSize * ((int)(vec.xCoord * (cubeSize - .0001))) + ((int)(vec.zCoord * (cubeSize - .0001))) ));
cte.setState(cte.getState().withProperty(property, value));
world.markBlockRangeForRenderUpdate(pos, pos);
}
return true;
}
@Override
public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos)
{
TileEntity te = world.getTileEntity(pos);
if(te instanceof CustomTileEntity)
{
CustomTileEntity cte = (CustomTileEntity) te;
return cte.getState();
}
return state;
}
@Override
protected BlockState createBlockState()
{
return new ExtendedBlockState(this, new IProperty[0], properties);
}
}
public static class CustomTileEntity extends TileEntity
{
private IExtendedBlockState state;
public CustomTileEntity() {}
public IExtendedBlockState getState()
{
if(state == null)
{
state = (IExtendedBlockState)getBlockType().getDefaultState();
}
return state;
}
public void setState(IExtendedBlockState state)
{
this.state = state;
}
}
public static class CustomModel implements IBakedModel, ISmartBlockModel, ISmartItemModel
{
private final TextureAtlasSprite base, overlay;
private boolean hasStateSet = false;
private final IExtendedBlockState state;
public CustomModel(TextureAtlasSprite base, TextureAtlasSprite overlay)
{
this(base, overlay, null);
}
public CustomModel(TextureAtlasSprite base, TextureAtlasSprite overlay, IExtendedBlockState state)
{
this.base = base;
this.overlay = overlay;
this.state = state;
}
@Override
public List<BakedQuad> getFaceQuads(EnumFacing side)
{
return Collections.emptyList();
}
private int[] vertexToInts(float x, float y, float z, int color, TextureAtlasSprite texture, float u, float v)
{
return new int[] {
Float.floatToRawIntBits(x),
Float.floatToRawIntBits(y),
Float.floatToRawIntBits(z),
color,
Float.floatToRawIntBits(texture.getInterpolatedU(u)),
Float.floatToRawIntBits(texture.getInterpolatedV(v)),
0
};
}
private BakedQuad createSidedBakedQuad(float x1, float x2, float z1, float z2, float y, TextureAtlasSprite texture, EnumFacing side)
{
Vec3 v1 = rotate(new Vec3(x1 - .5, y - .5, z1 - .5), side).addVector(.5, .5, .5);
Vec3 v2 = rotate(new Vec3(x1 - .5, y - .5, z2 - .5), side).addVector(.5, .5, .5);
Vec3 v3 = rotate(new Vec3(x2 - .5, y - .5, z2 - .5), side).addVector(.5, .5, .5);
Vec3 v4 = rotate(new Vec3(x2 - .5, y - .5, z1 - .5), side).addVector(.5, .5, .5);
return new BakedQuad(Ints.concat(
vertexToInts((float)v1.xCoord, (float)v1.yCoord, (float)v1.zCoord, -1, texture, 0, 0),
vertexToInts((float)v2.xCoord, (float)v2.yCoord, (float)v2.zCoord, -1, texture, 0, 16),
vertexToInts((float)v3.xCoord, (float)v3.yCoord, (float)v3.zCoord, -1, texture, 16, 16),
vertexToInts((float)v4.xCoord, (float)v4.yCoord, (float)v4.zCoord, -1, texture, 16, 0)
), -1, side);
}
@Override
public List<BakedQuad> getGeneralQuads()
{
int len = cubeSize * 5 + 1;
List<BakedQuad> ret = new ArrayList<BakedQuad>();
for(EnumFacing f : EnumFacing.values())
{
ret.add(createSidedBakedQuad(0, 1, 0, 1, 1, base, f));
for(int i = 0; i < cubeSize; i++)
{
for(int j = 0; j < cubeSize; j++)
{
if(state != null)
{
Integer value = (Integer)state.getValue(properties[f.ordinal()]);
if(value != null && (value & (1 << (i * cubeSize + j))) != 0)
{
ret.add(createSidedBakedQuad((float)(1 + i * 5) / len, (float)(5 + i * 5) / len, (float)(1 + j * 5) / len, (float)(5 + j * 5) / len, 1.0001f, overlay, f));
}
}
}
}
}
return ret;
}
@Override
public boolean isGui3d() { return true; }
@Override
public boolean isAmbientOcclusion() { return true; }
@Override
public boolean isBuiltInRenderer() { return false; }
@Override
public TextureAtlasSprite getTexture() { return this.base; }
@Override
public ItemCameraTransforms getItemCameraTransforms()
{
return ItemCameraTransforms.DEFAULT;
}
@Override
public IBakedModel handleBlockState(IBlockState state)
{
return new CustomModel(base, overlay, (IExtendedBlockState)state);
}
@Override
public IBakedModel handleItemState(ItemStack stack)
{
IExtendedBlockState itemState = ((IExtendedBlockState)CustomModelBlock.instance.getDefaultState()).withProperty(properties[1], (1 << (cubeSize * cubeSize)) - 1);
return new CustomModel(base, overlay, itemState);
}
}
private static Vec3 rotate(Vec3 vec, EnumFacing side)
{
switch(side)
{
case DOWN: return new Vec3( vec.xCoord, -vec.yCoord, -vec.zCoord);
case UP: return new Vec3( vec.xCoord, vec.yCoord, vec.zCoord);
case NORTH: return new Vec3( vec.xCoord, vec.zCoord, -vec.yCoord);
case SOUTH: return new Vec3( vec.xCoord, -vec.zCoord, vec.yCoord);
case WEST: return new Vec3(-vec.yCoord, vec.xCoord, vec.zCoord);
case EAST: return new Vec3( vec.yCoord, -vec.xCoord, vec.zCoord);
}
return null;
}
private static Vec3 revRotate(Vec3 vec, EnumFacing side)
{
switch(side)
{
case DOWN: return new Vec3( vec.xCoord, -vec.yCoord, -vec.zCoord);
case UP: return new Vec3( vec.xCoord, vec.yCoord, vec.zCoord);
case NORTH: return new Vec3( vec.xCoord, -vec.zCoord, vec.yCoord);
case SOUTH: return new Vec3( vec.xCoord, vec.zCoord, -vec.yCoord);
case WEST: return new Vec3( vec.yCoord, -vec.xCoord, vec.zCoord);
case EAST: return new Vec3(-vec.yCoord, vec.xCoord, vec.zCoord);
}
return null;
}
}