From e10e378300702e0822f1d940d919ee0d8ce9aa26 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Wed, 28 Jul 2021 04:24:49 +0200 Subject: [PATCH] moved highlight so it doesn't disrupt api --- .../me/youhavetrouble/blockedit/BEPlayer.java | 17 +++------- .../blockedit/commands/DeselCommand.java | 7 ++-- .../optionals/SelectionHighlight.java | 3 +- .../blockedit/wands/SelectionWand.java | 32 +++++++++++++++++++ 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java b/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java index cbc3581..d4d8734 100644 --- a/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java +++ b/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java @@ -30,7 +30,8 @@ public class BEPlayer { public void resetSelection() { this.selection = null; - Bukkit.getScheduler().runTaskAsynchronously(BlockEdit.getPlugin(),() -> SelectionHighlight.sendStop(getPlayer())); + this.selectionPoint1 = null; + this.selectionPoint2 = null; } private void updateSelection() { @@ -47,18 +48,6 @@ public class BEPlayer { return; } - Bukkit.getScheduler().runTaskAsynchronously(BlockEdit.getPlugin(), () -> { - SelectionHighlight.sendStop(getPlayer()); - if (selectionPoint1.equals(selectionPoint2)) { - SelectionHighlight.highlightBlock(getPlayer(), selectionPoint1, "#ffffff", "Selection Points", 10000); - } else { - SelectionHighlight.highlightBlock(getPlayer(), selectionPoint1, "#ffffff", "Selection Point 1", 10000); - SelectionHighlight.highlightBlock(getPlayer(), selectionPoint2, "#ffffff", "Selection Point 2", 10000); - } - - }); - - selection = BoundingBox.of(selectionPoint1, selectionPoint2); // bounding boxes are dumb. selection.expand(0.5, 0.5, 0.5); @@ -89,6 +78,7 @@ public class BEPlayer { * @return Clone of selectionPoint1 */ public Location getSelectionPoint1() { + if (selectionPoint1 == null) return null; return selectionPoint1.clone(); } @@ -96,6 +86,7 @@ public class BEPlayer { * @return Clone of selectionPoint2 */ public Location getSelectionPoint2() { + if (selectionPoint2 == null) return null; return selectionPoint2.clone(); } diff --git a/src/main/java/me/youhavetrouble/blockedit/commands/DeselCommand.java b/src/main/java/me/youhavetrouble/blockedit/commands/DeselCommand.java index c4292cd..94edae4 100644 --- a/src/main/java/me/youhavetrouble/blockedit/commands/DeselCommand.java +++ b/src/main/java/me/youhavetrouble/blockedit/commands/DeselCommand.java @@ -1,7 +1,10 @@ package me.youhavetrouble.blockedit.commands; import me.youhavetrouble.blockedit.BEPlayer; +import me.youhavetrouble.blockedit.BlockEdit; +import me.youhavetrouble.blockedit.optionals.SelectionHighlight; import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -15,8 +18,8 @@ public class DeselCommand implements TabExecutor { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { if (!(sender instanceof Player player)) return true; - BEPlayer bePlayer = BEPlayer.getByPlayer(player); - bePlayer.resetSelection(); + BEPlayer.getByPlayer(player).resetSelection(); + Bukkit.getScheduler().runTaskAsynchronously(BlockEdit.getPlugin(),() -> SelectionHighlight.sendStop(player)); player.sendMessage(Component.text("You have reset your selection")); return true; } diff --git a/src/main/java/me/youhavetrouble/blockedit/optionals/SelectionHighlight.java b/src/main/java/me/youhavetrouble/blockedit/optionals/SelectionHighlight.java index 38ab0a9..e4b48f3 100644 --- a/src/main/java/me/youhavetrouble/blockedit/optionals/SelectionHighlight.java +++ b/src/main/java/me/youhavetrouble/blockedit/optionals/SelectionHighlight.java @@ -15,7 +15,7 @@ import java.awt.*; import java.lang.reflect.InvocationTargetException; import java.nio.charset.StandardCharsets; -/** +/* * Highlighting selection blocks thanks to https://github.com/ArtFect/BlockHighlight * * MIT License @@ -43,6 +43,7 @@ import java.nio.charset.StandardCharsets; public class SelectionHighlight { public static void highlightBlock(Player player, Location location, String color, String text, int time) { + if (location == null) return; if (BlockEdit.getPlugin().getServer().getPluginManager().isPluginEnabled("ProtocolLib")) { sendBlockHighlight(player, location, color, text, time); } diff --git a/src/main/java/me/youhavetrouble/blockedit/wands/SelectionWand.java b/src/main/java/me/youhavetrouble/blockedit/wands/SelectionWand.java index 78ed084..4360971 100644 --- a/src/main/java/me/youhavetrouble/blockedit/wands/SelectionWand.java +++ b/src/main/java/me/youhavetrouble/blockedit/wands/SelectionWand.java @@ -1,10 +1,14 @@ package me.youhavetrouble.blockedit.wands; import me.youhavetrouble.blockedit.BEPlayer; +import me.youhavetrouble.blockedit.BlockEdit; import me.youhavetrouble.blockedit.api.BlockEditWand; import me.youhavetrouble.blockedit.api.BlockEditWands; +import me.youhavetrouble.blockedit.optionals.SelectionHighlight; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextDecoration; +import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -48,14 +52,42 @@ public class SelectionWand implements Listener, BlockEditWand { if (action.equals(Action.LEFT_CLICK_BLOCK)) { event.setCancelled(true); BEPlayer.getByPlayer(player).setSelectionPoint1(block.getLocation()); + highlightPoints(player); player.sendMessage(Component.text("First point set")); return; } if (action.equals(Action.RIGHT_CLICK_BLOCK)) { event.setCancelled(true); BEPlayer.getByPlayer(player).setSelectionPoint2(block.getLocation()); + highlightPoints(player); player.sendMessage(Component.text("Second point set")); return; } } + + private void highlightPoints(Player player) { + Bukkit.getScheduler().runTaskAsynchronously(BlockEdit.getPlugin(), () -> { + BEPlayer bePlayer = BEPlayer.getByPlayer(player); + SelectionHighlight.sendStop(player); + + Location selection1 = bePlayer.getSelectionPoint1(); + Location selection2 = bePlayer.getSelectionPoint2(); + + if (selection1 != null && selection1.equals(selection2)) { + SelectionHighlight.highlightBlock(player, bePlayer.getSelectionPoint1(), "#ffffff", "Selection Points", 10000); + return; + } + if (selection1 != null && !selection1.equals(selection2)) { + SelectionHighlight.highlightBlock(player, bePlayer.getSelectionPoint1(), "#ffffff", "Selection Point 1", 10000); + } + if (selection2 != null && !selection2.equals(selection1)) { + SelectionHighlight.highlightBlock(player, bePlayer.getSelectionPoint2(), "#ffffff", "Selection Point 2", 10000); + } + + + + + + }); + } }