-
Notifications
You must be signed in to change notification settings - Fork 79
/
ChiseledBlockPart.java
366 lines (314 loc) · 7.86 KB
/
ChiseledBlockPart.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
355
356
357
358
359
360
361
362
363
364
365
366
package mod.chiselsandbits.integration.mcmultipart;
import java.io.IOException;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import mcmultipart.microblock.IMicroMaterial;
import mcmultipart.microblock.IMicroblock;
import mcmultipart.microblock.MicroblockClass;
import mcmultipart.multipart.IMultipart;
import mcmultipart.multipart.INormallyOccludingPart;
import mcmultipart.multipart.ISolidPart;
import mcmultipart.multipart.Multipart;
import mcmultipart.multipart.OcclusionHelper;
import mcmultipart.multipart.PartSlot;
import mcmultipart.raytrace.PartMOP;
import mcmultipart.raytrace.RayTraceUtils.AdvancedRayTraceResultPart;
import mod.chiselsandbits.chiseledblock.BlockChiseled;
import mod.chiselsandbits.chiseledblock.BoxType;
import mod.chiselsandbits.chiseledblock.TileEntityBlockChiseled;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.core.Log;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
public class ChiseledBlockPart extends Multipart implements INormallyOccludingPart, ISolidPart, IMicroblock
{
protected TileEntityBlockChiseled inner; // never use directly..
protected BlockChiseled bc; // never use directly..
@Override
public void onAdded()
{
if ( inner != null )
{
inner.validate();
}
}
@Override
public void onRemoved()
{
if ( inner != null )
{
inner.invalidate();
}
}
public void saveChanges()
{
super.markDirty();
}
public void swapTile(
final TileEntityBlockChiseled newTileEntity )
{
newTileEntity.copyFrom( getTile() );
inner.invalidate();
inner = newTileEntity;
markRenderUpdate();
}
@Override
public boolean occlusionTest(
final IMultipart part )
{
return OcclusionHelper.defaultOcclusionTest( this, part ) && !( part instanceof ChiseledBlockPart );
}
@Override
public float getHardness(
final PartMOP hit )
{
return getTile().getBlockInfo( getBlock() ).hardness;
}
@Override
public void addCollisionBoxes(
final AxisAlignedBB mask,
final List<AxisAlignedBB> list,
final Entity collidingEntity )
{
getBlock().addCollisionBoxesToList( getTile(), BlockPos.ORIGIN, mask, list, collidingEntity );
}
@Override
public AdvancedRayTraceResultPart collisionRayTrace(
final Vec3d start,
final Vec3d end )
{
final RayTraceResult mop = getBlock().collisionRayTrace( getTile(), getPos(), start, end, true );
if ( mop == null )
{
return null;
}
final BlockPos myPos = getPos() == null ? BlockPos.ORIGIN : getPos();
final AxisAlignedBB bb = getBlock().getSelectedBoundingBox( getTile(), myPos );
return new AdvancedRayTraceResultPart( new PartMOP( mop, this ), bb == null ? null : bb.offset( -myPos.getX(), -myPos.getY(), -myPos.getZ() ) );
}
@Override
public void addOcclusionBoxes(
final List<AxisAlignedBB> list )
{
list.addAll( getTile().getBoxes( BoxType.OCCLUSION ) );
}
public TileEntityBlockChiseled getTile()
{
if ( inner == null )
{
inner = new TileEntityBlockChiseled();
}
// update tile stats..
inner.setWorldObj( getWorld() );
inner.setPos( getPos() );
if ( !( inner.occlusionState instanceof MultipartContainerWrapper ) )
{
inner.occlusionState = new MultipartContainerWrapper( this );
}
return inner;
}
public BlockChiseled getBlock()
{
if ( bc == null )
{
bc = (BlockChiseled) ChiselsAndBits.getBlocks().getChiseledDefaultState().getBlock();
}
return bc;
}
@Override
public ResourceLocation getModelPath()
{
return getBlock().getModel();
}
@Override
public int getLightValue()
{
final int lv = getTile().getLightValue();
return lv;
}
@Override
public ItemStack getPickBlock(
final EntityPlayer player,
final PartMOP hit )
{
return getBlock().getPickBlock( hit, hit.getBlockPos(), getTile() );
}
@Override
public boolean canRenderInLayer(
final BlockRenderLayer layer )
{
return true;
}
@Override
public IBlockState getExtendedState(
final IBlockState state,
final IBlockAccess world,
final BlockPos pos )
{
return getTile().getRenderState( world );
}
@Override
public List<ItemStack> getDrops()
{
return Collections.singletonList( getTile().getItemStack( null ) );
}
@Override
public void harvest(
final EntityPlayer player,
final PartMOP hit )
{
final World world = getWorld();
final BlockPos pos = getPos();
final double x = pos.getX() + 0.5, y = pos.getY() + 0.5, z = pos.getZ() + 0.5;
if ( ( player == null || !player.capabilities.isCreativeMode ) && !world.isRemote && world.getGameRules().getBoolean( "doTileDrops" )
&& !world.restoringBlockSnapshots )
{
final ItemStack stack = getTile().getItemStack( player );
final EntityItem item = new EntityItem( world, x, y, z, stack );
item.setDefaultPickupDelay();
world.spawnEntityInWorld( item );
}
getContainer().removePart( this );
}
@Override
public BlockStateContainer createBlockState()
{
return getBlock().getBlockState();
}
@Override
public boolean rotatePart(
final EnumFacing axis )
{
getTile().rotateBlock( axis );
return true;
}
@Override
public EnumFacing[] getValidRotations()
{
return EnumFacing.VALUES;
}
@Override
public NBTTagCompound writeToNBT(
final NBTTagCompound tag )
{
getTile().writeChisleData( tag );
return tag;
}
@Override
public void readFromNBT(
final NBTTagCompound tag )
{
if ( tag.hasNoTags() )
{
return;
}
getTile().readChisleData( tag );
}
@Override
public void writeUpdatePacket(
final PacketBuffer buf )
{
final NBTTagCompound tag = new NBTTagCompound();
getTile().writeChisleData( tag );
buf.writeNBTTagCompoundToBuffer( tag );
}
@Override
public void readUpdatePacket(
final PacketBuffer buf )
{
try
{
final NBTTagCompound tag = buf.readNBTTagCompoundFromBuffer();
getTile().readChisleData( tag );
}
catch ( final IOException e )
{
Log.logError( "Invalid Chisled Block Packet.", e );
}
}
@Override
public void notifyPartUpdate()
{
super.notifyPartUpdate();
}
@Override
public boolean isSideSolid(
final EnumFacing side )
{
return getTile().isSideSolid( side );
}
// IF ANY ONE ASKS, Amadornes told me too... :D
@Override
public EnumSet<PartSlot> getSlotMask()
{
return EnumSet.noneOf( PartSlot.class );
}
@Override
public AxisAlignedBB getBounds()
{
return getBlock().getSelectedBoundingBox( getTile(), getPos() );
}
@Override
public MicroblockClass getMicroClass()
{
return ChiseledMicroblock.instance;
}
@Override
public IMicroMaterial getMicroMaterial()
{
return (IMicroMaterial) this;
}
@Override
public int getSize()
{
return 0;
}
@Override
public PartSlot getSlot()
{
return null;
}
@Override
public void setSize(
final int arg0 )
{
}
@Override
public void setSlot(
final PartSlot arg0 )
{
}
@Override
public Boolean isAABBInsideMaterial(
final AxisAlignedBB bx,
final Material materialIn )
{
return BlockChiseled.sharedIsAABBInsideMaterial( getTile(), bx, materialIn );
}
@Override
public Boolean isEntityInsideMaterial(
final Entity entity,
final double yToTest,
final Material materialIn,
final boolean testingHead )
{
return BlockChiseled.sharedIsEntityInsideMaterial( getTile(), getPos(), entity, yToTest, materialIn, testingHead );
}
}