/
ItemNegativePrint.java
341 lines (288 loc) · 9.32 KB
/
ItemNegativePrint.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
package mod.chiselsandbits.items;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import mod.chiselsandbits.api.VoxelStats;
import mod.chiselsandbits.chiseledblock.BlockChiseled;
import mod.chiselsandbits.chiseledblock.NBTBlobConverter;
import mod.chiselsandbits.chiseledblock.TileEntityBlockChiseled;
import mod.chiselsandbits.chiseledblock.data.VoxelBlob;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.core.ClientSide;
import mod.chiselsandbits.helpers.ActingPlayer;
import mod.chiselsandbits.helpers.BitInventoryFeeder;
import mod.chiselsandbits.helpers.ContinousChisels;
import mod.chiselsandbits.helpers.IContinuousInventory;
import mod.chiselsandbits.helpers.LocalStrings;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.integration.mcmultipart.MCMultipartProxy;
import mod.chiselsandbits.interfaces.IItemScrollWheel;
import mod.chiselsandbits.interfaces.IPatternItem;
import mod.chiselsandbits.interfaces.IVoxelBlobItem;
import mod.chiselsandbits.network.NetworkRouter;
import mod.chiselsandbits.network.packets.PacketRotateVoxelBlob;
import mod.chiselsandbits.render.helpers.SimpleInstanceCache;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumFacing.Axis;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class ItemNegativePrint extends Item implements IVoxelBlobItem, IItemScrollWheel, IPatternItem
{
public ItemNegativePrint()
{
}
@SideOnly( Side.CLIENT )
protected void defaultAddInfo(
final ItemStack stack,
final World worldIn,
final List<String> tooltip,
final ITooltipFlag advanced )
{
super.addInformation( stack, worldIn, tooltip, advanced );
}
// add info cached info
SimpleInstanceCache<ItemStack, List<String>> toolTipCache = new SimpleInstanceCache<ItemStack, List<String>>( null, new ArrayList<String>() );
@Override
@SideOnly( Side.CLIENT )
public void addInformation(
final ItemStack stack,
final World worldIn,
final List<String> tooltip,
final ITooltipFlag advanced )
{
defaultAddInfo( stack, worldIn, tooltip, advanced );
ChiselsAndBits.getConfig().helpText( LocalStrings.HelpNegativePrint, tooltip,
ClientSide.instance.getKeyName( Minecraft.getMinecraft().gameSettings.keyBindUseItem ),
ClientSide.instance.getKeyName( Minecraft.getMinecraft().gameSettings.keyBindUseItem ) );
if ( isWritten( stack ) )
{
if ( ClientSide.instance.holdingShift() )
{
final List<String> details = toolTipCache.getCached();
if ( toolTipCache.needsUpdate( stack ) )
{
details.clear();
final VoxelBlob blob = ModUtil.getBlobFromStack( stack, null );
final int solid = blob.filled();
final int air = blob.air();
if ( solid > 0 )
{
details.add( solid + " " + LocalStrings.Filled.getLocal() );
}
if ( air > 0 )
{
details.add( air + " " + LocalStrings.Empty.getLocal() );
}
}
tooltip.addAll( details );
}
else
{
tooltip.add( LocalStrings.ShiftDetails.getLocal() );
}
}
}
@Override
public String getUnlocalizedName(
final ItemStack stack )
{
if ( isWritten( stack ) )
{
return super.getUnlocalizedName( stack ) + "_written";
}
return super.getUnlocalizedName( stack );
}
@Override
public boolean isWritten(
final ItemStack stack )
{
if ( stack != null && stack.hasTagCompound() )
{
final boolean a = ModUtil.getSubCompound( stack, ModUtil.NBT_BLOCKENTITYTAG, false ) != null;
final boolean b = ModUtil.getTagCompound( stack ).hasKey( NBTBlobConverter.NBT_LEGACY_VOXEL );
final boolean c = ModUtil.getTagCompound( stack ).hasKey( NBTBlobConverter.NBT_VERSIONED_VOXEL );
return a || b || c;
}
return false;
}
@Override
public EnumActionResult onItemUse(
final EntityPlayer player,
final World world,
final BlockPos pos,
final EnumHand hand,
final EnumFacing side,
final float hitX,
final float hitY,
final float hitZ )
{
final ItemStack stack = player.getHeldItem( hand );
final IBlockState blkstate = world.getBlockState( pos );
if ( ItemChiseledBit.checkRequiredSpace( player, blkstate ) ) {
return EnumActionResult.FAIL;
}
if ( !player.canPlayerEdit( pos, side, stack ) || !world.isBlockModifiable( player, pos ) )
{
return EnumActionResult.FAIL;
}
if ( !isWritten( stack ) )
{
final NBTTagCompound comp = getCompoundFromBlock( world, pos, player );
if ( comp != null )
{
stack.setTagCompound( comp );
return EnumActionResult.SUCCESS;
}
return EnumActionResult.FAIL;
}
final TileEntityBlockChiseled te = ModUtil.getChiseledTileEntity( world, pos, false );
if ( te != null )
{
// we can do this!
}
else if ( !BlockChiseled.replaceWithChisled( world, pos, blkstate, true ) && !MCMultipartProxy.proxyMCMultiPart.isMultiPartTileEntity( world, pos ) )
{
return EnumActionResult.FAIL;
}
final TileEntityBlockChiseled tec = ModUtil.getChiseledTileEntity( world, pos, true );
if ( tec != null )
{
final VoxelBlob vb = tec.getBlob();
final VoxelBlob pattern = ModUtil.getBlobFromStack( stack, player );
applyPrint( stack, world, pos, side, vb, pattern, player, hand );
tec.completeEditOperation( vb );
return EnumActionResult.SUCCESS;
}
return EnumActionResult.FAIL;
}
protected boolean convertToStone()
{
return true;
}
protected NBTTagCompound getCompoundFromBlock(
final World world,
final BlockPos pos,
final EntityPlayer player )
{
final TileEntityBlockChiseled te = ModUtil.getChiseledTileEntity( world, pos, false );
if ( te != null )
{
final NBTTagCompound comp = new NBTTagCompound();
te.writeChisleData( comp );
if ( convertToStone() )
{
final TileEntityBlockChiseled tmp = new TileEntityBlockChiseled();
tmp.readChisleData( comp );
final VoxelBlob bestBlob = tmp.getBlob();
bestBlob.binaryReplacement( 0, ModUtil.getStateId( Blocks.STONE.getDefaultState() ) );
tmp.setBlob( bestBlob );
tmp.writeChisleData( comp );
}
comp.setByte( ModUtil.NBT_SIDE, (byte) ModUtil.getPlaceFace( player ).ordinal() );
return comp;
}
return null;
}
@Override
public ItemStack getPatternedItem(
final ItemStack stack,
final boolean craftingBlocks )
{
if ( !isWritten( stack ) )
{
return null;
}
final NBTTagCompound tag = ModUtil.getTagCompound( stack );
// Detect and provide full blocks if pattern solid full and solid.
final NBTBlobConverter conv = new NBTBlobConverter();
conv.readChisleData( tag, VoxelBlob.VERSION_ANY );
if ( craftingBlocks && ChiselsAndBits.getConfig().fullBlockCrafting )
{
final VoxelStats stats = conv.getBlob().getVoxelStats();
if ( stats.isFullBlock )
{
final IBlockState state = ModUtil.getStateById( stats.mostCommonState );
final ItemStack is = ModUtil.getItemFromBlock( state );
if ( !ModUtil.isEmpty( is ) )
{
return is;
}
}
}
final IBlockState state = conv.getPrimaryBlockState();
final ItemStack itemstack = new ItemStack( ChiselsAndBits.getBlocks().getConversionWithDefault( state ), 1 );
itemstack.setTagInfo( ModUtil.NBT_BLOCKENTITYTAG, tag );
return itemstack;
}
protected void applyPrint(
@Nonnull final ItemStack stack,
@Nonnull final World world,
@Nonnull final BlockPos pos,
@Nonnull final EnumFacing side,
@Nonnull final VoxelBlob vb,
@Nonnull final VoxelBlob pattern,
@Nonnull final EntityPlayer who,
@Nonnull final EnumHand hand )
{
// snag a tool...
final ActingPlayer player = ActingPlayer.actingAs( who, hand );
final IContinuousInventory selected = new ContinousChisels( player, pos, side );
ItemStack spawnedItem = null;
final List<EntityItem> spawnlist = new ArrayList<EntityItem>();
for ( int z = 0; z < vb.detail && selected.isValid(); z++ )
{
for ( int y = 0; y < vb.detail && selected.isValid(); y++ )
{
for ( int x = 0; x < vb.detail && selected.isValid(); x++ )
{
final int blkID = vb.get( x, y, z );
if ( blkID != 0 && pattern.get( x, y, z ) == 0 )
{
spawnedItem = ItemChisel.chiselBlock( selected, player, vb, world, pos, side, x, y, z, spawnedItem, spawnlist );
}
}
}
}
BitInventoryFeeder feeder = new BitInventoryFeeder( who, world );
for ( final EntityItem ei : spawnlist )
{
feeder.addItem(ei);
}
}
@Override
public void scroll(
final EntityPlayer player,
final ItemStack stack,
final int dwheel )
{
final PacketRotateVoxelBlob p = new PacketRotateVoxelBlob();
p.rotationDirection = dwheel;
NetworkRouter.instance.sendToServer( p );
}
@Override
public void rotate(
final ItemStack stack,
final int rotationDirection )
{
EnumFacing side = ModUtil.getSide( stack );
if ( side.getAxis() == Axis.Y )
{
side = EnumFacing.NORTH;
}
side = rotationDirection > 0 ? side.rotateY() : side.rotateYCCW();
ModUtil.setSide( stack, side );
}
}