From c8df4b33ae67290b1f7016e040b4cef0a87cb060 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Sun, 23 Apr 2023 20:24:31 +0100 Subject: [PATCH] start work --- src/main/java/lol/pyr/znpcsplus/npc/NPC.java | 82 ------------------- .../java/lol/pyr/znpcsplus/npc/NPCType.java | 32 -------- .../pyr/znpcsplus/packets/PacketFactory.java | 27 +----- .../pyr/znpcsplus/packets/V1_8Factory.java | 54 ------------ 4 files changed, 4 insertions(+), 191 deletions(-) diff --git a/src/main/java/lol/pyr/znpcsplus/npc/NPC.java b/src/main/java/lol/pyr/znpcsplus/npc/NPC.java index 100279b..d619ef8 100644 --- a/src/main/java/lol/pyr/znpcsplus/npc/NPC.java +++ b/src/main/java/lol/pyr/znpcsplus/npc/NPC.java @@ -1,86 +1,4 @@ package lol.pyr.znpcsplus.npc; -import lol.pyr.znpcsplus.entity.PacketEntity; -import lol.pyr.znpcsplus.entity.PacketLocation; -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.entity.Player; - -import java.util.HashSet; -import java.util.Set; - public class NPC { - private final Set viewers = new HashSet<>(); - private final String worldName; - private PacketEntity entity; - private PacketLocation location; - private NPCType type; - - public NPC(World world, NPCType type, PacketLocation location) { - this.worldName = world.getName(); - this.type = type; - this.location = location; - entity = new PacketEntity(type.getType(), location); // TODO: Entity ID Provider - } - - public void setType(NPCType type) { - _hideAll(); - this.type = type; - entity = new PacketEntity(type.getType(), entity.getLocation()); - _showAll(); - } - - public NPCType getType() { - return type; - } - - public PacketLocation getLocation() { - return location; - } - - public void setLocation(PacketLocation location) { - this.location = location; - entity.setLocation(location, viewers); - } - - public World getWorld() { - return Bukkit.getWorld(worldName); - } - - public void delete() { - _hideAll(); - viewers.clear(); - } - - public void show(Player player) { - if (viewers.contains(player)) return; - _show(player); - viewers.add(player); - } - - private void _show(Player player) { - entity.spawn(player); - } - - public void hide(Player player) { - if (!viewers.contains(player)) return; - _hide(player); - viewers.remove(player); - } - - private void _hide(Player player) { - entity.despawn(player); - } - - private void _hideAll() { - for (Player viewer : viewers) _hide(viewer); - } - - private void _showAll() { - for (Player viewer : viewers) _show(viewer); - } - - public boolean isShown(Player player) { - return viewers.contains(player); - } } diff --git a/src/main/java/lol/pyr/znpcsplus/npc/NPCType.java b/src/main/java/lol/pyr/znpcsplus/npc/NPCType.java index 6773df1..4f90fe4 100644 --- a/src/main/java/lol/pyr/znpcsplus/npc/NPCType.java +++ b/src/main/java/lol/pyr/znpcsplus/npc/NPCType.java @@ -1,36 +1,4 @@ package lol.pyr.znpcsplus.npc; -import com.github.retrooper.packetevents.protocol.entity.type.EntityType; -import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; -import com.google.common.collect.ImmutableList; - -import java.util.List; - public class NPCType { - private final static ImmutableList npcTypes; - - public static List values() { - return npcTypes; - } - - private final EntityType type; - - public NPCType(EntityType type) { - this.type = type; - } - - public EntityType getType() { - return type; - } - - static { - ImmutableList.Builder builder = new ImmutableList.Builder<>(); - - builder.add(new NPCType(EntityTypes.PLAYER)); - builder.add(new NPCType(EntityTypes.CREEPER)); - builder.add(new NPCType(EntityTypes.ZOMBIE)); - builder.add(new NPCType(EntityTypes.SKELETON)); - - npcTypes = builder.build(); - } } diff --git a/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java b/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java index d57b4fb..ce5f394 100644 --- a/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java +++ b/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java @@ -2,39 +2,20 @@ package lol.pyr.znpcsplus.packets; import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.manager.server.ServerVersion; -import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.util.LazyLoader; -import org.bukkit.entity.Player; -import java.util.HashMap; import java.util.Map; public interface PacketFactory { - void spawnPlayer(Player player, PacketEntity entity); - void spawnEntity(Player player, PacketEntity entity); - void destroyEntity(Player player, PacketEntity entity); - void teleportEntity(Player player, PacketEntity entity); - void addTabPlayer(Player player, PacketEntity entity); - void removeTabPlayer(Player player, PacketEntity entity); - PacketFactory factory = get(); + Map> factories = buildFactoryMap(); + static PacketFactory get() { - if (factory != null) return factory; - ServerVersion version = PacketEvents.getAPI().getServerManager().getVersion(); - Map> factories = buildFactoryMap(); - if (factories.containsKey(version)) return factories.get(version).get(); - for (ServerVersion v : ServerVersion.reversedValues()) { - if (v.isNewerThan(version)) continue; - if (!factories.containsKey(v)) continue; - return factories.get(v).get(); - } - throw new RuntimeException("Unsupported version!"); + return factories.get(PacketEvents.getAPI().getServerManager().getVersion()).get(); } private static Map> buildFactoryMap() { - HashMap> map = new HashMap<>(); - map.put(ServerVersion.V_1_8, LazyLoader.of(V1_8Factory::new)); - return map; + } } diff --git a/src/main/java/lol/pyr/znpcsplus/packets/V1_8Factory.java b/src/main/java/lol/pyr/znpcsplus/packets/V1_8Factory.java index 3a31083..6db778c 100644 --- a/src/main/java/lol/pyr/znpcsplus/packets/V1_8Factory.java +++ b/src/main/java/lol/pyr/znpcsplus/packets/V1_8Factory.java @@ -1,58 +1,4 @@ package lol.pyr.znpcsplus.packets; -import com.github.retrooper.packetevents.PacketEvents; -import com.github.retrooper.packetevents.protocol.entity.type.EntityType; -import com.github.retrooper.packetevents.protocol.player.ClientVersion; -import com.github.retrooper.packetevents.protocol.player.UserProfile; -import com.github.retrooper.packetevents.util.Vector3d; -import com.github.retrooper.packetevents.wrapper.play.server.*; -import lol.pyr.znpcsplus.entity.PacketEntity; -import lol.pyr.znpcsplus.entity.PacketLocation; -import org.bukkit.entity.Player; - -import java.util.List; -import java.util.Optional; -import java.util.UUID; - public class V1_8Factory implements PacketFactory { - @Override - public void spawnPlayer(Player player, PacketEntity entity) { - addTabPlayer(player, entity); - PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerSpawnPlayer()); - // TODO - } - - @Override - public void spawnEntity(Player player, PacketEntity entity) { - PacketLocation location = entity.getLocation(); - EntityType type = entity.getType(); - ClientVersion clientVersion = PacketEvents.getAPI().getServerManager().getVersion().toClientVersion(); - PacketEvents.getAPI().getPlayerManager().sendPacket(player, type.getLegacyId(clientVersion) == -1 ? - new WrapperPlayServerSpawnLivingEntity(entity.getEntityId(), new UUID(0, 0), type, location.toVector3d(), - location.getYaw(), location.getPitch(), location.getPitch(), new Vector3d(), List.of()) : - new WrapperPlayServerSpawnEntity(entity.getEntityId(), Optional.empty(), entity.getType(), location.toVector3d(), - location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.empty())); - } - - @Override - public void destroyEntity(Player player, PacketEntity entity) { - PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerDestroyEntities(entity.getEntityId())); - } - - @Override - public void teleportEntity(Player player, PacketEntity entity) { - PacketLocation location = entity.getLocation(); - PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerEntityTeleport(entity.getEntityId(), - location.toVector3d(), location.getYaw(), location.getPitch(), true)); - } - - @Override - public void addTabPlayer(Player player, PacketEntity entity) { - - } - - @Override - public void removeTabPlayer(Player player, PacketEntity entity) { - new WrapperPlayServerPlayerInfo(WrapperPlayServerPlayerInfo.Action.REMOVE_PLAYER, new WrapperPlayServerPlayerInfo.PlayerData(null, new UserProfile(gameProfile.getId(), gameProfile.getName()), null, 1)) - } }