1616import net .minecraft .world .level .block .Block ;
1717import net .minecraft .world .level .block .Blocks ;
1818import net .minecraft .world .level .block .HorizontalDirectionalBlock ;
19+ import net .minecraft .world .level .block .entity .BlockEntity ;
1920import net .minecraft .world .level .block .state .BlockState ;
2021import net .minecraft .world .level .block .state .StateDefinition ;
2122import net .minecraft .world .level .block .state .properties .EnumProperty ;
@@ -109,10 +110,38 @@ protected BlockState updateShape(
109110 * to this specific part of the 3x3x2 grid.
110111 */
111112 private boolean isNeighborPartOfCoop (BlockState state , Direction dir ) {
112- // For a 3x3x2, we can be lazy: if ANY adjacent block of the same type is removed,
113- // the whole thing should probably go.
114- // You can refine this to only check the "Master" block if you want more stability.
115- return true ;
113+ CoopPart part = state .getValue (PART );
114+ Direction facing = state .getValue (HorizontalDirectionalBlock .FACING );
115+
116+ // 1. Get the local offset of the neighbor block relative to this part
117+ // We convert the world Direction into a local x, y, z change
118+ int dx = dir .getStepX ();
119+ int dy = dir .getStepY ();
120+ int dz = dir .getStepZ ();
121+
122+ // 2. Adjust for rotation (Facing)
123+ // This ensures that "Front" always matches your Enum's Z-axis logic
124+ // Note: This math varies slightly depending on how your placement logic
125+ // maps "Front" to the world. Below is a standard mapping:
126+ int localDx , localDz ;
127+ switch (facing ) {
128+ case NORTH -> { localDx = dx ; localDz = dz ; }
129+ case SOUTH -> { localDx = -dx ; localDz = -dz ; }
130+ case WEST -> { localDx = dz ; localDz = -dx ; }
131+ case EAST -> { localDx = -dz ; localDz = dx ; }
132+ default -> { localDx = dx ; localDz = dz ; }
133+ }
134+
135+ // 3. Calculate the neighbor's hypothetical grid position
136+ int neighborX = part .getXOffset () + localDx ;
137+ int neighborZ = part .getZOffset () + localDz ;
138+ int neighborY = part .getYOffset () + dy ;
139+
140+ // 4. Check if these coordinates are within the 3x2x3 bounds
141+ // Width: 0-2 (X), Depth: 0-1 (Z), Height: 0-2 (Y)
142+ return neighborX >= 0 && neighborX < 3 &&
143+ neighborZ >= 0 && neighborZ < 2 &&
144+ neighborY >= 0 && neighborY < 3 ;
116145 }
117146
118147 @ Override
@@ -121,29 +150,37 @@ public BlockState playerWillDestroy(Level level, BlockPos pos, BlockState state,
121150 Direction facing = state .getValue (FACING );
122151 CoopPart currentPart = state .getValue (PART );
123152
124- // Find the absolute origin (0,0,0) by subtracting the current part's offset
153+ // Calculate origin based on the piece being broken
125154 BlockPos origin = pos .subtract (calculateOffset (BlockPos .ZERO , facing ,
126155 currentPart .getXOffset (), currentPart .getZOffset (), currentPart .getYOffset ()));
127156
128- // Break all 18 blocks in the 3x3x2 grid
157+ // Use a flag to prevent re-entry if isNeighborPartOfCoop triggers
129158 for (int x = 0 ; x < 3 ; x ++) {
130159 for (int z = 0 ; z < 2 ; z ++) {
131160 for (int y = 0 ; y < 3 ; y ++) {
132161 BlockPos targetPos = calculateOffset (origin , facing , x , z , y );
133162 BlockState targetState = level .getBlockState (targetPos );
134163
135- // Only break blocks that belong to this mod's chicken coop
136164 if (targetState .is (this )) {
165+ // 1. Handle Drops: This checks the loot table (JSON) and drops items
166+ if (!player .isCreative ()) {
167+ BlockEntity blockEntity = targetState .hasBlockEntity () ? level .getBlockEntity (targetPos ) : null ;
168+ Block .dropResources (targetState , level , targetPos , blockEntity , player , player .getMainHandItem ());
169+ }
170+
171+ // 2. Set to AIR with flag 3 (Update neighbors + Send to clients)
172+ // Using destroyBlock with 'false' for drops since we handled it above
173+ // for better control, or just setBlock to AIR.
137174 level .setBlock (targetPos , Blocks .AIR .defaultBlockState (), 3 );
138- // 2001 is the ID for block break particles + sound
139- level .levelEvent (2001 , targetPos , Block .getId (targetState ));
175+
176+ // 3. Play break effects
177+ level .levelEvent (2001 , targetPos , Block .getId (targetState ));
140178 }
141179 }
142180 }
143181 }
144182 }
145183
146- // Call super and return the resulting state as required by 1.21.1
147184 return super .playerWillDestroy (level , pos , state , player );
148185 }
149186
0 commit comments