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.ItemStack;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.state.StateManager;
@ -94,7 +93,6 @@ public class CableBlock extends BlockWithEntity implements Waterloggable {
});
public final TRContent.Cables type;
private final CableShapeUtil cableShapeUtil;
public CableBlock(TRContent.Cables type) {
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)
.with(SOUTH, false).with(UP, false).with(DOWN, false).with(WATERLOGGED, false).with(COVERED, false));
BlockWrenchEventHandler.wrenableBlocks.add(this);
cableShapeUtil = new CableShapeUtil(this);
}
// BlockContainer
@ -203,7 +200,7 @@ public class CableBlock extends BlockWithEntity implements Waterloggable {
if (state.get(COVERED)) {
return VoxelShapes.fullCube();
}
return cableShapeUtil.getShape(state);
return CableShapeUtil.getShape(state);
}
@SuppressWarnings("deprecation")

View file

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

View file

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