Skip to content

Instantly share code, notes, and snippets.

@srleojaco
Forked from Swedz/FakePlayer.java
Created September 21, 2022 13:49
Show Gist options
  • Save srleojaco/86d58321c1222a0edda7748d18d72012 to your computer and use it in GitHub Desktop.
Save srleojaco/86d58321c1222a0edda7748d18d72012 to your computer and use it in GitHub Desktop.
Create Fake Players that are specific for just some players!
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R3.CraftServer;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.mojang.authlib.GameProfile;
import net.minecraft.server.v1_8_R3.EntityPlayer;
import net.minecraft.server.v1_8_R3.MinecraftServer;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityEquipment;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityHeadRotation;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityTeleport;
import net.minecraft.server.v1_8_R3.PacketPlayOutNamedEntitySpawn;
import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerInfo;
import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerInfo.EnumPlayerInfoAction;
import net.minecraft.server.v1_8_R3.PlayerConnection;
import net.minecraft.server.v1_8_R3.PlayerInteractManager;
import net.minecraft.server.v1_8_R3.WorldServer;
import net.minecraft.server.v1_8_R3.WorldSettings;
import net.swedz.mccubed.api.wrapper.WrapperPlayServerEntityMetadata;
/*
* NMS code was given to me by svdragster
*/
public class FakePlayer {
private final MinecraftServer server;
private EntityPlayer entityPlayer;
private boolean showInTab;
private Location location;
private boolean invis;
private ArrayList<Player> players;
private Player toFollow;
private Vector followRelatively;
public FakePlayer(UUID uuid, String name, Location location) {
server = ((CraftServer) Bukkit.getServer()).getServer();
final WorldServer world = ((CraftWorld) location.getWorld()).getHandle();
this.entityPlayer = new EntityPlayer(server, world, new GameProfile(uuid, name), new PlayerInteractManager(world));
this.entityPlayer.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
this.entityPlayer.ping = ThreadLocalRandom.current().nextInt(0, 500);
this.location = location;
this.invis = false;
this.players = new ArrayList<Player>();
this.toFollow = null;
this.followRelatively = new Vector(0, 0, 0);
}
public String getName() { return this.entityPlayer.getName(); }
public UUID getUUID() { return this.entityPlayer.getUniqueID(); }
public int getEntityID() { return this.entityPlayer.getId(); }
public boolean isInTab() { return this.showInTab; }
public Location getLocation() { return this.location; }
public int getPing() { return this.entityPlayer.ping; }
public WorldSettings.EnumGamemode getGameMode() { return this.entityPlayer.playerInteractManager.getGameMode(); }
public boolean isInvisible() { return this.invis; }
public Player getPlayerToFollow() { return this.toFollow; }
public boolean followsPlayer() { return this.toFollow != null; }
public Vector getFollowRelative() { return this.followRelatively; }
public FakePlayer updateRelativeLocation() {
if(this.toFollow == null || this.followRelatively == null)
return this;
this.setLocation(this.toFollow.getLocation().clone().add(this.followRelatively));
return this;
}
public FakePlayer setToFollow(Player player, Vector relatively) {
this.toFollow = player;
this.followRelatively = relatively;
return this;
}
public FakePlayer setFollowRelative(Vector relatively) {
this.followRelatively = relatively;
return this;
}
public FakePlayer setShowInTab(boolean tab) {
this.showInTab = tab;
return this;
}
public FakePlayer setLocation(Location location) {
this.entityPlayer.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
this.location = location;
for(Player player : this.players) {
PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
connection.sendPacket(new PacketPlayOutEntityTeleport(this.entityPlayer));
connection.sendPacket(new PacketPlayOutEntityHeadRotation(this.entityPlayer, (byte)(int)(location.getYaw() * 256.0F / 360.0F)));
} return this;
}
public FakePlayer setInvisible(boolean invis) {
this.invis = invis;
return this;
}
public FakePlayer setPing(int ping) {
this.entityPlayer.ping = ping;
return this;
}
public FakePlayer setGameMode(WorldSettings.EnumGamemode gamemode) {
this.entityPlayer.playerInteractManager.setGameMode(gamemode);
return this;
}
public FakePlayer setArmorSet(ItemStack helm, ItemStack chest, ItemStack legs, ItemStack boots) {
PacketPlayOutEntityEquipment helmPacket = new PacketPlayOutEntityEquipment(this.entityPlayer.getId(), 1, CraftItemStack.asNMSCopy(helm));
PacketPlayOutEntityEquipment chestPacket = new PacketPlayOutEntityEquipment(this.entityPlayer.getId(), 2, CraftItemStack.asNMSCopy(chest));
PacketPlayOutEntityEquipment legsPacket = new PacketPlayOutEntityEquipment(this.entityPlayer.getId(), 3, CraftItemStack.asNMSCopy(legs));
PacketPlayOutEntityEquipment bootsPacket = new PacketPlayOutEntityEquipment(this.entityPlayer.getId(), 4, CraftItemStack.asNMSCopy(boots));
for(Player player : this.players) {
((CraftPlayer)player).getHandle().playerConnection.sendPacket(helmPacket);
((CraftPlayer)player).getHandle().playerConnection.sendPacket(chestPacket);
((CraftPlayer)player).getHandle().playerConnection.sendPacket(legsPacket);
((CraftPlayer)player).getHandle().playerConnection.sendPacket(bootsPacket);
}
return this;
}
public boolean canSee(Player player) { return this.players.contains(player); }
public void addPlayer(Player player) {
if(this.players.contains(player))
return;
this.players.add(player);
this.spawn(player);
}
public void removePlayer(Player player) {
if(this.players.contains(player))
return;
this.players.remove(player);
this.despawn(player);
}
public void remove() {
for(Player lp : this.players)
if(lp != null && lp.isOnline())
this.despawn(lp);
this.players = new ArrayList<Player>();
}
@SuppressWarnings("unused")
private void spawn(Player player) {
if(player == null) return;
final PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
if(this.showInTab)
connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, this.entityPlayer));
connection.sendPacket(new PacketPlayOutNamedEntitySpawn(this.entityPlayer));
connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.UPDATE_GAME_MODE, this.entityPlayer));
if(this.invis) {
connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.UPDATE_LATENCY, this.entityPlayer));
try {
WrappedDataWatcher watcher = new WrappedDataWatcher();
watcher.setObject(0, false ? (byte)0 : 0x20);
WrapperPlayServerEntityMetadata update = new WrapperPlayServerEntityMetadata();
update.setEntityId(this.entityPlayer.getId());
update.setEntityMetadata(watcher.getWatchableObjects());
ProtocolLibrary.getProtocolManager().sendServerPacket(player, update.getHandle());
} catch (InvocationTargetException ex) {
ex.printStackTrace();
}
}
}
private void despawn(Player player) {
if(player == null) return;
final PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
connection.sendPacket(new PacketPlayOutEntityDestroy(this.entityPlayer.getId()));
if(this.showInTab)
main.getServer().getScheduler().scheduleSyncDelayedTask(mccubed.getMain(), () -> connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, this.entityPlayer)), 5);
}
private EntityPlayer getEntityPlayer() { return this.entityPlayer; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment