Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #662 from book000/fix/discord
Browse files Browse the repository at this point in the history
fix: Discord連携関連の処理修正
  • Loading branch information
book000 authored Jan 24, 2022
2 parents d05a11b + 3f58a4a commit 49911f2
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 86 deletions.
150 changes: 76 additions & 74 deletions src/main/java/com/jaoafa/mymaid4/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,76 +73,6 @@ public final class Main extends JavaPlugin {
private static Rollbar rollbar = null;
private MinecraftHelp<CommandSender> minecraftHelp;

public static void registerDiscordEvent(JDABuilder d) {
getJavaPlugin().getLogger().info("----- registerDiscordEvent -----");
try {
ClassFinder classFinder = new ClassFinder(getMain().getClassLoader());
for (Class<?> clazz : classFinder.findClasses("com.jaoafa.mymaid4.discordEvent")) {
if (!clazz.getName().startsWith("com.jaoafa.mymaid4.discordEvent.DiscordEvent_")) {
continue;
}
if (clazz.getEnclosingClass() != null) {
continue;
}
if (clazz.getName().contains("$")) {
continue;
}
String name = clazz.getName().substring("com.jaoafa.mymaid4.discordEvent.DiscordEvent_".length())
.toLowerCase();
try {
Constructor<?> construct = clazz.getConstructor();
Object instance = construct.newInstance();

d.addEventListeners(instance);
getJavaPlugin().getLogger().info(String.format("%s: Discordイベントの登録に成功しました。", clazz.getSimpleName()));
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
getJavaPlugin().getLogger().warning(String.format("%s: Discordイベントの登録に成功しました。", name));
e.printStackTrace();
MyMaidLibrary.reportError(Main.class, e);
}
}
} catch (ClassNotFoundException | IOException e) {
getJavaPlugin().getLogger().warning("Discordイベントの登録に失敗しました。");
e.printStackTrace();
MyMaidLibrary.reportError(Main.class, e);
}
}

public static WorldEditPlugin getWorldEdit() {
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");

if (!(plugin instanceof WorldEditPlugin)) {
return null;
}

return (WorldEditPlugin) plugin;
}

private static Component convertCause(final Throwable throwable) {
final Component msg = ComponentMessageThrowable.getOrConvertMessage(throwable);
return msg != null ? msg : Component.text("null");
}

public static Main getMain() {
return Main;
}

public static JavaPlugin getJavaPlugin() {
return Main;
}

public static Logger getMyMaidLogger() {
return Main.getLogger();
}

public static MyMaidConfig getMyMaidConfig() {
return config;
}

public static Rollbar getRollbar() {
return rollbar;
}

@Override
public void onEnable() {
Main = this;
Expand Down Expand Up @@ -195,6 +125,7 @@ public void onDisable() {
}
}


private void registerCommand() {
getLogger().info("----- registerCommand -----");
final PaperCommandManager<CommandSender> manager;
Expand Down Expand Up @@ -473,6 +404,47 @@ private void registerEvent() {
}
}

public static void registerDiscordEvent(JDABuilder d) {
getJavaPlugin().getLogger().info("----- registerDiscordEvent -----");
try {
ClassFinder classFinder = new ClassFinder(getMain().getClassLoader());
for (Class<?> clazz : classFinder.findClasses("com.jaoafa.mymaid4.discordEvent")) {
if (!clazz.getName().startsWith("com.jaoafa.mymaid4.discordEvent.DiscordEvent_")) {
continue;
}
if (clazz.getEnclosingClass() != null) {
continue;
}
if (clazz.getName().contains("$")) {
continue;
}
String name = clazz.getName().substring("com.jaoafa.mymaid4.discordEvent.DiscordEvent_".length())
.toLowerCase();
try {
Constructor<?> construct = clazz.getConstructor();
Object instance = construct.newInstance();

d.addEventListeners(instance);
getJavaPlugin().getLogger().info(String.format("%s: Discordイベントの登録に成功しました。", clazz.getSimpleName()));
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
getJavaPlugin().getLogger().warning(String.format("%s: Discordイベントの登録に成功しました。", name));
e.printStackTrace();
MyMaidLibrary.reportError(Main.class, e);
}
}
} catch (ClassNotFoundException | IOException e) {
getJavaPlugin().getLogger().warning("Discordイベントの登録に失敗しました。");
e.printStackTrace();
MyMaidLibrary.reportError(Main.class, e);
}
}

private void scheduleTasks() {
new MyMaidServer().runTaskAsynchronously(this);
new Task_Pigeon().runTaskTimerAsynchronously(this, 200L, 12000L); // 10秒後から10分毎
new Task_TabList().runTaskTimerAsynchronously(this, 200L, 1200L); // 10秒後から1分毎
}

private void initCreativeInventoryItems() {
try {
Path path = Paths.get(getDataFolder().getAbsolutePath(), "creative-items.tsv");
Expand Down Expand Up @@ -509,9 +481,39 @@ private void initCreativeInventoryItems() {
}
}

private void scheduleTasks() {
new MyMaidServer().runTaskAsynchronously(this);
new Task_Pigeon().runTaskTimerAsynchronously(this, 200L, 12000L); // 10秒後から10分毎
new Task_TabList().runTaskTimerAsynchronously(this, 200L, 1200L); // 10秒後から1分毎
public static WorldEditPlugin getWorldEdit() {
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");

if (!(plugin instanceof WorldEditPlugin)) {
return null;
}

return (WorldEditPlugin) plugin;
}

private static Component convertCause(final Throwable throwable) {
final Component msg = ComponentMessageThrowable.getOrConvertMessage(throwable);
return msg != null ? msg : Component.text("null");
}

public static Main getMain() {
return Main;
}

public static JavaPlugin getJavaPlugin() {
return Main;
}

public static Logger getMyMaidLogger() {
return Main.getLogger();
}

public static MyMaidConfig getMyMaidConfig() {
return config;
}

public static Rollbar getRollbar() {
return rollbar;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* jaoLicense
*
* Copyright (c) 2021 jao Minecraft Server
* Copyright (c) 2022 jao Minecraft Server
*
* The following license applies to this project: jaoLicense
*
Expand All @@ -15,9 +15,9 @@
import com.jaoafa.jaosuperachievement2.lib.Achievement;
import com.jaoafa.mymaid4.lib.MyMaidData;
import com.jaoafa.mymaid4.lib.MyMaidLibrary;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.hooks.SubscribeEvent;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
Expand All @@ -28,10 +28,9 @@
import java.sql.SQLException;
import java.util.UUID;

public class DiscordEvent_ListCmd {
public class DiscordEvent_ListCmd extends ListenerAdapter {
@SubscribeEvent
public void onListCommand(MessageReceivedEvent event) {
JDA jda = event.getJDA();
public void onMessageReceived(MessageReceivedEvent event) {
if (!event.isFromType(ChannelType.TEXT)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* jaoLicense
*
* Copyright (c) 2021 jao Minecraft Server
* Copyright (c) 2022 jao Minecraft Server
*
* The following license applies to this project: jaoLicense
*
Expand All @@ -14,11 +14,12 @@
import com.jaoafa.mymaid4.Main;
import com.jaoafa.mymaid4.lib.MyMaidData;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.hooks.SubscribeEvent;

public class DiscordEvent_Ready {
public class DiscordEvent_Ready extends ListenerAdapter {
@SubscribeEvent
public void onReadyEvent(ReadyEvent event) {
public void onReady(ReadyEvent event) {
System.out.println("Ready: " + event.getJDA().getSelfUser().getName());

MyMaidData.setReportChannel(event.getJDA().getTextChannelById(Main.getMyMaidConfig().getReportChannelId()));
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/jaoafa/mymaid4/lib/MyMaidConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* jaoLicense
*
* Copyright (c) 2021 jao Minecraft Server
* Copyright (c) 2022 jao Minecraft Server
*
* The following license applies to this project: jaoLicense
*
Expand All @@ -14,7 +14,7 @@
import com.jaoafa.mymaid4.Main;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.hooks.AnnotatedEventManager;
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down Expand Up @@ -70,11 +70,10 @@ public void init() {
if (discord.contains("token")) {
try {
JDABuilder jdabuilder = JDABuilder.createDefault(discord.getString("token"))
// .enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_PRESENCES)
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_PRESENCES, GatewayIntent.GUILD_MESSAGES)
.setAutoReconnect(true)
.setBulkDeleteSplittingEnabled(false)
.setContextEnabled(false)
.setEventManager(new AnnotatedEventManager())
.setRawEventsEnabled(false);

Main.registerDiscordEvent(jdabuilder);
Expand Down

0 comments on commit 49911f2

Please sign in to comment.