Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ColorSprayBehaviour for gt pipes and ae2 cables #2532

Draft
wants to merge 3 commits into
base: 1.20.1
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraftforge.common.Tags;

import appeng.api.util.AECableType;
import appeng.api.util.AEColor;
import appeng.blockentity.networking.CableBusBlockEntity;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -207,7 +208,6 @@ public void appendHoverText(ItemStack stack, @Nullable Level level, List<Compone
public InteractionResult useOn(UseOnContext context) {
var player = context.getPlayer();
var level = context.getLevel();
var facing = context.getClickedFace();
var pos = context.getClickedPos().mutable();
var stack = context.getItemInHand();
var block = level.getBlockState(pos).getBlock();
Expand All @@ -216,22 +216,34 @@ public InteractionResult useOn(UseOnContext context) {
player != null && player.isShiftKeyDown() ? ConfigHolder.INSTANCE.tools.sprayCanChainLength : 1);

if (player != null) {
for (int i = 0; i < maxBlocksToRecolor; i++) {
var direction = getPaintDirection(player);
var facing = direction.getOpposite();
var first = level.getBlockEntity(pos);
pos.move(direction);
for (int i = 1; i < maxBlocksToRecolor; i++) {

// Stop once we hit another block to prevent accidentally recoloring stuff
if (level.getBlockState(pos).getBlock() != block) {
break;
}

if (!tryPaintBlock(player, level, pos, facing)) {
return InteractionResult.PASS;
if (!tryPaintBlock(player, level, pos, facing, first)) {
break;
}

if (!useItemDurability(player, context.getHand(), stack, empty.get())) {
break;
}

pos.move(getPaintDirection(player));
pos.move(direction);
}

if (!tryPaintBlock(player, level, first.getBlockPos(), facing, first)) {
return InteractionResult.PASS;
}

if (!useItemDurability(player, context.getHand(), stack, empty.get())) {
return InteractionResult.PASS;
}

GTSoundEntries.SPRAY_CAN_TOOL.play(level, null, player.position(), 1.0f, 1.0f);
Expand All @@ -240,7 +252,7 @@ public InteractionResult useOn(UseOnContext context) {
return InteractionResult.PASS;
}

private Direction getPaintDirection(Player player) {
private static Direction getPaintDirection(Player player) {
if (player.getXRot() > 45F) {
return Direction.DOWN;
} else if (player.getXRot() < -45F) {
Expand All @@ -250,13 +262,14 @@ private Direction getPaintDirection(Player player) {
return player.getDirection();
}

private boolean tryPaintBlock(Player player, Level world, BlockPos pos, Direction side) {
private boolean tryPaintBlock(Player player, Level world, BlockPos pos, Direction side, BlockEntity first) {
var blockState = world.getBlockState(pos);
var block = blockState.getBlock();
if (color == null) {
return tryStripBlockColor(player, world, pos, block, side);
return tryStripBlockColor(player, world, pos, block, side, first);
}
return recolorBlockState(world, pos, side, this.color) || tryPaintSpecialBlock(player, world, pos, block);
return recolorBlockState(world, pos, side, this.color) ||
tryPaintSpecialBlock(player, world, pos, block, first);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -271,7 +284,7 @@ private static boolean recolorBlockState(Level level, BlockPos pos, Direction si
return false;
}

private boolean tryPaintSpecialBlock(Player player, Level world, BlockPos pos, Block block) {
private boolean tryPaintSpecialBlock(Player player, Level world, BlockPos pos, Block block, BlockEntity first) {
if (block.defaultBlockState().is(Tags.Blocks.GLASS)) {
if (recolorBlockNoState(GLASS_MAP, this.color, world, pos, Blocks.GLASS)) {
return true;
Expand Down Expand Up @@ -329,28 +342,40 @@ private boolean tryPaintSpecialBlock(Player player, Level world, BlockPos pos, B

// MTE special case
BlockEntity be = world.getBlockEntity(pos);
if (be instanceof IMachineBlockEntity machineBe) {
if (be instanceof IMachineBlockEntity machineBe && first instanceof IMachineBlockEntity og) {
MetaMachine mte = machineBe.getMetaMachine();
if (mte != null) {
if (mte.getPaintingColor() != this.color.getTextColor()) {
MetaMachine ogMte = og.getMetaMachine();
if (mte != null && ogMte != null) {
if (mte.getPaintingColor() != this.color.getTextColor() &&
mte.getPaintingColor() == ogMte.getPaintingColor()) {
mte.setPaintingColor(this.color.getTextColor());
return true;
} else return false;
}
}

// PipeBlockEntity special case
if (be instanceof PipeBlockEntity<?, ?> pipe) {
if (pipe.getPaintingColor() != this.color.getTextColor()) {
if (be instanceof PipeBlockEntity<?, ?> pipe && first instanceof PipeBlockEntity<?, ?> og) {
var side = getPaintDirection(player).getOpposite();
if (!first.getBlockPos().equals(be.getBlockPos()) && (!pipe.isConnected(side))) {
return false;
}
if (pipe.getPaintingColor() != this.color.getTextColor() &&
pipe.getPaintingColor() == og.getPaintingColor()) {
pipe.setPaintingColor(this.color.getTextColor());
return true;
} else return false;
}

if (GTCEu.isAE2Loaded()) {
if (be instanceof CableBusBlockEntity cable) {
if (be instanceof CableBusBlockEntity cable && first instanceof CableBusBlockEntity og) {
var side = getPaintDirection(player).getOpposite();
if (!first.getBlockPos().equals(be.getBlockPos()) &&
(cable.getPart(side) != null || cable.getCableConnectionType(side) == AECableType.NONE)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am reading this right, for each block, it checks if near side has a cable part, and if so, stops before painting that block. I think you also want to check is the previous block has a cable part on the far side.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for both ae2 cables and gt pipes it's almost the same thing iirc, if the previous cable has a part on the far side the near side of the current one will either be disconnected or have another part.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With no knowledge of how AE represents things internally, is that true of ME P2P tunnel? Certainly, from a logical perspective, the cable opposite the tunnel is connected to the tunnel. I don't know what that means in terms of getCableConnectionType though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, p2p is an edge case, I'll see how it can be handled

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the right way to do it would probably be a refactor of the behaviour to use some custom collector logic for each block paintable block type, that would also help if we want to paint the connected network instead of just a straight line, I'll give it a shot later

return false;
}
// do not try to recolor if it already is this color
if (cable.getColor().ordinal() != color.ordinal()) {
if (cable.getColor().ordinal() != color.ordinal() && cable.getColor() == og.getColor()) {
cable.recolourBlock(null, AEColor.values()[color.ordinal()], player);
return true;
}
Expand Down Expand Up @@ -382,7 +407,8 @@ private static boolean recolorBlockNoState(Map<DyeColor, Block> map, DyeColor co
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private static boolean tryStripBlockColor(Player player, Level world, BlockPos pos, Block block, Direction side) {
private static boolean tryStripBlockColor(Player player, Level world, BlockPos pos, Block block, Direction side,
BlockEntity first) {
// MC special cases
if (block instanceof StainedGlassBlock) {
world.setBlock(pos, Blocks.GLASS.defaultBlockState(), 3);
Expand Down Expand Up @@ -432,29 +458,37 @@ private static boolean tryStripBlockColor(Player player, Level world, BlockPos p

// MTE special case
BlockEntity be = world.getBlockEntity(pos);
if (be instanceof IMachineBlockEntity machineBe) {
if (be instanceof IMachineBlockEntity machineBe && first instanceof IMachineBlockEntity og) {
MetaMachine mte = machineBe.getMetaMachine();
if (mte != null) {
if (mte.isPainted()) {
MetaMachine ogMte = og.getMetaMachine();
if (mte != null && ogMte != null) {
if (mte.isPainted() && mte.getPaintingColor() == ogMte.getPaintingColor()) {
mte.setPaintingColor(mte.getDefaultPaintingColor());
return true;
} else return false;
}
}

// PipeBlockEntity special case
if (be instanceof PipeBlockEntity<?, ?> pipe) {
if (pipe.isPainted()) {
if (be instanceof PipeBlockEntity<?, ?> pipe && first instanceof PipeBlockEntity<?, ?> og) {
if (!first.getBlockPos().equals(be.getBlockPos()) && (!pipe.isConnected(side))) {
return false;
}
if (pipe.isPainted() && pipe.getPaintingColor() == og.getPaintingColor()) {
pipe.setPaintingColor(pipe.getDefaultPaintingColor());
return true;
} else return false;
}

// AE2 cable special case
if (GTCEu.isAE2Loaded()) {
if (be instanceof CableBusBlockEntity cable) {
// do not try to strip color if it is already colorless
if (cable.getColor() != AEColor.TRANSPARENT) {
if (be instanceof CableBusBlockEntity cable && first instanceof CableBusBlockEntity og) {
if (!first.getBlockPos().equals(be.getBlockPos()) &&
(cable.getPart(side) != null || cable.getCableConnectionType(side) == AECableType.NONE)) {
return false;
}
// do not try to recolor if it already is this color
if (cable.getColor() != AEColor.TRANSPARENT && cable.getColor() == og.getColor()) {
cable.recolourBlock(null, AEColor.TRANSPARENT, player);
return true;
} else return false;
Expand Down
Loading