-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Key changes: - BlockBot API for handling of events and listeners - Mixins now trigger events - When adding a bot, the registerBot method in BlockBot should be called. This will handle all the listeners needed for the bot - All bots need to implement the Bot interface and call the registerBot method This is not final but makes up the framework for the future layout of BlockBot. Any suggestions please contact me on the discord Signed-off-by: yitzy299 <[email protected]>
- Loading branch information
yitzy299
committed
Jun 15, 2021
1 parent
d14f22e
commit c4e5347
Showing
13 changed files
with
266 additions
and
151 deletions.
There are no files selected for viewing
35 changes: 29 additions & 6 deletions
35
src/main/java/com/github/quiltservertools/blockbot/BlockBot.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,57 @@ | ||
package com.github.quiltservertools.blockbot; | ||
|
||
import com.github.quiltservertools.blockbot.api.Bot; | ||
import com.github.quiltservertools.blockbot.api.event.ChatMessageEvent; | ||
import com.github.quiltservertools.blockbot.api.event.PlayerAdvancementGrantEvent; | ||
import com.github.quiltservertools.blockbot.api.event.PlayerDeathEvent; | ||
import net.fabricmc.api.DedicatedServerModInitializer; | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; | ||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; | ||
import net.minecraft.server.MinecraftServer; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import javax.security.auth.login.LoginException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class BlockBot implements DedicatedServerModInitializer { | ||
public static Config CONFIG; | ||
public static Logger LOG; | ||
public static Discord DISCORD; | ||
public static final List<Bot> bots = new ArrayList<>(); | ||
|
||
@Override | ||
public void onInitializeServer() { | ||
LOG = LogManager.getLogger(); | ||
CONFIG = new Config(); | ||
ServerLifecycleEvents.SERVER_STARTING.register(server -> { | ||
CONFIG = new Config(); | ||
try { | ||
DISCORD = new Discord(CONFIG, server); | ||
DISCORD.serverStatus(true); | ||
var bot = new BlockBotDiscord(); | ||
registerBot(bot, CONFIG, server); | ||
} catch (LoginException e) { | ||
e.printStackTrace(); | ||
server.stop(false); | ||
} | ||
}); | ||
ServerLifecycleEvents.SERVER_STARTED.register(server -> bots.forEach(bot -> bot.serverStatus(true))); | ||
ServerLifecycleEvents.SERVER_STOPPING.register(server -> { | ||
CONFIG.shutdown(); | ||
DISCORD.serverStatus(false); | ||
DISCORD.shutdown(); | ||
bots.forEach(bot -> { | ||
bot.serverStatus(false); | ||
bot.onShutdown(); | ||
}); | ||
}); | ||
} | ||
|
||
public void registerBot(Bot bot, Config config, MinecraftServer server) throws LoginException { | ||
bots.add(bot); | ||
bot.registerListeners(config, server); | ||
// Events | ||
ChatMessageEvent.EVENT.register(bot::onChatMessage); | ||
PlayerDeathEvent.EVENT.register(bot::onDeathMessage); | ||
PlayerAdvancementGrantEvent.EVENT.register(bot::onAdvancementGrant); | ||
|
||
ServerPlayConnectionEvents.JOIN.register(bot::onPlayerConnect); | ||
ServerPlayConnectionEvents.DISCONNECT.register(bot::onPlayerDisconnect); | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
src/main/java/com/github/quiltservertools/blockbot/BlockBotDiscord.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.github.quiltservertools.blockbot; | ||
|
||
import club.minnced.discord.webhook.WebhookClient; | ||
import club.minnced.discord.webhook.WebhookClientBuilder; | ||
import club.minnced.discord.webhook.send.*; | ||
import com.github.quiltservertools.blockbot.api.Bot; | ||
import com.github.quiltservertools.blockbot.api.event.ChatMessageEvent; | ||
import com.github.quiltservertools.blockbot.api.event.PlayerAdvancementGrantEvent; | ||
import com.github.quiltservertools.blockbot.api.event.PlayerDeathEvent; | ||
import net.dv8tion.jda.api.JDA; | ||
import net.dv8tion.jda.api.JDABuilder; | ||
import net.dv8tion.jda.api.entities.Activity; | ||
import net.dv8tion.jda.api.managers.Presence; | ||
import net.fabricmc.fabric.api.networking.v1.PacketSender; | ||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; | ||
import net.minecraft.advancement.Advancement; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.network.ServerPlayNetworkHandler; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.text.Text; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Protocol; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import javax.security.auth.login.LoginException; | ||
import java.util.Collections; | ||
import java.util.Objects; | ||
|
||
import static com.github.quiltservertools.blockbot.BlockBotUtils.getAvatarUrl; | ||
|
||
public class BlockBotDiscord implements Bot { | ||
private WebhookClient webhook; | ||
private JDA jda; | ||
private Status status; | ||
|
||
@Override | ||
public void registerListeners(Config config, MinecraftServer server) throws LoginException { | ||
jda = JDABuilder.createDefault(config.getIdentifier()).build(); | ||
jda.addEventListener(new Listeners(config, server)); | ||
BlockBot.LOG.info("Setup discord bot with token provided"); | ||
// Init webhook | ||
WebhookClientBuilder builder = new WebhookClientBuilder(config.getWebhook()); | ||
builder.setHttpClient(new OkHttpClient.Builder() | ||
.protocols(Collections.singletonList(Protocol.HTTP_1_1)) | ||
.build()); | ||
builder.setDaemon(true); | ||
builder.setAllowedMentions(AllowedMentions.none().withParseUsers(true)); | ||
this.webhook = builder.build(); | ||
|
||
this.status = new Status(); | ||
} | ||
|
||
public JDA getBot() { | ||
return jda; | ||
} | ||
|
||
@Override | ||
public void onChatMessage(ServerPlayerEntity player, String message) { | ||
webhook.send(prepareChatMessage(player, message)); | ||
} | ||
|
||
private WebhookMessage prepareChatMessage(ServerPlayerEntity player, String message) { | ||
WebhookMessageBuilder builder = new WebhookMessageBuilder(); | ||
builder.setUsername(player.getName().getString()); | ||
builder.setAvatarUrl(getAvatarUrl(player)); | ||
builder.setContent(message); | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public void serverStatus(boolean starting) { | ||
System.out.println(BlockBot.CONFIG.sendStatusMessages()); | ||
if (!BlockBot.CONFIG.sendStatusMessages()) return; | ||
WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); | ||
builder.setAuthor(new WebhookEmbed.EmbedAuthor(BlockBot.CONFIG.getName(), BlockBot.CONFIG.getLogo(), null)); | ||
builder.setColor(starting ? 3334259 : 14695980); | ||
builder.setDescription(starting ? "Server Started" : "Server Stopped"); | ||
webhook.send(builder.build()); | ||
} | ||
|
||
@Override | ||
public void onDeathMessage(ServerPlayerEntity player, Text message) { | ||
WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); | ||
builder.setAuthor(new WebhookEmbed.EmbedAuthor(player.getName().asString(), getAvatarUrl(player), null)); | ||
String msg = message.getString().replaceFirst(player.getName().asString() + " ", ""); | ||
msg = StringUtils.capitalize(msg); | ||
builder.setDescription(msg); | ||
builder.setColor(15789375); | ||
webhook.send(builder.build()); | ||
} | ||
|
||
@Override | ||
public void onAdvancementGrant(ServerPlayerEntity player, Advancement advancement) { | ||
if (!BlockBot.CONFIG.sendAdvancementMessages()) return; | ||
WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); | ||
builder.setDescription(player.getName().getString() + " has made the advancement [" + Objects.requireNonNull(advancement.getDisplay()).getTitle().getString() + "]"); | ||
builder.setColor(16771646); | ||
builder.setAuthor(new WebhookEmbed.EmbedAuthor(player.getName().asString(), getAvatarUrl(player), null)); | ||
webhook.send(builder.build()); | ||
} | ||
|
||
@Override | ||
public void onShutdown() { | ||
jda.shutdown(); | ||
webhook.close(); | ||
} | ||
|
||
@Override | ||
public void tickStatus(MinecraftServer server) { | ||
if (!BlockBot.CONFIG.showPresence()) return; | ||
Presence presence = jda.getPresence(); | ||
Activity status = Activity.playing(BlockBot.CONFIG.getName() + " - " + this.status.getPlayerCount() + " online"); | ||
presence.setActivity(status); | ||
} | ||
|
||
@Override | ||
public void onPlayerConnect(ServerPlayNetworkHandler handler, PacketSender packetSender, MinecraftServer server) { | ||
webhook.send(buildConnectMessage(handler.getPlayer(), true)); | ||
tickStatus(server); | ||
} | ||
|
||
@Override | ||
public void onPlayerDisconnect(ServerPlayNetworkHandler handler, MinecraftServer server) { | ||
webhook.send(buildConnectMessage(handler.getPlayer(), false)); | ||
tickStatus(server); | ||
} | ||
|
||
private WebhookEmbed buildConnectMessage(ServerPlayerEntity player, boolean joined) { | ||
WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); | ||
builder.setAuthor(new WebhookEmbed.EmbedAuthor(player.getName().asString(), getAvatarUrl(player), null)); | ||
builder.setDescription(joined ? "Joined the game" : "Left the game"); | ||
builder.setColor(joined ? 3334259 : 14695980); | ||
return builder.build(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/github/quiltservertools/blockbot/BlockBotUtils.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.github.quiltservertools.blockbot; | ||
|
||
import net.minecraft.server.network.ServerPlayerEntity; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class BlockBotUtils { | ||
public static String getAvatarUrl(ServerPlayerEntity player) { | ||
String url = "https://crafatar.com/avatars/" + player.getUuidAsString() + "?t=" + LocalDateTime.now().getHour(); | ||
if (BlockBot.CONFIG.enableSkinOverlay()) url = url.concat("&overlay"); | ||
return url; | ||
} | ||
} |
115 changes: 0 additions & 115 deletions
115
src/main/java/com/github/quiltservertools/blockbot/Discord.java
This file was deleted.
Oops, something went wrong.
This file contains 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
15 changes: 4 additions & 11 deletions
15
src/main/java/com/github/quiltservertools/blockbot/Status.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,26 @@ | ||
package com.github.quiltservertools.blockbot; | ||
|
||
import net.dv8tion.jda.api.entities.Activity; | ||
import net.dv8tion.jda.api.managers.Presence; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
public class Status { | ||
private List<UUID> players; | ||
private final List<UUID> players; | ||
|
||
public Status() { | ||
players = new ArrayList<>(); | ||
} | ||
|
||
private int getPlayerCount() { | ||
public int getPlayerCount() { | ||
return players.size(); | ||
} | ||
|
||
public void addPlayer(UUID uuid) { | ||
players.add(uuid); | ||
} | ||
|
||
public void removePlayer(UUID uuid) { | ||
players.remove(uuid); | ||
} | ||
|
||
public void update() { | ||
if (!BlockBot.CONFIG.showPresence()) return; | ||
Presence presence = BlockBot.DISCORD.getJda().getPresence(); | ||
Activity status = Activity.playing(BlockBot.CONFIG.getName() + " - " + getPlayerCount() + " online"); | ||
presence.setActivity(status); | ||
} | ||
} |
Oops, something went wrong.