12
12
import net .minecraft .world .InteractionResult ;
13
13
import net .minecraft .world .entity .player .Player ;
14
14
import net .minecraft .world .item .context .BlockPlaceContext ;
15
+ import net .minecraft .world .level .BlockGetter ;
15
16
import net .minecraft .world .level .Level ;
16
17
import net .minecraft .world .level .block .Block ;
17
18
import net .minecraft .world .level .block .DispenserBlock ;
24
25
import net .minecraft .world .level .block .state .StateDefinition ;
25
26
import net .minecraft .world .level .block .state .properties .EnumProperty ;
26
27
import net .minecraft .world .phys .BlockHitResult ;
28
+ import net .minecraft .world .phys .Vec3 ;
29
+ import net .minecraft .world .phys .shapes .CollisionContext ;
30
+ import net .minecraft .world .phys .shapes .Shapes ;
31
+ import net .minecraft .world .phys .shapes .VoxelShape ;
27
32
import org .jetbrains .annotations .Nullable ;
28
33
34
+ import java .util .Map ;
35
+
29
36
import static net .minecraft .world .level .block .state .properties .BlockStateProperties .HORIZONTAL_FACING ;
30
37
31
38
public class GargoyleBlock extends HorizontalDirectionalBlock implements EntityBlock {
32
39
33
40
public static final EnumProperty <AttachmentType > ATTACHMENT = EnumProperty .create ("attachment" , AttachmentType .class );
34
41
42
+ public final Map <BlockState , VoxelShape > SHAPES = getShapeForEachState (this ::getShapeFor );
43
+
35
44
public static final DispenseItemBehavior DISPENSE_ITEM_BEHAVIOR = (source , stack ) -> {
36
45
var dispenser = source .getBlockState ();
37
46
var facing = dispenser .getValue (DispenserBlock .FACING );
38
47
var targetPos = source .getPos ().relative (facing );
39
48
var target = source .getLevel ().getBlockEntity (targetPos );
40
49
41
50
if (target instanceof GargoyleBlockEntity gargoyle ) {
42
- gargoyle .interact (source .getLevel (), targetPos , null , stack );
51
+ gargoyle .interact (source .getLevel (), targetPos , null , stack , false );
43
52
}
44
53
45
54
return stack ;
@@ -98,7 +107,7 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
98
107
var blockEntity = level .getBlockEntity (pos );
99
108
if (blockEntity instanceof GargoyleBlockEntity gargoyle ) {
100
109
var stack = player .getItemInHand (hand );
101
- return gargoyle .interact (level , pos , player , stack );
110
+ return gargoyle .interact (level , pos , player , stack , false );
102
111
}
103
112
104
113
return super .use (state , level , pos , player , hand , hit );
@@ -109,16 +118,9 @@ public void animateTick(BlockState state, Level level, BlockPos pos, RandomSourc
109
118
super .animateTick (state , level , pos , random );
110
119
111
120
if (random .nextBoolean () && level .isRainingAt (pos .above ())) {
112
- var facing = state .getValue (FACING ).getOpposite ();
113
- var attachment = state .getValue (ATTACHMENT );
114
121
double spread = random .nextDouble () * 0.1 - 0.05 ;
115
- double offsetX = facing .getAxis () == Direction .Axis .X ? facing .getStepX () * attachment .horizontalOffset : spread ;
116
- double offsetZ = facing .getAxis () == Direction .Axis .Z ? facing .getStepZ () * attachment .horizontalOffset : spread ;
117
-
118
- double x = pos .getX () + 0.5 + offsetX ;
119
- double y = pos .getY () + attachment .verticalOffset ;
120
- double z = pos .getZ () + 0.5 + offsetZ ;
121
- level .addParticle (ParticleTypes .DRIPPING_DRIPSTONE_WATER , x , y , z , 0.0 , 0.0 , 0.0 );
122
+ var offset = AttachmentType .offset (state , pos , spread );
123
+ level .addParticle (ParticleTypes .DRIPPING_DRIPSTONE_WATER , offset .x , offset .y , offset .z , 0.0 , 0.0 , 0.0 );
122
124
}
123
125
}
124
126
@@ -142,10 +144,66 @@ public enum AttachmentType implements StringRepresentable {
142
144
this .verticalOffset = verticalOffset ;
143
145
}
144
146
147
+ public static Vec3 offset (BlockState state , BlockPos pos , double spread ) {
148
+ var facing = state .getValue (FACING ).getOpposite ();
149
+ var attachment = state .getValue (ATTACHMENT );
150
+ double offsetX = facing .getAxis () == Direction .Axis .X ? facing .getStepX () * attachment .horizontalOffset : spread ;
151
+ double offsetZ = facing .getAxis () == Direction .Axis .Z ? facing .getStepZ () * attachment .horizontalOffset : spread ;
152
+
153
+ double x = pos .getX () + 0.5 + offsetX ;
154
+ double y = pos .getY () + attachment .verticalOffset ;
155
+ double z = pos .getZ () + 0.5 + offsetZ ;
156
+
157
+ return new Vec3 (x , y , z );
158
+ }
159
+
145
160
@ Override
146
161
public String getSerializedName () {
147
162
return this .name ;
148
163
}
149
164
}
150
165
166
+ protected VoxelShape getShapeFor (BlockState state ) {
167
+ var facing = state .getValue (FACING );
168
+ var attachment = state .getValue (ATTACHMENT );
169
+
170
+ var xOffset = facing == Direction .EAST ? 1 : 0 ;
171
+ var xSize = facing .getAxis () == Direction .Axis .X ? 1 : 0 ;
172
+ var zOffset = facing == Direction .SOUTH ? 1 : 0 ;
173
+ var zSize = facing .getAxis () == Direction .Axis .Z ? 1 : 0 ;
174
+
175
+ if (attachment == AttachmentType .FLOOR ) {
176
+
177
+ var base = box (0.0 , 0.0 , 0.0 , 16.0 , 2.0 , 16.0 );
178
+ var legs = box (xOffset * 11.0 , 2.0 , zOffset * 11.0 , xOffset * 11.0 + 5.0 * xSize + 16.0 * zSize , 12.0 , zOffset * 11.0 + 5.0 * zSize + 16.0 * xSize );
179
+ var arms = box (xOffset * 12.0 + zSize - facing .getStepX () * 7.0 , 2.0 , zOffset * 12.0 + xSize - facing .getStepZ () * 7.0 , xOffset * 12.0 + 4.0 * xSize + 15.0 * zSize - facing .getStepX () * 7.0 , 17.0 , zOffset * 12.0 + 4.0 * zSize + 15.0 * xSize - facing .getStepZ () * 7.0 );
180
+ var body = box (zSize * 2.0 + xOffset * 5.0 + 1.0 , 7.0 , xSize * 2.0 + zOffset * 5.0 + 1.0 , xSize * 10.0 + xOffset * 5.0 + zSize * 13.0 , 16.0 , zSize * 10.0 + zOffset * 5.0 + xSize * 13.0 );
181
+ var head = box (zSize * 4.0 + xSize * 10.0 - xOffset * 10.0 , 7.0 , xSize * 4.0 + zSize * 10.0 - zOffset * 10.0 , zSize * 12.0 + xSize * 16.0 - xOffset * 10.0 , 15.0 , xSize * 12.0 + zSize * 16.0 - zOffset * 10.0 );
182
+
183
+ return Shapes .or (base , legs , arms , body , head );
184
+ }
185
+
186
+ if (attachment == AttachmentType .WALL ) {
187
+ var base = box (xOffset * 14.0 , 0.0 , zOffset * 14.0 , xOffset * 14.0 + 2.0 * xSize + 16.0 * zSize , 16.0 , zOffset * 14.0 + 2.0 * zSize + 16.0 * xSize );
188
+ var head = box (0.0 , 0.0 , 0.0 , 8.0 , 8.0 , 8.0 )
189
+ .move (facing .getStepX () * -1.25 , 5.0 / 16.0 , facing .getStepZ () * -1.25 )
190
+ .move (xOffset * 0.5 , 0.0 , zOffset * 0.5 )
191
+ .move (zSize * 0.25 , 0.0 , xSize * 0.25 );
192
+ var body = box (0.0 , 0.0 , 0.0 , 9.0 , 9.0 , 9.0 )
193
+ .move (facing .getStepX () * -11.0 / 16.0 , 7.0 / 16.0 , facing .getStepZ () * -11.0 / 16.0 )
194
+ .move (xOffset * 7.0 / 16.0 , 0.0 , zOffset * 7.0 / 16.0 )
195
+ .move (zSize * 0.25 , 0.0 , xSize * 0.25 );
196
+ var log = box (xOffset * 14.0 , 0.0 , zOffset * 14.0 , xOffset * 14.0 + 4.0 * xSize + 4.0 * zSize , 4.0 , zOffset * 14.0 + 4.0 * zSize + 16.0 * xSize )
197
+ .move (xSize * 0.125 - xOffset * 0.375 , 0.0 , zSize * 0.125 - zOffset * 0.375 );
198
+
199
+ return Shapes .or (base , body , head , log );
200
+ }
201
+
202
+ return box (0.0 , 0.0 , 0.0 , 16.0 , 16.0 , 16.0 );
203
+ }
204
+
205
+ @ Override
206
+ public VoxelShape getShape (BlockState state , BlockGetter level , BlockPos pos , CollisionContext context ) {
207
+ return SHAPES .get (state );
208
+ }
151
209
}
0 commit comments