diff --git a/SpigotCore/SpigotCore_Main/src/de/steamwar/cursor/Cursor.java b/SpigotCore/SpigotCore_Main/src/de/steamwar/cursor/Cursor.java index 3fe94054..61450b67 100644 --- a/SpigotCore/SpigotCore_Main/src/de/steamwar/cursor/Cursor.java +++ b/SpigotCore/SpigotCore_Main/src/de/steamwar/cursor/Cursor.java @@ -19,12 +19,13 @@ import org.bukkit.util.Vector; import java.util.Comparator; import java.util.List; +import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.BiFunction; import java.util.function.Predicate; @Getter -public abstract class Cursor implements SWPlayer.Component { +public class Cursor implements SWPlayer.Component { private final World WORLD = Bukkit.getWorlds().get(0); private final REntityServer targetServer; @@ -35,18 +36,17 @@ public abstract class Cursor implements SWPlayer.Component { private RFallingBlockEntity cursorEntity; private final REntityServer cursorServer; private Location cursorLocation; - private boolean isHittingEntity = false; + private REntity hitEntity; @Setter private Material cursorMaterial; @Setter private List allowedCursorModes; private final Material highlightMaterial; - private final TriConsumer onClick; + private final TriConsumer, Action> onClick; - - public Cursor(REntityServer targetServer, Player owner, Material highlightMaterial, Material cursorMaterial, List allowedModes, TriConsumer onClick) { + public Cursor(REntityServer targetServer, Player owner, Material highlightMaterial, Material cursorMaterial, List allowedModes, TriConsumer, Action> onClick) { this.targetServer = targetServer; this.owner = owner; this.highlightMaterial = highlightMaterial; @@ -60,7 +60,7 @@ public abstract class Cursor implements SWPlayer.Component { } public void renderDeduplicated() { - if(!isRendering.getAndSet(true)) { + if (!isRendering.getAndSet(true)) { render(); isRendering.set(false); } @@ -86,7 +86,7 @@ public abstract class Cursor implements SWPlayer.Component { : new Vector(hitEntity.getX(), hitEntity.getY(), hitEntity.getZ()).toLocation(WORLD); cursorLocation = activeCursorLocation; - isHittingEntity = hitEntity != null; + this.hitEntity = hitEntity; if (cursorEntity == null) { cursorEntity = new RFallingBlockEntity(cursorServer, activeCursorLocation, activeCursorMaterial); @@ -103,6 +103,14 @@ public abstract class Cursor implements SWPlayer.Component { } } + protected void handlePlayerClick(Action clickAction) { + renderDeduplicated(); + + if(cursorLocation != null) { + onClick.accept(this.cursorLocation, Optional.ofNullable(this.hitEntity), clickAction); + } + } + @Override public void onUnmount(SWPlayer player) { cursorServer.close(); diff --git a/SpigotCore/SpigotCore_Main/src/de/steamwar/cursor/CursorUpdater.java b/SpigotCore/SpigotCore_Main/src/de/steamwar/cursor/CursorListener.java similarity index 69% rename from SpigotCore/SpigotCore_Main/src/de/steamwar/cursor/CursorUpdater.java rename to SpigotCore/SpigotCore_Main/src/de/steamwar/cursor/CursorListener.java index 2aa05c1f..47f0a91d 100644 --- a/SpigotCore/SpigotCore_Main/src/de/steamwar/cursor/CursorUpdater.java +++ b/SpigotCore/SpigotCore_Main/src/de/steamwar/cursor/CursorListener.java @@ -7,17 +7,19 @@ import lombok.Getter; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; import java.util.*; @Linked -public class CursorUpdater implements Listener { +public class CursorListener implements Listener { @Getter - private static CursorUpdater instance; + private static CursorListener instance; - public CursorUpdater() { + public CursorListener() { if (instance == null) { instance = this; } @@ -35,4 +37,12 @@ public class CursorUpdater implements Listener { return packet; } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + SWPlayer.of(event.getPlayer()).getComponent(Cursor.class).ifPresent(cursor -> { + event.setCancelled(true); + cursor.handlePlayerClick(event.getAction()); + }); + } }