add among us to the position plugin (useful af)

yes it is SOOOOOO good
This commit is contained in:
Chayapak 2023-04-16 16:22:12 +07:00
parent 42ca7d0009
commit 604b14270a
2 changed files with 131 additions and 4 deletions

View file

@ -0,0 +1,9 @@
package land.chipmunk.chayapak.chomens_bot.data;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class Rotation {
public float yaw;
public float pitch;
}

View file

@ -1,17 +1,28 @@
package land.chipmunk.chayapak.chomens_bot.plugins;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.ClientboundMoveEntityPosPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.ClientboundMoveEntityPosRotPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.ClientboundMoveEntityRotPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.ClientboundRemoveEntitiesPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerPositionPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.spawn.ClientboundAddPlayerPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.level.ServerboundAcceptTeleportationPacket;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.packet.Packet;
import com.nukkitx.math.vector.Vector3i;
import land.chipmunk.chayapak.chomens_bot.chatParsers.data.MutablePlayerListEntry;
import land.chipmunk.chayapak.chomens_bot.data.Rotation;
import lombok.Getter;
import land.chipmunk.chayapak.chomens_bot.Bot;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// some part of the code used to be in a test plugin but i thought it would be useful in the future so i moved it here
public class PositionPlugin extends SessionAdapter {
private final Bot bot;
@ -19,6 +30,10 @@ public class PositionPlugin extends SessionAdapter {
@Getter private Vector3i position = Vector3i.from(0, 0, 0);
private final Map<Integer, MutablePlayerListEntry> entityIdMap = new HashMap<>();
private final Map<Integer, Vector3f> positionMap = new HashMap<>();
private final Map<Integer, Rotation> rotationMap = new HashMap<>();
public PositionPlugin (Bot bot) {
this.bot = bot;
bot.addListener(this);
@ -26,9 +41,12 @@ public class PositionPlugin extends SessionAdapter {
@Override
public void packetReceived (Session session, Packet packet) {
if (packet instanceof ClientboundPlayerPositionPacket) {
packetReceived((ClientboundPlayerPositionPacket) packet);
}
if (packet instanceof ClientboundPlayerPositionPacket) packetReceived((ClientboundPlayerPositionPacket) packet);
else if (packet instanceof ClientboundMoveEntityRotPacket) packetReceived((ClientboundMoveEntityRotPacket) packet);
else if (packet instanceof ClientboundMoveEntityPosPacket) packetReceived((ClientboundMoveEntityPosPacket) packet);
else if (packet instanceof ClientboundMoveEntityPosRotPacket) packetReceived((ClientboundMoveEntityPosRotPacket) packet);
else if (packet instanceof ClientboundAddPlayerPacket) packetReceived((ClientboundAddPlayerPacket) packet);
else if (packet instanceof ClientboundRemoveEntitiesPacket) packetReceived((ClientboundRemoveEntitiesPacket) packet);
}
public void packetReceived (ClientboundPlayerPositionPacket packet) {
@ -38,6 +56,106 @@ public class PositionPlugin extends SessionAdapter {
for (PositionListener listener : listeners) { listener.positionChange(position); }
}
public void packetReceived (ClientboundAddPlayerPacket packet) {
final MutablePlayerListEntry entry = bot.players().getEntry(packet.getUuid());
if (entry == null) return;
entityIdMap.remove(packet.getEntityId());
positionMap.remove(packet.getEntityId());
rotationMap.remove(packet.getEntityId());
entityIdMap.put(packet.getEntityId(), entry);
positionMap.put(packet.getEntityId(), Vector3f.from(packet.getX(), packet.getY(), packet.getZ()));
rotationMap.put(packet.getEntityId(), new Rotation(packet.getYaw(), packet.getPitch()));
}
public void packetReceived (ClientboundRemoveEntitiesPacket packet) {
final int[] ids = packet.getEntityIds();
for (int id : ids) {
entityIdMap.remove(id);
positionMap.remove(id);
rotationMap.remove(id);
}
}
public void packetReceived (ClientboundMoveEntityRotPacket packet) {
final MutablePlayerListEntry player = entityIdMap.get(packet.getEntityId());
if (player == null) return;
rotationMap.put(packet.getEntityId(), new Rotation(packet.getYaw(), packet.getPitch()));
}
public void packetReceived (ClientboundMoveEntityPosPacket packet) {
final MutablePlayerListEntry player = entityIdMap.get(packet.getEntityId());
if (player == null) return;
final Vector3f lastPosition = positionMap.get(packet.getEntityId());
positionMap.remove(packet.getEntityId());
final Vector3f position = Vector3f.from(
packet.getMoveX() + lastPosition.getX(),
packet.getMoveY() + lastPosition.getY(),
packet.getMoveZ() + lastPosition.getZ()
);
positionMap.put(packet.getEntityId(), position);
}
public void packetReceived (ClientboundMoveEntityPosRotPacket packet) {
final MutablePlayerListEntry player = entityIdMap.get(packet.getEntityId());
if (player == null) return;
final Vector3f lastPosition = positionMap.get(packet.getEntityId());
positionMap.remove(packet.getEntityId());
rotationMap.remove(packet.getEntityId());
final Vector3f position = Vector3f.from(
packet.getMoveX() + lastPosition.getX(),
packet.getMoveY() + lastPosition.getY(),
packet.getMoveZ() + lastPosition.getZ()
);
positionMap.put(packet.getEntityId(), position);
rotationMap.put(packet.getEntityId(), new Rotation(packet.getYaw(), packet.getPitch()));
}
public Vector3f getPlayerPosition (String playerName) {
int entityId = -1;
for (Map.Entry<Integer, MutablePlayerListEntry> entry : entityIdMap.entrySet()) {
if (entry.getValue().profile().getName().equals(playerName)) entityId = entry.getKey();
}
if (entityId == -1) return null;
for (Map.Entry<Integer, Vector3f> entry : positionMap.entrySet()) {
if (entry.getKey() == entityId) return entry.getValue();
}
return null;
}
public Rotation getPlayerRotation (String playerName) {
int entityId = -1;
for (Map.Entry<Integer, MutablePlayerListEntry> entry : entityIdMap.entrySet()) {
if (entry.getValue().profile().getName().equals(playerName)) entityId = entry.getKey();
}
if (entityId == -1) return null;
for (Map.Entry<Integer, Rotation> entry : rotationMap.entrySet()) {
if (entry.getKey() == entityId) return entry.getValue();
}
return null;
}
public void addListener (PositionListener listener) { listeners.add(listener); }
public static class PositionListener {