|
|
|
@ -22,7 +22,7 @@ package de.steamwar.bausystem.features.simulator2;
|
|
|
|
|
import com.comphenix.tinyprotocol.Reflection;
|
|
|
|
|
import com.comphenix.tinyprotocol.TinyProtocol;
|
|
|
|
|
import de.steamwar.bausystem.BauSystem;
|
|
|
|
|
import de.steamwar.bausystem.SWUtils;
|
|
|
|
|
import de.steamwar.bausystem.features.simulator2.data.Simulator;
|
|
|
|
|
import de.steamwar.bausystem.utils.ItemUtils;
|
|
|
|
|
import de.steamwar.bausystem.utils.RayTraceUtils;
|
|
|
|
|
import de.steamwar.entity.REntity;
|
|
|
|
@ -30,6 +30,8 @@ import de.steamwar.entity.REntityServer;
|
|
|
|
|
import de.steamwar.entity.RFallingBlockEntity;
|
|
|
|
|
import de.steamwar.linkage.Linked;
|
|
|
|
|
import de.steamwar.linkage.api.Plain;
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import lombok.Getter;
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
import org.bukkit.Location;
|
|
|
|
|
import org.bukkit.Material;
|
|
|
|
@ -37,17 +39,13 @@ import org.bukkit.World;
|
|
|
|
|
import org.bukkit.block.BlockFace;
|
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
|
import org.bukkit.event.player.PlayerDropItemEvent;
|
|
|
|
|
import org.bukkit.event.player.PlayerItemHeldEvent;
|
|
|
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
|
|
|
|
import org.bukkit.event.player.PlayerQuitEvent;
|
|
|
|
|
import org.bukkit.event.player.*;
|
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
import org.bukkit.util.Vector;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.function.BiFunction;
|
|
|
|
|
|
|
|
|
|
@Linked
|
|
|
|
@ -58,7 +56,8 @@ public class SimulatorCursor implements Plain, Listener {
|
|
|
|
|
private Class<?> look = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInFlying$PacketPlayInLook");
|
|
|
|
|
private Class<?> positionLook = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInFlying$PacketPlayInPositionLook");
|
|
|
|
|
|
|
|
|
|
private Map<Player, REntityServer> cursors = new HashMap<>();
|
|
|
|
|
private Map<Player, CursorType> cursorType = Collections.synchronizedMap(new HashMap<>());
|
|
|
|
|
private Map<Player, REntityServer> cursors = Collections.synchronizedMap(new HashMap<>());
|
|
|
|
|
|
|
|
|
|
public static boolean isSimulatorItem(ItemStack itemStack) {
|
|
|
|
|
return ItemUtils.isItem(itemStack, "simulator");
|
|
|
|
@ -66,17 +65,7 @@ public class SimulatorCursor implements Plain, Listener {
|
|
|
|
|
|
|
|
|
|
public SimulatorCursor() {
|
|
|
|
|
BiFunction<Player, Object, Object> function = (player, object) -> {
|
|
|
|
|
if (!isSimulatorItem(player.getInventory().getItemInMainHand()) && !isSimulatorItem(player.getInventory().getItemInOffHand())) {
|
|
|
|
|
removeCursor(player);
|
|
|
|
|
return object;
|
|
|
|
|
}
|
|
|
|
|
RayTraceUtils.RRayTraceResult rayTraceResult = RayTraceUtils.traceREntity(player, player.getLocation(), new ArrayList<>());
|
|
|
|
|
if (rayTraceResult == null) {
|
|
|
|
|
removeCursor(player);
|
|
|
|
|
return object;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showCursor(player, rayTraceResult);
|
|
|
|
|
calcCursor(player);
|
|
|
|
|
return object;
|
|
|
|
|
};
|
|
|
|
|
TinyProtocol.instance.addFilter(position, function);
|
|
|
|
@ -86,78 +75,107 @@ public class SimulatorCursor implements Plain, Listener {
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onPlayerJoin(PlayerJoinEvent event) {
|
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
|
if (!isSimulatorItem(player.getInventory().getItemInMainHand()) && !isSimulatorItem(player.getInventory().getItemInOffHand())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
RayTraceUtils.RRayTraceResult rayTraceResult = RayTraceUtils.traceREntity(player, player.getLocation(), new ArrayList<>());
|
|
|
|
|
if (rayTraceResult == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showCursor(player, rayTraceResult);
|
|
|
|
|
calcCursor(event.getPlayer());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onPlayerDropItem(PlayerDropItemEvent event) {
|
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
|
if (!isSimulatorItem(player.getInventory().getItemInMainHand()) && !isSimulatorItem(player.getInventory().getItemInOffHand())) {
|
|
|
|
|
removeCursor(player);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
RayTraceUtils.RRayTraceResult rayTraceResult = RayTraceUtils.traceREntity(player, player.getLocation(), new ArrayList<>());
|
|
|
|
|
if (rayTraceResult == null) {
|
|
|
|
|
removeCursor(player);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showCursor(player, rayTraceResult);
|
|
|
|
|
calcCursor(event.getPlayer());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onPlayerItemHeld(PlayerItemHeldEvent event) {
|
|
|
|
|
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
|
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
|
if (!isSimulatorItem(player.getInventory().getItemInMainHand()) && !isSimulatorItem(player.getInventory().getItemInOffHand())) {
|
|
|
|
|
removeCursor(player);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
RayTraceUtils.RRayTraceResult rayTraceResult = RayTraceUtils.traceREntity(player, player.getLocation(), new ArrayList<>());
|
|
|
|
|
if (rayTraceResult == null) {
|
|
|
|
|
removeCursor(player);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showCursor(player, rayTraceResult);
|
|
|
|
|
calcCursor(event.getPlayer());
|
|
|
|
|
}, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onPlayerQuit(PlayerQuitEvent event) {
|
|
|
|
|
cursorType.remove(event.getPlayer());
|
|
|
|
|
cursors.remove(event.getPlayer());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void removeCursor(Player player) {
|
|
|
|
|
cursors.computeIfPresent(player, (player1, rEntityServer) -> {
|
|
|
|
|
rEntityServer.close();
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
private static final Map<Player, Long> LAST_SNEAKS = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> {
|
|
|
|
|
long millis = System.currentTimeMillis();
|
|
|
|
|
LAST_SNEAKS.entrySet().removeIf(entry -> millis - entry.getValue() > 200);
|
|
|
|
|
}, 1, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showCursor(Player player, RayTraceUtils.RRayTraceResult rayTraceResult) {
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGH)
|
|
|
|
|
public void onPlayerToggleSneak(PlayerToggleSneakEvent event) {
|
|
|
|
|
if (!event.isSneaking()) return;
|
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
|
if (!isSimulatorItem(player.getInventory().getItemInMainHand()) && !isSimulatorItem(player.getInventory().getItemInOffHand())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (LAST_SNEAKS.containsKey(player)) {
|
|
|
|
|
CursorType type = cursorType.getOrDefault(player, CursorType.TNT).switchType();
|
|
|
|
|
cursorType.put(player, type);
|
|
|
|
|
calcCursor(player);
|
|
|
|
|
} else {
|
|
|
|
|
LAST_SNEAKS.put(player, System.currentTimeMillis());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void calcCursor(Player player) {
|
|
|
|
|
if (!isSimulatorItem(player.getInventory().getItemInMainHand()) && !isSimulatorItem(player.getInventory().getItemInOffHand())) {
|
|
|
|
|
removeCursor(player);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Simulator simulator = SimulatorStorage.getSimulator(player);
|
|
|
|
|
List<REntity> entities = SimulatorWatcher.getEntitiesOfSimulator(simulator);
|
|
|
|
|
RayTraceUtils.RRayTraceResult rayTraceResult = RayTraceUtils.traceREntity(player, player.getLocation(), entities);
|
|
|
|
|
if (rayTraceResult == null) {
|
|
|
|
|
removeCursor(player);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showCursor(player, rayTraceResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void removeCursor(Player player) {
|
|
|
|
|
REntityServer entityServer = cursors.get(player);
|
|
|
|
|
if (entityServer != null) {
|
|
|
|
|
entityServer.getEntities().forEach(REntity::die);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private synchronized void showCursor(Player player, RayTraceUtils.RRayTraceResult rayTraceResult) {
|
|
|
|
|
REntityServer entityServer = cursors.computeIfAbsent(player, __ -> {
|
|
|
|
|
REntityServer rEntityServer = new REntityServer();
|
|
|
|
|
rEntityServer.addPlayer(player);
|
|
|
|
|
RFallingBlockEntity rFallingBlockEntity = new RFallingBlockEntity(rEntityServer, new Location(WORLD, 0, 0, 0, 0, 0), Material.GLASS);
|
|
|
|
|
rFallingBlockEntity.setNoGravity(true);
|
|
|
|
|
return rEntityServer;
|
|
|
|
|
});
|
|
|
|
|
entityServer.getEntities().forEach(rEntity -> {
|
|
|
|
|
rEntity.move(getPos(player, rayTraceResult).toLocation(WORLD));
|
|
|
|
|
|
|
|
|
|
CursorType type = cursorType.getOrDefault(player, CursorType.TNT);
|
|
|
|
|
REntity hitEntity = rayTraceResult.getHitEntity();
|
|
|
|
|
Location location = hitEntity != null ? new Vector(hitEntity.getX(), hitEntity.getY(), hitEntity.getZ()).toLocation(WORLD) :
|
|
|
|
|
type.position.apply(player, rayTraceResult).toLocation(WORLD);
|
|
|
|
|
|
|
|
|
|
Material material = hitEntity != null ? Material.GLASS : type.getMaterial();
|
|
|
|
|
List<RFallingBlockEntity> entities = entityServer.getEntitiesByType(RFallingBlockEntity.class);
|
|
|
|
|
entities.removeIf(rFallingBlockEntity -> {
|
|
|
|
|
if (rFallingBlockEntity.getMaterial() != material) {
|
|
|
|
|
rFallingBlockEntity.die();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
rFallingBlockEntity.move(location);
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
if (entities.isEmpty()) {
|
|
|
|
|
RFallingBlockEntity rFallingBlockEntity = new RFallingBlockEntity(entityServer, location, material);
|
|
|
|
|
rFallingBlockEntity.setNoGravity(true);
|
|
|
|
|
if (material == Material.GLASS) {
|
|
|
|
|
rFallingBlockEntity.setGlowing(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector getPos(Player player, RayTraceUtils.RRayTraceResult result) {
|
|
|
|
|
public static Vector getPosTNT(Player player, RayTraceUtils.RRayTraceResult result) {
|
|
|
|
|
Vector pos = result.getHitPosition();
|
|
|
|
|
|
|
|
|
|
BlockFace face = result.getHitBlockFace();
|
|
|
|
@ -196,4 +214,54 @@ public class SimulatorCursor implements Plain, Listener {
|
|
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Vector getPosRedstoneBlock(Player player, RayTraceUtils.RRayTraceResult result) {
|
|
|
|
|
Vector pos = result.getHitPosition();
|
|
|
|
|
|
|
|
|
|
BlockFace face = result.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);
|
|
|
|
|
pos.setY(pos.getBlockY());
|
|
|
|
|
pos.setZ(pos.getBlockZ() + 0.5);
|
|
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
|
public enum CursorType {
|
|
|
|
|
TNT(Material.TNT, SimulatorCursor::getPosTNT),
|
|
|
|
|
REDSTONE_BLOCK(Material.REDSTONE_BLOCK, SimulatorCursor::getPosRedstoneBlock),
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
private Material material;
|
|
|
|
|
private BiFunction<Player, RayTraceUtils.RRayTraceResult, Vector> position;
|
|
|
|
|
|
|
|
|
|
public CursorType switchType() {
|
|
|
|
|
if (this == TNT) {
|
|
|
|
|
return REDSTONE_BLOCK;
|
|
|
|
|
}
|
|
|
|
|
return TNT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|