-
Notifications
You must be signed in to change notification settings - Fork 79
/
JsonModelExport.java
160 lines (135 loc) · 4.55 KB
/
JsonModelExport.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
package mod.chiselsandbits.commands;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import mod.chiselsandbits.chiseledblock.ItemBlockChiseled;
import mod.chiselsandbits.chiseledblock.data.VoxelBlob;
import mod.chiselsandbits.core.ClientSide;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.render.helpers.ModelQuadReader;
import mod.chiselsandbits.render.helpers.ModelUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.text.TextComponentString;
public class JsonModelExport extends CommandBase
{
@Override
public int getRequiredPermissionLevel()
{
return 0;
}
@Override
public String getCommandName()
{
return "c&b.exportmodel";
}
@Override
public String getCommandUsage(
final ICommandSender sender )
{
return "chiselsandbits.commands.exportjsonmodel.usage";
}
@Override
public void execute(
final MinecraftServer server,
final ICommandSender sender,
final String[] args ) throws CommandException
{
final EntityPlayer player = ClientSide.instance.getPlayer();
final ItemStack is = player.getHeldItemMainhand();
if ( is != null && is.getItem() != null )
{
IBakedModel model = Minecraft.getMinecraft().getRenderItem().getItemModelMesher().getItemModel( is );
// handle overrides.
model = model.getOverrides().handleItemState( model, is, player.getEntityWorld(), player );
final Map<TextureAtlasSprite, String> textures = new HashMap<TextureAtlasSprite, String>();
for ( final EnumFacing face : EnumFacing.VALUES )
{
outputFaces( model.getQuads( null, face, 0 ), face, textures );
}
outputFaces( model.getQuads( null, null, 0 ), null, textures );
String data = "N/A";
if ( is.getItem() instanceof ItemBlockChiseled && is.hasTagCompound() )
{
final VoxelBlob blob = ModUtil.getBlobFromStack( is, null );
final byte[] bd = blob.blobToBytes( VoxelBlob.VERSION_CROSSWORLD );
data = Arrays.toString( bd );
}
final StringBuilder output = new StringBuilder( "{ \"source\": \"" ).append( data ).append( "\",\n\"textures\": {" );
for ( final TextureAtlasSprite s : textures.keySet() )
{
output.append( "\"" ).append( System.identityHashCode( s ) ).append( "\": \"" ).append( s.getIconName() ).append( "\",\n" );
}
if ( !textures.values().isEmpty() )
{
// delete line ending + comma.
output.deleteCharAt( output.length() - 1 );
output.deleteCharAt( output.length() - 1 );
}
output.append( "},\n\"elements\": [\n" );
for ( final String json : textures.values() )
{
output.append( json );
}
if ( !textures.values().isEmpty() )
{
// delete line ending + comma.
output.deleteCharAt( output.length() - 1 );
output.deleteCharAt( output.length() - 1 );
}
output.append( "\n],\n\"display\": { \"thirdperson\": { \"rotation\": [ 10, -45, 170 ], \"translation\": [ 0, 1.5, -2.75 ], \"scale\": [ 0.375, 0.375, 0.375 ] } } }" );
final String modelJSON = output.toString();
GuiScreen.setClipboardString( modelJSON );
sender.addChatMessage( new TextComponentString( "Json Posted to Clipboard" ) );
}
else
{
sender.addChatMessage( new TextComponentString( "No Item in Hand." ) );
}
}
private void outputFaces(
final List<BakedQuad> faceQuads,
final EnumFacing cullFace,
final Map<TextureAtlasSprite, String> textures )
{
for ( final BakedQuad quad : faceQuads )
{
try
{
final TextureAtlasSprite sprite = ModelUtil.findQuadTexture( quad, null );
final ModelQuadReader mqr = new ModelQuadReader( "#" + System.identityHashCode( sprite ), sprite, quad.getFace(), cullFace );
quad.pipe( mqr );
final String newJSON = mqr.toString();
String old = textures.get( sprite );
if ( old == null )
{
old = "";
}
textures.put( sprite, old + newJSON );
}
catch ( final IllegalArgumentException e )
{
e.printStackTrace();
}
catch ( final IllegalAccessException e )
{
e.printStackTrace();
}
catch ( final NullPointerException e )
{
e.printStackTrace();
}
}
}
}