|
4 | 4 | import net.minecraft.block.Block; |
5 | 5 | import net.minecraft.block.state.IBlockState; |
6 | 6 | import net.minecraft.entity.player.EntityPlayerMP; |
7 | | -import net.minecraft.init.Blocks; |
8 | 7 | import net.minecraft.item.ItemStack; |
9 | | -import net.minecraft.util.EnumFacing; |
10 | 8 | import net.minecraft.util.math.BlockPos; |
11 | 9 | import net.minecraft.world.World; |
12 | 10 | import net.minecraftforge.common.MinecraftForge; |
13 | 11 | import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; |
14 | 12 | import net.minecraftforge.fml.common.gameevent.TickEvent; |
| 13 | +import net.minecraftforge.fml.relauncher.Side; |
15 | 14 |
|
16 | 15 | import javax.annotation.Nonnull; |
17 | 16 | import java.util.*; |
18 | | -import java.util.function.Function; |
19 | 17 | import java.util.stream.Collectors; |
20 | 18 |
|
21 | | -import static gregtech.api.items.toolitem.ToolHelper.RELOCATE_MINED_BLOCKS_KEY; |
22 | | - |
23 | 19 | public final class TreeFellingListener { |
24 | 20 |
|
25 | 21 | private final EntityPlayerMP player; |
26 | 22 | private final ItemStack tool; |
27 | 23 | private final Deque<BlockPos> orderedBlocks; |
28 | | - private final BlockPos samplePos; |
29 | | - private final int minY; |
30 | | - |
31 | | - private int minX, maxX, minZ, maxZ; |
32 | | - private boolean purgeLeaves; |
33 | | - private Block targetLeaves; |
34 | | - private Iterator<BlockPos.MutableBlockPos> leavesToPurge; |
35 | 24 |
|
36 | 25 | private TreeFellingListener(EntityPlayerMP player, ItemStack tool, Deque<BlockPos> orderedBlocks) { |
37 | 26 | this.player = player; |
38 | 27 | this.tool = tool; |
39 | 28 | this.orderedBlocks = orderedBlocks; |
40 | | - this.samplePos = orderedBlocks.getFirst(); |
41 | | - this.minY = orderedBlocks.getLast().getY(); |
42 | | - this.minX = this.maxX = this.samplePos.getX(); |
43 | | - this.minZ = this.maxZ = this.samplePos.getZ(); |
44 | 29 | } |
45 | 30 |
|
46 | 31 | public static void start(@Nonnull IBlockState state, ItemStack tool, BlockPos start, @Nonnull EntityPlayerMP player) { |
@@ -87,102 +72,12 @@ public static void start(@Nonnull IBlockState state, ItemStack tool, BlockPos st |
87 | 72 |
|
88 | 73 | @SubscribeEvent |
89 | 74 | public void onWorldTick(@Nonnull TickEvent.WorldTickEvent event) { |
90 | | - if (event.phase == TickEvent.Phase.START && event.world == player.world) { |
91 | | - if (purgeLeaves) { |
92 | | - if (targetLeaves == null) { |
93 | | - targetLeaves = Arrays.stream(EnumFacing.VALUES) |
94 | | - .map(facing -> player.world.getBlockState(this.samplePos).getBlock()) |
95 | | - // Cannot use fastutil map::new here as setValue throws UOE |
96 | | - .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) |
97 | | - .entrySet() |
98 | | - .stream() |
99 | | - .max(Map.Entry.comparingByValue()) |
100 | | - .map(Map.Entry::getKey) |
101 | | - .orElse(Blocks.AIR); |
102 | | - BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos(this.samplePos); |
103 | | - int topY = mutablePos.getY(); |
104 | | - int tries = 2; |
105 | | - while (tries > 0) { |
106 | | - IBlockState state; |
107 | | - do { |
108 | | - topY = mutablePos.getY() + 1; |
109 | | - mutablePos.setY(topY); |
110 | | - } while (targetLeaves == Blocks.AIR ? |
111 | | - (state = player.world.getBlockState(mutablePos)).getBlock().isLeaves(state, player.world, mutablePos) : |
112 | | - player.world.getBlockState(mutablePos).getBlock() == targetLeaves); |
113 | | - tries--; |
114 | | - } |
115 | | - int offsetMinX = 3; |
116 | | - int offsetMaxX = 3; |
117 | | - int offsetMinZ = 3; |
118 | | - int offsetMaxZ = 3; |
119 | | - for (BlockPos.MutableBlockPos check : BlockPos.getAllInBoxMutable(this.minX - offsetMinX, this.minY, this.minZ - offsetMinZ, this.maxX + offsetMaxX, this.minY, this.maxZ + offsetMaxZ)) { |
120 | | - if (check.getX() == this.samplePos.getX() && check.getZ() == this.samplePos.getZ()) { |
121 | | - continue; |
122 | | - } |
123 | | - if (player.world.getBlockState(check).getBlock().isWood(player.world, check)) { |
124 | | - int diff = this.samplePos.getX() - check.getX(); |
125 | | - if (diff > 0 && diff < offsetMaxX) { |
126 | | - offsetMaxX = diff; |
127 | | - } else if (Math.abs(diff) < offsetMinX) { |
128 | | - offsetMinX = Math.abs(diff); |
129 | | - } |
130 | | - diff = this.samplePos.getZ() - check.getZ(); |
131 | | - if (diff > 0 && diff < offsetMaxZ) { |
132 | | - offsetMaxZ = diff; |
133 | | - } else if (Math.abs(diff) < offsetMinZ) { |
134 | | - offsetMinZ = Math.abs(diff); |
135 | | - } |
136 | | - } |
137 | | - } |
138 | | - leavesToPurge = BlockPos.getAllInBoxMutable(this.minX - offsetMinX, this.minY, this.minZ - offsetMinZ, this.maxX + offsetMaxX, topY, this.maxZ + offsetMaxZ).iterator(); |
139 | | - return; |
140 | | - } |
141 | | - while (leavesToPurge.hasNext()) { |
142 | | - BlockPos.MutableBlockPos check = leavesToPurge.next(); |
143 | | - IBlockState state = player.world.getBlockState(check); |
144 | | - if (targetLeaves == Blocks.AIR ? state.getBlock().isLeaves(state, player.world, check) : state.getBlock() == targetLeaves) { |
145 | | - if(ToolHelper.getBehaviorsTag(tool).getBoolean(RELOCATE_MINED_BLOCKS_KEY)) { |
146 | | - List<ItemStack> drops = state.getBlock().getDrops(player.world, check, state, ToolHelper.getFortuneOrLootingLevel(tool)); |
147 | | - for (ItemStack dropStack : drops) { |
148 | | - if(!player.addItemStackToInventory(dropStack)) { |
149 | | - player.dropItem(dropStack, false, false); |
150 | | - } |
151 | | - } |
152 | | - } |
153 | | - else { |
154 | | - state.getBlock().dropBlockAsItem(player.world, check, state, 0); |
155 | | - } |
156 | | - player.world.setBlockToAir(check); |
157 | | - } |
158 | | - } |
| 75 | + if (event.phase == TickEvent.Phase.START && event.world == player.world && event.side == Side.SERVER) { |
| 76 | + if (orderedBlocks.isEmpty() || tool.isEmpty()) { |
159 | 77 | MinecraftForge.EVENT_BUS.unregister(this); |
160 | 78 | return; |
161 | 79 | } |
162 | | - if (tool.isEmpty()) { |
163 | | - MinecraftForge.EVENT_BUS.unregister(this); |
164 | | - return; |
165 | | - } |
166 | | - if (!orderedBlocks.isEmpty()) { |
167 | | - BlockPos posToBreak = orderedBlocks.removeLast(); |
168 | | - int x = posToBreak.getX(); |
169 | | - if (x > this.maxX) { |
170 | | - this.maxX = x; |
171 | | - } else if (x < this.minX) { |
172 | | - this.minX = x; |
173 | | - } |
174 | | - int z = posToBreak.getZ(); |
175 | | - if (z > this.maxZ) { |
176 | | - this.maxZ = z; |
177 | | - } else if (z < this.minZ) { |
178 | | - this.minZ = z; |
179 | | - } |
180 | | - if (!ToolHelper.breakBlockRoutine(player, tool, posToBreak)) { |
181 | | - purgeLeaves = true; |
182 | | - } |
183 | | - } else { |
184 | | - purgeLeaves = true; |
185 | | - } |
| 80 | + ToolHelper.breakBlockRoutine(player, tool, orderedBlocks.removeLast()); |
186 | 81 | } |
187 | 82 | } |
188 | 83 | } |
0 commit comments