Skip to content

Commit

Permalink
new: 适配Residence的noFly选项
Browse files Browse the repository at this point in the history
    - 设置为false或remove为允许飞行,设置为true或未知选项为禁止飞行
  • Loading branch information
MATRIX-feather committed Dec 9, 2023
1 parent ed78e6d commit 725a941
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 3 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/xiamomc/morph/MorphPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
36 changes: 34 additions & 2 deletions src/main/java/xiamomc/morph/abilities/impl/FlyAbility.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<FlyOption>
{
Expand Down Expand Up @@ -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)
Expand All @@ -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())
{
Expand Down Expand Up @@ -232,4 +237,31 @@ public void onGameModeChange(PlayerGameModeChangeEvent e)
this.appliedPlayers.remove(player);
}
}

private static final Map<Player, Stack<Object>> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
}

0 comments on commit 725a941

Please sign in to comment.