add a missing placement helper

- for placing small cogs against integrated cog wheels
This commit is contained in:
zelophed 2021-05-04 17:17:09 +02:00
parent 3c4e504f61
commit dc6baae0ea
2 changed files with 83 additions and 29 deletions

View file

@ -41,7 +41,7 @@ public class CogwheelBlockItem extends BlockItem {
large = block.isLarge; large = block.isLarge;
placementHelperId = PlacementHelpers.register(large ? new LargeCogHelper() : new SmallCogHelper()); placementHelperId = PlacementHelpers.register(large ? new LargeCogHelper() : new SmallCogHelper());
integratedCogHelperId = large ? PlacementHelpers.register(new IntegratedCogHelper()) : -1; integratedCogHelperId = PlacementHelpers.register(large ? new IntegratedLargeCogHelper() : new IntegratedSmallCogHelper());
} }
@Override @Override
@ -93,7 +93,7 @@ public class CogwheelBlockItem extends BlockItem {
for (int offset1 : Iterate.positiveAndNegative) { for (int offset1 : Iterate.positiveAndNegative) {
for (int offset2 : Iterate.positiveAndNegative) { for (int offset2 : Iterate.positiveAndNegative) {
BlockPos connectedPos = pos.offset(d1, offset1) BlockPos connectedPos = pos.offset(d1, offset1)
.offset(d2, offset2); .offset(d2, offset2);
BlockState blockState = world.getBlockState(connectedPos); BlockState blockState = world.getBlockState(connectedPos);
if (!(blockState.getBlock() instanceof CogWheelBlock)) if (!(blockState.getBlock() instanceof CogWheelBlock))
continue; continue;
@ -123,7 +123,7 @@ public class CogwheelBlockItem extends BlockItem {
if (!((CogWheelBlock) state.getBlock()).isLarge) { if (!((CogWheelBlock) state.getBlock()).isLarge) {
List<Direction> directions = List<Direction> directions =
IPlacementHelper.orderedByDistanceExceptAxis(pos, ray.getHitVec(), state.get(AXIS)); IPlacementHelper.orderedByDistanceExceptAxis(pos, ray.getHitVec(), state.get(AXIS));
for (Direction dir : directions) { for (Direction dir : directions) {
BlockPos newPos = pos.offset(dir); BlockPos newPos = pos.offset(dir);
@ -132,8 +132,8 @@ public class CogwheelBlockItem extends BlockItem {
continue; continue;
if (!world.getBlockState(newPos) if (!world.getBlockState(newPos)
.getMaterial() .getMaterial()
.isReplaceable()) .isReplaceable())
continue; continue;
return PlacementOffset.success(newPos, s -> s.with(AXIS, state.get(AXIS))); return PlacementOffset.success(newPos, s -> s.with(AXIS, state.get(AXIS)));
@ -162,19 +162,19 @@ public class CogwheelBlockItem extends BlockItem {
if (((CogWheelBlock) state.getBlock()).isLarge) { if (((CogWheelBlock) state.getBlock()).isLarge) {
Direction side = IPlacementHelper.orderedByDistanceOnlyAxis(pos, ray.getHitVec(), state.get(AXIS)) Direction side = IPlacementHelper.orderedByDistanceOnlyAxis(pos, ray.getHitVec(), state.get(AXIS))
.get(0); .get(0);
List<Direction> directions = List<Direction> directions =
IPlacementHelper.orderedByDistanceExceptAxis(pos, ray.getHitVec(), state.get(AXIS)); IPlacementHelper.orderedByDistanceExceptAxis(pos, ray.getHitVec(), state.get(AXIS));
for (Direction dir : directions) { for (Direction dir : directions) {
BlockPos newPos = pos.offset(dir) BlockPos newPos = pos.offset(dir)
.offset(side); .offset(side);
if (!CogWheelBlock.isValidCogwheelPosition(true, world, newPos, dir.getAxis())) if (!CogWheelBlock.isValidCogwheelPosition(true, world, newPos, dir.getAxis()))
continue; continue;
if (!world.getBlockState(newPos) if (!world.getBlockState(newPos)
.getMaterial() .getMaterial()
.isReplaceable()) .isReplaceable())
continue; continue;
return PlacementOffset.success(newPos, s -> s.with(AXIS, dir.getAxis())); return PlacementOffset.success(newPos, s -> s.with(AXIS, dir.getAxis()));
@ -199,16 +199,16 @@ public class CogwheelBlockItem extends BlockItem {
public PlacementOffset getOffset(PlayerEntity player, World world, BlockState state, BlockPos pos, BlockRayTraceResult ray) { public PlacementOffset getOffset(PlayerEntity player, World world, BlockState state, BlockPos pos, BlockRayTraceResult ray) {
// diagonal gears of different size // diagonal gears of different size
Direction closest = IPlacementHelper.orderedByDistanceExceptAxis(pos, ray.getHitVec(), state.get(AXIS)) Direction closest = IPlacementHelper.orderedByDistanceExceptAxis(pos, ray.getHitVec(), state.get(AXIS))
.get(0); .get(0);
List<Direction> directions = IPlacementHelper.orderedByDistanceExceptAxis(pos, ray.getHitVec(), List<Direction> directions = IPlacementHelper.orderedByDistanceExceptAxis(pos, ray.getHitVec(),
state.get(AXIS), d -> d.getAxis() != closest.getAxis()); state.get(AXIS), d -> d.getAxis() != closest.getAxis());
for (Direction dir : directions) { for (Direction dir : directions) {
BlockPos newPos = pos.offset(dir) BlockPos newPos = pos.offset(dir)
.offset(closest); .offset(closest);
if (!world.getBlockState(newPos) if (!world.getBlockState(newPos)
.getMaterial() .getMaterial()
.isReplaceable()) .isReplaceable())
continue; continue;
if (!CogWheelBlock.isValidCogwheelPosition(ICogWheel.isLargeCog(state), world, newPos, state.get(AXIS))) if (!CogWheelBlock.isValidCogwheelPosition(ICogWheel.isLargeCog(state), world, newPos, state.get(AXIS)))
@ -222,16 +222,16 @@ public class CogwheelBlockItem extends BlockItem {
protected boolean hitOnShaft(BlockState state, BlockRayTraceResult ray) { protected boolean hitOnShaft(BlockState state, BlockRayTraceResult ray) {
return AllShapes.SIX_VOXEL_POLE.get(state.get(AXIS)) return AllShapes.SIX_VOXEL_POLE.get(state.get(AXIS))
.getBoundingBox() .getBoundingBox()
.grow(0.001) .grow(0.001)
.contains(ray.getHitVec() .contains(ray.getHitVec()
.subtract(ray.getHitVec() .subtract(ray.getHitVec()
.align(Iterate.axisSet))); .align(Iterate.axisSet)));
} }
} }
@MethodsReturnNonnullByDefault @MethodsReturnNonnullByDefault
public static class IntegratedCogHelper implements IPlacementHelper { public static class IntegratedLargeCogHelper implements IPlacementHelper {
@Override @Override
public Predicate<ItemStack> getItemPredicate() { public Predicate<ItemStack> getItemPredicate() {
@ -250,10 +250,10 @@ public class CogwheelBlockItem extends BlockItem {
if (state.contains(HorizontalKineticBlock.HORIZONTAL_FACING)) if (state.contains(HorizontalKineticBlock.HORIZONTAL_FACING))
newAxis = state.get(HorizontalKineticBlock.HORIZONTAL_FACING) newAxis = state.get(HorizontalKineticBlock.HORIZONTAL_FACING)
.getAxis(); .getAxis();
else if (state.contains(DirectionalKineticBlock.FACING)) else if (state.contains(DirectionalKineticBlock.FACING))
newAxis = state.get(DirectionalKineticBlock.FACING) newAxis = state.get(DirectionalKineticBlock.FACING)
.getAxis(); .getAxis();
else else
newAxis = Axis.Y; newAxis = Axis.Y;
@ -261,15 +261,15 @@ public class CogwheelBlockItem extends BlockItem {
return PlacementOffset.fail(); return PlacementOffset.fail();
List<Direction> directions = List<Direction> directions =
IPlacementHelper.orderedByDistanceExceptAxis(pos, ray.getHitVec(), face.getAxis(), newAxis); IPlacementHelper.orderedByDistanceExceptAxis(pos, ray.getHitVec(), face.getAxis(), newAxis);
for (Direction d : directions) { for (Direction d : directions) {
BlockPos newPos = pos.offset(face) BlockPos newPos = pos.offset(face)
.offset(d); .offset(d);
if (!world.getBlockState(newPos) if (!world.getBlockState(newPos)
.getMaterial() .getMaterial()
.isReplaceable()) .isReplaceable())
continue; continue;
if (!CogWheelBlock.isValidCogwheelPosition(false, world, newPos, newAxis)) if (!CogWheelBlock.isValidCogwheelPosition(false, world, newPos, newAxis))
@ -282,4 +282,57 @@ public class CogwheelBlockItem extends BlockItem {
} }
} }
@MethodsReturnNonnullByDefault
public static class IntegratedSmallCogHelper implements IPlacementHelper {
@Override
public Predicate<ItemStack> getItemPredicate() {
return ((Predicate<ItemStack>) ICogWheel::isSmallCogItem).and(ICogWheel::isDedicatedCogItem);
}
@Override
public Predicate<BlockState> getStatePredicate() {
return s -> !ICogWheel.isDedicatedCogWheel(s.getBlock()) && ICogWheel.isSmallCog(s);
}
@Override
public PlacementOffset getOffset(PlayerEntity player, World world, BlockState state, BlockPos pos, BlockRayTraceResult ray) {
Direction face = ray.getFace();
Axis newAxis;
if (state.contains(HorizontalKineticBlock.HORIZONTAL_FACING))
newAxis = state.get(HorizontalKineticBlock.HORIZONTAL_FACING)
.getAxis();
else if (state.contains(DirectionalKineticBlock.FACING))
newAxis = state.get(DirectionalKineticBlock.FACING)
.getAxis();
else
newAxis = Axis.Y;
if (face.getAxis() == newAxis)
return PlacementOffset.fail();
List<Direction> directions = IPlacementHelper.orderedByDistanceExceptAxis(pos, ray.getHitVec(), newAxis);
for (Direction d : directions) {
BlockPos newPos = pos.offset(d);
if (!world.getBlockState(newPos)
.getMaterial()
.isReplaceable())
continue;
if (!CogWheelBlock.isValidCogwheelPosition(false, world, newPos, newAxis))
return PlacementOffset.fail();
return PlacementOffset.success()
.at(newPos)
.withTransform(s -> s.with(CogWheelBlock.AXIS, newAxis));
}
return PlacementOffset.fail();
}
}
} }

View file

@ -97,11 +97,12 @@ public class UIRenderHelper {
private static void streak(MatrixStack ms, int width, int height, int c1, int c2, int c3, int c4) { private static void streak(MatrixStack ms, int width, int height, int c1, int c2, int c3, int c4) {
double split1 = .5; double split1 = .5;
double split2 = .75; double split2 = .75;
Matrix4f model = ms.peek() Matrix4f model = ms.peek().getModel();
.getModel(); RenderSystem.disableAlphaTest();
GuiUtils.drawGradientRect(model, 0, -width, 0, width, (int) (split1 * height), c1, c2); GuiUtils.drawGradientRect(model, 0, -width, 0, width, (int) (split1 * height), c1, c2);
GuiUtils.drawGradientRect(model, 0, -width, (int) (split1 * height), width, (int) (split2 * height), c2, c3); GuiUtils.drawGradientRect(model, 0, -width, (int) (split1 * height), width, (int) (split2 * height), c2, c3);
GuiUtils.drawGradientRect(model, 0, -width, (int) (split2 * height), width, height, c3, c4); GuiUtils.drawGradientRect(model, 0, -width, (int) (split2 * height), width, height, c3, c4);
RenderSystem.enableAlphaTest();
} }
/** /**