Skip to content

Commit

Permalink
add disguise 'global' arg
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 17, 2020
1 parent 5c9f9ee commit 9b0b68f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 33 deletions.
Expand Up @@ -611,6 +611,19 @@ public String toString() {
return identify();
}

@Override
public int hashCode() {
return getOfflinePlayer().getUniqueId().hashCode();
}

@Override
public boolean equals(Object other) {
if (!(other instanceof PlayerTag)) {
return false;
}
return getOfflinePlayer().getUniqueId().equals(((PlayerTag) other).getOfflinePlayer().getUniqueId());
}

public static void registerTags() {

AbstractFlagTracker.registerFlagHandlers(tagProcessor);
Expand Down
Expand Up @@ -14,6 +14,7 @@
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.*;
Expand All @@ -22,16 +23,16 @@ public class DisguiseCommand extends AbstractCommand {

public DisguiseCommand() {
setName("disguise");
setSyntax("disguise [<entity>] [cancel/as:<type>] (players:<player>|...)");
setRequiredArguments(2, 3);
setSyntax("disguise [<entity>] [cancel/as:<type>] (global/players:<player>|...)");
setRequiredArguments(2, 4);
isProcedural = false;
}

// <--[command]
// @Name disguise
// @Syntax disguise [<entity>] [cancel/as:<type>] (players:<player>|...)
// @Syntax disguise [<entity>] [cancel/as:<type>] (global/players:<player>|...)
// @Required 2
// @Maximum 3
// @Maximum 4
// @Short Makes the player see an entity as though it were a different type of entity.
// @Group player
//
Expand All @@ -49,6 +50,7 @@ public DisguiseCommand() {
//
// Optionally, specify a list of players to show or cancel the entity to.
// If unspecified, will default to the linked player.
// Or, specify 'global' to make the disguise or cancel apply for all players. If using global, use "players:<player>" to show to the self-player.
//
// To remove a disguise, use the 'cancel' argument.
//
Expand All @@ -72,6 +74,10 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
&& arg.matchesPrefix("to", "players")) {
scriptEntry.addObject("players", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("global")
&& arg.matches("global")) {
scriptEntry.addObject("global", new ElementTag(true));
}
else if (arg.matchesPrefix("as")
&& arg.matchesArgumentType(EntityTag.class)) {
scriptEntry.addObject("as", arg.asType(EntityTag.class));
Expand All @@ -89,7 +95,12 @@ else if (!scriptEntry.hasObject("cancel")
}
}
if (!scriptEntry.hasObject("players") && Utilities.entryHasPlayer(scriptEntry)) {
scriptEntry.defaultObject("players", Arrays.asList(Utilities.getEntryPlayer(scriptEntry)));
if (scriptEntry.hasObject("global")) {
scriptEntry.defaultObject("players", new ArrayList<>());
}
else {
scriptEntry.defaultObject("players", Arrays.asList(Utilities.getEntryPlayer(scriptEntry)));
}
}
if (!scriptEntry.hasObject("as") && !scriptEntry.hasObject("cancel")) {
throw new InvalidArgumentsException("Must specify a valid type to disguise as!");
Expand All @@ -108,16 +119,23 @@ public static class TrackedDisguise {

public EntityTag as;

public HashSet<UUID> players;

public FakeEntity fake;

public TrackedDisguise(EntityTag entity, EntityTag as, List<PlayerTag> players) {
public TrackedDisguise(EntityTag entity, EntityTag as) {
this.entity = entity;
this.as = as;
this.players = new HashSet<>();
for (PlayerTag player : players ) {
this.players.add(player.getOfflinePlayer().getUniqueId());
}

public void removeFor(PlayerTag player) {
if (fake != null && player.getOfflinePlayer().getUniqueId().equals(entity.getUUID())) {
if (player.isOnline()) {
NMSHandler.getPacketHelper().removeNoCollideTeam(player.getPlayerEntity(), fake.entity.getUUID());
}
fake.cancelEntity();
fake = null;
}
else if (player.isOnline()) {
NMSHandler.getPlayerHelper().deTrackEntity(player.getPlayerEntity(), entity.getBukkitEntity());
}
}

Expand All @@ -133,7 +151,7 @@ public void sendTo(List<PlayerTag> players) {
new BukkitRunnable() {
@Override
public void run() {
if (!fake.entity.isFakeValid || !player.isOnline()) {
if (fake == null || !fake.entity.isFakeValid || !player.isOnline()) {
fake = null;
cancel();
return;
Expand Down Expand Up @@ -169,44 +187,73 @@ public void execute(ScriptEntry scriptEntry) {
EntityTag entity = scriptEntry.getObjectTag("entity");
EntityTag as = scriptEntry.getObjectTag("as");
ElementTag cancel = scriptEntry.getElement("cancel");
ElementTag global = scriptEntry.getElement("global");
List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), entity.debug()
+ (cancel != null ? cancel.debug() : as.debug())
+ (global != null ? global.debug() : "")
+ ArgumentHelper.debugList("players", players));
}
boolean isGlobal = global != null && global.asBoolean();
if (cancel != null && cancel.asBoolean()) {
for (PlayerTag player : players) {
HashMap<UUID, TrackedDisguise> playerMap = disguises.get(entity.getUUID());
if (playerMap != null) {
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;
HashMap<UUID, TrackedDisguise> playerMap = disguises.get(entity.getUUID());
if (playerMap != null) {
if (isGlobal) {
for (Map.Entry<UUID, TrackedDisguise> entry : playerMap.entrySet()) {
if (entry.getKey() == null) {
for (Player player : entity.getWorld().getPlayers()) {
entry.getValue().removeFor(new PlayerTag(player));
}
}
else if (player.isOnline()) {
NMSHandler.getPlayerHelper().deTrackEntity(player.getPlayerEntity(), entity.getBukkitEntity());
else {
PlayerTag player = new PlayerTag(entry.getKey());
entry.getValue().removeFor(player);
}
if (playerMap.isEmpty()) {
disguises.remove(entity.getUUID());
}
disguises.remove(entity.getUUID());
}
else {
for (PlayerTag player : players) {
TrackedDisguise disguise = playerMap.remove(player.getOfflinePlayer().getUniqueId());
if (disguise != null) {
disguise.removeFor(player);
if (playerMap.isEmpty()) {
disguises.remove(entity.getUUID());
}
}
}
}
}
}
else {
TrackedDisguise disguise = new TrackedDisguise(entity, as, players);
for (PlayerTag player : players) {
TrackedDisguise disguise = new TrackedDisguise(entity, as);
if (isGlobal) {
HashMap<UUID, TrackedDisguise> playerMap = disguises.get(entity.getUUID());
if (playerMap == null) {
playerMap = new HashMap<>();
disguises.put(entity.getUUID(), playerMap);
}
playerMap.put(player.getOfflinePlayer().getUniqueId(), disguise);
playerMap.put(null, disguise);
ArrayList<PlayerTag> playerSet = new ArrayList<>(players);
for (Player player : entity.getWorld().getPlayers()) {
if (!playerSet.contains(new PlayerTag(player)) && !player.getUniqueId().equals(entity.getUUID())) {
playerSet.add(new PlayerTag(player));
}
}
disguise.sendTo(playerSet);
}
else {
for (PlayerTag player : players) {
HashMap<UUID, TrackedDisguise> playerMap = disguises.get(entity.getUUID());
if (playerMap == null) {
playerMap = new HashMap<>();
disguises.put(entity.getUUID(), playerMap);
}
playerMap.put(player.getOfflinePlayer().getUniqueId(), disguise);
}
disguise.sendTo(players);
}
disguise.sendTo(players);
}
}
}
Expand Up @@ -2,6 +2,7 @@

import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.events.world.TimeChangeScriptEvent;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.ScoreboardHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
Expand All @@ -15,6 +16,7 @@
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scoreboard.Scoreboard;

import java.util.HashMap;
Expand Down Expand Up @@ -103,4 +105,8 @@ public void playerLogin(PlayerLoginEvent event) {
PlayerTag.notePlayer(event.getPlayer());
}

@EventHandler
public void playerQuit(PlayerQuitEvent event) {
NMSHandler.getPacketHelper().removeNoCollideTeam(event.getPlayer(), null);
}
}
Expand Up @@ -294,24 +294,40 @@ public void sendRename(Player player, Entity entity, String name) {
}
}

public static HashMap<String, ScoreboardTeam> noCollideTeamMap = new HashMap<>();
public static HashMap<UUID, HashMap<UUID, 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);
HashMap<UUID, ScoreboardTeam> map = noCollideTeamMap.get(player.getUniqueId());
if (map == null) {
map = new HashMap<>();
noCollideTeamMap.put(player.getUniqueId(), map);
}
map.put(noCollide, team);
sendPacket(player, new PacketPlayOutScoreboardTeam(team, 0));
}

@Override
public void removeNoCollideTeam(Player player, UUID noCollide) {
ScoreboardTeam team = noCollideTeamMap.remove(player.getUniqueId() + "_" + noCollide);
if (noCollide == null || !player.isOnline()) {
noCollideTeamMap.remove(player.getUniqueId());
return;
}
HashMap<UUID, ScoreboardTeam> map = noCollideTeamMap.get(player.getUniqueId());
if (map == null) {
return;
}
ScoreboardTeam team = map.remove(noCollide);
if (team != null) {
sendPacket(player, new PacketPlayOutScoreboardTeam(team, 1));
}
if (map.isEmpty()) {
noCollideTeamMap.remove(player.getUniqueId());
}
}

public static void sendPacket(Player player, Packet packet) {
Expand Down
Expand Up @@ -211,7 +211,10 @@ else if (packet instanceof PacketPlayOutSpawnEntityLiving) {
}
DisguiseCommand.TrackedDisguise disguise = playerMap.get(player.getUniqueID());
if (disguise == null) {
return false;
disguise = playerMap.get(null);
if (disguise == null) {
return false;
}
}
antiDuplicate = true;
disguise.sendTo(Collections.singletonList(new PlayerTag(player.getBukkitEntity())));
Expand Down

0 comments on commit 9b0b68f

Please sign in to comment.