Skip to content

Commit 3095268

Browse files
committed
fix: use Actor#runAction to enforce one-at-a-time
- fixes #482
1 parent f281153 commit 3095268

1 file changed

Lines changed: 131 additions & 133 deletions

File tree

  • src/main/java/com/thevoxelbox/voxelsniper/sniper

src/main/java/com/thevoxelbox/voxelsniper/sniper/Sniper.java

Lines changed: 131 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.thevoxelbox.voxelsniper.sniper;
22

3-
import com.fastasyncworldedit.core.Fawe;
43
import com.fastasyncworldedit.core.configuration.Caption;
5-
import com.fastasyncworldedit.core.queue.implementation.QueueHandler;
64
import com.sk89q.worldedit.EditSession;
75
import com.sk89q.worldedit.LocalSession;
86
import com.sk89q.worldedit.WorldEdit;
97
import com.sk89q.worldedit.bukkit.BukkitAdapter;
8+
import com.sk89q.worldedit.internal.util.LogManagerCompat;
109
import com.sk89q.worldedit.math.BlockVector3;
1110
import com.sk89q.worldedit.session.request.Request;
1211
import com.sk89q.worldedit.util.formatting.text.Component;
@@ -27,6 +26,7 @@
2726
import com.thevoxelbox.voxelsniper.sniper.toolkit.ToolkitProperties;
2827
import com.thevoxelbox.voxelsniper.util.material.Materials;
2928
import com.thevoxelbox.voxelsniper.util.message.VoxelSniperText;
29+
import org.apache.logging.log4j.Logger;
3030
import org.bukkit.Bukkit;
3131
import org.bukkit.block.Block;
3232
import org.bukkit.block.BlockFace;
@@ -43,6 +43,8 @@
4343

4444
public class Sniper implements SniperCommander {
4545

46+
private static final Logger LOGGER = LogManagerCompat.getLogger();
47+
4648
private final UUID uuid;
4749
private Player player;
4850
private final List<Toolkit> toolkits = new ArrayList<>();
@@ -179,25 +181,23 @@ public boolean snipe(
179181
return false;
180182
}
181183
com.sk89q.worldedit.entity.Player wePlayer = BukkitAdapter.adapt(player);
182-
LocalSession session = wePlayer.getSession();
183-
QueueHandler queue = Fawe.instance().getQueueHandler();
184-
queue.async(() -> {
185-
synchronized (session) {
186-
if (!player.isValid()) {
187-
return;
188-
}
189-
snipeOnCurrentThread(
190-
wePlayer,
191-
player,
192-
action,
193-
clickedBlock,
194-
clickedBlockFace,
195-
toolkit,
196-
toolAction,
197-
currentBrushProperties
198-
);
199-
}
200-
});
184+
wePlayer.runAction(
185+
() -> {
186+
if (!player.isValid()) {
187+
return;
188+
}
189+
snipeOnCurrentThread(
190+
wePlayer,
191+
player,
192+
action,
193+
clickedBlock,
194+
clickedBlockFace,
195+
toolkit,
196+
toolAction,
197+
currentBrushProperties
198+
);
199+
}, false, true
200+
);
201201
return true;
202202
}
203203

