Skip to content

Commit

Permalink
Merge pull request #157 from D3v1s0m/2.X
Browse files Browse the repository at this point in the history
Enhancements: Sitting Entities, Creaking NPCs, and Updated PacketEvents
  • Loading branch information
Pyrbu authored Dec 13, 2024
2 parents 8b1fde3 + 79c0897 commit 051a989
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 56 deletions.
2 changes: 1 addition & 1 deletion plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies {
compileOnly "me.clip:placeholderapi:2.11.6" // Placeholder support
implementation "com.google.code.gson:gson:2.10.1" // JSON parsing
implementation "org.bstats:bstats-bukkit:3.0.2" // Plugin stats
implementation "com.github.retrooper:packetevents-spigot:2.6.0" // Packets
implementation "com.github.retrooper:packetevents-spigot:2.7.0" // Packets
implementation "space.arim.dazzleconf:dazzleconf-ext-snakeyaml:1.2.1" // Configs
implementation "lol.pyr:director-adventure:2.1.2" // Commands

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package lol.pyr.znpcsplus.entity;

import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import lol.pyr.znpcsplus.api.entity.EntityProperty;
import lol.pyr.znpcsplus.api.entity.PropertyHolder;
import org.bukkit.inventory.ItemStack;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Represents an armor stand vehicle entity.
* <p>
* This entity is used to make the NPC sit on an invisible armor stand.
* </p>
*/
public class ArmorStandVehicleProperties implements PropertyHolder {

private final Map<EntityPropertyImpl<?>, Object> propertyMap = new HashMap<>();

public ArmorStandVehicleProperties(EntityPropertyRegistryImpl propertyRegistry) {
_setProperty(propertyRegistry.getByName("small", Boolean.class), true);
_setProperty(propertyRegistry.getByName("invisible", Boolean.class), true);
_setProperty(propertyRegistry.getByName("base_plate", Boolean.class), false);
}

@SuppressWarnings("unchecked")
public <T> T getProperty(EntityProperty<T> key) {
return hasProperty(key) ? (T) propertyMap.get((EntityPropertyImpl<?>) key) : key.getDefaultValue();
}

@Override
public boolean hasProperty(EntityProperty<?> key) {
return propertyMap.containsKey((EntityPropertyImpl<?>) key);
}

@SuppressWarnings("unchecked")
private <T> void _setProperty(EntityProperty<T> key, T value) {
Object val = value;
if (val instanceof ItemStack) val = SpigotConversionUtil.fromBukkitItemStack((ItemStack) val);

setProperty((EntityPropertyImpl<T>) key, (T) val);
}

@Override
public <T> void setProperty(EntityProperty<T> key, T value) {
throw new UnsupportedOperationException("Cannot set properties on armor stands");
}

@Override
public void setItemProperty(EntityProperty<?> key, ItemStack value) {
throw new UnsupportedOperationException("Cannot set item properties on armor stands");
}

@Override
public ItemStack getItemProperty(EntityProperty<?> key) {
throw new UnsupportedOperationException("Cannot get item properties on armor stands");
}

public <T> void setProperty(EntityPropertyImpl<T> key, T value) {
if (key == null) return;
if (value == null || value.equals(key.getDefaultValue())) propertyMap.remove(key);
else propertyMap.put(key, value);
}

public Set<EntityProperty<?>> getAllProperties() {
return Collections.unmodifiableSet(propertyMap.keySet());
}

@Override
public Set<EntityProperty<?>> getAppliedProperties() {
return Collections.unmodifiableSet(propertyMap.keySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ public void registerTypes(ZNpcsPlusBootstrap plugin, PacketFactory packetFactory
register(new BooleanProperty("baby", babyIndex, false, legacyBooleans));
}

register(new EntitySittingProperty(packetFactory, this));

// Player
register(new DummyProperty<>("skin", SkinDescriptor.class, false));
final int skinLayersIndex;
Expand Down Expand Up @@ -665,6 +667,16 @@ public void registerTypes(ZNpcsPlusBootstrap plugin, PacketFactory packetFactory

// Bogged
register(new BooleanProperty("bogged_sheared", 16, false, legacyBooleans));

if (!ver.isNewerThanOrEquals(ServerVersion.V_1_21_2)) return;

// Creaking
register(new BooleanProperty("creaking_active", 17, false, legacyBooleans));

if (!ver.isNewerThanOrEquals(ServerVersion.V_1_21_4)) return;

// Creaking
register(new BooleanProperty("creaking_crumbling", 18, false, legacyBooleans));
}

private void registerSerializer(PropertySerializer<?> serializer) {
Expand Down
49 changes: 45 additions & 4 deletions plugin/src/main/java/lol/pyr/znpcsplus/entity/PacketEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@
import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.reflection.Reflections;
import lol.pyr.znpcsplus.util.NpcLocation;
import lol.pyr.znpcsplus.util.Viewable;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Collection;
import java.util.Set;
import java.util.UUID;

public class PacketEntity implements PropertyHolder {
private final PacketFactory packetFactory;

private final PropertyHolder properties;
private final Viewable viewable;
private final int entityId;
private final UUID uuid;

private final EntityType type;
private NpcLocation location;

public PacketEntity(PacketFactory packetFactory, PropertyHolder properties, EntityType type, NpcLocation location) {
private PacketEntity vehicle;

public PacketEntity(PacketFactory packetFactory, PropertyHolder properties, Viewable viewable, EntityType type, NpcLocation location) {
this.packetFactory = packetFactory;
this.properties = properties;
this.viewable = viewable;
this.entityId = reserveEntityID();
this.uuid = UUID.randomUUID();
this.type = type;
Expand All @@ -51,22 +55,59 @@ public EntityType getType() {
return type;
}

public void setLocation(NpcLocation location, Collection<Player> viewers) {
public void setLocation(NpcLocation location) {
this.location = location;
for (Player viewer : viewers) packetFactory.teleportEntity(viewer, this);
if (vehicle != null) {
vehicle.setLocation(location.withY(location.getY() - 0.9));
return;
}
for (Player viewer : viewable.getViewers()) packetFactory.teleportEntity(viewer, this);
}

public void spawn(Player player) {
if (type == EntityTypes.PLAYER) packetFactory.spawnPlayer(player, this, properties);
else packetFactory.spawnEntity(player, this, properties);
if (vehicle != null) {
vehicle.spawn(player);
packetFactory.setPassenger(player, vehicle, this);
}
}

public void setHeadRotation(Player player, float yaw, float pitch) {
packetFactory.sendHeadRotation(player, this, yaw, pitch);
}

public PacketEntity getVehicle() {
return vehicle;
}

public Viewable getViewable() {
return viewable;
}

public void setVehicle(PacketEntity vehicle) {
// remove old vehicle
if (this.vehicle != null) {
for (Player player : viewable.getViewers()) {
packetFactory.setPassenger(player, this.vehicle, null);
this.vehicle.despawn(player);
packetFactory.teleportEntity(player, this);
}
}

this.vehicle = vehicle;
if (this.vehicle == null) return;

vehicle.setLocation(location.withY(location.getY() - 0.9));
for (Player player : viewable.getViewers()) {
vehicle.spawn(player);
packetFactory.setPassenger(player, vehicle, this);
}
}

public void despawn(Player player) {
packetFactory.destroyEntity(player, this, properties);
if (vehicle != null) vehicle.despawn(player);
}

public void refreshMeta(Player player) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lol.pyr.znpcsplus.entity.properties;

import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import lol.pyr.znpcsplus.entity.ArmorStandVehicleProperties;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.packets.PacketFactory;
import org.bukkit.entity.Player;

import java.util.Map;

public class EntitySittingProperty extends EntityPropertyImpl<Boolean> {
private final PacketFactory packetFactory;
private final EntityPropertyRegistryImpl propertyRegistry;

public EntitySittingProperty(PacketFactory packetFactory, EntityPropertyRegistryImpl propertyRegistry) {
super("entity_sitting", false, Boolean.class);
this.packetFactory = packetFactory;
this.propertyRegistry = propertyRegistry;
}

@Override
public void apply(Player player, PacketEntity entity, boolean isSpawned, Map<Integer, EntityData> properties) {
boolean sitting = entity.getProperty(this);
if (sitting) {
if (entity.getVehicle() == null) {
PacketEntity vehiclePacketEntity = new PacketEntity(packetFactory, new ArmorStandVehicleProperties(propertyRegistry),
entity.getViewable(), EntityTypes.ARMOR_STAND, entity.getLocation().withY(entity.getLocation().getY() - 0.9));
entity.setVehicle(vehiclePacketEntity);
}
} else if (entity.getVehicle() != null) {
entity.setVehicle(null);
}
}
}
22 changes: 9 additions & 13 deletions plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public HologramImpl(EntityPropertyRegistryImpl propertyRegistry, ConfigManager c
}

public void addTextLineComponent(Component line) {
HologramText newLine = new HologramText(propertyRegistry, packetFactory, null, line);
HologramText newLine = new HologramText(this, propertyRegistry, packetFactory, null, line);
lines.add(newLine);
relocateLines(newLine);
relocateLines();
for (Player viewer : getViewers()) newLine.show(viewer.getPlayer());
}

Expand All @@ -57,9 +57,9 @@ public void addItemLine(String serializedItem) {
}

public void addItemLinePEStack(ItemStack item) {
HologramItem newLine = new HologramItem(propertyRegistry, packetFactory, null, item);
HologramItem newLine = new HologramItem(this, propertyRegistry, packetFactory, null, item);
lines.add(newLine);
relocateLines(newLine);
relocateLines();
for (Player viewer : getViewers()) newLine.show(viewer.getPlayer());
}

Expand Down Expand Up @@ -99,9 +99,9 @@ public void clearLines() {
}

public void insertTextLineComponent(int index, Component line) {
HologramText newLine = new HologramText(propertyRegistry, packetFactory, null, line);
HologramText newLine = new HologramText(this, propertyRegistry, packetFactory, null, line);
lines.add(index, newLine);
relocateLines(newLine);
relocateLines();
for (Player viewer : getViewers()) newLine.show(viewer.getPlayer());
}

Expand All @@ -114,9 +114,9 @@ public void insertItemLineStack(int index, org.bukkit.inventory.ItemStack item)
}

public void insertItemLinePEStack(int index, ItemStack item) {
HologramItem newLine = new HologramItem(propertyRegistry, packetFactory, null, item);
HologramItem newLine = new HologramItem(this, propertyRegistry, packetFactory, null, item);
lines.add(index, newLine);
relocateLines(newLine);
relocateLines();
for (Player viewer : getViewers()) newLine.show(viewer.getPlayer());
}

Expand Down Expand Up @@ -172,14 +172,10 @@ public void setLocation(NpcLocation location) {
}

private void relocateLines() {
relocateLines(null);
}

private void relocateLines(HologramLine<?> newLine) {
final double lineSpacing = configManager.getConfig().lineSpacing();
double height = location.getY() + (lines.size() - 1) * lineSpacing + getOffset();
for (HologramLine<?> line : lines) {
line.setLocation(location.withY(height), line == newLine ? Collections.emptySet() : getViewers());
line.setLocation(location.withY(height));
height -= lineSpacing;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.util.NpcLocation;
import org.bukkit.entity.Player;

import java.util.Collection;
import lol.pyr.znpcsplus.util.Viewable;

public class HologramItem extends HologramLine<ItemStack> {
public HologramItem(EntityPropertyRegistryImpl propertyRegistry, PacketFactory packetFactory, NpcLocation location, ItemStack item) {
super(item, packetFactory, EntityTypes.ITEM, location);
public HologramItem(Viewable viewable, EntityPropertyRegistryImpl propertyRegistry, PacketFactory packetFactory, NpcLocation location, ItemStack item) {
super(viewable, item, packetFactory, EntityTypes.ITEM, location);
addProperty(propertyRegistry.getByName("holo_item"));
}

Expand All @@ -33,8 +31,8 @@ public <T> T getProperty(EntityProperty<T> key) {
}

@Override
public void setLocation(NpcLocation location, Collection<Player> viewers) {
super.setLocation(location.withY(location.getY() + 2.05), viewers);
public void setLocation(NpcLocation location) {
super.setLocation(location.withY(location.getY() + 2.05));
}

public static boolean ensureValidItemInput(String in) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.util.NpcLocation;
import lol.pyr.znpcsplus.util.Viewable;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -19,9 +19,9 @@ public class HologramLine<M> implements PropertyHolder {
private final PacketEntity entity;
private final Set<EntityProperty<?>> properties;

public HologramLine(M value, PacketFactory packetFactory, EntityType type, NpcLocation location) {
public HologramLine(Viewable viewable, M value, PacketFactory packetFactory, EntityType type, NpcLocation location) {
this.value = value;
this.entity = new PacketEntity(packetFactory, this, type, location);
this.entity = new PacketEntity(packetFactory, this, viewable, type, location);
this.properties = new HashSet<>();
}

Expand All @@ -45,8 +45,8 @@ protected void hide(Player player) {
entity.despawn(player);
}

public void setLocation(NpcLocation location, Collection<Player> viewers) {
entity.setLocation(location, viewers);
public void setLocation(NpcLocation location) {
entity.setLocation(location);
}

public int getEntityId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.util.NpcLocation;
import lol.pyr.znpcsplus.util.Viewable;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;

public class HologramText extends HologramLine<Component> {

private static final Component BLANK = Component.text("%blank%");

public HologramText(EntityPropertyRegistryImpl propertyRegistry, PacketFactory packetFactory, NpcLocation location, Component text) {
super(text, packetFactory, EntityTypes.ARMOR_STAND, location);
public HologramText(Viewable viewable, EntityPropertyRegistryImpl propertyRegistry, PacketFactory packetFactory, NpcLocation location, Component text) {
super(viewable, text, packetFactory, EntityTypes.ARMOR_STAND, location);
addProperty(propertyRegistry.getByName("name"));
addProperty(propertyRegistry.getByName("invisible"));
}
Expand Down
Loading

0 comments on commit 051a989

Please sign in to comment.