From 725a941c6dd7030aac4ca2da5585c088fd0c93a8 Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Sat, 9 Dec 2023 13:52:14 +0800 Subject: [PATCH] =?UTF-8?q?new:=20=E9=80=82=E9=85=8DResidence=E7=9A=84noFl?= =?UTF-8?q?y=E9=80=89=E9=A1=B9=20=20=20=20=20-=20=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E4=B8=BAfalse=E6=88=96remove=E4=B8=BA=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E9=A3=9E=E8=A1=8C=EF=BC=8C=E8=AE=BE=E7=BD=AE=E4=B8=BAtrue?= =?UTF-8?q?=E6=88=96=E6=9C=AA=E7=9F=A5=E9=80=89=E9=A1=B9=E4=B8=BA=E7=A6=81?= =?UTF-8?q?=E6=AD=A2=E9=A3=9E=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 3 + src/main/java/xiamomc/morph/MorphPlugin.java | 21 +++++- .../morph/abilities/impl/FlyAbility.java | 36 ++++++++++- .../residence/ResidenceEventProcessor.java | 64 +++++++++++++++++++ 4 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 src/main/java/xiamomc/morph/misc/integrations/residence/ResidenceEventProcessor.java diff --git a/build.gradle.kts b/build.gradle.kts index 10913be9..642d4900 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -50,6 +50,9 @@ dependencies { exclude("org.spigotmc", "spigot") } + compileOnly(files("libs/CMILib1.4.3.5.jar")) + compileOnly(files("libs/Residence5.1.4.0.jar")) + implementation("org.bstats:bstats-bukkit:${project.property("bstats_version")}") { exclude("com.google.code.gson", "gson") diff --git a/src/main/java/xiamomc/morph/MorphPlugin.java b/src/main/java/xiamomc/morph/MorphPlugin.java index 210d7551..c37e064b 100644 --- a/src/main/java/xiamomc/morph/MorphPlugin.java +++ b/src/main/java/xiamomc/morph/MorphPlugin.java @@ -18,6 +18,7 @@ import xiamomc.morph.messages.vanilla.VanillaMessageStore; import xiamomc.morph.misc.NetworkingHelper; import xiamomc.morph.misc.PlayerOperationSimulator; +import xiamomc.morph.misc.integrations.residence.ResidenceEventProcessor; import xiamomc.morph.updates.UpdateHandler; import xiamomc.morph.misc.integrations.gsit.GSitCompactProcessor; import xiamomc.morph.misc.integrations.placeholderapi.PlaceholderIntegration; @@ -29,6 +30,8 @@ import xiamomc.pluginbase.Messages.MessageStore; import xiamomc.pluginbase.XiaMoJavaPlugin; +import java.util.Arrays; + public final class MorphPlugin extends XiaMoJavaPlugin { private static MorphPlugin instance; @@ -89,16 +92,32 @@ public void onEnable() this.metrics = new Metrics(this, 18062); pluginManager = Bukkit.getPluginManager(); + this.registerListener(softDeps); var playerTracker = new PlayerTracker(); - softDeps.setHandle("GSit", s -> this.registerListener(new GSitCompactProcessor()), true); + softDeps.setHandle("GSit", s -> + { + logger.info("GSit detected, applying integrations..."); + this.registerListener(new GSitCompactProcessor()); + }, true); + softDeps.setHandle("PlaceholderAPI", p -> { + logger.info("Registering Placeholders..."); placeholderIntegration = new PlaceholderIntegration(dependencyManager); placeholderIntegration.register(); }, true); + softDeps.setHandle("Residence", r -> + { + logger.info("Residence detected, applying integrations..."); + this.registerListener(new ResidenceEventProcessor()); + }, true); + + var plugins = Bukkit.getPluginManager().getPlugins(); + logger.info(Arrays.toString(plugins)); + //缓存依赖 dependencyManager.cache(this); dependencyManager.cache(new NetworkingHelper()); diff --git a/src/main/java/xiamomc/morph/abilities/impl/FlyAbility.java b/src/main/java/xiamomc/morph/abilities/impl/FlyAbility.java index 442cd1ed..f85293ee 100644 --- a/src/main/java/xiamomc/morph/abilities/impl/FlyAbility.java +++ b/src/main/java/xiamomc/morph/abilities/impl/FlyAbility.java @@ -1,5 +1,7 @@ package xiamomc.morph.abilities.impl; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import net.minecraft.world.phys.Vec3; import org.bukkit.GameEvent; import org.bukkit.GameMode; @@ -23,6 +25,8 @@ import xiamomc.pluginbase.Bindables.BindableList; import java.util.List; +import java.util.Map; +import java.util.Stack; public class FlyAbility extends MorphAbility { @@ -80,7 +84,7 @@ private void load(MorphConfigManager configManager) @Override public boolean handle(Player player, DisguiseState state) { - if (plugin.getCurrentTick() % 4 != 0) return true; + if (plugin.getCurrentTick() % 2 != 0) return true; var gameMode = player.getGameMode(); if (gameMode == GameMode.CREATIVE || gameMode == GameMode.SPECTATOR) @@ -92,7 +96,8 @@ public boolean handle(Player player, DisguiseState state) var data = nmsPlayer.getFoodData(); var allowFlight = this.allowFlight.get() && data.foodLevel > config.getMinimumHunger() - && !noFlyWorlds.contains(player.getWorld().getName()); + && !noFlyWorlds.contains(player.getWorld().getName()) + && !playerBlocked(player); if (player.isFlying()) { @@ -232,4 +237,31 @@ public void onGameModeChange(PlayerGameModeChangeEvent e) this.appliedPlayers.remove(player); } } + + private static final Map> blockedPlayersMap = new Object2ObjectOpenHashMap<>(); + + public static boolean playerBlocked(Player player) + { + var stack = blockedPlayersMap.getOrDefault(player, null); + return stack != null && !stack.isEmpty(); + } + + public static void blockPlayer(Player player, Object requestSource) + { + var stack = blockedPlayersMap.getOrDefault(player, null); + if (stack == null) + { + stack = new Stack<>(); + blockedPlayersMap.put(player, stack); + } + + if (!stack.contains(requestSource)) + stack.push(requestSource); + } + + public static void unBlockPlayer(Player player, Object requestSource) + { + var stack = blockedPlayersMap.getOrDefault(player, new Stack<>()); + stack.remove(requestSource); + } } diff --git a/src/main/java/xiamomc/morph/misc/integrations/residence/ResidenceEventProcessor.java b/src/main/java/xiamomc/morph/misc/integrations/residence/ResidenceEventProcessor.java new file mode 100644 index 00000000..80fb2c32 --- /dev/null +++ b/src/main/java/xiamomc/morph/misc/integrations/residence/ResidenceEventProcessor.java @@ -0,0 +1,64 @@ +package xiamomc.morph.misc.integrations.residence; + +import com.bekvon.bukkit.residence.Residence; +import com.bekvon.bukkit.residence.containers.Flags; +import com.bekvon.bukkit.residence.event.ResidenceChangedEvent; +import com.bekvon.bukkit.residence.event.ResidenceFlagChangeEvent; +import com.bekvon.bukkit.residence.protection.FlagPermissions; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import xiamomc.morph.MorphPluginObject; +import xiamomc.morph.abilities.impl.FlyAbility; + +public class ResidenceEventProcessor extends MorphPluginObject implements Listener +{ + @EventHandler + public void onEnterResidence(ResidenceChangedEvent e) + { + var player = e.getPlayer(); + var newResidence = e.getTo(); + + if (newResidence == null) + { + FlyAbility.unBlockPlayer(player, this); + return; + } + + var residencePermissions = newResidence.getPermissions(); + if (residencePermissions.playerHas(player, Flags.nofly, false)) + { + FlyAbility.blockPlayer(player, this); + } + else + { + FlyAbility.unBlockPlayer(player, this); + } + } + + @EventHandler + public void onResidenceFlagChange(ResidenceFlagChangeEvent e) + { + var residence = e.getResidence(); + var players = residence.getPlayersInResidence(); + + var flagName = e.getFlag(); + if (!flagName.equals(Flags.nofly.getName())) + return; + + var newState = e.getNewState(); + var canFly = newState == FlagPermissions.FlagState.FALSE || newState == FlagPermissions.FlagState.NEITHER; + + players.forEach(p -> + { + if (!canFly) + { + FlyAbility.blockPlayer(p, this); + } + else + { + FlyAbility.unBlockPlayer(p, this); + } + }); + } +}