Skip to content

Commit

Permalink
refactor attachment to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 3, 2020
1 parent 962747d commit c1e5252
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 118 deletions.
Expand Up @@ -30,7 +30,7 @@ public class PlayerLoginScriptEvent extends BukkitScriptEvent implements Listene
//
// @Determine
// "KICKED" to kick the player from the server.
// "KICKED " + ElementTag to kick the player and specify a message to show.
// "KICKED:" + ElementTag to kick the player and specify a message to show.
//
// @Player Always.
//
Expand Down Expand Up @@ -67,7 +67,7 @@ public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag) {
String determination = determinationObj.toString();
if (CoreUtilities.toLowerCase(determination).startsWith("kicked")) {
String message = determination.length() > 7 ? determination.substring(7) : determination;
String message = determination.length() > "KICKED:".length() ? determination.substring("KICKED:".length()) : determination;
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, message);
return true;
}
Expand Down
57 changes: 0 additions & 57 deletions plugin/src/main/java/com/denizenscript/denizen/nms/NMSHandler.java
Expand Up @@ -13,11 +13,9 @@
import com.denizenscript.denizen.utilities.packets.DenizenPacketHandler;
import org.bukkit.Location;
import org.bukkit.block.Biome;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;

import java.util.*;

Expand Down Expand Up @@ -171,59 +169,4 @@ public boolean isCorrectMappingsCode() {
public abstract int getPort();

public abstract String getTitle(Inventory inventory);

public static Vector fixOffset(Vector offset, double yaw, double pitch) {
yaw = Math.toRadians(yaw);
pitch = Math.toRadians(pitch);
Vector offsetPatched = offset.clone();
// x rotation
double cosPitch = Math.cos(pitch);
double sinPitch = Math.sin(pitch);
double y1 = (offsetPatched.getY() * cosPitch) - (offsetPatched.getZ() * sinPitch);
double z1 = (offsetPatched.getY() * sinPitch) + (offsetPatched.getZ() * cosPitch);
offsetPatched.setY(y1);
offsetPatched.setZ(z1);
// y rotation
double cosYaw = Math.cos(yaw);
double sinYaw = Math.sin(yaw);
double x2 = (offsetPatched.getX() * cosYaw) + (offsetPatched.getZ() * sinYaw);
double z2 = (offsetPatched.getX() * -sinYaw) + (offsetPatched.getZ() * cosYaw);
offsetPatched.setX(x2);
offsetPatched.setZ(z2);
return offsetPatched;
}

public HashMap<UUID, UUID> attachmentsA = new HashMap<>(); // Key follows value
public HashMap<UUID, List<UUID>> attachments2 = new HashMap<>(); // Value follows key
public HashMap<UUID, Vector> attachmentOffsets = new HashMap<>();
public HashSet<UUID> attachmentRotations = new HashSet<>();
public HashMap<UUID, Vector> visiblePositions = new HashMap<>();

public void forceAttachMove(Entity a, Entity b, Vector offset, boolean matchRotation) {
if (attachmentsA.containsKey(a.getUniqueId())) {
UUID bid = attachmentsA.get(a.getUniqueId());
List<UUID> subAttachments = attachments2.get(bid);
subAttachments.remove(a.getUniqueId());
if (subAttachments.isEmpty()) {
attachments2.remove(bid);
}
attachmentsA.remove(a.getUniqueId());
attachmentOffsets.remove(a.getUniqueId());
attachmentRotations.remove(a.getUniqueId());
}
if (b == null) {
return;
}
attachmentsA.put(a.getUniqueId(), b.getUniqueId());
List<UUID> subAttachments = attachments2.get(b.getUniqueId());
if (subAttachments == null) {
subAttachments = new ArrayList<>();
attachments2.put(b.getUniqueId(), subAttachments);
}
subAttachments.add(a.getUniqueId());
attachmentOffsets.put(a.getUniqueId(), offset);
if (matchRotation) {
attachmentRotations.add(a.getUniqueId());
}
}
}
Expand Up @@ -10,6 +10,7 @@
import com.denizenscript.denizen.utilities.blocks.ModernBlockData;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.utilities.entity.DenizenEntityType;
import com.denizenscript.denizen.utilities.entity.EntityAttachmentHelper;
import com.denizenscript.denizen.utilities.entity.FakeEntity;
import com.denizenscript.denizen.utilities.nbt.CustomNBT;
import com.denizenscript.denizencore.objects.*;
Expand Down Expand Up @@ -2558,10 +2559,10 @@ public void adjust(Mechanism mechanism) {
rotateWith = new ElementTag(list.get(2)).asBoolean();
}
}
NMSHandler.getInstance().forceAttachMove(entity, EntityTag.valueOf(list.get(0), mechanism.context).getBukkitEntity(), offset, rotateWith);
EntityAttachmentHelper.forceAttachMove(entity, EntityTag.valueOf(list.get(0), mechanism.context).getBukkitEntity(), offset, rotateWith);
}
else {
NMSHandler.getInstance().forceAttachMove(entity, null, null, false);
EntityAttachmentHelper.forceAttachMove(entity, null, null, false);
}
}

