10 Commits

Author SHA1 Message Date
de4f32feb2 Started on implementing printer tool
All checks were successful
SteamWarCI Build successful
2025-05-20 23:19:32 +02:00
a3d2d2be1e Remove softRef construct, add onPlayerLeave cleanup
All checks were successful
SteamWarCI Build successful
2025-05-08 20:58:16 +02:00
7a000efb93 Fix
All checks were successful
SteamWarCI Build successful
2025-05-08 18:02:56 +02:00
42c9d6d612 Added cleanup of cursor
All checks were successful
SteamWarCI Build successful
2025-05-08 17:56:12 +02:00
67c679a20f Add onclick handler
All checks were successful
SteamWarCI Build successful
2025-05-08 17:33:14 +02:00
a071d8a1f2 Fix jumpy cursor
All checks were successful
SteamWarCI Build successful
2025-05-07 19:49:14 +02:00
7b25bd31fc Fix nullpointer in RCursor
All checks were successful
SteamWarCI Build successful
2025-05-05 22:46:10 +02:00
139a2c275c Fixed bugs, added block align or free render
All checks were successful
SteamWarCI Build successful
2025-04-30 12:57:39 +02:00
aa787b5a25 Implemented cursor manager for deduplicated cursor updating
All checks were successful
SteamWarCI Build successful
2025-04-30 12:17:48 +02:00
54adc12045 Started refactoring simulator cursor for reuse
All checks were successful
SteamWarCI Build successful
2025-04-29 23:25:22 +02:00
3 changed files with 353 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package de.steamwar.bausystem.features.printerTool;
import de.steamwar.bausystem.utils.ItemUtils;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@Linked
public class PrinterToolItem implements Listener {
public static ItemStack getItem(Player p) {
ItemStack itemStack = new SWItem(Material.WOODEN_PICKAXE, "Printer Tool").getItemStack();
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setCustomModelData(1);
itemStack.setItemMeta(itemMeta);
ItemUtils.setItem(itemStack, "printer_tool");
return itemStack;
}
public static boolean isItem(ItemStack itemStack) {
return itemStack != null && ItemUtils.isItem(itemStack, "printer_tool");
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
ItemStack item = event.getItem();
if(PrinterToolItem.isItem(item)) {
}
}
}

View File

@ -0,0 +1,204 @@
package de.steamwar.bausystem.utils.cursor;
import de.steamwar.bausystem.utils.RayTraceUtils;
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.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.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
public class RCursor {
private final World WORLD = Bukkit.getWorlds().get(0);
private final REntityServer targetServer;
private final REntityServer cursorServer;
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;
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);
RCursorManager.getInstance().registerCursor(this);
}
public void render() {
if (!visible) return;
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;
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);
} 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);
}
}
}
public void hide() {
visible = false;
if (cursorEntity != null) {
cursorEntity.die();
cursorEntity = null;
}
}
public void show() {
visible = true;
}
void handleClick(Action action) {
if(!visible || cursorLocation == null) return;
onClickHandler.handle(cursorLocation, isHittingEntity, action);
}
public void close() {
cursorServer.close();
RCursorManager.getInstance().unregisterCursor(this);
}
@AllArgsConstructor
public enum CursorMode {
FREE((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);
}
}
if (!player.isSneaking()) {
pos.setX(pos.getBlockX() + 0.5);
if (face == null || face.getModY() == 0)
pos.setY(pos.getBlockY() + 0.0);
pos.setZ(pos.getBlockZ() + 0.5);
}
return pos;
}),
BLOCK_ALIGNED((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;
});
private final BiFunction<Player, RayTraceUtils.RRayTraceResult, Vector> positionTransform;
}
public interface ClickHandler{
public void handle(Location cursorLocation, boolean didHitEntity, Action action);
}
}

View File

@ -0,0 +1,112 @@
package de.steamwar.bausystem.utils.cursor;
import com.comphenix.tinyprotocol.TinyProtocol;
import de.steamwar.Reflection;
import de.steamwar.bausystem.BauSystem;
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.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
@Linked
public class RCursorManager implements Listener {
@Getter
private static RCursorManager instance;
private final Set<Player> calculationActive = new HashSet<>();
private final Map<Player, RCursor> activeCursors = new HashMap<>();
public RCursorManager() {
if (instance == null) {
instance = this;
}
BiFunction<Player, Object, Object> function = (player, object) -> {
updateCursor(player);
return object;
};
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
Set<Player> playersWithActiveCursor = activeCursors.keySet();
playersWithActiveCursor.forEach(this::updateCursor);
}, 0);
Class<?> positionPacketClass = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundMovePlayerPacket$Pos");
Class<?> lookPacketClass = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundMovePlayerPacket$Rot");
Class<?> positionLookPacketClass = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundMovePlayerPacket$PosRot");
TinyProtocol.instance.addFilter(positionPacketClass, function);
TinyProtocol.instance.addFilter(lookPacketClass, function);
TinyProtocol.instance.addFilter(positionLookPacketClass, function);
}
void registerCursor(RCursor cursor) {
closeCursorOf(cursor.getOwner());
activeCursors.put(cursor.getOwner(), cursor);
}
void unregisterCursor(RCursor cursor) {
activeCursors.remove(cursor.getOwner());
}
private void closeCursorOf(Player player) {
RCursor currentPlayerCursor = activeCursors.get(player);
if(currentPlayerCursor != null) {
currentPlayerCursor.close();
};
}
private void updateCursor(Player player) {
if (!activeCursors.containsKey(player)) {
return;
}
synchronized (calculationActive) {
if (calculationActive.contains(player)) {
return;
} else {
calculationActive.add(player);
}
}
RCursor cursor = activeCursors.get(player);
if (cursor != null) {
cursor.render();
}
synchronized (calculationActive) {
calculationActive.remove(player);
}
}
@EventHandler
private void handlePlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
RCursor cursorOfPlayer = activeCursors.get(player);
cursorOfPlayer.handleClick(event.getAction());
}
@EventHandler
private void handlePlayerQuit(PlayerQuitEvent event) {
closeCursorOf(event.getPlayer());
}
}