Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Plugin: create HookManager
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFaser committed Aug 12, 2023
1 parent 3d5c0e4 commit 0f6abf5
Show file tree
Hide file tree
Showing 23 changed files with 208 additions and 164 deletions.
57 changes: 2 additions & 55 deletions src/main/java/net/flectone/Main.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package net.flectone;

import net.flectone.integrations.discordsrv.FDiscordSRV;
import net.flectone.integrations.expansions.FExpansion;
import net.flectone.integrations.luckperms.FLuckPerms;
import net.flectone.integrations.supervanish.FSuperVanish;
import net.flectone.integrations.vault.FVault;
import net.flectone.integrations.voicechats.simplevoicechat.RegisterSimpleVoiceChat;
import net.flectone.listeners.PlayerAdvancementDoneListener;
import net.flectone.listeners.PlayerDeathEventListener;
import net.flectone.managers.FPlayerManager;
import net.flectone.managers.FileManager;
import net.flectone.managers.HookManager;
import net.flectone.managers.TickerManager;
import net.flectone.misc.commands.FTabCompleter;
import net.flectone.sqlite.Database;
Expand All @@ -25,11 +20,6 @@

public final class Main extends JavaPlugin {

public static boolean isHavePAPI = false;

public static boolean isHavePlasmoVoice = false;

public static boolean isHaveInteractiveChat = false;
private static Main instance;
private Database database;

Expand Down Expand Up @@ -65,7 +55,7 @@ public void onEnable() {
FPlayerManager.loadBanList();

registerClasses();
hookPlugins();
HookManager.hookPlugins();

TickerManager.start();
PlayerDeathEventListener.reload();
Expand All @@ -91,49 +81,6 @@ private void registerClasses() {
});
}

private void hookPlugins() {
if (Bukkit.getPluginManager().getPlugin("Vault") != null) {
FVault.register();
getLogger().info("\uD83D\uDD12 Vault detected and hooked");
}

if (Bukkit.getPluginManager().getPlugin("voicechat") != null) {
new RegisterSimpleVoiceChat();
getLogger().info("\uD83D\uDD12 SimpleVoiceChat detected and hooked");
}

if (Bukkit.getPluginManager().getPlugin("plasmovoice") != null) {
isHavePlasmoVoice = true;
getLogger().info("\uD83D\uDD12 PlasmoVoice detected and hooked");
}

if (Bukkit.getPluginManager().getPlugin("InteractiveChat") != null) {
isHaveInteractiveChat = true;
getLogger().info("\uD83D\uDD12 InteractiveChat detected and hooked");
}

if (Bukkit.getPluginManager().getPlugin("LuckPerms") != null) {
new FLuckPerms(this);
getLogger().info("\uD83D\uDD12 LuckPerms detected and hooked");
}

if (Bukkit.getPluginManager().getPlugin("SuperVanish") != null) {
Bukkit.getPluginManager().registerEvents(new FSuperVanish(), this);
getLogger().info("\uD83D\uDD12 SuperVanish detected and hooked");
}

if (Bukkit.getPluginManager().getPlugin("DiscordSRV") != null) {
FDiscordSRV.register();
getLogger().info("\uD83D\uDD12 DiscordSRV detected and hooked");
}

if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
isHavePAPI = true;
getLogger().info("\uD83D\uDD12 PlaceholderAPI detected and hooked");
new FExpansion().register();
}
}

