Skip to content

Commit 1449c57

Browse files
fix: AI comments
1 parent 445ba9a commit 1449c57

2 files changed

Lines changed: 60 additions & 12 deletions

File tree

src/main/java/com/tcm/MineTale/block/ChickenCoopBlock.java

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.minecraft.world.level.block.Block;
1717
import net.minecraft.world.level.block.Blocks;
1818
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
19+
import net.minecraft.world.level.block.entity.BlockEntity;
1920
import net.minecraft.world.level.block.state.BlockState;
2021
import net.minecraft.world.level.block.state.StateDefinition;
2122
import 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

src/main/java/com/tcm/MineTale/util/CoopPart.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,25 @@ public enum CoopPart implements StringRepresentable {
5252
public int getZOffset() { return zOffset; }
5353

5454
/**
55-
* Logic to find the enum based on the position in the 3x3x2 grid.
55+
* Retrieves the CoopPart corresponding to the given grid offsets.
56+
* The grid is structured as 3x2x3 (width x depth x height),
57+
* corresponding to the x, z, and y axes respectively.
58+
*
59+
* @param x The width offset
60+
* @param z The depth offset
61+
* @param y The height offset
62+
* @return The matching CoopPart
63+
* @throws IllegalArgumentException if no part exists at the specified coordinates
5664
*/
5765
public static CoopPart getPartFromCoords(int x, int z, int y) {
5866
for (CoopPart part : values()) {
5967
if (part.xOffset == x && part.zOffset == z && part.yOffset == y) {
6068
return part;
6169
}
6270
}
63-
return BOTTOM_FRONT_LEFT;
71+
72+
throw new IllegalArgumentException(
73+
String.format("No CoopPart found at coordinates: x=%d, z=%d, y=%d", x, z, y)
74+
);
6475
}
6576
}

0 commit comments

Comments
 (0)