add living entity
This commit is contained in:
parent
7295b671f5
commit
5539f5cd8b
5 changed files with 172 additions and 11 deletions
|
@ -3,6 +3,7 @@ package me.tofaa.entitylib;
|
||||||
import com.github.retrooper.packetevents.PacketEventsAPI;
|
import com.github.retrooper.packetevents.PacketEventsAPI;
|
||||||
import com.github.retrooper.packetevents.event.PacketListenerAbstract;
|
import com.github.retrooper.packetevents.event.PacketListenerAbstract;
|
||||||
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
|
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
|
||||||
|
import com.github.retrooper.packetevents.manager.server.ServerVersion;
|
||||||
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
|
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
|
||||||
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
|
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
|
||||||
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
|
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
|
||||||
|
@ -10,8 +11,11 @@ import com.github.retrooper.packetevents.wrapper.PacketWrapper;
|
||||||
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientInteractEntity;
|
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientInteractEntity;
|
||||||
import me.tofaa.entitylib.entity.EntityInteractionProcessor;
|
import me.tofaa.entitylib.entity.EntityInteractionProcessor;
|
||||||
import me.tofaa.entitylib.entity.WrapperEntity;
|
import me.tofaa.entitylib.entity.WrapperEntity;
|
||||||
|
import me.tofaa.entitylib.entity.WrapperLivingEntity;
|
||||||
|
import me.tofaa.entitylib.exception.InvalidVersionException;
|
||||||
import me.tofaa.entitylib.meta.EntityMeta;
|
import me.tofaa.entitylib.meta.EntityMeta;
|
||||||
import me.tofaa.entitylib.meta.Metadata;
|
import me.tofaa.entitylib.meta.Metadata;
|
||||||
|
import me.tofaa.entitylib.meta.types.LivingEntityMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
@ -87,7 +91,13 @@ public final class EntityLib {
|
||||||
int id = WrapperEntity.ID_PROVIDER.provide();
|
int id = WrapperEntity.ID_PROVIDER.provide();
|
||||||
EntityMeta meta = createMeta(id, entityType);
|
EntityMeta meta = createMeta(id, entityType);
|
||||||
if (meta == null) return null;
|
if (meta == null) return null;
|
||||||
WrapperEntity entity = new WrapperEntity(uuid, entityType, meta);
|
WrapperEntity entity;
|
||||||
|
if (meta instanceof LivingEntityMeta) {
|
||||||
|
entity = new WrapperLivingEntity(uuid, entityType, meta);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
entity = new WrapperEntity(uuid, entityType, meta);
|
||||||
|
}
|
||||||
entities.put(uuid, entity);
|
entities.put(uuid, entity);
|
||||||
entitiesById.put(id, entity);
|
entitiesById.put(id, entity);
|
||||||
return entity;
|
return entity;
|
||||||
|
@ -145,6 +155,12 @@ public final class EntityLib {
|
||||||
EntityLib.interactionProcessor = interactionProcessor;
|
EntityLib.interactionProcessor = interactionProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void verifyVersion(ServerVersion supported, String msg) {
|
||||||
|
if (packetEvents.getServerManager().getVersion().isOlderThan(supported)) {
|
||||||
|
throw new InvalidVersionException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void checkInit() {
|
private static void checkInit() {
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
throw new IllegalStateException("EntityLib is not initialized");
|
throw new IllegalStateException("EntityLib is not initialized");
|
||||||
|
|
|
@ -4,10 +4,7 @@ import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
|
||||||
import com.github.retrooper.packetevents.protocol.player.User;
|
import com.github.retrooper.packetevents.protocol.player.User;
|
||||||
import com.github.retrooper.packetevents.protocol.world.Location;
|
import com.github.retrooper.packetevents.protocol.world.Location;
|
||||||
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
|
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
|
||||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerDestroyEntities;
|
import com.github.retrooper.packetevents.wrapper.play.server.*;
|
||||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityRotation;
|
|
||||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityTeleport;
|
|
||||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnEntity;
|
|
||||||
import me.tofaa.entitylib.EntityLib;
|
import me.tofaa.entitylib.EntityLib;
|
||||||
import me.tofaa.entitylib.meta.EntityMeta;
|
import me.tofaa.entitylib.meta.EntityMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
@ -23,7 +20,6 @@ public class WrapperEntity {
|
||||||
private final Optional<UUID> uuid;
|
private final Optional<UUID> uuid;
|
||||||
private final EntityMeta meta;
|
private final EntityMeta meta;
|
||||||
private final Set<UUID> viewers = new HashSet<>();
|
private final Set<UUID> viewers = new HashSet<>();
|
||||||
|
|
||||||
private Location location;
|
private Location location;
|
||||||
private boolean onGround;
|
private boolean onGround;
|
||||||
private boolean spawned;
|
private boolean spawned;
|
||||||
|
@ -61,8 +57,8 @@ public class WrapperEntity {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Collection<UUID> getViewers() {
|
public void rotateHead(Location location) {
|
||||||
return Collections.unmodifiableCollection(viewers);
|
rotateHead(location.getYaw(), location.getPitch());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -80,6 +76,10 @@ public class WrapperEntity {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public @NotNull Collection<UUID> getViewers() {
|
||||||
|
return Collections.unmodifiableCollection(viewers);
|
||||||
|
}
|
||||||
|
|
||||||
public void teleport(Location location) {
|
public void teleport(Location location) {
|
||||||
teleport(location, true);
|
teleport(location, true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,116 @@
|
||||||
|
package me.tofaa.entitylib.entity;
|
||||||
|
|
||||||
|
import com.github.retrooper.packetevents.manager.server.ServerVersion;
|
||||||
|
import com.github.retrooper.packetevents.protocol.item.ItemStack;
|
||||||
|
import com.github.retrooper.packetevents.protocol.player.Equipment;
|
||||||
|
import com.github.retrooper.packetevents.protocol.player.EquipmentSlot;
|
||||||
|
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityEquipment;
|
||||||
|
import me.tofaa.entitylib.EntityLib;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class WrapperEntityEquipment {
|
||||||
|
|
||||||
|
private static final EquipmentSlot[] EQUIPMENT_SLOTS = EquipmentSlot.values();
|
||||||
|
|
||||||
|
private final WrapperLivingEntity entity;
|
||||||
|
|
||||||
|
|
||||||
|
// 0 = main hand, 1 = offhand, 2 = boots, 3 = leggings, 4 = chestplate, 5 = helmet
|
||||||
|
private final ItemStack[] equipment = new ItemStack[6];
|
||||||
|
|
||||||
|
public WrapperEntityEquipment(WrapperLivingEntity entity) {
|
||||||
|
this.entity = entity;
|
||||||
|
Arrays.fill(equipment, ItemStack.EMPTY);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHelmet(@NotNull ItemStack itemStack) {
|
||||||
|
equipment[5] = itemStack;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChestplate(@NotNull ItemStack itemStack) {
|
||||||
|
equipment[4] = itemStack;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLeggings(@NotNull ItemStack itemStack) {
|
||||||
|
equipment[3] = itemStack;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBoots(@NotNull ItemStack itemStack) {
|
||||||
|
equipment[2] = itemStack;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMainHand(@NotNull ItemStack itemStack) {
|
||||||
|
equipment[0] = itemStack;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOffhand(@NotNull ItemStack itemStack) {
|
||||||
|
EntityLib.verifyVersion(ServerVersion.V_1_9, "Offhand is only supported on 1.9+");
|
||||||
|
equipment[1] = itemStack;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItem(@NotNull EquipmentSlot slot, @NotNull ItemStack itemStack) {
|
||||||
|
equipment[slot.ordinal()] = itemStack;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ItemStack getItem(@NotNull EquipmentSlot slot) {
|
||||||
|
ItemStack itemStack = equipment[slot.ordinal()];
|
||||||
|
if (itemStack == null) {
|
||||||
|
return ItemStack.EMPTY;
|
||||||
|
}
|
||||||
|
return itemStack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ItemStack getHelmet() {
|
||||||
|
return getItem(EquipmentSlot.HELMET);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ItemStack getChestplate() {
|
||||||
|
return getItem(EquipmentSlot.CHEST_PLATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ItemStack getLeggings() {
|
||||||
|
return getItem(EquipmentSlot.LEGGINGS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ItemStack getBoots() {
|
||||||
|
return getItem(EquipmentSlot.BOOTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ItemStack getMainHand() {
|
||||||
|
return getItem(EquipmentSlot.MAIN_HAND);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ItemStack getOffhand() {
|
||||||
|
EntityLib.verifyVersion(ServerVersion.V_1_9, "Offhand is only supported on 1.9+");
|
||||||
|
return getItem(EquipmentSlot.OFF_HAND);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WrapperPlayServerEntityEquipment createPacket() {
|
||||||
|
List<Equipment> equipment = new ArrayList<>();
|
||||||
|
for (int i = 0; i < this.equipment.length; i++) {
|
||||||
|
ItemStack itemStack = this.equipment[i];
|
||||||
|
if (itemStack == null || itemStack.equals(ItemStack.EMPTY)) continue;
|
||||||
|
equipment.add(new Equipment(EQUIPMENT_SLOTS[i], itemStack));
|
||||||
|
}
|
||||||
|
return new WrapperPlayServerEntityEquipment(
|
||||||
|
entity.getEntityId(),
|
||||||
|
equipment
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void refresh() {
|
||||||
|
this.entity.sendPacketToViewers(createPacket());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package me.tofaa.entitylib.entity;
|
||||||
|
|
||||||
|
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
|
||||||
|
import me.tofaa.entitylib.meta.EntityMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class WrapperLivingEntity extends WrapperEntity{
|
||||||
|
|
||||||
|
private final WrapperEntityEquipment equipment;
|
||||||
|
|
||||||
|
public WrapperLivingEntity(@NotNull UUID uuid, EntityType entityType, EntityMeta meta) {
|
||||||
|
super(uuid, entityType, meta);
|
||||||
|
this.equipment = new WrapperEntityEquipment(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WrapperEntityEquipment getEquipment() {
|
||||||
|
return equipment;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,12 +2,16 @@ package me.tofaa.entitylib;
|
||||||
|
|
||||||
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
|
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
|
||||||
import com.github.retrooper.packetevents.protocol.world.Location;
|
import com.github.retrooper.packetevents.protocol.world.Location;
|
||||||
|
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
|
||||||
import me.tofaa.entitylib.entity.WrapperEntity;
|
import me.tofaa.entitylib.entity.WrapperEntity;
|
||||||
|
import me.tofaa.entitylib.entity.WrapperLivingEntity;
|
||||||
import me.tofaa.entitylib.meta.EntityMeta;
|
import me.tofaa.entitylib.meta.EntityMeta;
|
||||||
|
import org.bukkit.Material;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -15,7 +19,7 @@ import java.util.UUID;
|
||||||
|
|
||||||
public class TestEntityCommand implements CommandExecutor {
|
public class TestEntityCommand implements CommandExecutor {
|
||||||
|
|
||||||
private WrapperEntity entity;
|
private WrapperLivingEntity entity;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
|
@ -23,7 +27,7 @@ public class TestEntityCommand implements CommandExecutor {
|
||||||
Player player = (Player) sender;
|
Player player = (Player) sender;
|
||||||
|
|
||||||
if (entity == null) {
|
if (entity == null) {
|
||||||
entity = EntityLib.createEntity(UUID.randomUUID(), EntityTypes.PIG);
|
entity = (WrapperLivingEntity) EntityLib.createEntity(UUID.randomUUID(), EntityTypes.ZOMBIE);
|
||||||
if (entity == null) {
|
if (entity == null) {
|
||||||
player.sendMessage("idk");
|
player.sendMessage("idk");
|
||||||
return false;
|
return false;
|
||||||
|
@ -31,7 +35,10 @@ public class TestEntityCommand implements CommandExecutor {
|
||||||
entity.addViewer(player.getUniqueId());
|
entity.addViewer(player.getUniqueId());
|
||||||
entity.spawn(fromBukkit(player.getLocation()));
|
entity.spawn(fromBukkit(player.getLocation()));
|
||||||
}
|
}
|
||||||
|
ItemStack held = player.getInventory().getItemInMainHand();
|
||||||
|
if (held != null && held.getType() != Material.AIR) {
|
||||||
|
entity.getEquipment().setBoots(SpigotConversionUtil.fromBukkitItemStack(held));
|
||||||
|
}
|
||||||
EntityMeta meta = entity.getMeta();
|
EntityMeta meta = entity.getMeta();
|
||||||
meta.setOnFire(!meta.isOnFire());
|
meta.setOnFire(!meta.isOnFire());
|
||||||
meta.setHasGlowingEffect(!meta.hasGlowingEffect());
|
meta.setHasGlowingEffect(!meta.hasGlowingEffect());
|
||||||
|
|
Loading…
Reference in a new issue