@Override
public void onDisable() {
FPlayerManager.uploadPlayers();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/flectone/commands/CommandMaintenance.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command

String maintenanceMessage = "command.maintenance.turned-" + strings[0].toLowerCase() + ".message";

FDiscordSRV.sendModerationMessage(locale.getString(maintenanceMessage));
if (FDiscordSRV.isEnable()) {
FDiscordSRV.sendModerationMessage(locale.getString(maintenanceMessage));
}

fCommand.sendMeMessage(maintenanceMessage);
config.set("command.maintenance.enable", haveMaintenance);
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/net/flectone/commands/CommandMute.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package net.flectone.commands;

import net.flectone.Main;
import net.flectone.misc.commands.FCommand;
import net.flectone.misc.entity.FPlayer;
import net.flectone.misc.commands.FTabCompleter;
import net.flectone.integrations.discordsrv.FDiscordSRV;
import net.flectone.integrations.voicechats.plasmovoice.FlectonePlasmoVoice;
import net.flectone.integrations.voicechats.plasmovoice.FPlasmoVoice;
import net.flectone.managers.FPlayerManager;
import net.flectone.misc.commands.FCommand;
import net.flectone.misc.commands.FTabCompleter;
import net.flectone.misc.entity.FPlayer;
import net.flectone.utils.ObjectUtil;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
Expand All @@ -22,8 +21,8 @@
import java.util.Set;
import java.util.stream.Collectors;

import static net.flectone.managers.FileManager.locale;
import static net.flectone.managers.FileManager.config;
import static net.flectone.managers.FileManager.locale;

public class CommandMute implements FTabCompleter {

Expand Down Expand Up @@ -69,7 +68,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command

boolean announceModeration = config.getBoolean("command.mute.announce");

if (announceModeration) FDiscordSRV.sendModerationMessage(formatString);
if (announceModeration && FDiscordSRV.isEnable()) FDiscordSRV.sendModerationMessage(formatString);

Set<Player> receivers = announceModeration
? new HashSet<>(Bukkit.getOnlinePlayers())
Expand All @@ -79,8 +78,8 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command

fCommand.sendGlobalMessage(receivers, formatString, false);

if (Main.isHavePlasmoVoice) {
FlectonePlasmoVoice.mute(mutedFPlayer.isMuted(), mutedFPlayer.getRealName(), strings[1], reason);
if (FPlasmoVoice.isEnable()) {
FPlasmoVoice.mute(mutedFPlayer.isMuted(), mutedFPlayer.getRealName(), strings[1], reason);
}

mutedFPlayer.mute(time, reason);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/flectone/commands/CommandStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command

});

FDiscordSRV.sendModerationMessage(stringBuilder.toString());
if (FDiscordSRV.isEnable()) {
FDiscordSRV.sendModerationMessage(stringBuilder.toString());
}

fCommand.sendGlobalMessage(stringBuilder.toString());

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/flectone/commands/CommandTempban.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command

boolean announceModeration = config.getBoolean("command.tempban.announce");

if (announceModeration) FDiscordSRV.sendModerationMessage(globalMessage);
if (announceModeration && FDiscordSRV.isEnable()) FDiscordSRV.sendModerationMessage(globalMessage);

Set<Player> receivers = announceModeration
? new HashSet<>(Bukkit.getOnlinePlayers())
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/net/flectone/commands/CommandUnmute.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package net.flectone.commands;

import net.flectone.Main;
import net.flectone.integrations.voicechats.plasmovoice.FPlasmoVoice;
import net.flectone.managers.FPlayerManager;
import net.flectone.misc.commands.FCommand;
import net.flectone.misc.entity.FPlayer;
import net.flectone.misc.commands.FTabCompleter;
import net.flectone.integrations.voicechats.plasmovoice.FlectonePlasmoVoice;
import net.flectone.managers.FPlayerManager;
import net.flectone.misc.entity.FPlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -37,8 +36,8 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command

if (fCommand.isHaveCD()) return true;

if (Main.isHavePlasmoVoice) {
FlectonePlasmoVoice.unmute(fPlayer.getRealName());
if (FPlasmoVoice.isEnable()) {
FPlasmoVoice.unmute(fPlayer.getRealName());
}

fPlayer.unmute();
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/flectone/integrations/HookInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.flectone.integrations;

@FunctionalInterface
public interface HookInterface {

void hook();
}
29 changes: 13 additions & 16 deletions src/main/java/net/flectone/integrations/discordsrv/FDiscordSRV.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import github.scarsz.discordsrv.util.MessageUtil;
import github.scarsz.discordsrv.util.PlaceholderUtil;
import github.scarsz.discordsrv.util.TimeUtil;
import net.flectone.Main;
import net.flectone.integrations.HookInterface;
import net.flectone.misc.advancement.FAdvancement;
import net.flectone.utils.ObjectUtil;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -54,22 +56,11 @@ License, or (at your option) any later version.
*/

public class FDiscordSRV implements Listener {
public class FDiscordSRV implements Listener, HookInterface {

private static boolean isEnable = false;

public FDiscordSRV() {
isEnable = true;
}

public static void register() {
FDiscordSRV fDiscordSRV = new FDiscordSRV();
DiscordSRV.api.subscribe(fDiscordSRV);
}

public static void sendDeathMessage(@NotNull Player player, @NotNull String message, @Nullable Entity finalEntity, @Nullable Material finalBlock, @Nullable Entity killer, @Nullable ItemStack killerItem) {
if (!isEnable) return;

message = message.replace("<player>", player.getName());
if (finalEntity != null) message = message
.replace("<killer>", finalEntity.getName())
Expand Down Expand Up @@ -145,8 +136,6 @@ public static void sendDeathMessage(@NotNull Player player, @NotNull String mess
}

public static void sendModerationMessage(@NotNull String message) {
if (!isEnable) return;

message = ObjectUtil.formatString(message, null);
message = PlaceholderUtil.replacePlaceholdersToDiscord(message);

Expand All @@ -161,8 +150,6 @@ public static void sendModerationMessage(@NotNull String message) {
}

public static void sendAdvancementMessage(@NotNull Player player, @NotNull FAdvancement fAdvancement, @NotNull String lastAdvancement) {
if (!isEnable) return;

String channelName = DiscordSRV.getPlugin().getOptionalChannel("awards");

MessageFormat messageFormat = DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerAchievementMessage");
Expand Down Expand Up @@ -230,4 +217,14 @@ public void onChatMessageFromInGame(@NotNull GameChatMessagePreProcessEvent even
event.setMessage(message);
}

public static boolean isEnable() {
return isEnable;
}

@Override
public void hook() {
DiscordSRV.api.subscribe(this);
isEnable = true;
Main.info("\uD83D\uDD12 DiscordSRV detected and hooked");
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package net.flectone.integrations.expansions;

import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.flectone.misc.entity.FPlayer;
import net.flectone.Main;
import net.flectone.integrations.HookInterface;
import net.flectone.managers.FPlayerManager;
import net.flectone.misc.entity.FPlayer;
import net.flectone.utils.ObjectUtil;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class FExpansion extends PlaceholderExpansion {
public class FPlaceholderAPI extends PlaceholderExpansion implements HookInterface {

public FExpansion() {}
private static boolean isEnable;

public static boolean isEnable() {
return isEnable;
}

@Override
public @NotNull String getIdentifier() {
Expand Down Expand Up @@ -49,4 +55,11 @@ public String onRequest(@Nullable OfflinePlayer player, @NotNull String params)
default -> null;
};
}

@Override
public void hook() {
register();
isEnable = true;
Main.info("\uD83D\uDD12 PlaceholderAPI detected and hooked");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.flectone.integrations.interactivechat;

import com.loohp.interactivechat.api.InteractiveChatAPI;
import net.flectone.Main;
import net.flectone.integrations.HookInterface;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public class FInteractiveChat implements HookInterface {

private static boolean isEnable;

public static boolean isEnable() {
return isEnable;
}

@NotNull
public static String mark(@NotNull String message, @NotNull UUID sender) {
return InteractiveChatAPI.markSender(message, sender);
}

@Override
public void hook() {
isEnable = true;
Main.info("\uD83D\uDD12 InteractiveChat detected and hooked");
}
}

This file was deleted.

28 changes: 17 additions & 11 deletions src/main/java/net/flectone/integrations/luckperms/FLuckPerms.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package net.flectone.integrations.luckperms;

import net.flectone.Main;
import net.flectone.misc.entity.FPlayer;
import net.flectone.integrations.HookInterface;
import net.flectone.managers.FPlayerManager;
import net.flectone.misc.entity.FPlayer;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.event.EventBus;
import net.luckperms.api.event.node.NodeAddEvent;
import net.luckperms.api.event.node.NodeMutateEvent;
import net.luckperms.api.model.user.User;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.jetbrains.annotations.NotNull;

public class FLuckPerms {

public FLuckPerms(@NotNull Main plugin) {

LuckPerms luckPerms = Bukkit.getServicesManager().getRegistration(LuckPerms.class).getProvider();

EventBus eventBus = luckPerms.getEventBus();

eventBus.subscribe(plugin, NodeAddEvent.class, this::onUserPromote);
}
public class FLuckPerms implements HookInterface {

private void onUserPromote(@NotNull NodeMutateEvent event) {
if (event.getTarget() instanceof User) {
Expand All @@ -39,4 +32,17 @@ private void onUserPromote(@NotNull NodeMutateEvent event) {
fPlayer.setDisplayName();
});
}

@Override
public void hook() {
RegisteredServiceProvider<LuckPerms> serviceProvider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);

if (serviceProvider == null) return;

EventBus eventBus = serviceProvider.getProvider().getEventBus();

eventBus.subscribe(Main.getInstance(), NodeAddEvent.class, this::onUserPromote);

Main.info("\uD83D\uDD12 LuckPerms detected and hooked");
}
}

0 comments on commit 0f6abf5

Please sign in to comment.