forked from SteamWar/SteamWar
Add proper onclick handleing
This commit is contained in:
@@ -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;
|
||||
@@ -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();
|
||||
|
||||
+13
-3
@@ -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());
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user