From 9891dcc21476002991f5d9140a74e5643f9cbb74 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 25 Mar 2022 22:18:04 +0100 Subject: [PATCH 01/14] Add initial TeamPlayer --- src/de/steamwar/lobby/LobbySystem.java | 6 + .../steamwar/lobby/display/Displayable.java | 22 +++- src/de/steamwar/lobby/display/NPC.java | 31 ++++- src/de/steamwar/lobby/team/TeamPlayer.java | 117 ++++++++++++++++++ 4 files changed, 169 insertions(+), 7 deletions(-) create mode 100644 src/de/steamwar/lobby/team/TeamPlayer.java diff --git a/src/de/steamwar/lobby/LobbySystem.java b/src/de/steamwar/lobby/LobbySystem.java index 977188a..0655238 100644 --- a/src/de/steamwar/lobby/LobbySystem.java +++ b/src/de/steamwar/lobby/LobbySystem.java @@ -24,6 +24,7 @@ import de.steamwar.lobby.command.FlyCommand; import de.steamwar.lobby.command.HologramCommand; import de.steamwar.lobby.command.PortalCommand; import de.steamwar.lobby.listener.*; +import de.steamwar.lobby.team.TeamPlayer; import de.steamwar.message.Message; import org.bukkit.plugin.java.JavaPlugin; @@ -56,6 +57,7 @@ public class LobbySystem extends JavaPlugin { new ParticleListener(); new InventoryInteraction(); new WorldInteraction(); + new TeamPlayer(); new AlphaWall(l -> l.getX() > 1199, AlphaWall.REFLECT_X); new AlphaWall(l -> l.getX() < 2977, AlphaWall.REFLECT_X); @@ -64,6 +66,10 @@ public class LobbySystem extends JavaPlugin { } + @Override + public void onDisable() { + TeamPlayer.cleanup(); + } public static LobbySystem getPlugin() { return plugin; diff --git a/src/de/steamwar/lobby/display/Displayable.java b/src/de/steamwar/lobby/display/Displayable.java index 0d7b272..327b83d 100644 --- a/src/de/steamwar/lobby/display/Displayable.java +++ b/src/de/steamwar/lobby/display/Displayable.java @@ -38,24 +38,34 @@ public class Displayable extends BasicListener { private final Set visible = new HashSet<>(); - private final int chunkX; - private final int chunkZ; + private int chunkX; + private int chunkZ; private final Consumer show; private final Consumer hide; + private final Consumer move; private final Function playerFilter; - public Displayable(Location location, Consumer show, Consumer hide) { - this(location, show, hide, player -> true); + public Displayable(Location location, Consumer show, Consumer hide, Consumer move) { + this(location, show, hide, move, player -> true); } public Displayable(Location location, Consumer show, Consumer hide, Function playerFilter) { - this.chunkX = posToChunk(location.getX()); - this.chunkZ = posToChunk(location.getZ()); + this(location, show, hide, player -> {}, playerFilter); + } + + public Displayable(Location location, Consumer show, Consumer hide, Consumer move, Function playerFilter) { this.show = show; this.hide = hide; + this.move = move; this.playerFilter = playerFilter; + setLocation(location); + } + public void setLocation(Location location) { + chunkX = posToChunk(location.getX()); + chunkZ = posToChunk(location.getZ()); Bukkit.getOnlinePlayers().forEach(this::checkLocation); + visible.forEach(move); } public Set getVisitors() { diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index 4a56d02..e24c3f2 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -62,6 +62,15 @@ public class NPC { 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 static final Class movePacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityTeleport"); + private static final Reflection.ConstructorInvoker movePacketConstructor = Reflection.getConstructor(movePacket); + private static final Reflection.FieldAccessor movePacketEntity = Reflection.getField(movePacket, int.class, 0); + private static final Reflection.FieldAccessor movePacketX = Reflection.getField(movePacket, double.class, 0); + private static final Reflection.FieldAccessor movePacketY = Reflection.getField(movePacket, double.class, 1); + private static final Reflection.FieldAccessor movePacketZ = Reflection.getField(movePacket, double.class, 2); + private static final Reflection.FieldAccessor movePacketYaw = Reflection.getField(movePacket, byte.class, 0); + private static final Reflection.FieldAccessor movePacketPitch = Reflection.getField(movePacket, byte.class, 1); + private static final Reflection.FieldAccessor movePacketOnGround = Reflection.getField(movePacket, boolean.class, 0); private final Displayable display; @@ -75,6 +84,7 @@ public class NPC { private final Object headRotation; private final Object removePlayerInfo; private final Object destroy; + private Object move; public NPC(Location location, UUID uuid, String name) { this.entityId = Hologram.createEntityId(); @@ -101,7 +111,21 @@ public class NPC { headRotationEntity.set(headRotation, entityId); headRotationYaw.set(headRotation, yaw); - display = new Displayable(location, this::show, this::hide); + display = new Displayable(location, this::show, this::hide, this::move); + } + + public void setLocation(Location location) { + byte yaw = (byte)(int)(location.getYaw() * 256.0 / 360.0); + headRotationYaw.set(headRotation, yaw); + move = movePacketConstructor.invoke(); + movePacketEntity.set(move, entityId); + movePacketX.set(move, location.getX()); + movePacketY.set(move, location.getY()); + movePacketZ.set(move, location.getZ()); + movePacketYaw.set(move, yaw); + movePacketPitch.set(move, (byte)(int)(location.getPitch() * 256.0 / 360.0)); + movePacketOnGround.set(move, true); + display.setLocation(location); } public Location getLocation() { @@ -123,6 +147,11 @@ public class NPC { TinyProtocol.instance.sendPacket(player, destroy); } + private void move(Player player) { + TinyProtocol.instance.sendPacket(player, headRotation); + TinyProtocol.instance.sendPacket(player, move); + } + public void delete() { display.delete(); } diff --git a/src/de/steamwar/lobby/team/TeamPlayer.java b/src/de/steamwar/lobby/team/TeamPlayer.java new file mode 100644 index 0000000..047c69a --- /dev/null +++ b/src/de/steamwar/lobby/team/TeamPlayer.java @@ -0,0 +1,117 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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 . + */ + +package de.steamwar.lobby.team; + +import de.steamwar.lobby.LobbySystem; +import de.steamwar.lobby.display.NPC; +import de.steamwar.lobby.listener.BasicListener; +import de.steamwar.sql.SteamwarUser; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Villager; +import org.bukkit.event.EventHandler; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityInteractEvent; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +import java.util.HashMap; +import java.util.Map; + +public class TeamPlayer extends BasicListener { + + private static Map entities = new HashMap<>(); + + public static void spawnTeamPlayer(World world, String name) { + Location location = new Location(world, 1524.5, 52, 1493.5); + NPC npc = new NPC(location, SteamwarUser.get(name).getUUID(), name); + Villager villager = (Villager) world.spawnEntity(location, EntityType.VILLAGER); + villager.setSilent(true); + villager.setInvulnerable(true); + villager.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1, false, false, false)); + villager.setCustomName(name); + villager.setProfession(Villager.Profession.NITWIT); + entities.put(villager, npc); + } + + public static void cleanup() { + entities.forEach((entity, npc) -> entity.remove()); + } + + { + World world = Bukkit.getWorld("Lobby"); + for (int x = -10; x < 10; x++) { + for (int z = -10; z < 10; z++) { + world.setChunkForceLoaded(95 + x , 93 + z, true); + } + } + world.getEntitiesByClasses(Villager.class).forEach(Entity::remove); + for (int x = -10; x < 10; x++) { + for (int z = -10; z < 10; z++) { + world.setChunkForceLoaded(95 + x , 93 + z, true); + } + } + spawnTeamPlayer(world, "AdmiralSeekrank"); + spawnTeamPlayer(world, "SalzgehaltSensei"); + spawnTeamPlayer(world, "Sehfxhler"); + spawnTeamPlayer(world, "Tim7077"); + spawnTeamPlayer(world, "LordMainex"); + spawnTeamPlayer(world, "Lixi266"); + spawnTeamPlayer(world, "PxlPain"); + spawnTeamPlayer(world, "KopFilme"); + spawnTeamPlayer(world, "Lixfel"); + spawnTeamPlayer(world, "Chaoscaot"); + spawnTeamPlayer(world, "YoyoNow"); + spawnTeamPlayer(world, "Zeanon"); + spawnTeamPlayer(world, "zOnlyKroks"); + spawnTeamPlayer(world, "Legula"); + spawnTeamPlayer(world, "xJoul"); + spawnTeamPlayer(world, "Maybe_Creative"); + spawnTeamPlayer(world, "Tim16227"); + spawnTeamPlayer(world, "FoehnX"); + spawnTeamPlayer(world, "_Noerf_"); + spawnTeamPlayer(world, "Edmund_Strong"); + spawnTeamPlayer(world, "TheBreadBeard"); + spawnTeamPlayer(world, "Basic_Redstone"); + + Bukkit.getScheduler().runTaskTimer(LobbySystem.getPlugin(), () -> { + entities.forEach((entity, npc) -> { + npc.setLocation(entity.getLocation()); + }); + }, 1L, 1L); + } + + @EventHandler + public void onEntityInteract(EntityInteractEvent event) { + if (event.getEntityType() == EntityType.VILLAGER) { + event.setCancelled(true); + } + } + + @EventHandler + public void onEntityDamage(EntityDamageEvent event) { + if (event.getEntityType() == EntityType.VILLAGER) { + event.setCancelled(true); + } + } +} From 5ae38b6d3e91ddb2dc28b915f0734f3669ba7429 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 25 Mar 2022 22:48:30 +0100 Subject: [PATCH 02/14] Fix NPC.hide --- src/de/steamwar/lobby/display/NPC.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index e24c3f2..8e0e6e5 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -22,6 +22,8 @@ package de.steamwar.lobby.display; import com.comphenix.tinyprotocol.Reflection; import com.comphenix.tinyprotocol.TinyProtocol; import com.mojang.authlib.GameProfile; +import de.steamwar.lobby.LobbySystem; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -143,7 +145,9 @@ public class NPC { } private void hide(Player player) { - TinyProtocol.instance.sendPacket(player, removePlayerInfo); + if (Bukkit.getOnlinePlayers().stream().noneMatch(p -> p.getUniqueId().equals(uuid))) { + TinyProtocol.instance.sendPacket(player, removePlayerInfo); + } TinyProtocol.instance.sendPacket(player, destroy); } From 1cca911153c1d9c2afa45082ad63d9753ef6b33b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 25 Mar 2022 23:11:35 +0100 Subject: [PATCH 03/14] Implement skinparts (not working) --- src/de/steamwar/lobby/display/NPC.java | 42 +++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index 8e0e6e5..9907f16 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -22,11 +22,12 @@ package de.steamwar.lobby.display; import com.comphenix.tinyprotocol.Reflection; import com.comphenix.tinyprotocol.TinyProtocol; import com.mojang.authlib.GameProfile; -import de.steamwar.lobby.LobbySystem; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; import java.util.Collections; import java.util.List; import java.util.UUID; @@ -74,6 +75,41 @@ public class NPC { private static final Reflection.FieldAccessor movePacketPitch = Reflection.getField(movePacket, byte.class, 1); private static final Reflection.FieldAccessor movePacketOnGround = Reflection.getField(movePacket, boolean.class, 0); + private static final Class dataWatcherObject = Reflection.getClass("{nms.network.syncher}.DataWatcherObject"); + private static final Class dataWatcherRegistry = Reflection.getClass("{nms.network.syncher}.DataWatcherRegistry"); + private static final Class dataWatcherSerializer = Reflection.getClass("{nms.network.syncher}.DataWatcherSerializer"); + private static final Reflection.ConstructorInvoker dataWatcherObjectConstructor = Reflection.getConstructor(dataWatcherObject, int.class, dataWatcherSerializer); + private static Object getDataWatcherObject(int index, Class type) { + for(Field field : dataWatcherRegistry.getFields()) { + if(dataWatcherSerializer.isAssignableFrom(field.getType()) && type.equals(((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0])) { + try { + return dataWatcherObjectConstructor.invoke(index, field.get(null)); + } catch (IllegalAccessException e) { + throw new SecurityException("Could not get field", e); + } + } + } + throw new SecurityException("Could not find Serializer for " + type.getName()); + } + + private static final Class item = Reflection.getClass("{nms.network.syncher}.DataWatcher$Item"); + private static final Reflection.ConstructorInvoker itemConstructor = Reflection.getConstructor(item, dataWatcherObject, Object.class); + private static Object getDataWatcherItem(Object dwo, Object value) { + return itemConstructor.invoke(dwo, value); + } + + private static final Class metadataPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityMetadata"); + private static final Reflection.FieldAccessor metadataEntity = Reflection.getField(metadataPacket, int.class, 0); + private static final Reflection.FieldAccessor metadataMetadata = Reflection.getField(metadataPacket, List.class, 0); + private Object getDataWatcherPacket(Object dataWatcherObject, Object value) { + Object packet = Reflection.newInstance(metadataPacket); + metadataEntity.set(packet, entityId); + metadataMetadata.set(packet, Collections.singletonList(getDataWatcherItem(dataWatcherObject, value))); + return packet; + } + + private static Object skinPartsDataWatcherObject = getDataWatcherObject(17, Byte.class); + private final Displayable display; private final int entityId; @@ -83,6 +119,7 @@ public class NPC { private final Object addPlayerInfo; private final Object namedSpawn; + private final Object skinParts; private final Object headRotation; private final Object removePlayerInfo; private final Object destroy; @@ -100,6 +137,8 @@ public class NPC { removePlayerInfo = playerInfoPacket(removePlayer, profile); destroy = Hologram.destroyPacket(entityId); + skinParts = getDataWatcherPacket(skinPartsDataWatcherObject, (byte) 0xFF); + namedSpawn = namedSpawnConstructor.invoke(); namedSpawnEntity.set(namedSpawn, entityId); namedSpawnUUID.set(namedSpawn, uuid); @@ -142,6 +181,7 @@ public class NPC { TinyProtocol.instance.sendPacket(player, addPlayerInfo); TinyProtocol.instance.sendPacket(player, namedSpawn); TinyProtocol.instance.sendPacket(player, headRotation); + TinyProtocol.instance.sendPacket(player, skinParts); } private void hide(Player player) { From deac3a2b40a0270bf4e70508a34869470cf79aa7 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 08:30:39 +0100 Subject: [PATCH 04/14] Fix NPC --- src/de/steamwar/lobby/display/NPC.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index 9907f16..a3b9de8 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -137,7 +137,7 @@ public class NPC { removePlayerInfo = playerInfoPacket(removePlayer, profile); destroy = Hologram.destroyPacket(entityId); - skinParts = getDataWatcherPacket(skinPartsDataWatcherObject, (byte) 0xFF); + skinParts = getDataWatcherPacket(skinPartsDataWatcherObject, (byte) 0x7F); namedSpawn = namedSpawnConstructor.invoke(); namedSpawnEntity.set(namedSpawn, entityId); From 4ac8aebaa73017095f6a764a3e252c75da13030f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 11:38:22 +0100 Subject: [PATCH 05/14] Fix NPC Fix TeamPlayer spawnlocation --- src/de/steamwar/lobby/display/NPC.java | 4 +++- src/de/steamwar/lobby/team/TeamPlayer.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index a3b9de8..6c0b563 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -178,7 +178,9 @@ public class NPC { } private void show(Player player) { - TinyProtocol.instance.sendPacket(player, addPlayerInfo); + if (Bukkit.getOnlinePlayers().stream().noneMatch(p -> p.getUniqueId().equals(uuid))) { + TinyProtocol.instance.sendPacket(player, addPlayerInfo); + } TinyProtocol.instance.sendPacket(player, namedSpawn); TinyProtocol.instance.sendPacket(player, headRotation); TinyProtocol.instance.sendPacket(player, skinParts); diff --git a/src/de/steamwar/lobby/team/TeamPlayer.java b/src/de/steamwar/lobby/team/TeamPlayer.java index 047c69a..3b37196 100644 --- a/src/de/steamwar/lobby/team/TeamPlayer.java +++ b/src/de/steamwar/lobby/team/TeamPlayer.java @@ -43,7 +43,7 @@ public class TeamPlayer extends BasicListener { private static Map entities = new HashMap<>(); public static void spawnTeamPlayer(World world, String name) { - Location location = new Location(world, 1524.5, 52, 1493.5); + Location location = new Location(world, 1524.5, 52, 1481.5); NPC npc = new NPC(location, SteamwarUser.get(name).getUUID(), name); Villager villager = (Villager) world.spawnEntity(location, EntityType.VILLAGER); villager.setSilent(true); From 55b1a26b6a3d030db72b1fd8e4305d5cfabfcf93 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 12:02:03 +0100 Subject: [PATCH 06/14] Update TeamPlayer --- src/de/steamwar/lobby/team/TeamPlayer.java | 48 +++++++--------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/src/de/steamwar/lobby/team/TeamPlayer.java b/src/de/steamwar/lobby/team/TeamPlayer.java index 3b37196..cad19f6 100644 --- a/src/de/steamwar/lobby/team/TeamPlayer.java +++ b/src/de/steamwar/lobby/team/TeamPlayer.java @@ -58,41 +58,23 @@ public class TeamPlayer extends BasicListener { entities.forEach((entity, npc) -> entity.remove()); } + private void forceLoad(World world, boolean setTo) { + for (int x = -10; x < 10; x++) { + for (int z = -10; z < 10; z++) { + world.setChunkForceLoaded(95 + x , 93 + z, setTo); + } + } + } + { - World world = Bukkit.getWorld("Lobby"); - for (int x = -10; x < 10; x++) { - for (int z = -10; z < 10; z++) { - world.setChunkForceLoaded(95 + x , 93 + z, true); - } - } + World world = Bukkit.getWorlds().get(0); + forceLoad(world, true); world.getEntitiesByClasses(Villager.class).forEach(Entity::remove); - for (int x = -10; x < 10; x++) { - for (int z = -10; z < 10; z++) { - world.setChunkForceLoaded(95 + x , 93 + z, true); - } - } - spawnTeamPlayer(world, "AdmiralSeekrank"); - spawnTeamPlayer(world, "SalzgehaltSensei"); - spawnTeamPlayer(world, "Sehfxhler"); - spawnTeamPlayer(world, "Tim7077"); - spawnTeamPlayer(world, "LordMainex"); - spawnTeamPlayer(world, "Lixi266"); - spawnTeamPlayer(world, "PxlPain"); - spawnTeamPlayer(world, "KopFilme"); - spawnTeamPlayer(world, "Lixfel"); - spawnTeamPlayer(world, "Chaoscaot"); - spawnTeamPlayer(world, "YoyoNow"); - spawnTeamPlayer(world, "Zeanon"); - spawnTeamPlayer(world, "zOnlyKroks"); - spawnTeamPlayer(world, "Legula"); - spawnTeamPlayer(world, "xJoul"); - spawnTeamPlayer(world, "Maybe_Creative"); - spawnTeamPlayer(world, "Tim16227"); - spawnTeamPlayer(world, "FoehnX"); - spawnTeamPlayer(world, "_Noerf_"); - spawnTeamPlayer(world, "Edmund_Strong"); - spawnTeamPlayer(world, "TheBreadBeard"); - spawnTeamPlayer(world, "Basic_Redstone"); + forceLoad(world, false); + + SteamwarUser.getServerTeam().forEach(user -> { + spawnTeamPlayer(world, user.getUserName()); + }); Bukkit.getScheduler().runTaskTimer(LobbySystem.getPlugin(), () -> { entities.forEach((entity, npc) -> { From 14c3be143df5778899ea24cca28772ca9be00934 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 13:33:44 +0100 Subject: [PATCH 07/14] Fix skin overlays --- src/de/steamwar/lobby/display/NPC.java | 14 ++++++---- src/de/steamwar/lobby/team/TeamPlayer.java | 30 ++++++++++++++-------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index 6c0b563..39c7dc1 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -22,8 +22,13 @@ package de.steamwar.lobby.display; import com.comphenix.tinyprotocol.Reflection; import com.comphenix.tinyprotocol.TinyProtocol; import com.mojang.authlib.GameProfile; +import net.minecraft.server.v1_15_R1.DataWatcher; +import net.minecraft.server.v1_15_R1.DataWatcherRegistry; +import net.minecraft.server.v1_15_R1.EntityHuman; +import net.minecraft.server.v1_15_R1.PacketPlayOutEntityMetadata; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.entity.Player; import java.lang.reflect.Field; @@ -108,14 +113,14 @@ public class NPC { return packet; } - private static Object skinPartsDataWatcherObject = getDataWatcherObject(17, Byte.class); + private static Object skinPartsDataWatcherObject = getDataWatcherObject(16, Byte.class); private final Displayable display; private final int entityId; private final UUID uuid; private final String name; - private final Location location; + private Location location; private final Object addPlayerInfo; private final Object namedSpawn; @@ -156,6 +161,7 @@ public class NPC { } public void setLocation(Location location) { + this.location = location; byte yaw = (byte)(int)(location.getYaw() * 256.0 / 360.0); headRotationYaw.set(headRotation, yaw); move = movePacketConstructor.invoke(); @@ -178,9 +184,7 @@ public class NPC { } private void show(Player player) { - if (Bukkit.getOnlinePlayers().stream().noneMatch(p -> p.getUniqueId().equals(uuid))) { - TinyProtocol.instance.sendPacket(player, addPlayerInfo); - } + TinyProtocol.instance.sendPacket(player, addPlayerInfo); TinyProtocol.instance.sendPacket(player, namedSpawn); TinyProtocol.instance.sendPacket(player, headRotation); TinyProtocol.instance.sendPacket(player, skinParts); diff --git a/src/de/steamwar/lobby/team/TeamPlayer.java b/src/de/steamwar/lobby/team/TeamPlayer.java index cad19f6..b45ed6e 100644 --- a/src/de/steamwar/lobby/team/TeamPlayer.java +++ b/src/de/steamwar/lobby/team/TeamPlayer.java @@ -37,28 +37,33 @@ import org.bukkit.potion.PotionEffectType; import java.util.HashMap; import java.util.Map; +import java.util.logging.Level; public class TeamPlayer extends BasicListener { - private static Map entities = new HashMap<>(); + private static final World world = Bukkit.getWorlds().get(0); + private static final Map entities = new HashMap<>(); - public static void spawnTeamPlayer(World world, String name) { - Location location = new Location(world, 1524.5, 52, 1481.5); - NPC npc = new NPC(location, SteamwarUser.get(name).getUUID(), name); + public static void spawnTeamPlayer(World world, SteamwarUser steamwarUser) { + Location location = new Location(world, 1524.5, 52, 1484.5); + String name = steamwarUser.getUserName(); + NPC npc = new NPC(location, steamwarUser.getUUID(), name); Villager villager = (Villager) world.spawnEntity(location, EntityType.VILLAGER); villager.setSilent(true); villager.setInvulnerable(true); villager.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1, false, false, false)); villager.setCustomName(name); villager.setProfession(Villager.Profession.NITWIT); - entities.put(villager, npc); + entities.put(name, npc); } public static void cleanup() { - entities.forEach((entity, npc) -> entity.remove()); + forceLoad(world, true); + world.getEntitiesByClasses(Villager.class).forEach(Entity::remove); + forceLoad(world, false); } - private void forceLoad(World world, boolean setTo) { + private static void forceLoad(World world, boolean setTo) { for (int x = -10; x < 10; x++) { for (int z = -10; z < 10; z++) { world.setChunkForceLoaded(95 + x , 93 + z, setTo); @@ -67,18 +72,21 @@ public class TeamPlayer extends BasicListener { } { - World world = Bukkit.getWorlds().get(0); forceLoad(world, true); world.getEntitiesByClasses(Villager.class).forEach(Entity::remove); forceLoad(world, false); SteamwarUser.getServerTeam().forEach(user -> { - spawnTeamPlayer(world, user.getUserName()); + spawnTeamPlayer(world, user); }); + LobbySystem.getPlugin().getLogger().log(Level.INFO, "Loaded " + entities.size() + " team players"); Bukkit.getScheduler().runTaskTimer(LobbySystem.getPlugin(), () -> { - entities.forEach((entity, npc) -> { - npc.setLocation(entity.getLocation()); + world.getEntitiesByClasses(Villager.class).forEach(entity -> { + NPC npc = entities.get(entity.getName()); + if (npc != null) { + npc.setLocation(entity.getLocation()); + } }); }, 1L, 1L); } From be861b6bbf0cd8fb5f881670149c73b3dffef6a2 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 13:57:30 +0100 Subject: [PATCH 08/14] Fix packet stuff --- src/de/steamwar/lobby/display/NPC.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index 39c7dc1..58b49ee 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -161,6 +161,9 @@ public class NPC { } public void setLocation(Location location) { + if (isSimilarLocation(location)) { + return; + } this.location = location; byte yaw = (byte)(int)(location.getYaw() * 256.0 / 360.0); headRotationYaw.set(headRotation, yaw); @@ -175,6 +178,25 @@ public class NPC { display.setLocation(location); } + private boolean isSimilarLocation(Location location) { + if (Location.normalizeYaw(this.location.getYaw()) != Location.normalizeYaw(location.getYaw())) { + return false; + } + if (Location.normalizePitch(this.location.getPitch()) != Location.normalizePitch(location.getPitch())) { + return false; + } + if (Math.abs(this.location.getX() - location.getX()) > 0.1) { + return false; + } + if (Math.abs(this.location.getY() - location.getY()) > 0.1) { + return false; + } + if (Math.abs(this.location.getZ() - location.getZ()) > 0.1) { + return false; + } + return true; + } + public Location getLocation() { return location; } From c6beebed05c17484e43d16026ca4437735464038 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 14:38:39 +0100 Subject: [PATCH 09/14] Fix NPE --- src/de/steamwar/lobby/display/NPC.java | 4 ++- src/de/steamwar/lobby/team/TeamPlayer.java | 41 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index 58b49ee..2040d5f 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -221,7 +221,9 @@ public class NPC { private void move(Player player) { TinyProtocol.instance.sendPacket(player, headRotation); - TinyProtocol.instance.sendPacket(player, move); + if (move != null) { + TinyProtocol.instance.sendPacket(player, move); + } } public void delete() { diff --git a/src/de/steamwar/lobby/team/TeamPlayer.java b/src/de/steamwar/lobby/team/TeamPlayer.java index b45ed6e..41fefb1 100644 --- a/src/de/steamwar/lobby/team/TeamPlayer.java +++ b/src/de/steamwar/lobby/team/TeamPlayer.java @@ -23,6 +23,7 @@ import de.steamwar.lobby.LobbySystem; import de.steamwar.lobby.display.NPC; import de.steamwar.lobby.listener.BasicListener; import de.steamwar.sql.SteamwarUser; +import lombok.AllArgsConstructor; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; @@ -35,12 +36,37 @@ import org.bukkit.event.entity.EntityInteractEvent; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.logging.Level; public class TeamPlayer extends BasicListener { + @AllArgsConstructor + private static class Cuboid { + private double x1; + private double y1; + private double z1; + private double x2; + private double y2; + private double z2; + + public boolean contains(Location location) { + return location.getX() >= x1 && location.getX() <= x2 && location.getY() >= y1 && location.getY() <= y2 && location.getZ() >= z1 && location.getZ() <= z2; + } + } + + private static final List cuboids = new ArrayList<>(); + static { + cuboids.add(new Cuboid(1509, 52, 1464, 1510, 58, 1469)); + cuboids.add(new Cuboid(1538, 52, 1464, 1539, 58, 1469)); + cuboids.add(new Cuboid(1518, 55, 1433, 1530, 60, 1434)); + cuboids.add(new Cuboid(1587, 52, 1471, 1588, 56, 1475)); + cuboids.add(new Cuboid(1479, 52, 1461, 1478, 56, 1463)); + } + private static final World world = Bukkit.getWorlds().get(0); private static final Map entities = new HashMap<>(); @@ -53,7 +79,7 @@ public class TeamPlayer extends BasicListener { villager.setInvulnerable(true); villager.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1, false, false, false)); villager.setCustomName(name); - villager.setProfession(Villager.Profession.NITWIT); + villager.setProfession(Villager.Profession.NONE); entities.put(name, npc); } @@ -85,12 +111,25 @@ public class TeamPlayer extends BasicListener { world.getEntitiesByClasses(Villager.class).forEach(entity -> { NPC npc = entities.get(entity.getName()); if (npc != null) { + if (illegalLocation(entity.getLocation())) { + entity.teleport(npc.getLocation()); + return; + } npc.setLocation(entity.getLocation()); } }); }, 1L, 1L); } + private boolean illegalLocation(Location location) { + for (Cuboid cuboid : cuboids) { + if (cuboid.contains(location)) { + return true; + } + } + return false; + } + @EventHandler public void onEntityInteract(EntityInteractEvent event) { if (event.getEntityType() == EntityType.VILLAGER) { From 1de921b7ffb7f12347f2302afd98e2f579eea28b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 15:40:51 +0100 Subject: [PATCH 10/14] Update TeamPlayer on DB update ever hour --- src/de/steamwar/lobby/display/NPC.java | 3 ++ src/de/steamwar/lobby/team/TeamPlayer.java | 37 ++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index 2040d5f..de0abfc 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -172,6 +172,9 @@ public class NPC { movePacketX.set(move, location.getX()); movePacketY.set(move, location.getY()); movePacketZ.set(move, location.getZ()); + namedSpawnX.set(namedSpawn, location.getX()); + namedSpawnY.set(namedSpawn, location.getY()); + namedSpawnZ.set(namedSpawn, location.getZ()); movePacketYaw.set(move, yaw); movePacketPitch.set(move, (byte)(int)(location.getPitch() * 256.0 / 360.0)); movePacketOnGround.set(move, true); diff --git a/src/de/steamwar/lobby/team/TeamPlayer.java b/src/de/steamwar/lobby/team/TeamPlayer.java index 41fefb1..56fbe46 100644 --- a/src/de/steamwar/lobby/team/TeamPlayer.java +++ b/src/de/steamwar/lobby/team/TeamPlayer.java @@ -36,11 +36,10 @@ import org.bukkit.event.entity.EntityInteractEvent; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; +import java.util.stream.Collectors; public class TeamPlayer extends BasicListener { @@ -59,12 +58,13 @@ public class TeamPlayer extends BasicListener { } private static final List cuboids = new ArrayList<>(); + static { cuboids.add(new Cuboid(1509, 52, 1464, 1510, 58, 1469)); cuboids.add(new Cuboid(1538, 52, 1464, 1539, 58, 1469)); cuboids.add(new Cuboid(1518, 55, 1433, 1530, 60, 1434)); cuboids.add(new Cuboid(1587, 52, 1471, 1588, 56, 1475)); - cuboids.add(new Cuboid(1479, 52, 1461, 1478, 56, 1463)); + cuboids.add(new Cuboid(1478, 52, 1461, 1479, 56, 1463)); } private static final World world = Bukkit.getWorlds().get(0); @@ -92,7 +92,7 @@ public class TeamPlayer extends BasicListener { private static void forceLoad(World world, boolean setTo) { for (int x = -10; x < 10; x++) { for (int z = -10; z < 10; z++) { - world.setChunkForceLoaded(95 + x , 93 + z, setTo); + world.setChunkForceLoaded(95 + x, 93 + z, setTo); } } } @@ -106,8 +106,33 @@ public class TeamPlayer extends BasicListener { spawnTeamPlayer(world, user); }); + AtomicInteger count = new AtomicInteger(); LobbySystem.getPlugin().getLogger().log(Level.INFO, "Loaded " + entities.size() + " team players"); Bukkit.getScheduler().runTaskTimer(LobbySystem.getPlugin(), () -> { + count.incrementAndGet(); + if (count.get() % 20 * 60 * 60 == 0) { + count.set(0); + List steamwarUsers = SteamwarUser.getServerTeam(); + AtomicInteger added = new AtomicInteger(); + steamwarUsers.forEach(user -> { + if (!entities.containsKey(user.getUserName())) { + spawnTeamPlayer(world, user); + added.incrementAndGet(); + } + }); + AtomicInteger removed = new AtomicInteger(); + List names = steamwarUsers.stream().map(SteamwarUser::getUserName).collect(Collectors.toList()); + new HashSet<>(entities.keySet()).stream().filter(name -> !names.contains(name)).forEach(name -> { + world.getEntitiesByClasses(Villager.class).forEach(entity -> { + if (entity.getName().equals(name)) { + entity.remove(); + } + }); + entities.remove(name).delete(); + removed.incrementAndGet(); + }); + LobbySystem.getPlugin().getLogger().log(Level.INFO, "Loaded " + added.get() + " team players, removed " + removed.get() + " team players"); + } world.getEntitiesByClasses(Villager.class).forEach(entity -> { NPC npc = entities.get(entity.getName()); if (npc != null) { From 6e1b0f70dd614c3abb4b13e61505ebd94352ab91 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 15:42:57 +0100 Subject: [PATCH 11/14] Fix last NPC stuff --- src/de/steamwar/lobby/display/NPC.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index de0abfc..3d7e058 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -166,18 +166,21 @@ public class NPC { } this.location = location; byte yaw = (byte)(int)(location.getYaw() * 256.0 / 360.0); + byte pitch = (byte)(int)(location.getPitch() * 256.0 / 360.0); headRotationYaw.set(headRotation, yaw); move = movePacketConstructor.invoke(); movePacketEntity.set(move, entityId); movePacketX.set(move, location.getX()); movePacketY.set(move, location.getY()); movePacketZ.set(move, location.getZ()); + movePacketYaw.set(move, yaw); + movePacketPitch.set(move, pitch); + movePacketOnGround.set(move, true); namedSpawnX.set(namedSpawn, location.getX()); namedSpawnY.set(namedSpawn, location.getY()); namedSpawnZ.set(namedSpawn, location.getZ()); - movePacketYaw.set(move, yaw); - movePacketPitch.set(move, (byte)(int)(location.getPitch() * 256.0 / 360.0)); - movePacketOnGround.set(move, true); + namedSpawnYaw.set(namedSpawn, yaw); + namedSpawnPitch.set(namedSpawn, pitch); display.setLocation(location); } From 4b0f4328157fc0d22509830650741d73018b1b1b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 15:49:29 +0100 Subject: [PATCH 12/14] Optimize stuff --- src/de/steamwar/lobby/display/NPC.java | 37 +++++++++------------- src/de/steamwar/lobby/team/TeamPlayer.java | 6 ++-- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index 3d7e058..3672e3a 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -22,13 +22,8 @@ package de.steamwar.lobby.display; import com.comphenix.tinyprotocol.Reflection; import com.comphenix.tinyprotocol.TinyProtocol; import com.mojang.authlib.GameProfile; -import net.minecraft.server.v1_15_R1.DataWatcher; -import net.minecraft.server.v1_15_R1.DataWatcherRegistry; -import net.minecraft.server.v1_15_R1.EntityHuman; -import net.minecraft.server.v1_15_R1.PacketPlayOutEntityMetadata; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.entity.Player; import java.lang.reflect.Field; @@ -135,7 +130,6 @@ 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); @@ -147,15 +141,14 @@ public class NPC { namedSpawn = namedSpawnConstructor.invoke(); namedSpawnEntity.set(namedSpawn, entityId); namedSpawnUUID.set(namedSpawn, uuid); - namedSpawnX.set(namedSpawn, location.getX()); - namedSpawnY.set(namedSpawn, location.getY()); - namedSpawnZ.set(namedSpawn, location.getZ()); - 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); + + move = movePacketConstructor.invoke(); + movePacketEntity.set(move, entityId); + + setPackets(location); display = new Displayable(location, this::show, this::hide, this::move); } @@ -165,11 +158,14 @@ public class NPC { return; } this.location = location; + setPackets(location); + display.setLocation(location); + } + + private void setPackets(Location location) { byte yaw = (byte)(int)(location.getYaw() * 256.0 / 360.0); byte pitch = (byte)(int)(location.getPitch() * 256.0 / 360.0); headRotationYaw.set(headRotation, yaw); - move = movePacketConstructor.invoke(); - movePacketEntity.set(move, entityId); movePacketX.set(move, location.getX()); movePacketY.set(move, location.getY()); movePacketZ.set(move, location.getZ()); @@ -181,10 +177,12 @@ public class NPC { namedSpawnZ.set(namedSpawn, location.getZ()); namedSpawnYaw.set(namedSpawn, yaw); namedSpawnPitch.set(namedSpawn, pitch); - display.setLocation(location); } private boolean isSimilarLocation(Location location) { + if (this.location == null) { + return false; + } if (Location.normalizeYaw(this.location.getYaw()) != Location.normalizeYaw(location.getYaw())) { return false; } @@ -197,10 +195,7 @@ public class NPC { if (Math.abs(this.location.getY() - location.getY()) > 0.1) { return false; } - if (Math.abs(this.location.getZ() - location.getZ()) > 0.1) { - return false; - } - return true; + return !(Math.abs(this.location.getZ() - location.getZ()) > 0.1); } public Location getLocation() { @@ -227,9 +222,7 @@ public class NPC { private void move(Player player) { TinyProtocol.instance.sendPacket(player, headRotation); - if (move != null) { - TinyProtocol.instance.sendPacket(player, move); - } + TinyProtocol.instance.sendPacket(player, move); } public void delete() { diff --git a/src/de/steamwar/lobby/team/TeamPlayer.java b/src/de/steamwar/lobby/team/TeamPlayer.java index 56fbe46..ea44726 100644 --- a/src/de/steamwar/lobby/team/TeamPlayer.java +++ b/src/de/steamwar/lobby/team/TeamPlayer.java @@ -110,7 +110,7 @@ public class TeamPlayer extends BasicListener { LobbySystem.getPlugin().getLogger().log(Level.INFO, "Loaded " + entities.size() + " team players"); Bukkit.getScheduler().runTaskTimer(LobbySystem.getPlugin(), () -> { count.incrementAndGet(); - if (count.get() % 20 * 60 * 60 == 0) { + if (count.get() % (20 * 60 * 60) == 0) { count.set(0); List steamwarUsers = SteamwarUser.getServerTeam(); AtomicInteger added = new AtomicInteger(); @@ -131,7 +131,9 @@ public class TeamPlayer extends BasicListener { entities.remove(name).delete(); removed.incrementAndGet(); }); - LobbySystem.getPlugin().getLogger().log(Level.INFO, "Loaded " + added.get() + " team players, removed " + removed.get() + " team players"); + if (added.get() > 0 || removed.get() > 0) { + LobbySystem.getPlugin().getLogger().log(Level.INFO, "Loaded " + added.get() + " team players, removed " + removed.get() + " team players"); + } } world.getEntitiesByClasses(Villager.class).forEach(entity -> { NPC npc = entities.get(entity.getName()); From 7d150d24e1e2270b065a197d42b3a8faf300b5af Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 16:09:49 +0100 Subject: [PATCH 13/14] Add clickable messages --- src/de/steamwar/lobby/LobbySystem.properties | 4 ++ src/de/steamwar/lobby/team/TeamPlayer.java | 59 +++++++++++++++----- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/de/steamwar/lobby/LobbySystem.properties b/src/de/steamwar/lobby/LobbySystem.properties index a157b1b..84304c5 100644 --- a/src/de/steamwar/lobby/LobbySystem.properties +++ b/src/de/steamwar/lobby/LobbySystem.properties @@ -3,6 +3,10 @@ TIME = HH:mm:ss DATE=........ COMMAND_HELP_HEAD=§7---=== (§e{0}§7) ===--- +# ServerTeamNPC's +NPC_CHAT_1 = §fHallo, ich bin {0} und habe den Rang {1}. +NPC_CHAT_2 = §fWillkommen auf §eSteam§8War§f, viel Spaß dir. + # Portal Command PORTAL_COMMAND_LIST_HELP = §8/§7portal §elist §8- §7Listet alle Portale auf PORTAL_COMMAND_ADD_HELP = §8/§7portal §ecreate §8[§7PortalType§8] §8[§7PortalName§8] §8- §7Fügt ein Portal hinzu diff --git a/src/de/steamwar/lobby/team/TeamPlayer.java b/src/de/steamwar/lobby/team/TeamPlayer.java index ea44726..6c79212 100644 --- a/src/de/steamwar/lobby/team/TeamPlayer.java +++ b/src/de/steamwar/lobby/team/TeamPlayer.java @@ -29,10 +29,13 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; import org.bukkit.entity.Villager; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityInteractEvent; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; @@ -43,20 +46,6 @@ import java.util.stream.Collectors; public class TeamPlayer extends BasicListener { - @AllArgsConstructor - private static class Cuboid { - private double x1; - private double y1; - private double z1; - private double x2; - private double y2; - private double z2; - - public boolean contains(Location location) { - return location.getX() >= x1 && location.getX() <= x2 && location.getY() >= y1 && location.getY() <= y2 && location.getZ() >= z1 && location.getZ() <= z2; - } - } - private static final List cuboids = new ArrayList<>(); static { @@ -70,6 +59,15 @@ public class TeamPlayer extends BasicListener { private static final World world = Bukkit.getWorlds().get(0); private static final Map entities = new HashMap<>(); + private Set players = new HashSet<>(); + + private Random random = new Random(); + private List strings = new ArrayList<>(); + { + strings.add("NPC_CHAT_1"); + strings.add("NPC_CHAT_2"); + } + public static void spawnTeamPlayer(World world, SteamwarUser steamwarUser) { Location location = new Location(world, 1524.5, 52, 1484.5); String name = steamwarUser.getUserName(); @@ -157,6 +155,25 @@ public class TeamPlayer extends BasicListener { return false; } + @EventHandler + public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { + if (!(event.getRightClicked() instanceof Villager)) { + return; + } + if (!players.add(event.getPlayer())) { + players.remove(event.getPlayer()); + return; + } + SteamwarUser user = SteamwarUser.get(event.getRightClicked().getName()); + String message = strings.get(random.nextInt(strings.size())); + LobbySystem.getMessage().send(message, event.getPlayer(), event.getRightClicked().getName(), user.getUserGroup().getColorCode() + user.getUserGroup().name()); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + players.remove(event.getPlayer()); + } + @EventHandler public void onEntityInteract(EntityInteractEvent event) { if (event.getEntityType() == EntityType.VILLAGER) { @@ -170,4 +187,18 @@ public class TeamPlayer extends BasicListener { event.setCancelled(true); } } + + @AllArgsConstructor + private static class Cuboid { + private double x1; + private double y1; + private double z1; + private double x2; + private double y2; + private double z2; + + public boolean contains(Location location) { + return location.getX() >= x1 && location.getX() <= x2 && location.getY() >= y1 && location.getY() <= y2 && location.getZ() >= z1 && location.getZ() <= z2; + } + } } From ed4acbad3f572619a100355bf7c1d40834bf0fbf Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 16:27:37 +0100 Subject: [PATCH 14/14] Update messages --- src/de/steamwar/lobby/LobbySystem.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/lobby/LobbySystem.properties b/src/de/steamwar/lobby/LobbySystem.properties index 84304c5..576f1aa 100644 --- a/src/de/steamwar/lobby/LobbySystem.properties +++ b/src/de/steamwar/lobby/LobbySystem.properties @@ -4,7 +4,7 @@ DATE=........ COMMAND_HELP_HEAD=§7---=== (§e{0}§7) ===--- # ServerTeamNPC's -NPC_CHAT_1 = §fHallo, ich bin {0} und habe den Rang {1}. +NPC_CHAT_1 = §fHallo, ich bin {0} und bin ein {1}. NPC_CHAT_2 = §fWillkommen auf §eSteam§8War§f, viel Spaß dir. # Portal Command