From 7c64252557e7b2c051294e5823f94e0e6019b82d Mon Sep 17 00:00:00 2001 From: PseudoKnight Date: Tue, 27 Feb 2024 22:47:18 -0800 Subject: [PATCH] Add functions to hide entities from players --- .../com/laytonsmith/abstraction/MCPlayer.java | 6 + .../bukkit/entities/BukkitMCPlayer.java | 28 +++ .../core/functions/PlayerManagement.java | 167 ++++++++++++++++++ 3 files changed, 201 insertions(+) diff --git a/src/main/java/com/laytonsmith/abstraction/MCPlayer.java b/src/main/java/com/laytonsmith/abstraction/MCPlayer.java index a117e6932..d6f2b9dbc 100644 --- a/src/main/java/com/laytonsmith/abstraction/MCPlayer.java +++ b/src/main/java/com/laytonsmith/abstraction/MCPlayer.java @@ -119,6 +119,12 @@ public interface MCPlayer extends MCCommandSender, MCHumanEntity, MCOfflinePlaye void setVanished(boolean set, MCPlayer to); + void hideEntity(MCEntity entity); + + void showEntity(MCEntity entity); + + boolean canSeeEntity(MCEntity entity); + boolean isNewPlayer(); String getHost(); diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java b/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java index aba579440..339c38356 100644 --- a/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java @@ -465,6 +465,34 @@ public void setVanished(boolean set, MCPlayer to) { } } + @Override + public void hideEntity(MCEntity entity) { + try { + p.hideEntity(CommandHelperPlugin.self, (Entity) entity.getHandle()); + } catch(NoSuchMethodError ex) { + // probably before 1.18 + } + } + + @Override + public void showEntity(MCEntity entity) { + try { + p.showEntity(CommandHelperPlugin.self, (Entity) entity.getHandle()); + } catch(NoSuchMethodError ex) { + // probably before 1.18 + } + } + + @Override + public boolean canSeeEntity(MCEntity entity) { + try { + return p.canSee((Entity) entity.getHandle()); + } catch(NoSuchMethodError ex) { + // probably before 1.18 + return true; + } + } + @Override public void setWhitelisted(boolean value) { p.setWhitelisted(value); diff --git a/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java b/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java index 7301ccf02..68d3ea9d7 100644 --- a/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java +++ b/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java @@ -6888,4 +6888,171 @@ public Boolean runAsync() { return false; } } + + @api + public static class phide_entity extends AbstractFunction { + + @Override + public String getName() { + return "phide_entity"; + } + + @Override + public String docs() { + return "void {[player], entityUUID} Sets an entity to no longer be seen or tracked by the player's client." + + " Resets to default on player rejoin." + + " (MC 1.18+)"; + } + + @Override + public Integer[] numArgs() { + return new Integer[]{1, 2}; + } + + @Override + public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException { + MCPlayer p; + MCEntity e; + if(args.length == 1) { + p = env.getEnv(CommandHelperEnvironment.class).GetPlayer(); + Static.AssertPlayerNonNull(p, t); + e = Static.getEntity(args[0], t); + } else { + p = Static.GetPlayer(args[0], t); + e = Static.getEntity(args[1], t); + } + p.hideEntity(e); + return CVoid.VOID; + } + + @Override + public Class[] thrown() { + return new Class[]{CREPlayerOfflineException.class, CRELengthException.class, CREBadEntityException.class}; + } + + @Override + public Version since() { + return MSVersion.V3_3_5; + } + + @Override + public boolean isRestricted() { + return true; + } + + @Override + public Boolean runAsync() { + return false; + } + } + + @api + public static class pshow_entity extends AbstractFunction { + + @Override + public String getName() { + return "pshow_entity"; + } + + @Override + public String docs() { + return "void {[player], entityUUID} Sets an entity to be sent to the player's client again. (MC 1.18+)"; + } + + @Override + public Integer[] numArgs() { + return new Integer[]{1, 2}; + } + + @Override + public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException { + MCPlayer p; + MCEntity e; + if(args.length == 1) { + p = env.getEnv(CommandHelperEnvironment.class).GetPlayer(); + Static.AssertPlayerNonNull(p, t); + e = Static.getEntity(args[0], t); + } else { + p = Static.GetPlayer(args[0], t); + e = Static.getEntity(args[1], t); + } + p.showEntity(e); + return CVoid.VOID; + } + + @Override + public Class[] thrown() { + return new Class[]{CREPlayerOfflineException.class, CRELengthException.class, CREBadEntityException.class}; + } + + @Override + public Version since() { + return MSVersion.V3_3_5; + } + + @Override + public boolean isRestricted() { + return true; + } + + @Override + public Boolean runAsync() { + return false; + } + } + + @api + public static class pcan_see_entity extends AbstractFunction { + + @Override + public String getName() { + return "pcan_see_entity"; + } + + @Override + public String docs() { + return "boolean {[player], entityUUID} Gets whether the entity is known by the player's client or" + + " hidden by a plugin. (MC 1.18+)"; + } + + @Override + public Integer[] numArgs() { + return new Integer[]{1, 2}; + } + + @Override + public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException { + MCPlayer p; + MCEntity e; + if(args.length == 1) { + p = env.getEnv(CommandHelperEnvironment.class).GetPlayer(); + Static.AssertPlayerNonNull(p, t); + e = Static.getEntity(args[0], t); + } else { + p = Static.GetPlayer(args[0], t); + e = Static.getEntity(args[1], t); + } + return CBoolean.get(p.canSeeEntity(e)); + } + + @Override + public Class[] thrown() { + return new Class[]{CREPlayerOfflineException.class, CRELengthException.class, CREBadEntityException.class}; + } + + @Override + public Version since() { + return MSVersion.V3_3_5; + } + + @Override + public boolean isRestricted() { + return true; + } + + @Override + public Boolean runAsync() { + return false; + } + } }