diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/utils/cursor/RCursor.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/utils/cursor/RCursor.java index df884cd7..d4b72f6a 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/utils/cursor/RCursor.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/utils/cursor/RCursor.java @@ -13,8 +13,12 @@ import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.block.Action; import org.bukkit.util.Vector; +import yapion.hierarchy.types.YAPIONType; +import java.util.function.BiConsumer; import java.util.function.BiFunction; @Getter @@ -26,21 +30,28 @@ public class RCursor { private final Player owner; private final Material highlightMaterial; + private final ClickHandler onClickHandler; + + private RFallingBlockEntity cursorEntity; + + private Location cursorLocation; + private boolean isHittingEntity = false; @Setter private Material cursorMaterial; @Setter private CursorMode cursorMode; - private boolean visible = true; - private RFallingBlockEntity cursorEntity; - public RCursor(REntityServer targetServer, Player owner, Material highlightMaterial, Material cursorMaterial, CursorMode cursorMode) { + + + public RCursor(REntityServer targetServer, Player owner, Material highlightMaterial, Material cursorMaterial, CursorMode cursorMode, ClickHandler onClickHandler) { this.targetServer = targetServer; this.owner = owner; this.highlightMaterial = highlightMaterial; this.cursorMaterial = cursorMaterial; this.cursorMode = cursorMode; + this.onClickHandler = onClickHandler; cursorServer = new REntityServer(); cursorServer.addPlayer(owner); @@ -65,6 +76,9 @@ public class RCursor { Location activeCursorLocation = hitEntity == null ? cursorMode.positionTransform.apply(owner, rayTraceResult).toLocation(WORLD) : new Vector(hitEntity.getX(), hitEntity.getY(), hitEntity.getZ()).toLocation(WORLD); + cursorLocation = activeCursorLocation; + isHittingEntity = hitEntity != null; + if (cursorEntity == null) { cursorEntity = new RFallingBlockEntity(cursorServer, activeCursorLocation, activeCursorMaterial); cursorEntity.setNoGravity(true); @@ -92,6 +106,12 @@ public class RCursor { visible = true; } + void handleClick(Action action) { + if(!visible || cursorLocation == null) return; + + onClickHandler.handle(cursorLocation, isHittingEntity, action); + } + @AllArgsConstructor public enum CursorMode { FREE((player, rayTraceResult) -> { @@ -172,4 +192,8 @@ public class RCursor { private final BiFunction positionTransform; } + + public interface ClickHandler{ + public void handle(Location cursorLocation, boolean didHitEntity, Action action); + } } diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/utils/cursor/RCursorManager.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/utils/cursor/RCursorManager.java index b72e00b7..b7052ba3 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/utils/cursor/RCursorManager.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/utils/cursor/RCursorManager.java @@ -7,6 +7,8 @@ import de.steamwar.linkage.Linked; import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerInteractEvent; import java.lang.ref.SoftReference; import java.util.HashMap; @@ -80,4 +82,13 @@ public class RCursorManager { } } + @EventHandler + private void handlePlayerInteract(PlayerInteractEvent event) { + Player player = event.getPlayer(); + RCursor cursorOfPlayer = activeCursors.get(player).get(); + + if(cursorOfPlayer == null) return; + + cursorOfPlayer.handleClick(event.getAction()); + } }