properties woo
This commit is contained in:
parent
a74bee40a1
commit
ab3acf1076
10 changed files with 128 additions and 68 deletions
|
@ -1,15 +0,0 @@
|
|||
package lol.pyr.znpcsplus.entity;
|
||||
|
||||
import io.github.znetworkw.znpcservers.reflection.Reflections;
|
||||
import io.github.znetworkw.znpcservers.utility.Utils;
|
||||
|
||||
public class EntityIDProvider {
|
||||
public static int reserve() {
|
||||
if (Utils.versionNewer(14)) return Reflections.ATOMIC_ENTITY_ID_FIELD.get().incrementAndGet();
|
||||
else {
|
||||
int id = Reflections.ENTITY_ID_MODIFIER.get();
|
||||
Reflections.ENTITY_ID_MODIFIER.set(id + 1);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,6 +2,8 @@ package lol.pyr.znpcsplus.entity;
|
|||
|
||||
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
|
||||
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
|
||||
import io.github.znetworkw.znpcservers.reflection.Reflections;
|
||||
import io.github.znetworkw.znpcservers.utility.Utils;
|
||||
import lol.pyr.znpcsplus.packets.PacketFactory;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
|
@ -16,7 +18,8 @@ public class PacketEntity {
|
|||
private PacketLocation location;
|
||||
|
||||
public PacketEntity(EntityType type, PacketLocation location) {
|
||||
this.entityId = EntityIDProvider.reserve();
|
||||
if (type == EntityTypes.PLAYER) throw new RuntimeException("Wrong class used for player");
|
||||
this.entityId = reserveEntityID();
|
||||
this.uuid = UUID.randomUUID();
|
||||
this.type = type;
|
||||
this.location = location;
|
||||
|
@ -44,11 +47,19 @@ public class PacketEntity {
|
|||
}
|
||||
|
||||
public void spawn(Player player) {
|
||||
if (type == EntityTypes.PLAYER) PacketFactory.get().spawnPlayer(player, this);
|
||||
else PacketFactory.get().spawnEntity(player, this);
|
||||
PacketFactory.get().spawnEntity(player, this);
|
||||
}
|
||||
|
||||
public void despawn(Player player) {
|
||||
PacketFactory.get().destroyEntity(player, this);
|
||||
}
|
||||
|
||||
private static int reserveEntityID() {
|
||||
if (Utils.versionNewer(14)) return Reflections.ATOMIC_ENTITY_ID_FIELD.get().incrementAndGet();
|
||||
else {
|
||||
int id = Reflections.ENTITY_ID_MODIFIER.get();
|
||||
Reflections.ENTITY_ID_MODIFIER.set(id + 1);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
24
src/main/java/lol/pyr/znpcsplus/entity/PacketPlayer.java
Normal file
24
src/main/java/lol/pyr/znpcsplus/entity/PacketPlayer.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
package lol.pyr.znpcsplus.entity;
|
||||
|
||||
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
|
||||
import com.github.retrooper.packetevents.protocol.player.UserProfile;
|
||||
import lol.pyr.znpcsplus.packets.PacketFactory;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class PacketPlayer extends PacketEntity {
|
||||
private final UserProfile gameProfile;
|
||||
|
||||
public PacketPlayer(PacketLocation location) {
|
||||
super(EntityTypes.PLAYER, location);
|
||||
this.gameProfile = new UserProfile(getUuid(), Integer.toString(getEntityId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spawn(Player player) {
|
||||
PacketFactory.get().spawnPlayer(player, this);
|
||||
}
|
||||
|
||||
public UserProfile getGameProfile() {
|
||||
return gameProfile;
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
package lol.pyr.znpcsplus.npc;
|
||||
|
||||
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
|
||||
import lol.pyr.znpcsplus.entity.PacketEntity;
|
||||
import lol.pyr.znpcsplus.entity.PacketLocation;
|
||||
import lol.pyr.znpcsplus.properties.NPCProperty;
|
||||
import lol.pyr.znpcsplus.properties.NPCPropertyKey;
|
||||
import lol.pyr.znpcsplus.entity.PacketPlayer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
@ -20,19 +20,19 @@ public class NPC {
|
|||
private PacketLocation location;
|
||||
private NPCType type;
|
||||
|
||||
private final Map<NPCPropertyKey, NPCProperty> propertyMap = new HashMap<>();
|
||||
private final Map<NPCPropertyKey<?>, Object> propertyMap = new HashMap<>();
|
||||
|
||||
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
|
||||
entity = new PacketEntity(type.getType(), location);
|
||||
}
|
||||
|
||||
public void setType(NPCType type) {
|
||||
_hideAll();
|
||||
this.type = type;
|
||||
entity = new PacketEntity(type.getType(), entity.getLocation());
|
||||
entity = type.getType() == EntityTypes.PLAYER ? new PacketPlayer(entity.getLocation()) : new PacketEntity(type.getType(), entity.getLocation());
|
||||
_showAll();
|
||||
}
|
||||
|
||||
|
@ -40,6 +40,10 @@ public class NPC {
|
|||
return type;
|
||||
}
|
||||
|
||||
public PacketEntity getEntity() {
|
||||
return entity;
|
||||
}
|
||||
|
||||
public PacketLocation getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
@ -58,22 +62,31 @@ public class NPC {
|
|||
viewers.clear();
|
||||
}
|
||||
|
||||
public void respawn() {
|
||||
_hideAll();
|
||||
_showAll();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public boolean isShown(Player player) {
|
||||
return viewers.contains(player);
|
||||
}
|
||||
|
||||
private void _show(Player player) {
|
||||
entity.spawn(player);
|
||||
}
|
||||
|
||||
private void _hide(Player player) {
|
||||
entity.despawn(player);
|
||||
}
|
||||
|
@ -86,15 +99,21 @@ public class NPC {
|
|||
for (Player viewer : viewers) _show(viewer);
|
||||
}
|
||||
|
||||
public boolean isShown(Player player) {
|
||||
return viewers.contains(player);
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getProperty(NPCPropertyKey<T> key) {
|
||||
return (T) propertyMap.get(key);
|
||||
}
|
||||
|
||||
public NPCProperty getProperty(NPCPropertyKey key) {
|
||||
return propertyMap.get(key);
|
||||
}
|
||||
|
||||
public boolean hasProperty(NPCPropertyKey key) {
|
||||
public boolean hasProperty(NPCPropertyKey<?> key) {
|
||||
return propertyMap.containsKey(key);
|
||||
}
|
||||
|
||||
public <T> void setProperty(NPCPropertyKey<T> key, T value) {
|
||||
propertyMap.put(key, value);
|
||||
key.update(this, value);
|
||||
}
|
||||
|
||||
public void removeProperty(NPCPropertyKey<?> key) {
|
||||
propertyMap.remove(key);
|
||||
}
|
||||
}
|
||||
|
|
35
src/main/java/lol/pyr/znpcsplus/npc/NPCPropertyKey.java
Normal file
35
src/main/java/lol/pyr/znpcsplus/npc/NPCPropertyKey.java
Normal file
|
@ -0,0 +1,35 @@
|
|||
package lol.pyr.znpcsplus.npc;
|
||||
|
||||
import com.github.retrooper.packetevents.protocol.player.TextureProperty;
|
||||
import io.github.znetworkw.znpcservers.npc.NPCSkin;
|
||||
import lol.pyr.znpcsplus.entity.PacketPlayer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class NPCPropertyKey<T> {
|
||||
private final UpdateCallback<T> updateCallback;
|
||||
|
||||
public NPCPropertyKey() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public NPCPropertyKey(UpdateCallback<T> updateCallback) {
|
||||
this.updateCallback = updateCallback;
|
||||
}
|
||||
|
||||
public void update(NPC npc, T value) {
|
||||
if (updateCallback != null) updateCallback.onUpdate(npc, value);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface UpdateCallback<T> {
|
||||
void onUpdate(NPC npc, T value);
|
||||
}
|
||||
|
||||
public static NPCPropertyKey<NPCSkin> NPC_SKIN = new NPCPropertyKey<>((npc, skin) -> {
|
||||
if (!(npc.getEntity() instanceof PacketPlayer entity))
|
||||
throw new RuntimeException("Tried to set a skin on an entity that isn't a player");
|
||||
entity.getGameProfile().setTextureProperties(List.of(new TextureProperty("textures", skin.getTexture(), skin.getSignature())));
|
||||
npc.respawn();
|
||||
});
|
||||
}
|
|
@ -3,6 +3,7 @@ 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.entity.PacketPlayer;
|
||||
import lol.pyr.znpcsplus.util.LazyLoader;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
|
@ -10,14 +11,14 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
public interface PacketFactory {
|
||||
void spawnPlayer(Player player, PacketEntity entity);
|
||||
void spawnPlayer(Player player, PacketPlayer 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);
|
||||
void createTeam(Player player, PacketEntity entity);
|
||||
void removeTeam(Player player, PacketEntity entity);
|
||||
void addTabPlayer(Player player, PacketPlayer entity);
|
||||
void removeTabPlayer(Player player, PacketPlayer entity);
|
||||
void createTeam(Player player, PacketPlayer entity);
|
||||
void removeTeam(Player player, PacketPlayer entity);
|
||||
|
||||
PacketFactory factory = get();
|
||||
|
||||
|
|
|
@ -2,10 +2,9 @@ package lol.pyr.znpcsplus.packets;
|
|||
|
||||
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
|
||||
import com.github.retrooper.packetevents.protocol.player.GameMode;
|
||||
import com.github.retrooper.packetevents.protocol.player.UserProfile;
|
||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoRemove;
|
||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoUpdate;
|
||||
import lol.pyr.znpcsplus.entity.PacketEntity;
|
||||
import lol.pyr.znpcsplus.entity.PacketPlayer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
|
@ -13,17 +12,17 @@ import java.util.EnumSet;
|
|||
|
||||
public class V1_19Factory extends V1_14Factory {
|
||||
@Override
|
||||
public void addTabPlayer(Player player, PacketEntity entity) {
|
||||
public void addTabPlayer(Player player, PacketPlayer entity) {
|
||||
if (entity.getType() != EntityTypes.PLAYER) return;
|
||||
WrapperPlayServerPlayerInfoUpdate.PlayerInfo info = new WrapperPlayServerPlayerInfoUpdate.PlayerInfo(
|
||||
new UserProfile(entity.getUuid(), Integer.toString(entity.getEntityId())), false, 1, GameMode.CREATIVE,
|
||||
entity.getGameProfile(), false, 1, GameMode.CREATIVE,
|
||||
Component.empty(), null);
|
||||
sendPacket(player, new WrapperPlayServerPlayerInfoUpdate(EnumSet.of(WrapperPlayServerPlayerInfoUpdate.Action.ADD_PLAYER,
|
||||
WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_LISTED), info, info));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTabPlayer(Player player, PacketEntity entity) {
|
||||
public void removeTabPlayer(Player player, PacketPlayer entity) {
|
||||
if (entity.getType() != EntityTypes.PLAYER) return;
|
||||
sendPacket(player, new WrapperPlayServerPlayerInfoRemove(entity.getUuid()));
|
||||
}
|
||||
|
|
|
@ -5,13 +5,13 @@ import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
|
|||
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
|
||||
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
|
||||
import com.github.retrooper.packetevents.protocol.player.GameMode;
|
||||
import com.github.retrooper.packetevents.protocol.player.UserProfile;
|
||||
import com.github.retrooper.packetevents.util.Vector3d;
|
||||
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
|
||||
import com.github.retrooper.packetevents.wrapper.play.server.*;
|
||||
import lol.pyr.znpcsplus.ZNPCsPlus;
|
||||
import lol.pyr.znpcsplus.entity.PacketEntity;
|
||||
import lol.pyr.znpcsplus.entity.PacketLocation;
|
||||
import lol.pyr.znpcsplus.entity.PacketPlayer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
@ -21,7 +21,7 @@ import java.util.Optional;
|
|||
|
||||
public class V1_8Factory implements PacketFactory {
|
||||
@Override
|
||||
public void spawnPlayer(Player player, PacketEntity entity) {
|
||||
public void spawnPlayer(Player player, PacketPlayer entity) {
|
||||
addTabPlayer(player, entity);
|
||||
createTeam(player, entity);
|
||||
PacketLocation location = entity.getLocation();
|
||||
|
@ -45,7 +45,7 @@ public class V1_8Factory implements PacketFactory {
|
|||
@Override
|
||||
public void destroyEntity(Player player, PacketEntity entity) {
|
||||
sendPacket(player, new WrapperPlayServerDestroyEntities(entity.getEntityId()));
|
||||
if (entity.getType() == EntityTypes.PLAYER) removeTeam(player, entity);
|
||||
if (entity.getType() == EntityTypes.PLAYER) removeTeam(player, (PacketPlayer) entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -56,27 +56,24 @@ public class V1_8Factory implements PacketFactory {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void addTabPlayer(Player player, PacketEntity entity) {
|
||||
public void addTabPlayer(Player player, PacketPlayer entity) {
|
||||
if (entity.getType() != EntityTypes.PLAYER) return;
|
||||
sendPacket(player, new WrapperPlayServerPlayerInfo(
|
||||
WrapperPlayServerPlayerInfo.Action.ADD_PLAYER, new WrapperPlayServerPlayerInfo.PlayerData(Component.text(""),
|
||||
new UserProfile(entity.getUuid(), Integer.toString(entity.getEntityId())), GameMode.CREATIVE, 1)));
|
||||
WrapperPlayServerPlayerInfo.Action.ADD_PLAYER, new WrapperPlayServerPlayerInfo.PlayerData(Component.text(""), entity.getGameProfile(), GameMode.CREATIVE, 1)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTabPlayer(Player player, PacketEntity entity) {
|
||||
public void removeTabPlayer(Player player, PacketPlayer entity) {
|
||||
if (entity.getType() != EntityTypes.PLAYER) return;
|
||||
sendPacket(player, new WrapperPlayServerPlayerInfo(
|
||||
WrapperPlayServerPlayerInfo.Action.REMOVE_PLAYER, new WrapperPlayServerPlayerInfo.PlayerData(null,
|
||||
new UserProfile(entity.getUuid(), null), null, -1)));
|
||||
entity.getGameProfile(), null, -1)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTeam(Player player, PacketEntity entity) {
|
||||
public void createTeam(Player player, PacketPlayer entity) {
|
||||
sendPacket(player, new WrapperPlayServerTeams("npc_team_" + entity.getEntityId(), WrapperPlayServerTeams.TeamMode.CREATE, new WrapperPlayServerTeams.ScoreBoardTeamInfo(
|
||||
Component.empty(),
|
||||
Component.empty(),
|
||||
Component.empty(),
|
||||
Component.empty(), Component.empty(), Component.empty(),
|
||||
WrapperPlayServerTeams.NameTagVisibility.NEVER,
|
||||
WrapperPlayServerTeams.CollisionRule.NEVER,
|
||||
NamedTextColor.WHITE,
|
||||
|
@ -86,7 +83,7 @@ public class V1_8Factory implements PacketFactory {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void removeTeam(Player player, PacketEntity entity) {
|
||||
public void removeTeam(Player player, PacketPlayer entity) {
|
||||
sendPacket(player, new WrapperPlayServerTeams("npc_team_" + entity.getEntityId(), WrapperPlayServerTeams.TeamMode.REMOVE, (WrapperPlayServerTeams.ScoreBoardTeamInfo) null));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
package lol.pyr.znpcsplus.properties;
|
||||
|
||||
public class NPCProperty {
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package lol.pyr.znpcsplus.properties;
|
||||
|
||||
public class NPCPropertyKey<T> {
|
||||
public NPCPropertyKey() {}
|
||||
|
||||
public static NPCPropertyKey<>
|
||||
}
|
Loading…
Reference in a new issue