|
1 | 1 | package gregtech.common.items.behaviors.spray; |
2 | 2 |
|
| 3 | +import gregtech.api.color.ColoredBlockContainer; |
3 | 4 | import gregtech.api.items.metaitem.MetaItem; |
4 | 5 | import gregtech.api.items.metaitem.stats.IItemBehaviour; |
5 | | -import gregtech.api.util.color.ColoredBlockContainer; |
6 | | -import gregtech.api.color.ColoredBlockContainer; |
| 6 | +import gregtech.api.pipenet.tile.IPipeTile; |
| 7 | +import gregtech.common.pipelike.PipeCollectorWalker; |
7 | 8 | import gregtech.core.sound.GTSoundEvents; |
8 | 9 |
|
9 | 10 | import net.minecraft.entity.player.EntityPlayer; |
10 | 11 | import net.minecraft.item.EnumDyeColor; |
11 | 12 | import net.minecraft.item.ItemStack; |
| 13 | +import net.minecraft.tileentity.TileEntity; |
12 | 14 | import net.minecraft.util.ActionResult; |
13 | 15 | import net.minecraft.util.EnumActionResult; |
14 | 16 | import net.minecraft.util.EnumFacing; |
@@ -86,20 +88,64 @@ public ActionResult<ItemStack> onItemUse(EntityPlayer player, World world, Block |
86 | 88 | return EnumActionResult.PASS; |
87 | 89 | } else if (!player.canPlayerEdit(pos, facing, sprayCan)) { |
88 | 90 | return EnumActionResult.FAIL; |
89 | | - } else if (!tryPaintBlock(player, world, pos, facing, sprayCan)) { |
| 91 | + } |
| 92 | + |
| 93 | + int returnCode = tryPaintBlock(world, pos, player, hand, facing, sprayCan); |
| 94 | + if (returnCode == 0) { |
90 | 95 | return EnumActionResult.PASS; |
| 96 | + } else if (returnCode == 1) { |
| 97 | + onSpray(player, hand, sprayCan); |
91 | 98 | } |
92 | 99 |
|
93 | | - onSpray(player, hand, sprayCan); |
94 | 100 | world.playSound(null, player.posX, player.posY, player.posZ, GTSoundEvents.SPRAY_CAN_TOOL, |
95 | 101 | SoundCategory.PLAYERS, 1.0f, 1.0f); |
96 | 102 | return EnumActionResult.SUCCESS; |
97 | 103 | } |
98 | 104 |
|
99 | | - @SuppressWarnings("BooleanMethodIsAlwaysInverted") |
100 | | - protected boolean tryPaintBlock(@NotNull EntityPlayer player, @NotNull World world, @NotNull BlockPos pos, |
101 | | - @NotNull EnumFacing side, @NotNull ItemStack sprayCan) { |
| 105 | + /** |
| 106 | + * Return codes:<br/> |
| 107 | + * {@code -1}: didn't paint any block(s)<br/> |
| 108 | + * {@code 0}: colored 1 block</br> |
| 109 | + * {@code 1+}: colored multiple blocks and {@link #onSpray(EntityPlayer, EnumHand, ItemStack)} was handled already |
| 110 | + */ |
| 111 | + protected int tryPaintBlock(@NotNull World world, @NotNull BlockPos pos, @NotNull EntityPlayer player, |
| 112 | + @NotNull EnumHand hand, @NotNull EnumFacing side, @NotNull ItemStack sprayCan) { |
| 113 | + if (player.isSneaking()) { |
| 114 | + TileEntity te = world.getTileEntity(pos); |
| 115 | + if (te instanceof IPipeTile<?, ?>pipeTile) { |
| 116 | + return traversePipes(world, player, hand, pos, pipeTile, sprayCan); |
| 117 | + } |
| 118 | + } |
| 119 | + |
102 | 120 | ColoredBlockContainer blockContainer = ColoredBlockContainer.getInstance(world, pos, side, player); |
103 | | - return blockContainer.isValid() && blockContainer.setColor(getColor(sprayCan)); |
| 121 | + if (blockContainer.isValid() && blockContainer.setColor(getColor(sprayCan))) { |
| 122 | + return 0; |
| 123 | + } |
| 124 | + |
| 125 | + return -1; |
| 126 | + } |
| 127 | + |
| 128 | + protected int traversePipes(@NotNull World world, @NotNull EntityPlayer player, @NotNull EnumHand hand, |
| 129 | + @NotNull BlockPos startPos, @NotNull IPipeTile<?, ?> startingPipe, |
| 130 | + @NotNull ItemStack sprayCan) { |
| 131 | + EnumDyeColor dyeColor = getColor(sprayCan); |
| 132 | + int color = dyeColor == null ? -1 : dyeColor.colorValue; |
| 133 | + int[] paintedCountHolder = { 0 }; |
| 134 | + PipeCollectorWalker.collectPipeNet(world, startPos, startingPipe, pipe -> { |
| 135 | + if (!canSpray(sprayCan)) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + if (pipe.getPaintingColor() != color) { |
| 140 | + pipe.setPaintingColor(color); |
| 141 | + pipe.scheduleRenderUpdate(); |
| 142 | + onSpray(player, hand, sprayCan); |
| 143 | + paintedCountHolder[0]++; |
| 144 | + } |
| 145 | + |
| 146 | + return true; |
| 147 | + }); |
| 148 | + |
| 149 | + return paintedCountHolder[0]; |
104 | 150 | } |
105 | 151 | } |
0 commit comments