Fix cable shape computation. Closes #2435. Thanks to Technici4n

* Fix cable shape computation. Closes #2435.
* Remove unused import
This commit is contained in:
Technici4n 2021-10-08 07:48:11 +02:00 committed by GitHub
parent 0ddf6688c9
commit f39c619c22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 32 deletions

View file

@ -37,7 +37,6 @@ import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleTypes; import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents; import net.minecraft.sound.SoundEvents;
import net.minecraft.state.StateManager; import net.minecraft.state.StateManager;
@ -94,7 +93,6 @@ public class CableBlock extends BlockWithEntity implements Waterloggable {
}); });
public final TRContent.Cables type; public final TRContent.Cables type;
private final CableShapeUtil cableShapeUtil;
public CableBlock(TRContent.Cables type) { public CableBlock(TRContent.Cables type) {
super(Block.Settings.of(Material.STONE).strength(1f, 8f)); super(Block.Settings.of(Material.STONE).strength(1f, 8f));
@ -102,7 +100,6 @@ public class CableBlock extends BlockWithEntity implements Waterloggable {
setDefaultState(this.getStateManager().getDefaultState().with(EAST, false).with(WEST, false).with(NORTH, false) setDefaultState(this.getStateManager().getDefaultState().with(EAST, false).with(WEST, false).with(NORTH, false)
.with(SOUTH, false).with(UP, false).with(DOWN, false).with(WATERLOGGED, false).with(COVERED, false)); .with(SOUTH, false).with(UP, false).with(DOWN, false).with(WATERLOGGED, false).with(COVERED, false));
BlockWrenchEventHandler.wrenableBlocks.add(this); BlockWrenchEventHandler.wrenableBlocks.add(this);
cableShapeUtil = new CableShapeUtil(this);
} }
// BlockContainer // BlockContainer
@ -203,7 +200,7 @@ public class CableBlock extends BlockWithEntity implements Waterloggable {
if (state.get(COVERED)) { if (state.get(COVERED)) {
return VoxelShapes.fullCube(); return VoxelShapes.fullCube();
} }
return cableShapeUtil.getShape(state); return CableShapeUtil.getShape(state);
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")

View file

@ -26,51 +26,41 @@ package techreborn.blocks.cable;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.util.Util;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes; import net.minecraft.util.shape.VoxelShapes;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
public final class CableShapeUtil { public final class CableShapeUtil {
private final CableBlock cableBlock; private static final Map<BlockState, VoxelShape> SHAPE_CACHE = new IdentityHashMap<>();
private final HashMap<BlockState, VoxelShape> shapes;
public CableShapeUtil(CableBlock cableBlock) { private static VoxelShape getStateShape(BlockState state) {
this.cableBlock = cableBlock; CableBlock cableBlock = (CableBlock) state.getBlock();
this.shapes = createStateShapeMap();
}
private HashMap<BlockState, VoxelShape> createStateShapeMap() { final double size = cableBlock.type.cableThickness;
return Util.make(new HashMap<>(), map -> cableBlock.getStateManager().getStates() final VoxelShape baseShape = Block.createCuboidShape(size, size, size, 1 - size, 1 - size, 1 - size);
.forEach(state -> map.put(state, getStateShape(state)))
);
}
private VoxelShape getStateShape(BlockState state) {
final double size = cableBlock.type != null ? cableBlock.type.cableThickness : 6;
final VoxelShape baseShape = Block.createCuboidShape(size, size, size, 16.0D - size, 16.0D - size, 16.0D - size);
final List<VoxelShape> connections = new ArrayList<>(); final List<VoxelShape> connections = new ArrayList<>();
for (Direction dir : Direction.values()) { for (Direction dir : Direction.values()) {
if (state.get(CableBlock.PROPERTY_MAP.get(dir))) { if (state.get(CableBlock.PROPERTY_MAP.get(dir))) {
double x = dir == Direction.WEST ? 0 : dir == Direction.EAST ? 16D : size; double[] mins = new double[] { size, size, size };
double z = dir == Direction.NORTH ? 0 : dir == Direction.SOUTH ? 16D : size; double[] maxs = new double[] { 1 - size, 1 - size, 1 - size };
double y = dir == Direction.DOWN ? 0 : dir == Direction.UP ? 16D : size; int axis = dir.getAxis().ordinal();
if (dir.getDirection() == Direction.AxisDirection.POSITIVE) {
VoxelShape shape = VoxelShapes.cuboidUnchecked(x, y, z, 16.0D - size, 16.0D - size, 16.0D - size); maxs[axis] = 1;
connections.add(shape); } else {
mins[axis] = 0;
}
connections.add(VoxelShapes.cuboid(mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]));
} }
} }
return VoxelShapes.union(baseShape, connections.toArray(new VoxelShape[]{})); return VoxelShapes.union(baseShape, connections.toArray(new VoxelShape[]{}));
} }
public VoxelShape getShape(BlockState state) { public static VoxelShape getShape(BlockState state) {
return shapes.get(state); return SHAPE_CACHE.computeIfAbsent(state, CableShapeUtil::getStateShape);
} }
} }

View file

@ -359,7 +359,7 @@ public class TRContent {
name = this.toString().toLowerCase(Locale.ROOT); name = this.toString().toLowerCase(Locale.ROOT);
this.transferRate = transferRate; this.transferRate = transferRate;
this.defaultTransferRate = transferRate; this.defaultTransferRate = transferRate;
this.cableThickness = cableThickness / 2; this.cableThickness = cableThickness / 2 / 16;
this.canKill = canKill; this.canKill = canKill;
this.defaultCanKill = canKill; this.defaultCanKill = canKill;
this.tier = tier; this.tier = tier;