Skip to content

Commit

Permalink
attach_to entity mech
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Feb 23, 2018
1 parent 85cb6bc commit 61fa2ff
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 1 deletion.
Expand Up @@ -13,6 +13,7 @@
import net.aufdemrand.denizen.nms.util.jnbt.Tag;
import org.bukkit.Location;
import org.bukkit.block.Biome;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -111,4 +112,8 @@ public static JavaPlugin getJavaPlugin() {
public abstract CompoundTag createCompoundTag(Map<String, Tag> value);

public abstract int getPort();

public void forceAttachMove(Entity a, Entity b) {
throw new RuntimeException("Unsupported forceAttachMove!");
}
}
17 changes: 17 additions & 0 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -2932,6 +2932,23 @@ public void adjust(Mechanism mechanism) {
NMSHandler.getInstance().getEntityHelper().setItemInOffHand(getLivingEntity(), value.asType(dItem.class).getItemStack());
}

// <--[mechanism]
// @object dEntity
// @name attach_to
// @input dEntity
// @description
// Attaches this entity's client-visible motion to another entity.
// Run with no value to disable attachment.
// -->
if (mechanism.matches("attach_to")) {
if (mechanism.hasValue()) {
NMSHandler.getInstance().forceAttachMove(entity, mechanism.getValue().asType(dEntity.class).getBukkitEntity());
}
else {
NMSHandler.getInstance().forceAttachMove(entity, null);
}
}

