forked from SteamWar/SteamWar
Refactor to use Player components for better usability
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
package de.steamwar.cursor;
|
||||
|
||||
import de.steamwar.core.SWPlayer;
|
||||
import de.steamwar.entity.REntity;
|
||||
import de.steamwar.entity.REntityServer;
|
||||
import de.steamwar.entity.RFallingBlockEntity;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.logging.log4j.util.TriConsumer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@Getter
|
||||
public abstract class Cursor implements SWPlayer.Component {
|
||||
private final World WORLD = Bukkit.getWorlds().get(0);
|
||||
|
||||
private final REntityServer targetServer;
|
||||
private final Player owner;
|
||||
|
||||
private final AtomicBoolean isRendering = new AtomicBoolean(false);
|
||||
|
||||
private RFallingBlockEntity cursorEntity;
|
||||
private final REntityServer cursorServer;
|
||||
private Location cursorLocation;
|
||||
private boolean isHittingEntity = false;
|
||||
|
||||
@Setter
|
||||
private Material cursorMaterial;
|
||||
@Setter
|
||||
private List<CursorMode> allowedCursorModes;
|
||||
private final Material highlightMaterial;
|
||||
private final TriConsumer<Location, Boolean, Action> onClick;
|
||||
|
||||
|
||||
|
||||
public Cursor(REntityServer targetServer, Player owner, Material highlightMaterial, Material cursorMaterial, List<CursorMode> allowedModes, TriConsumer<Location, Boolean, Action> onClick) {
|
||||
this.targetServer = targetServer;
|
||||
this.owner = owner;
|
||||
this.highlightMaterial = highlightMaterial;
|
||||
this.cursorMaterial = cursorMaterial;
|
||||
this.onClick = onClick;
|
||||
|
||||
cursorServer = new REntityServer();
|
||||
cursorServer.addPlayer(owner);
|
||||
|
||||
SWPlayer.of(owner).setComponent(this);
|
||||
}
|
||||
|
||||
public void renderDeduplicated() {
|
||||
if(!isRendering.getAndSet(true)) {
|
||||
render();
|
||||
isRendering.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void render() {
|
||||
RayTraceUtils.RRayTraceResult rayTraceResult = RayTraceUtils.traceREntity(owner, owner.getLocation(), targetServer.getEntities());
|
||||
if (rayTraceResult == null) {
|
||||
if (cursorEntity != null)
|
||||
cursorEntity.die();
|
||||
cursorEntity = null;
|
||||
return;
|
||||
}
|
||||
|
||||
REntity hitEntity = rayTraceResult.getHitEntity() == cursorEntity ? null : rayTraceResult.getHitEntity();
|
||||
|
||||
|
||||
Material activeCursorMaterial = hitEntity == null ? cursorMaterial : highlightMaterial;
|
||||
CursorMode activeCursorMode = allowedCursorModes.stream().filter((mode) -> mode.isActive.test(owner)).min(Comparator.comparingInt(a -> a.priority))
|
||||
.orElse(CursorMode.BLOCK_ALIGNED);
|
||||
|
||||
Location activeCursorLocation = hitEntity == null ? activeCursorMode.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);
|
||||
} else if (cursorEntity.getMaterial() == activeCursorMaterial) {
|
||||
cursorEntity.move(activeCursorLocation);
|
||||
} else {
|
||||
cursorEntity.die();
|
||||
cursorEntity = new RFallingBlockEntity(cursorServer, activeCursorLocation, activeCursorMaterial);
|
||||
cursorEntity.setNoGravity(true);
|
||||
if (activeCursorMaterial == highlightMaterial) {
|
||||
cursorEntity.setGlowing(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUnmount(SWPlayer player) {
|
||||
cursorServer.close();
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
public enum CursorMode {
|
||||
FREE(1, (player, rayTraceResult) -> {
|
||||
Vector pos = rayTraceResult.getHitPosition();
|
||||
|
||||
BlockFace face = rayTraceResult.getHitBlockFace();
|
||||
if (face != null) {
|
||||
switch (face) {
|
||||
case DOWN:
|
||||
pos.setY(pos.getY() - 0.98);
|
||||
break;
|
||||
case EAST:
|
||||
pos.setX(pos.getX() + 0.49);
|
||||
break;
|
||||
case WEST:
|
||||
pos.setX(pos.getX() - 0.49);
|
||||
break;
|
||||
case NORTH:
|
||||
pos.setZ(pos.getZ() - 0.49);
|
||||
break;
|
||||
case SOUTH:
|
||||
pos.setZ(pos.getZ() + 0.49);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (face.getModY() == 0 && player.isSneaking()) {
|
||||
pos.setY(pos.getY() - 0.49);
|
||||
}
|
||||
}
|
||||
|
||||
return pos;
|
||||
}, Player::isSneaking),
|
||||
|
||||
BLOCK_ALIGNED(0, (player, rayTraceResult) -> {
|
||||
Vector pos = rayTraceResult.getHitPosition();
|
||||
|
||||
BlockFace face = rayTraceResult.getHitBlockFace();
|
||||
if (face != null) {
|
||||
switch (face) {
|
||||
case DOWN:
|
||||
pos.setY(pos.getY() - 0.98);
|
||||
break;
|
||||
case EAST:
|
||||
pos.setX(pos.getX() + 0.49);
|
||||
break;
|
||||
case WEST:
|
||||
pos.setX(pos.getX() - 0.49);
|
||||
break;
|
||||
case NORTH:
|
||||
pos.setZ(pos.getZ() - 0.49);
|
||||
break;
|
||||
case SOUTH:
|
||||
pos.setZ(pos.getZ() + 0.49);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pos.setX(pos.getBlockX() + 0.5);
|
||||
if (pos.getY() - pos.getBlockY() != 0 && face == BlockFace.UP) {
|
||||
pos.setY(pos.getBlockY() + 1.0);
|
||||
} else {
|
||||
pos.setY(pos.getBlockY());
|
||||
}
|
||||
pos.setZ(pos.getBlockZ() + 0.5);
|
||||
return pos;
|
||||
}, (player) -> true);
|
||||
|
||||
|
||||
private final int priority;
|
||||
private final BiFunction<Player, RayTraceUtils.RRayTraceResult, Vector> positionTransform;
|
||||
private final Predicate<Player> isActive;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package de.steamwar.cursor;
|
||||
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
import de.steamwar.core.SWPlayer;
|
||||
import de.steamwar.linkage.Linked;
|
||||
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.Listener;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
@Linked
|
||||
public class CursorUpdater implements Listener {
|
||||
@Getter
|
||||
private static CursorUpdater instance;
|
||||
|
||||
public CursorUpdater() {
|
||||
if (instance == null) {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
TinyProtocol.instance.addFilter(ServerboundMovePlayerPacket.Pos.class, this::updateCursorFromPacket);
|
||||
TinyProtocol.instance.addFilter(ServerboundMovePlayerPacket.Rot.class, this::updateCursorFromPacket);
|
||||
TinyProtocol.instance.addFilter(ServerboundMovePlayerPacket.PosRot.class, this::updateCursorFromPacket);
|
||||
}
|
||||
|
||||
public Packet<?> updateCursorFromPacket(Player player, Packet<?> packet) {
|
||||
SWPlayer swPlayer = SWPlayer.of(player);
|
||||
Optional<Cursor> activeCursor = swPlayer.getComponent(Cursor.class);
|
||||
|
||||
activeCursor.ifPresent(Cursor::renderDeduplicated);
|
||||
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2026 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.cursor;
|
||||
|
||||
import de.steamwar.entity.REntity;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.FluidCollisionMode;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.RayTraceResult;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@UtilityClass
|
||||
public class RayTraceUtils {
|
||||
|
||||
public static RRayTraceResult traceREntity(Player player, Location to, List<REntity> entityList) {
|
||||
if (player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Location startPos = to.clone().add(0.0, player.getEyeHeight(), 0.0);
|
||||
Vector direction = to.getDirection();
|
||||
RayTraceResult blocks = player.getWorld().rayTraceBlocks(startPos, direction, 10.0, FluidCollisionMode.NEVER, true);
|
||||
|
||||
REntity nearestHitEntity = null;
|
||||
RRayTraceResult nearestHitResult = null;
|
||||
double nearestDistanceSq = Double.MAX_VALUE;
|
||||
for (REntity entity : entityList) {
|
||||
if (!isOccluded(startPos.toVector(), direction, new Vector(entity.getX(), entity.getY() + 0.5, entity.getZ()))) {
|
||||
continue;
|
||||
}
|
||||
double distanceSq = new Vector(entity.getX(), entity.getY() + 0.5, entity.getZ()).distanceSquared(startPos.toVector());
|
||||
if (distanceSq > 100.0) continue;
|
||||
if (distanceSq < nearestDistanceSq) {
|
||||
nearestHitEntity = entity;
|
||||
nearestHitResult = new RRayTraceResult(new Vector(entity.getX(), entity.getY() + 0.5, entity.getZ()), null, null, entity);
|
||||
nearestDistanceSq = distanceSq;
|
||||
}
|
||||
}
|
||||
RRayTraceResult entities = nearestHitEntity == null ? null : new RRayTraceResult(nearestHitResult.getHitPosition(), nearestHitResult.getHitBlock(), nearestHitResult.getHitBlockFace(), nearestHitEntity);
|
||||
|
||||
if (blocks == null) {
|
||||
return entities;
|
||||
} else if (entities == null) {
|
||||
return RRayTraceResult.fromRayTraceResult(blocks);
|
||||
} else {
|
||||
Vector startVec = startPos.toVector();
|
||||
double blockHitDistance = startVec.distance(blocks.getHitPosition());
|
||||
double entityHitDistanceSquared = startVec.distanceSquared(entities.getHitPosition());
|
||||
return entityHitDistanceSquared < blockHitDistance * blockHitDistance ? entities : RRayTraceResult.fromRayTraceResult(blocks);
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class RRayTraceResult {
|
||||
private final Vector hitPosition;
|
||||
private final Block hitBlock;
|
||||
private final BlockFace hitBlockFace;
|
||||
private final REntity hitEntity;
|
||||
|
||||
public static RRayTraceResult fromRayTraceResult(RayTraceResult rayTraceResult) {
|
||||
return new RRayTraceResult(rayTraceResult.getHitPosition(), rayTraceResult.getHitBlock(), rayTraceResult.getHitBlockFace(), null);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isOccluded(Vector a, Vector n, Vector b) {
|
||||
// a = Head pos, n = View direction (normalized), b = entity center
|
||||
// https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Vector_formulation
|
||||
|
||||
double abX = b.getX() - a.getX();
|
||||
double abY = b.getY() - a.getY();
|
||||
double abZ = b.getZ() - a.getZ();
|
||||
double lambda = abX * n.getX() + abY * n.getY() + abZ * n.getZ();
|
||||
double distX = abX - n.getX() * lambda;
|
||||
double distY = abY - n.getY() * lambda;
|
||||
double distZ = abZ - n.getZ() * lambda;
|
||||
return Math.abs(distX) < 0.5 && Math.abs(distY) < 0.5 && Math.abs(distZ) < 0.5;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user