@@ -212,134 +212,132 @@ public synchronized boolean snipeOnCurrentThread(
212212
BrushProperties currentBrushProperties
213213
) {
214214
LocalSession session = wePlayer.getSession();
215-
synchronized (session) {
216-
EditSession editSession = session.createEditSession(wePlayer);
215+
EditSession editSession = session.createEditSession(wePlayer);
217216

218-
try {
219-
ToolkitProperties toolkitProperties = toolkit.getProperties();
220-
BlockVector3 rayTraceTargetBlock = null;
221-
BlockVector3 rayTraceLastBlock = null;
222-
{
223-
Request.reset();
224-
Request.request().setExtent(editSession);
225-
if (clickedBlock == null) {
226-
BlockTracer blockTracer = toolkitProperties.createBlockTracer(player);
227-
BlockVector3 targetRayTraceResult = blockTracer.getTargetBlock();
228-
if (targetRayTraceResult != null) {
229-
rayTraceTargetBlock = targetRayTraceResult;
230-
}
231-
BlockVector3 lastRayTraceResult = blockTracer.getLastBlock();
232-
if (lastRayTraceResult != null) {
233-
rayTraceLastBlock = lastRayTraceResult;
234-
}
217+
try {
218+
ToolkitProperties toolkitProperties = toolkit.getProperties();
219+
BlockVector3 rayTraceTargetBlock = null;
220+
BlockVector3 rayTraceLastBlock = null;
221+
{
222+
Request.reset();
223+
Request.request().setExtent(editSession);
224+
if (clickedBlock == null) {
225+
BlockTracer blockTracer = toolkitProperties.createBlockTracer(player);
226+
BlockVector3 targetRayTraceResult = blockTracer.getTargetBlock();
227+
if (targetRayTraceResult != null) {
228+
rayTraceTargetBlock = targetRayTraceResult;
229+
}
230+
BlockVector3 lastRayTraceResult = blockTracer.getLastBlock();
231+
if (lastRayTraceResult != null) {
232+
rayTraceLastBlock = lastRayTraceResult;
235233
}
236234
}
237-
BlockVector3 targetBlock = clickedBlock == null
238-
? rayTraceTargetBlock
239-
: BukkitAdapter.asBlockVector(clickedBlock.getLocation());
240-
if (player.isSneaking()) {
241-
SnipeMessenger messenger = new SnipeMessenger(toolkitProperties, currentBrushProperties, player);
242-
if (action == Action.LEFT_CLICK_BLOCK || action == Action.LEFT_CLICK_AIR) {
243-
if (toolAction == ToolAction.ARROW) {
244-
BlockType blockType;
245-
if (targetBlock == null || Materials.isEmpty(
246-
(blockType = editSession.getBlockType(
247-
targetBlock.x(),
248-
targetBlock.y(),
249-
targetBlock.z()
250-
))
251-
)) {
252-
toolkitProperties.resetPattern();
253-
} else {
254-
toolkitProperties.setPattern(new BrushPattern(blockType));
255-
}
256-
messenger.sendPatternMessage();
257-
return true;
258-
} else if (toolAction == ToolAction.GUNPOWDER) {
259-
BlockState blockState;
260-
if (targetBlock == null || Materials.isEmpty(
261-
(blockState = editSession.getBlock(targetBlock)).getBlockType()
262-
)) {
263-
toolkitProperties.resetPattern();
264-
} else {
265-
toolkitProperties.setPattern(new BrushPattern(blockState));
266-
}
267-
messenger.sendPatternMessage();
268-
return true;
269-
}
270-
return false;
271-
} else if (action == Action.RIGHT_CLICK_BLOCK || action == Action.RIGHT_CLICK_AIR) {
272-
if (toolAction == ToolAction.ARROW) {
273-
if (targetBlock == null) {
274-
toolkitProperties.resetReplacePattern();
275-
} else {
276-
BlockType blockType = editSession.getBlockType(
235+
}
236+
BlockVector3 targetBlock = clickedBlock == null
237+
? rayTraceTargetBlock
238+
: BukkitAdapter.asBlockVector(clickedBlock.getLocation());
239+
if (player.isSneaking()) {
240+
SnipeMessenger messenger = new SnipeMessenger(toolkitProperties, currentBrushProperties, player);
241+
if (action == Action.LEFT_CLICK_BLOCK || action == Action.LEFT_CLICK_AIR) {
242+
if (toolAction == ToolAction.ARROW) {
243+
BlockType blockType;
244+
if (targetBlock == null || Materials.isEmpty(
245+
(blockType = editSession.getBlockType(
277246
targetBlock.x(),
278247
targetBlock.y(),
279248
targetBlock.z()
280-
);
281-
toolkitProperties.setReplacePattern(new BrushPattern(blockType));
282-
}
283-
messenger.sendReplacePatternMessage();
284-
return true;
285-
} else if (toolAction == ToolAction.GUNPOWDER) {
286-
if (targetBlock == null) {
287-
toolkitProperties.resetReplacePattern();
288-
} else {
289-
toolkitProperties.setReplacePattern(new BrushPattern(editSession.getBlock(targetBlock)));
290-
}
291-
messenger.sendReplacePatternMessage();
292-
return true;
249+
))
250+
)) {
251+
toolkitProperties.resetPattern();
252+
} else {
253+
toolkitProperties.setPattern(new BrushPattern(blockType));
293254
}
294-
return false;
255+
messenger.sendPatternMessage();
256+
return true;
257+
} else if (toolAction == ToolAction.GUNPOWDER) {
258+
BlockState blockState;
259+
if (targetBlock == null || Materials.isEmpty(
260+
(blockState = editSession.getBlock(targetBlock)).getBlockType()
261+
)) {
262+
toolkitProperties.resetPattern();
263+
} else {
264+
toolkitProperties.setPattern(new BrushPattern(blockState));
265+
}
266+
messenger.sendPatternMessage();
267+
return true;
295268
}
296269
return false;
297-
} else {
298-
if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
299-
if (targetBlock == null || (targetBlock.y() != editSession.getMinY() &&
300-
Materials.isEmpty(editSession.getBlockType(
301-
targetBlock.x(),
302-
targetBlock.y(),
303-
targetBlock.z()
304-
)))) {
305-
print(Caption.of("voxelsniper.sniper.target-invisible"));
306-
return true;
270+
} else if (action == Action.RIGHT_CLICK_BLOCK || action == Action.RIGHT_CLICK_AIR) {
271+
if (toolAction == ToolAction.ARROW) {
272+
if (targetBlock == null) {
273+
toolkitProperties.resetReplacePattern();
274+
} else {
275+
BlockType blockType = editSession.getBlockType(
276+
targetBlock.x(),
277+
targetBlock.y(),
278+
targetBlock.z()
279+
);
280+
toolkitProperties.setReplacePattern(new BrushPattern(blockType));
307281
}
308-
Brush currentBrush = toolkit.getCurrentBrush();
309-
if (currentBrush == null) {
310-
return false;
311-
}
312-
Snipe snipe = new Snipe(this, toolkit, toolkitProperties, currentBrushProperties, currentBrush);
313-
if (currentBrushProperties.getBrushPatternType() == BrushPatternType.SINGLE_BLOCK
314-
&& toolkitProperties.getPattern().asBlockType() == null) {
315-
print(Caption.of("voxelsniper.sniper.single-block-pattern"));
316-
return false;
317-
}
318-
319-
if (currentBrush instanceof PerformerBrush performerBrush) {
320-
performerBrush.initialize(snipe);
282+
messenger.sendReplacePatternMessage();
283+
return true;
284+
} else if (toolAction == ToolAction.GUNPOWDER) {
285+
if (targetBlock == null) {
286+
toolkitProperties.resetReplacePattern();
287+
} else {
288+
toolkitProperties.setReplacePattern(new BrushPattern(editSession.getBlock(targetBlock)));
321289
}
322-
BlockVector3 lastBlock = clickedBlock == null
323-
? rayTraceLastBlock
324-
: targetBlock.add(
325-
clickedBlockFace.getModX(),
326-
clickedBlockFace.getModY(),
327-
clickedBlockFace.getModZ()
328-
);
329-
currentBrush.perform(snipe, toolAction, editSession, targetBlock, lastBlock);
290+
messenger.sendReplacePatternMessage();
330291
return true;
331292
}
293+
return false;
332294
}
333295
return false;
334-
} catch (Throwable t) {
335-
t.printStackTrace();
336-
print(Caption.of("voxelsniper.error.unexpected"));
337-
return false;
338-
} finally {
339-
session.remember(editSession);
340-
editSession.flushQueue();
341-
WorldEdit.getInstance().flushBlockBag(wePlayer, editSession);
296+
} else {
297+
if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
298+
if (targetBlock == null || (targetBlock.y() != editSession.getMinY() &&
299+
Materials.isEmpty(editSession.getBlockType(
300+
targetBlock.x(),
301+
targetBlock.y(),
302+
targetBlock.z()
303+
)))) {
304+
print(Caption.of("voxelsniper.sniper.target-invisible"));
305+
return true;
306+
}
307+
Brush currentBrush = toolkit.getCurrentBrush();
308+
if (currentBrush == null) {
309+
return false;
310+
}
311+
Snipe snipe = new Snipe(this, toolkit, toolkitProperties, currentBrushProperties, currentBrush);
312+
if (currentBrushProperties.getBrushPatternType() == BrushPatternType.SINGLE_BLOCK
313+
&& toolkitProperties.getPattern().asBlockType() == null) {
314+
print(Caption.of("voxelsniper.sniper.single-block-pattern"));
315+
return false;
316+
}
317+
318+
if (currentBrush instanceof PerformerBrush performerBrush) {
319+
performerBrush.initialize(snipe);
320+
}
321+
BlockVector3 lastBlock = clickedBlock == null
322+
? rayTraceLastBlock
323+
: targetBlock.add(
324+
clickedBlockFace.getModX(),
325+
clickedBlockFace.getModY(),
326+
clickedBlockFace.getModZ()
327+
);
328+
currentBrush.perform(snipe, toolAction, editSession, targetBlock, lastBlock);
329+
return true;
330+
}
342331
}
332+
return false;
333+
} catch (Throwable t) {
334+
LOGGER.error("Unexpected error during FAVS action", t);
335+
print(Caption.of("voxelsniper.error.unexpected"));
336+
return false;
337+
} finally {
338+
session.remember(editSession);
339+
editSession.flushQueue();
340+
WorldEdit.getInstance().flushBlockBag(wePlayer, editSession);
343341
}
344342
}
345343

0 commit comments

Comments
 (0)