// <--[mechanism]
// @object dEntity
// @name shooter
Expand Down
Expand Up @@ -28,9 +28,12 @@
import org.bukkit.block.Biome;
import org.bukkit.craftbukkit.v1_12_R1.CraftServer;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Handler_v1_12_R1 extends NMSHandler {

Expand Down Expand Up @@ -211,4 +214,20 @@ public ProfileEditor getProfileEditor() {
public BiomeNMS getBiomeNMS(Biome biome) {
return new BiomeNMS_v1_12_R1(biome);
}

public HashMap<UUID, UUID> attachmentsA = new HashMap<UUID, UUID>();
public HashMap<UUID, UUID> attachments2 = new HashMap<UUID, UUID>();

@Override
public void forceAttachMove(Entity a, Entity b) {
if (attachmentsA.containsKey(a.getUniqueId())) {
attachments2.remove(attachmentsA.get(a.getUniqueId()));
attachmentsA.remove(a.getUniqueId());
}
if (b == null) {
return;
}
attachmentsA.put(a.getUniqueId(), b.getUniqueId());
attachments2.put(b.getUniqueId(), a.getUniqueId());
}
}
Expand Up @@ -4,6 +4,7 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import net.aufdemrand.denizen.nms.Handler_v1_12_R1;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.impl.ProfileEditor_v1_12_R1;
import net.aufdemrand.denizen.nms.impl.entities.EntityFakePlayer_v1_12_R1;
Expand All @@ -15,15 +16,18 @@
import net.aufdemrand.denizen.nms.impl.packets.PacketOutWindowItems_v1_12_R1;
import net.aufdemrand.denizen.nms.interfaces.packets.PacketHandler;
import net.aufdemrand.denizen.nms.interfaces.packets.PacketOutSpawnEntity;
import net.aufdemrand.denizen.nms.util.ReflectionHelper;
import net.minecraft.server.v1_12_R1.*;
import net.minecraft.server.v1_12_R1.Entity;
import net.minecraft.server.v1_12_R1.PacketPlayOutPlayerInfo.EnumPlayerInfoAction;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.entity.*;

import javax.crypto.SecretKey;
import java.lang.reflect.Field;
import java.net.SocketAddress;
import java.util.UUID;

public class DenizenNetworkManager_v1_12_R1 extends NetworkManager {

Expand Down Expand Up @@ -80,13 +84,122 @@ public void setPacketListener(PacketListener packetlistener) {
oldManager.setPacketListener(packetlistener);
}

public static Field ENTITY_ID_PACKENT = ReflectionHelper.getFields(PacketPlayOutEntity.class).get("a");
public static Field ENTITY_ID_PACKVELENT = ReflectionHelper.getFields(PacketPlayOutEntityVelocity.class).get("a");
public static Field ENTITY_ID_PACKTELENT = ReflectionHelper.getFields(PacketPlayOutEntityTeleport.class).get("a");

public static Object duplo(Object a) {
try {
Class clazz = a.getClass();
Object reter = clazz.newInstance();
for (Field f : clazz.getDeclaredFields()) {
f.setAccessible(true);
f.set(reter, f.get(a));
}
Class subc = clazz;
while (subc.getSuperclass() != null) {
subc = subc.getSuperclass();
for (Field f : subc.getDeclaredFields()) {
f.setAccessible(true);
f.set(reter, f.get(a));
}
}
return reter;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}

public void sendPacket(Packet packet) {
// If the packet sending isn't cancelled, allow normal sending
if (packet instanceof PacketPlayOutChat) {
if (!packetHandler.sendPacket(player.getBukkitEntity(), new PacketOutChat_v1_12_R1((PacketPlayOutChat) packet))) {
oldManager.sendPacket(packet);
}
}
else if (packet instanceof PacketPlayOutEntity) {
try {
int ider = ENTITY_ID_PACKENT.getInt(packet);
Entity e = player.getWorld().getEntity(ider);
if (e == null) {
oldManager.sendPacket(packet);
}
else {
if (!((Handler_v1_12_R1) NMSHandler.getInstance()).attachmentsA.containsKey(e.getUniqueID())
|| ((Handler_v1_12_R1) NMSHandler.getInstance()).attachmentsA.get(e.getUniqueID()).equals(player.getUniqueID())) {
oldManager.sendPacket(packet);
}
UUID att = ((Handler_v1_12_R1) NMSHandler.getInstance()).attachments2.get(e.getUniqueID());
if (att != null) {
org.bukkit.entity.Entity target = Bukkit.getEntity(att);
if (target != null) {
Packet pNew = (Packet) duplo(packet);
ENTITY_ID_PACKENT.setInt(pNew, target.getEntityId());
oldManager.sendPacket(pNew);
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
else if (packet instanceof PacketPlayOutEntityVelocity) {
try {
int ider = ENTITY_ID_PACKVELENT.getInt(packet);
Entity e = player.getWorld().getEntity(ider);
if (e == null) {
oldManager.sendPacket(packet);
}
else {
if (!((Handler_v1_12_R1) NMSHandler.getInstance()).attachmentsA.containsKey(e.getUniqueID())
|| ((Handler_v1_12_R1) NMSHandler.getInstance()).attachmentsA.get(e.getUniqueID()).equals(player.getUniqueID())) {
oldManager.sendPacket(packet);
}
UUID att = ((Handler_v1_12_R1) NMSHandler.getInstance()).attachments2.get(e.getUniqueID());
if (att != null) {
org.bukkit.entity.Entity target = Bukkit.getEntity(att);
if (target != null) {
Packet pNew = (Packet) duplo(packet);
ENTITY_ID_PACKVELENT.setInt(pNew, target.getEntityId());
oldManager.sendPacket(pNew);
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
else if (packet instanceof PacketPlayOutEntityTeleport) {
try {
int ider = ENTITY_ID_PACKTELENT.getInt(packet);
Entity e = player.getWorld().getEntity(ider);
if (e == null) {
oldManager.sendPacket(packet);
}
else {
if (!((Handler_v1_12_R1) NMSHandler.getInstance()).attachmentsA.containsKey(e.getUniqueID())
|| ((Handler_v1_12_R1) NMSHandler.getInstance()).attachmentsA.get(e.getUniqueID()).equals(player.getUniqueID())) {
oldManager.sendPacket(packet);
}
UUID att = ((Handler_v1_12_R1) NMSHandler.getInstance()).attachments2.get(e.getUniqueID());
if (att != null) {
org.bukkit.entity.Entity target = Bukkit.getEntity(att);
if (target != null) {
Packet pNew = (Packet) duplo(packet);
ENTITY_ID_PACKTELENT.setInt(pNew, target.getEntityId());
oldManager.sendPacket(pNew);
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
else if (packet instanceof PacketPlayOutNamedEntitySpawn
|| packet instanceof PacketPlayOutSpawnEntity
|| packet instanceof PacketPlayOutSpawnEntityLiving
Expand Down

0 comments on commit 61fa2ff

Please sign in to comment.