Last active
February 27, 2019 17:27
-
-
Save Joo200/39672a561474668540bb20e9d9668062 to your computer and use it in GitHub Desktop.
ACF Implementation for WorldGuard flags.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package de.terraconia.commands.context; | |
| import co.aikar.commands.BukkitCommandExecutionContext; | |
| import co.aikar.commands.CommandContexts; | |
| import com.sk89q.worldguard.LocalPlayer; | |
| import com.sk89q.worldguard.WorldGuard; | |
| import com.sk89q.worldguard.bukkit.WorldGuardPlugin; | |
| import com.sk89q.worldguard.protection.flags.Flag; | |
| import com.sk89q.worldguard.protection.regions.RegionContainer; | |
| import org.bukkit.entity.Player; | |
| public class FlagHolder<T> { | |
| private Flag<T> flag; | |
| private T value; | |
| public FlagHolder(Flag<T> flag) { | |
| this.flag = flag; | |
| } | |
| @SuppressWarnings("unchecked") | |
| public FlagHolder(String flagName) { | |
| Flag<?> flag1 = WorldGuard.getInstance().getFlagRegistry().get(flagName); | |
| if(flag1 == null) | |
| throw new IllegalArgumentException("Unknown flag: " + flagName); | |
| try { | |
| flag = (Flag<T>) flag1; // Warning - unchecked cast. | |
| } catch(ClassCastException e) { | |
| throw new IllegalArgumentException("Invalid object type requested."); | |
| } | |
| } | |
| public FlagHolder context(Player player) { | |
| LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); | |
| RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); | |
| value = container.createQuery().queryValue(localPlayer.getLocation(), localPlayer, this.flag); | |
| return this; | |
| } | |
| public boolean hasValue() { | |
| return value != null; | |
| } | |
| public T getValue() { | |
| return value; | |
| } | |
| public static void registerContext(CommandContexts<BukkitCommandExecutionContext> context) { | |
| context.registerIssuerOnlyContext(FlagHolder.class, bContext -> { | |
| String flagName = bContext.getFlagValue("flag", (String) null); | |
| if(flagName == null) throw new IllegalArgumentException("Missing Flag annotation or Flag value"); | |
| return (FlagHolder<?>) new FlagHolder<>(flagName).context(bContext.getPlayer()); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment