Skip to content

Commit

Permalink
Disguise: ability to view self disguise (janky)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 17, 2020
1 parent 5f44418 commit 3bf3334
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
Expand Up @@ -12,6 +12,7 @@
import org.bukkit.inventory.ItemStack;

import java.util.List;
import java.util.UUID;

public interface PacketHelper {

Expand Down Expand Up @@ -66,4 +67,12 @@ public interface PacketHelper {
default void sendRename(Player player, Entity entity, String name) {
throw new UnsupportedOperationException();
}

default void generateNoCollideTeam(Player player, UUID noCollide) {
throw new UnsupportedOperationException();
}

default void removeNoCollideTeam(Player player, UUID noCollide) {
throw new UnsupportedOperationException();
}
}
@@ -1,17 +1,20 @@
package com.denizenscript.denizen.scripts.commands.player;

import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.entity.FakeEntity;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.*;

Expand Down Expand Up @@ -106,6 +109,8 @@ public static class TrackedDisguise {

public HashSet<UUID> players;

public FakeEntity fake;

public TrackedDisguise(EntityTag entity, EntityTag as, List<PlayerTag> players) {
this.entity = entity;
this.as = as;
Expand All @@ -116,8 +121,41 @@ public TrackedDisguise(EntityTag entity, EntityTag as, List<PlayerTag> players)
}

public void sendTo(List<PlayerTag> players) {
PlayerTag remove = null;
for (PlayerTag player : players) {
NMSHandler.getPlayerHelper().sendEntityDestroy(player.getPlayerEntity(), entity.getBukkitEntity());
if (player.getOfflinePlayer().getUniqueId().equals(entity.getUUID())) {
remove = player;
if (fake == null) {
if (player.isOnline()) {
fake = FakeEntity.showFakeEntityTo(Collections.singletonList(player), as, player.getLocation(), null);
NMSHandler.getPacketHelper().generateNoCollideTeam(player.getPlayerEntity(), fake.entity.getUUID());
new BukkitRunnable() {
@Override
public void run() {
if (!fake.entity.isFakeValid) {
fake = null;
cancel();
return;
}
NMSHandler.getEntityHelper().move(fake.entity.getBukkitEntity(), player.getLocation().toVector().subtract(fake.entity.getLocation().toVector()));
NMSHandler.getEntityHelper().look(fake.entity.getBukkitEntity(), player.getLocation().getYaw(), player.getLocation().getPitch());
}
}.runTaskTimer(Denizen.getInstance(), 1, 1);
}
}
}
else {
NMSHandler.getPlayerHelper().sendEntityDestroy(player.getPlayerEntity(), entity.getBukkitEntity());
}
}
if (remove != null) {
if (players.size() == 1) {
return;
}
players.remove(remove);
}
if (players.isEmpty()) {
return;
}
NMSHandler.getPlayerHelper().sendEntitySpawn(players, as.getBukkitEntityType(), entity.getLocation(), as.getWaitingMechanisms(), entity.getBukkitEntity().getEntityId(), entity.getUUID(), false);
}
Expand All @@ -140,8 +178,14 @@ public void execute(ScriptEntry scriptEntry) {
for (PlayerTag player : players) {
HashMap<UUID, TrackedDisguise> playerMap = disguises.get(entity.getUUID());
if (playerMap != null) {
if (playerMap.remove(player.getOfflinePlayer().getUniqueId()) != null) {
if (player.isOnline()) {
TrackedDisguise disguise = playerMap.remove(player.getOfflinePlayer().getUniqueId());
if (disguise != null) {
if (disguise.fake != null && player.getOfflinePlayer().getUniqueId().equals(entity.getUUID())) {
NMSHandler.getPacketHelper().removeNoCollideTeam(player.getPlayerEntity(), disguise.fake.entity.getUUID());
disguise.fake.cancelEntity();
disguise.fake = null;
}
else if (player.isOnline()) {
NMSHandler.getPlayerHelper().deTrackEntity(player.getPlayerEntity(), entity.getBukkitEntity());
}
if (playerMap.isEmpty()) {
Expand Down
@@ -1,6 +1,8 @@
package com.denizenscript.denizen.nms.v1_16.helpers;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.v1_16.impl.SidebarImpl;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizen.nms.v1_16.Handler;
import com.denizenscript.denizen.nms.v1_16.impl.jnbt.CompoundTagImpl;
Expand Down Expand Up @@ -30,10 +32,7 @@
import org.bukkit.inventory.ItemStack;

import java.lang.invoke.MethodHandle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.*;

public class PacketHelperImpl implements PacketHelper {

Expand Down Expand Up @@ -295,6 +294,26 @@ public void sendRename(Player player, Entity entity, String name) {
}
}

public static HashMap<String, ScoreboardTeam> noCollideTeamMap = new HashMap<>();

@Override
public void generateNoCollideTeam(Player player, UUID noCollide) {
removeNoCollideTeam(player, noCollide);
ScoreboardTeam team = new ScoreboardTeam(SidebarImpl.dummyScoreboard, Utilities.generateRandomColors(8));
team.getPlayerNameSet().add(noCollide.toString());
team.setCollisionRule(ScoreboardTeamBase.EnumTeamPush.NEVER);
noCollideTeamMap.put(player.getUniqueId() + "_" + noCollide, team);
sendPacket(player, new PacketPlayOutScoreboardTeam(team, 0));
}

@Override
public void removeNoCollideTeam(Player player, UUID noCollide) {
ScoreboardTeam team = noCollideTeamMap.remove(player.getUniqueId() + "_" + noCollide);
if (team != null) {
sendPacket(player, new PacketPlayOutScoreboardTeam(team, 1));
}
}

public static void sendPacket(Player player, Packet packet) {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
}
Expand Down
Expand Up @@ -13,8 +13,8 @@

public class SidebarImpl extends Sidebar {

private static final Scoreboard dummyScoreboard = new Scoreboard();
private static final IScoreboardCriteria dummyCriteria = new IScoreboardCriteria("dummy"); // what
public static final Scoreboard dummyScoreboard = new Scoreboard();
public static final IScoreboardCriteria dummyCriteria = new IScoreboardCriteria("dummy"); // what

private ScoreboardObjective obj1;
private ScoreboardObjective obj2;
Expand Down
Expand Up @@ -202,6 +202,9 @@ else if (packet instanceof PacketPlayOutSpawnEntityLiving) {
}
if (ider != -1) {
Entity e = player.getWorld().getEntity(ider);
if (e == null) {
return false;
}
HashMap<UUID, DisguiseCommand.TrackedDisguise> playerMap = DisguiseCommand.disguises.get(e.getUniqueID());
if (playerMap == null) {
return false;
Expand Down

0 comments on commit 3bf3334

Please sign in to comment.