Expand Down
@@ -0,0 +1,76 @@
package com.denizenscript.denizen.utilities.entity;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.util.Vector;

import java.util.*;

public class EntityAttachmentHelper {

public static class AttachmentData {

public Entity attached, to;

public boolean offsetRelative, changeAngle;

public float yawAngleOffset, pitchAngleOffset;

public Location positionalOffset;
}

public static Vector fixOffset(Vector offset, double yaw, double pitch) {
yaw = Math.toRadians(yaw);
pitch = Math.toRadians(pitch);
Vector offsetPatched = offset.clone();
// x rotation
double cosPitch = Math.cos(pitch);
double sinPitch = Math.sin(pitch);
double y1 = (offsetPatched.getY() * cosPitch) - (offsetPatched.getZ() * sinPitch);
double z1 = (offsetPatched.getY() * sinPitch) + (offsetPatched.getZ() * cosPitch);
offsetPatched.setY(y1);
offsetPatched.setZ(z1);
// y rotation
double cosYaw = Math.cos(yaw);
double sinYaw = Math.sin(yaw);
double x2 = (offsetPatched.getX() * cosYaw) + (offsetPatched.getZ() * sinYaw);
double z2 = (offsetPatched.getX() * -sinYaw) + (offsetPatched.getZ() * cosYaw);
offsetPatched.setX(x2);
offsetPatched.setZ(z2);
return offsetPatched;
}

public static HashMap<UUID, UUID> attachmentsA = new HashMap<>(); // Key follows value
public static HashMap<UUID, List<UUID>> attachments2 = new HashMap<>(); // Value follows key
public static HashMap<UUID, Vector> attachmentOffsets = new HashMap<>();
public static HashSet<UUID> attachmentRotations = new HashSet<>();
public static HashMap<UUID, Vector> visiblePositions = new HashMap<>();

public static void forceAttachMove(Entity a, Entity b, Vector offset, boolean matchRotation) {
if (attachmentsA.containsKey(a.getUniqueId())) {
UUID bid = attachmentsA.get(a.getUniqueId());
List<UUID> subAttachments = attachments2.get(bid);
subAttachments.remove(a.getUniqueId());
if (subAttachments.isEmpty()) {
attachments2.remove(bid);
}
attachmentsA.remove(a.getUniqueId());
attachmentOffsets.remove(a.getUniqueId());
attachmentRotations.remove(a.getUniqueId());
}
if (b == null) {
return;
}
attachmentsA.put(a.getUniqueId(), b.getUniqueId());
List<UUID> subAttachments = attachments2.get(b.getUniqueId());
if (subAttachments == null) {
subAttachments = new ArrayList<>();
attachments2.put(b.getUniqueId(), subAttachments);
}
subAttachments.add(a.getUniqueId());
attachmentOffsets.put(a.getUniqueId(), offset);
if (matchRotation) {
attachmentRotations.add(a.getUniqueId());
}
}
}
Expand Up @@ -7,6 +7,7 @@
import com.denizenscript.denizen.nms.v1_13.impl.packets.PacketOutSpawnEntityImpl;
import com.denizenscript.denizen.nms.interfaces.packets.PacketOutSpawnEntity;
import com.denizenscript.denizen.nms.util.ReflectionHelper;
import com.denizenscript.denizen.utilities.entity.EntityAttachmentHelper;
import com.denizenscript.denizen.utilities.packets.DenizenPacketHandler;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
Expand Down Expand Up @@ -144,33 +145,33 @@ else if (packet instanceof PacketPlayOutEntity) {
oldManager.sendPacket(packet, genericfuturelistener);
}
else {
if (!NMSHandler.getInstance().attachmentsA.containsKey(e.getUniqueID())
|| NMSHandler.getInstance().attachmentsA.get(e.getUniqueID()).equals(player.getUniqueID())) {
if (!EntityAttachmentHelper.attachmentsA.containsKey(e.getUniqueID())
|| EntityAttachmentHelper.attachmentsA.get(e.getUniqueID()).equals(player.getUniqueID())) {
oldManager.sendPacket(packet, genericfuturelistener);
}
List<UUID> attList = NMSHandler.getInstance().attachments2.get(e.getUniqueID());
List<UUID> attList = EntityAttachmentHelper.attachments2.get(e.getUniqueID());
if (attList != null) {
for (UUID att : attList) {
org.bukkit.entity.Entity target = Bukkit.getEntity(att);
if (target != null) {
Packet pNew = (Packet) duplo(packet);
ENTITY_ID_PACKENT.setInt(pNew, target.getEntityId());
Vector offset = NMSHandler.getInstance().attachmentOffsets.get(att);
Vector offset = EntityAttachmentHelper.attachmentOffsets.get(att);
if (offset != null && (packet instanceof PacketPlayOutEntity.PacketPlayOutRelEntityMove || packet instanceof PacketPlayOutEntity.PacketPlayOutRelEntityMoveLook)) {
boolean rotationBasis = NMSHandler.getInstance().attachmentRotations.contains(att);
boolean rotationBasis = EntityAttachmentHelper.attachmentRotations.contains(att);
Vector goalPosition;
if (!rotationBasis) {
goalPosition = new Vector(e.locX, e.locY, e.locZ).add(offset);
}
else {
goalPosition = new Vector(e.locX, e.locY, e.locZ).add(NMSHandler.fixOffset(offset, -e.yaw, e.pitch));
goalPosition = new Vector(e.locX, e.locY, e.locZ).add(EntityAttachmentHelper.fixOffset(offset, -e.yaw, e.pitch));
}
Vector oldPos = NMSHandler.getInstance().visiblePositions.get(target.getUniqueId());
Vector oldPos = EntityAttachmentHelper.visiblePositions.get(target.getUniqueId());
if (oldPos == null) {
oldPos = target.getLocation().toVector();
}
Vector moveNeeded = goalPosition.clone().subtract(oldPos);
NMSHandler.getInstance().visiblePositions.put(target.getUniqueId(), goalPosition.clone());
EntityAttachmentHelper.visiblePositions.put(target.getUniqueId(), goalPosition.clone());
int offX = (int) (moveNeeded.getX() * (32 * 128));
int offY = (int) (moveNeeded.getY() * (32 * 128));
int offZ = (int) (moveNeeded.getZ() * (32 * 128));
Expand Down Expand Up @@ -207,11 +208,11 @@ else if (packet instanceof PacketPlayOutEntityVelocity) {
oldManager.sendPacket(packet, genericfuturelistener);
}
else {
if (!NMSHandler.getInstance().attachmentsA.containsKey(e.getUniqueID())
|| NMSHandler.getInstance().attachmentsA.get(e.getUniqueID()).equals(player.getUniqueID())) {
if (!EntityAttachmentHelper.attachmentsA.containsKey(e.getUniqueID())
|| EntityAttachmentHelper.attachmentsA.get(e.getUniqueID()).equals(player.getUniqueID())) {
oldManager.sendPacket(packet, genericfuturelistener);
}
List<UUID> attList = NMSHandler.getInstance().attachments2.get(e.getUniqueID());
List<UUID> attList = EntityAttachmentHelper.attachments2.get(e.getUniqueID());
if (attList != null) {
for (UUID att : attList) {
org.bukkit.entity.Entity target = Bukkit.getEntity(att);
Expand All @@ -236,34 +237,34 @@ else if (packet instanceof PacketPlayOutEntityTeleport) {
oldManager.sendPacket(packet, genericfuturelistener);
}
else {
if (!NMSHandler.getInstance().attachmentsA.containsKey(e.getUniqueID())
|| NMSHandler.getInstance().attachmentsA.get(e.getUniqueID()).equals(player.getUniqueID())) {
if (!EntityAttachmentHelper.attachmentsA.containsKey(e.getUniqueID())
|| EntityAttachmentHelper.attachmentsA.get(e.getUniqueID()).equals(player.getUniqueID())) {
oldManager.sendPacket(packet, genericfuturelistener);
}
List<UUID> attList = NMSHandler.getInstance().attachments2.get(e.getUniqueID());
List<UUID> attList = EntityAttachmentHelper.attachments2.get(e.getUniqueID());
if (attList != null) {
for (UUID att : attList) {
org.bukkit.entity.Entity target = Bukkit.getEntity(att);
if (target != null) {
Packet pNew = (Packet) duplo(packet);
ENTITY_ID_PACKTELENT.setInt(pNew, target.getEntityId());
Vector offset = NMSHandler.getInstance().attachmentOffsets.get(att);
Vector offset = EntityAttachmentHelper.attachmentOffsets.get(att);
Vector resultPos = new Vector(POS_X_PACKTELENT.getDouble(pNew), POS_Y_PACKTELENT.getDouble(pNew), POS_Z_PACKTELENT.getDouble(pNew));
if (offset != null) {
boolean rotationBasis = NMSHandler.getInstance().attachmentRotations.contains(att);
boolean rotationBasis = EntityAttachmentHelper.attachmentRotations.contains(att);
Vector goalOffset;
if (!rotationBasis) {
goalOffset = offset;
}
else {
goalOffset = NMSHandler.fixOffset(offset, -e.yaw, e.pitch);
goalOffset = EntityAttachmentHelper.fixOffset(offset, -e.yaw, e.pitch);
}
POS_X_PACKTELENT.setDouble(pNew, POS_X_PACKTELENT.getDouble(pNew) + goalOffset.getX());
POS_Y_PACKTELENT.setDouble(pNew, POS_Y_PACKTELENT.getDouble(pNew) + goalOffset.getY());
POS_Z_PACKTELENT.setDouble(pNew, POS_Z_PACKTELENT.getDouble(pNew) + goalOffset.getZ());
resultPos.add(goalOffset);
}
NMSHandler.getInstance().visiblePositions.put(target.getUniqueId(), resultPos);
EntityAttachmentHelper.visiblePositions.put(target.getUniqueId(), resultPos);
oldManager.sendPacket(pNew);
}
}
Expand Down

0 comments on commit c1e5252

Please sign in to comment.