Skip to content

Commit

Permalink
랜덤 룰렛 기능 추가, 컨피그 리로드 기능 추가.
Browse files Browse the repository at this point in the history
  • Loading branch information
taromati committed Apr 26, 2024
1 parent d126325 commit 76da88e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = 'me.taromati'
version = '1.0.0'
version = '1.1.0'

repositories {
mavenCentral()
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/me/taromati/doneconnector/DoneConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import java.util.*;

public final class DoneConnector extends JavaPlugin implements Listener {
public static boolean debug;
public static Plugin plugin;

public static boolean debug;
public static boolean random;

private static final List<Map<String, String>> chzzkUserList = new ArrayList<>();
private static final HashMap<Integer, List<String>> donationRewards = new HashMap<>();
List<ChzzkWebSocket> chzzkWebSocketList = new ArrayList<>();
Expand Down Expand Up @@ -59,10 +61,19 @@ public void onDisable() {
Logger.info(ChatColor.GREEN + "플러그인 비활성화 완료.");
}

private void clearConfig() {
debug = false;
random = false;
chzzkUserList.clear();
donationRewards.clear();
reloadConfig();
}

private void loadConfig() throws DoneException {
this.saveResource("config.yml", false);
try {
debug = this.getConfig().getBoolean("디버그");
random = this.getConfig().getBoolean("랜덤 보상");
} catch (Exception e) {
throw new DoneException(ExceptionCode.CONFIG_LOAD_ERROR);
}
Expand Down Expand Up @@ -156,14 +167,22 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
} else {
try {
if (args[0].equalsIgnoreCase("on")) {
Logger.info(ChatColor.GREEN + "후원 기능을 활성화 합니다.");
Logger.info(ChatColor.YELLOW + "후원 기능을 활성화 합니다.");
connectChzzk(chzzkUserList);
} else if (args[0].equalsIgnoreCase("off")) {
Logger.info(ChatColor.YELLOW + "후원 기능을 비활성화 합니다.");
disconnectChzzk(chzzkWebSocketList);
} else if (args[0].equalsIgnoreCase("reconnect")) {
Logger.info(ChatColor.YELLOW + "후원 기능을 재접속합니다.");
Logger.say(ChatColor.YELLOW + "후원 기능을 재접속합니다.");
disconnectChzzk(chzzkWebSocketList);
connectChzzk(chzzkUserList);
} else if (args[0].equalsIgnoreCase("reload")) {
Logger.info(ChatColor.YELLOW + "후원 설정을 다시 불러옵니다.");
Logger.say(ChatColor.YELLOW + "후원 설정을 다시 불러옵니다.");
disconnectChzzk(chzzkWebSocketList);
clearConfig();
loadConfig();
connectChzzk(chzzkUserList);
} else {
return false;
Expand All @@ -181,7 +200,7 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String labe
if (sender.isOp() == false) {
return Collections.emptyList();
} else if (args.length == 1) {
List<String> commandList = new ArrayList<>(Arrays.asList("on", "off", "reconnect"));
List<String> commandList = new ArrayList<>(Arrays.asList("on", "off", "reconnect", "reload"));

if (args[0].isEmpty()) {
return commandList;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/me/taromati/doneconnector/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ public class Logger {
public static void info(String msg) {
Bukkit.getConsoleSender().sendMessage(prefix + msg);
}

public static void say(String msg) {
String command = "say " + msg;
Bukkit.getScheduler()
.callSyncMethod(DoneConnector.plugin, () -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command));
}
}
44 changes: 24 additions & 20 deletions src/main/java/me/taromati/doneconnector/chzzk/ChzzkWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import org.json.simple.parser.JSONParser;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;

public class ChzzkWebSocket extends WebSocketClient {
private final String chatChannelId;
Expand Down Expand Up @@ -112,26 +109,32 @@ public void onMessage(String message) {
int payAmount = Integer.parseInt(extraObject.get("payAmount").toString());

Logger.info(ChatColor.YELLOW + nickname + ChatColor.WHITE + "님께서 " + ChatColor.GREEN + payAmount + "원" + ChatColor.WHITE + "을 후원해주셨습니다.");

List<String> commands = null;
if (donationRewards.containsKey(payAmount)) {
commands = donationRewards.get(payAmount);
} else {
commands = donationRewards.get(0);
}

List<String> commands = donationRewards.get(payAmount);
if (commands == null) {
return;
}

for (String command : commands) {
String tempCommand = command;
tempCommand = tempCommand.replaceAll("%tag%", chzzkUser.get("tag"));
tempCommand = tempCommand.replaceAll("%name%", nickname);
tempCommand = tempCommand.replaceAll("%amount%", String.valueOf(payAmount));
tempCommand = tempCommand.replaceAll("%message%", msg);
String finalCommand = tempCommand;
Bukkit.getScheduler()
.callSyncMethod(DoneConnector.plugin, () -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), finalCommand));
}
if (DoneConnector.random) {
Random rand = new Random();
int randomIndex = rand.nextInt(commands.size());
String command = commands.get(randomIndex);

String tempCommand = command;
tempCommand = tempCommand.replaceAll("%tag%", chzzkUser.get("tag"));
tempCommand = tempCommand.replaceAll("%name%", nickname);
tempCommand = tempCommand.replaceAll("%amount%", String.valueOf(payAmount));
tempCommand = tempCommand.replaceAll("%message%", msg);
String finalCommand = tempCommand;
Bukkit.getScheduler()
.callSyncMethod(DoneConnector.plugin, () -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), finalCommand));
} else {
List<String> commands = donationRewards.get(0);
if (commands == null) {
return;
}

for (String command : commands) {
String tempCommand = command;
tempCommand = tempCommand.replaceAll("%tag%", chzzkUser.get("tag"));
Expand All @@ -143,6 +146,7 @@ public void onMessage(String message) {
.callSyncMethod(DoneConnector.plugin, () -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), finalCommand));
}
}

} catch (Exception e) {
Logger.info(ChatColor.RED + "치지직 메시지 파싱 중 오류가 발생했습니다.");
Logger.info(ChatColor.LIGHT_PURPLE + e.getMessage());
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
식별자: f722959d1b8e651bd56209b343932c01
마크닉네임: kanna

랜덤 보상: false
후원 보상:
0:
- say 가격을 0원으로 쓰면 설정 안된 가격을 의미합니다.
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ api-version: '1.20'
commands:
done:
description: 후원 기능 상태를 변경.
usage: /<command> [on|off|reconnect]
usage: /<command> [on|off|reconnect|reload]

0 comments on commit 76da88e

Please sign in to comment.