Add setGridLocked to falling block disguise

This commit is contained in:
libraryaddict 2019-11-05 17:26:38 +13:00
parent 6c93866122
commit cc4156f914
4 changed files with 149 additions and 35 deletions

View File

@ -4,7 +4,7 @@
<!-- A good example on why temporary names for project identification shouldn't be used --> <!-- A good example on why temporary names for project identification shouldn't be used -->
<groupId>LibsDisguises</groupId> <groupId>LibsDisguises</groupId>
<artifactId>LibsDisguises</artifactId> <artifactId>LibsDisguises</artifactId>
<version>9.8.3</version> <version>9.8.3-SNAPSHOT</version>
<build> <build>
<defaultGoal>clean install</defaultGoal> <defaultGoal>clean install</defaultGoal>

View File

@ -1,14 +1,24 @@
package me.libraryaddict.disguise.disguisetypes.watchers; package me.libraryaddict.disguise.disguisetypes.watchers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLib;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.lang.reflect.InvocationTargetException;
public class FallingBlockWatcher extends FlagWatcher { public class FallingBlockWatcher extends FlagWatcher {
private ItemStack block = new ItemStack(Material.STONE); private ItemStack block = new ItemStack(Material.STONE);
private boolean gridLocked;
public FallingBlockWatcher(Disguise disguise) { public FallingBlockWatcher(Disguise disguise) {
super(disguise); super(disguise);
@ -22,6 +32,49 @@ public class FallingBlockWatcher extends FlagWatcher {
return watcher; return watcher;
} }
public boolean isGridLocked() {
return gridLocked;
}
private short conRel(double oldCord, double newCord) {
return (short) (((oldCord - newCord) * 4096) * (isGridLocked() ? -1 : 1));
}
public void setGridLocked(boolean gridLocked) {
if (isGridLocked() == gridLocked) {
return;
}
this.gridLocked = gridLocked;
if (getDisguise().isDisguiseInUse() && getDisguise().getEntity() != null) {
PacketContainer relMove = new PacketContainer(PacketType.Play.Server.REL_ENTITY_MOVE);
StructureModifier<Short> shorts = relMove.getShorts();
Location loc = getDisguise().getEntity().getLocation();
relMove.getModifier().write(0, getDisguise().getEntity().getEntityId());
shorts.write(0, conRel(loc.getX(), loc.getBlockX() + 0.5));
shorts.write(1, conRel(loc.getY(), loc.getBlockY()));
shorts.write(2, conRel(loc.getZ(), loc.getBlockZ() + 0.5));
try {
for (Player player : DisguiseUtilities.getPerverts(getDisguise())) {
if (player == getDisguise().getEntity()) {
PacketContainer temp = relMove.shallowClone();
temp.getModifier().write(0, DisguiseAPI.getSelfDisguiseId());
ProtocolLibrary.getProtocolManager().sendServerPacket(player, temp, isGridLocked());
} else {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, relMove, isGridLocked());
}
}
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
public ItemStack getBlock() { public ItemStack getBlock() {
return block; return block;
} }

View File

@ -6,6 +6,7 @@ import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.watchers.FallingBlockWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler; import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets; import me.libraryaddict.disguise.utilities.packets.LibsPackets;
@ -13,6 +14,7 @@ import org.bukkit.Location;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.Vector;
/** /**
* Created by libraryaddict on 3/01/2019. * Created by libraryaddict on 3/01/2019.
@ -24,11 +26,61 @@ public class PacketHandlerMovement implements IPacketHandler {
PacketType.Play.Server.ENTITY_TELEPORT, PacketType.Play.Server.REL_ENTITY_MOVE}; PacketType.Play.Server.ENTITY_TELEPORT, PacketType.Play.Server.REL_ENTITY_MOVE};
} }
private short conRel(int oldCord, int newCord) {
return (short) ((oldCord - newCord) * 4096);
}
@Override @Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) { Entity entity) {
// If falling block should be appearing in center of blocks
if (sentPacket.getType() != PacketType.Play.Server.ENTITY_LOOK &&
disguise.getType() == DisguiseType.FALLING_BLOCK &&
((FallingBlockWatcher) disguise.getWatcher()).isGridLocked()) {
packets.clear();
if (disguise.getType() == DisguiseType.RABBIT && PacketContainer movePacket = sentPacket.shallowClone();
// If relational movement
if (sentPacket.getType() != PacketType.Play.Server.ENTITY_TELEPORT) {
StructureModifier<Short> shorts = movePacket.getShorts();
Location current = entity.getLocation();
Vector diff = new Vector(shorts.read(0) / 4096D, shorts.read(1) / 4096D, shorts.read(2) / 4096D);
Location newLoc = current.clone().subtract(diff);
boolean sameBlock =
current.getBlockX() == newLoc.getBlockX() && current.getBlockY() == newLoc.getBlockY() &&
current.getBlockZ() == newLoc.getBlockZ();
if (sameBlock) {
// Make no modifications
return;
} else {
shorts.write(0, conRel(current.getBlockX(), newLoc.getBlockX()));
shorts.write(1, conRel(current.getBlockY(), newLoc.getBlockY()));
shorts.write(2, conRel(current.getBlockZ(), newLoc.getBlockZ()));
}
} else {
Location loc = entity.getLocation();
StructureModifier<Double> doubles = movePacket.getDoubles();
// Center the block
doubles.write(0, loc.getBlockX() + 0.5);
doubles.write(1, (double) loc.getBlockY());
doubles.write(2, loc.getBlockZ() + 0.5);
}
packets.addPacket(movePacket);
StructureModifier<Byte> bytes = movePacket.getBytes();
byte yawValue = bytes.read(0);
byte pitchValue = bytes.read(1);
bytes.write(0, DisguiseUtilities.getYaw(disguise.getType(), entity.getType(), yawValue));
bytes.write(1, DisguiseUtilities.getPitch(disguise.getType(), entity.getType(), pitchValue));
} else if (disguise.getType() == DisguiseType.RABBIT &&
(sentPacket.getType() == PacketType.Play.Server.REL_ENTITY_MOVE || (sentPacket.getType() == PacketType.Play.Server.REL_ENTITY_MOVE ||
sentPacket.getType() == PacketType.Play.Server.REL_ENTITY_MOVE_LOOK)) { sentPacket.getType() == PacketType.Play.Server.REL_ENTITY_MOVE_LOOK)) {
// When did the rabbit disguise last hop // When did the rabbit disguise last hop
@ -55,8 +107,7 @@ public class PacketHandlerMovement implements IPacketHandler {
statusPacket.getIntegers().write(0, entity.getEntityId()); statusPacket.getIntegers().write(0, entity.getEntityId());
statusPacket.getBytes().write(0, (byte) 1); statusPacket.getBytes().write(0, (byte) 1);
} }
} } else
// Stop wither skulls from looking // Stop wither skulls from looking
if (sentPacket.getType() == PacketType.Play.Server.ENTITY_LOOK && if (sentPacket.getType() == PacketType.Play.Server.ENTITY_LOOK &&
disguise.getType() == DisguiseType.WITHER_SKULL) { disguise.getType() == DisguiseType.WITHER_SKULL) {

View File

@ -303,11 +303,21 @@ public class PacketHandlerSpawn implements IPacketHandler {
disguise.getWatcher())); disguise.getWatcher()));
} else if (disguise.getType().isMisc()) { } else if (disguise.getType().isMisc()) {
int data = ((MiscDisguise) disguise).getData(); int data = ((MiscDisguise) disguise).getData();
double x = loc.getX();
double y = loc.getY();
double z = loc.getZ();
if (disguise.getType() == DisguiseType.FALLING_BLOCK) { if (disguise.getType() == DisguiseType.FALLING_BLOCK) {
ItemStack block = ((FallingBlockWatcher) disguise.getWatcher()).getBlock(); ItemStack block = ((FallingBlockWatcher) disguise.getWatcher()).getBlock();
data = ReflectionManager.getCombinedIdByItemStack(block); data = ReflectionManager.getCombinedIdByItemStack(block);
if (((FallingBlockWatcher) disguise.getWatcher()).isGridLocked()) {
// Center the block
x = loc.getBlockX() + 0.5;
y = loc.getBlockY();
z = loc.getBlockZ() + 0.5;
}
} else if (disguise.getType() == DisguiseType.FISHING_HOOK && data == -1) { } else if (disguise.getType() == DisguiseType.FISHING_HOOK && data == -1) {
// If the MiscDisguise data isn't set. Then no entity id was provided, so default to the owners // If the MiscDisguise data isn't set. Then no entity id was provided, so default to the owners
// entity id // entity id
@ -318,8 +328,8 @@ public class PacketHandlerSpawn implements IPacketHandler {
Object entityType = ReflectionManager.getEntityType(disguise.getType().getEntityType()); Object entityType = ReflectionManager.getEntityType(disguise.getType().getEntityType());
Object[] params = new Object[]{disguisedEntity.getEntityId(), disguisedEntity.getUniqueId(), loc.getX(), Object[] params = new Object[]{disguisedEntity.getEntityId(), disguisedEntity.getUniqueId(), x, y, z,
loc.getY(), loc.getZ(), loc.getPitch(), loc.getYaw(), entityType, data, loc.getPitch(), loc.getYaw(), entityType, data,
ReflectionManager.getVec3D(disguisedEntity.getVelocity())}; ReflectionManager.getVec3D(disguisedEntity.getVelocity())};
PacketContainer spawnEntity = ProtocolLibrary.getProtocolManager() PacketContainer spawnEntity = ProtocolLibrary.getProtocolManager()