From 57e3d40300635d4f2856d62f3d5a0d5161e86248 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 29 Jan 2022 11:55:59 +0100 Subject: [PATCH] NPCs --- .../steamwar/lobby/command/PortalCommand.java | 33 +++++++- src/de/steamwar/lobby/display/NPC.java | 13 +++- .../lobby/portal/FightserverPortal.java | 78 ++++++++++++++----- src/de/steamwar/lobby/portal/Portal.java | 2 +- 4 files changed, 105 insertions(+), 21 deletions(-) diff --git a/src/de/steamwar/lobby/command/PortalCommand.java b/src/de/steamwar/lobby/command/PortalCommand.java index c8c6942..d069d6c 100644 --- a/src/de/steamwar/lobby/command/PortalCommand.java +++ b/src/de/steamwar/lobby/command/PortalCommand.java @@ -15,6 +15,7 @@ import lombok.Data; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.command.CommandSender; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.entity.Player; import java.util.ArrayList; @@ -62,7 +63,7 @@ public class PortalCommand extends SWCommand { if (noPermissions(player)) return; PortalLocations tuple = getSelection(player); if (tuple == null) return; - new Portal(portalName, tuple.k, tuple.v, portal -> new FightserverPortal(portal, gamemode.toLowerCase(), order, target)); + new Portal(portalName, tuple.k, tuple.v, portal -> new FightserverPortal(portal, gamemode.toLowerCase(), order, target, new ArrayList<>(), new ArrayList<>())); } @Register({"create", "teleport"}) @@ -82,6 +83,36 @@ public class PortalCommand extends SWCommand { new Portal(portalName, tuple.k, tuple.v, portal -> new StackPortal(portal, portalDestination, String.join(" ", command))); } + @Register({"addblue"}) + public void portalAddBlue(Player player, Portal portal) { + FightserverPortal handler = (FightserverPortal) portal.getHandler(); + handler.addBlue(locationOfPlayer(player)); + } + + @Register({"addred"}) + public void portalAddRed(Player player, Portal portal) { + FightserverPortal handler = (FightserverPortal) portal.getHandler(); + handler.addRed(locationOfPlayer(player)); + } + + private Location locationOfPlayer(Player player) { + Location l = player.getLocation(); + l.setYaw(((CraftPlayer)player).getHandle().getHeadRotation()); + return l; + } + + @Register({"removeblue"}) + public void portalRemoveBlue(Player player, Portal portal, int i) { + FightserverPortal handler = (FightserverPortal) portal.getHandler(); + handler.removeBlue(i-1); + } + + @Register({"removered"}) + public void portalRemoveRed(Player player, Portal portal, int i) { + FightserverPortal handler = (FightserverPortal) portal.getHandler(); + handler.removeRed(i-1); + } + @Register("remove") public void portalRemove(Player player, Portal portal) { if (noPermissions(player)) return; diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index 5fc53c5..426ca54 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -58,6 +58,10 @@ public class NPC { private static final Reflection.FieldAccessor namedSpawnZ = Reflection.getField(namedSpawnPacket, double.class, 2); private static final Reflection.FieldAccessor namedSpawnYaw = Reflection.getField(namedSpawnPacket, byte.class, 0); private static final Reflection.FieldAccessor namedSpawnPitch = Reflection.getField(namedSpawnPacket, byte.class, 1); + private static final Class headRotationPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityHeadRotation"); + private static final Reflection.ConstructorInvoker headRotationConstructor = Reflection.getConstructor(headRotationPacket); + private static final Reflection.FieldAccessor headRotationEntity = Reflection.getField(headRotationPacket, int.class, 0); + private static final Reflection.FieldAccessor headRotationYaw = Reflection.getField(headRotationPacket, byte.class, 0); private final Displayable display; @@ -68,6 +72,7 @@ public class NPC { private final Object addPlayerInfo; private final Object namedSpawn; + private final Object headRotation; private final Object removePlayerInfo; private final Object destroy; @@ -76,6 +81,7 @@ public class NPC { this.uuid = uuid; this.name = name; this.location = location; + byte yaw = (byte)(int)(location.getYaw() * 256.0 / 360.0); GameProfile profile = new GameProfile(uuid, name); addPlayerInfo = playerInfoPacket(addPlayer, profile); @@ -88,15 +94,20 @@ public class NPC { namedSpawnX.set(namedSpawn, location.getX()); namedSpawnY.set(namedSpawn, location.getY()); namedSpawnZ.set(namedSpawn, location.getZ()); - namedSpawnYaw.set(namedSpawn, (byte)(int)(location.getYaw() * 256.0 / 360.0)); + namedSpawnYaw.set(namedSpawn, yaw); namedSpawnPitch.set(namedSpawn, (byte)(int)(location.getPitch() * 256.0 / 360.0)); + headRotation = headRotationConstructor.invoke(); + headRotationEntity.set(headRotation, entityId); + headRotationYaw.set(headRotation, yaw); + display = new Displayable(location, this::show, this::hide); } private void show(Player player) { TinyProtocol.instance.sendPacket(player, addPlayerInfo); TinyProtocol.instance.sendPacket(player, namedSpawn); + TinyProtocol.instance.sendPacket(player, headRotation); } private void hide(Player player) { diff --git a/src/de/steamwar/lobby/portal/FightserverPortal.java b/src/de/steamwar/lobby/portal/FightserverPortal.java index 97258f2..21e65f5 100644 --- a/src/de/steamwar/lobby/portal/FightserverPortal.java +++ b/src/de/steamwar/lobby/portal/FightserverPortal.java @@ -20,8 +20,11 @@ package de.steamwar.lobby.portal; import de.steamwar.comms.packets.FightInfoPacket; -import de.steamwar.lobby.display.Hologram; import de.steamwar.lobby.Fightserver; +import de.steamwar.lobby.LobbySystem; +import de.steamwar.lobby.display.Hologram; +import de.steamwar.lobby.display.NPC; +import de.steamwar.sql.SteamwarUser; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.util.Vector; @@ -45,29 +48,41 @@ public class FightserverPortal implements PortalHandler, Comparable bluePlayers; + private final List redPlayers; private Fightserver server = null; private PortalHandler handler = new DummyPortal(); - public FightserverPortal(Map section, Portal portal) { - this.portal = portal; - this.gamemode = (String) section.get("group"); - this.target = (String) section.get("target"); - this.order = (int) section.get("order"); - hologram = new Hologram(null, portal.getPos1().clone().add(portal.getOrientation().clone().divide(new Vector(2, 2, 2))), ""); + private final Hologram hologram; + private final List blueNPCs = new ArrayList<>(); + private final List redNPCs = new ArrayList<>(); - init(); + public FightserverPortal(Map section, Portal portal) { + this( + portal, + (String) section.get("group"), + (int) section.get("order"), + (String) section.get("target"), + (List) section.getOrDefault("bluePlayers", new ArrayList<>()), + (List) section.getOrDefault("redPlayers", new ArrayList<>()) + ); } - public FightserverPortal(Portal portal, String gamemode, int order, String target) { + public FightserverPortal(Portal portal, String gamemode, int order, String target, List bluePlayers, List redPlayers) { this.portal = portal; this.gamemode = gamemode; this.order = order; this.target = target; + this.bluePlayers = bluePlayers; + this.redPlayers = redPlayers; hologram = new Hologram(null, portal.getPos1().clone().add(portal.getOrientation().clone().divide(new Vector(2, 2, 2))), ""); - init(); + setServer(null); + + List list = portals.computeIfAbsent(gamemode, mode -> new ArrayList<>()); + list.add(this); + list.sort(null); } public void setServer(Fightserver server) { @@ -93,19 +108,44 @@ public class FightserverPortal implements PortalHandler, Comparable locations, List npcs, List players) { + npcs.forEach(NPC::delete); + npcs.clear(); - List list = portals.computeIfAbsent(gamemode, mode -> new ArrayList<>()); - list.add(this); - list.sort(null); + for(int i = 0; i < players.size(); i++) { + if(i >= locations.size()) + break; + + SteamwarUser user = SteamwarUser.get(players.get(i)); + npcs.add(new NPC(locations.get(i), user.getUUID(), user.getUserName())); + } + } + + public void addBlue(Location location) { + bluePlayers.add(location); + LobbySystem.config().save(); + } + + public void addRed(Location location) { + redPlayers.add(location); + LobbySystem.config().save(); + } + + public void removeBlue(int i) { + bluePlayers.remove(i); + LobbySystem.config().save(); + } + + public void removeRed(int i) { + redPlayers.remove(i); + LobbySystem.config().save(); } private void setHandler(PortalHandler handler) { @@ -123,6 +163,8 @@ public class FightserverPortal implements PortalHandler, Comparable