Add proper onclick handleing

This commit is contained in:
D4rkr34lm
2026-06-01 21:27:34 +02:00
parent a018af1c8a
commit 3810ccd63d
2 changed files with 28 additions and 10 deletions
@@ -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<CursorMode> allowedCursorModes;
private final Material highlightMaterial;
private final TriConsumer<Location, Boolean, Action> onClick;
private final TriConsumer<Location, Optional<REntity>, Action> onClick;
public Cursor(REntityServer targetServer, Player owner, Material highlightMaterial, Material cursorMaterial, List<CursorMode> allowedModes, TriConsumer<Location, Boolean, Action> onClick) {
public Cursor(REntityServer targetServer, Player owner, Material highlightMaterial, Material cursorMaterial, List<CursorMode> allowedModes, TriConsumer<Location, Optional<REntity>, 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();
@@ -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());
});
}
}