Skip to content

Commit

Permalink
Merge pull request #53 from AlbertoRutigliano/improve/messages-user-e…
Browse files Browse the repository at this point in the history
…xperience

Added messages management from messages.yml config file
  • Loading branch information
AlbertoRutigliano authored May 29, 2024
2 parents 7f30658 + 4f04c4a commit ea6cf6a
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 4 deletions.
73 changes: 73 additions & 0 deletions src/main/java/lar/minecraft/hg/MessageUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package lar.minecraft.hg;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import lar.minecraft.hg.enums.MessageKey;

public class MessageUtils {

private static FileConfiguration languageConfig;
private static Logger logger;

public static void init() {
logger = SpigotPlugin.getPlugin(SpigotPlugin.class).getLogger();
// Load the language file
File languageFile = new File(SpigotPlugin.getPlugin(SpigotPlugin.class).getDataFolder(), "messages.yml");
if (!languageFile.exists()) {
try (InputStream in = SpigotPlugin.getPlugin(SpigotPlugin.class).getResource("messages.yml")) {
if (in != null) {
Files.copy(in, languageFile.toPath());
logger.info("Default messages.yml has been created.");
} else {
logger.warning("Default messages.yml not found in the JAR.");
}
} catch (IOException e) {
logger.log(Level.SEVERE, "Could not create messages.yml", e);
}
}

languageConfig = YamlConfiguration.loadConfiguration(languageFile);
}

public static String getMessage(MessageKey key, Object... placeholders) {
return getMessage(key.toString(), placeholders);
}
// Method to get a message from the language file and replace placeholders by position
public static String getMessage(String key, Object... placeholders) {
if (languageConfig == null || !languageConfig.contains(key)) {
logger.warning("Message key '" + key + "' not found in language file.");
return "Message not found for key: " + key;
}

String message;
if (languageConfig.isList(key)) {
// Handle case where message is a list and pick a random message
List<String> messages = languageConfig.getStringList(key);
if (messages.isEmpty()) {
logger.warning("Message key '" + key + "' contains no messages.");
return "Message not found for key: " + key;
}
message = messages.get(new Random().nextInt(messages.size()));
} else {
// Handle case where message is a single string
message = languageConfig.getString(key);
}

// Replace placeholders in the message
for (int i = 0; i < placeholders.length; i++) {
message = message.replace("{" + i + "}", String.valueOf(placeholders[i]));
}
return ChatColor.translateAlternateColorCodes('&', "&7" + message);
}
}
7 changes: 4 additions & 3 deletions src/main/java/lar/minecraft/hg/ServerSchedulers.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import lar.minecraft.hg.entities.ItemStackProbability;
import lar.minecraft.hg.enums.HGPhase;
import lar.minecraft.hg.enums.MessageKey;
import lar.minecraft.hg.managers.DatabaseManager;
import lar.minecraft.hg.managers.PlayerClassManager;
import lar.minecraft.hg.managers.PlayerManager;
Expand Down Expand Up @@ -330,7 +331,8 @@ public void run() {
for(Player p : SpigotPlugin.server.getOnlinePlayers()) {
p.spigot().sendMessage(
ChatMessageType.ACTION_BAR,
TextComponent.fromLegacyText("A supply drop chest will be spawned in " + Math.abs(passedSeconds) + " seconds."));
TextComponent.fromLegacyText(MessageUtils.getMessage(MessageKey.supply_drop_alert, Math.abs(passedSeconds))));

}
}

Expand Down Expand Up @@ -396,8 +398,7 @@ public static void spawnSupplyDrop() {

ServerManager.sendSound(Sound.BLOCK_BELL_USE);
chest.getWorld().strikeLightning(chestLocation);
Bukkit.broadcastMessage("Supply chest dropped at (x = " + chestLocation.getX() + ", y = " + chestLocation.getY() + ", z = " + chestLocation.getZ() + ")");

Bukkit.broadcastMessage(MessageUtils.getMessage(MessageKey.supply_drop, chestLocation.getX(), chestLocation.getY(), chestLocation.getZ()));
}

}
1 change: 1 addition & 0 deletions src/main/java/lar/minecraft/hg/SpigotPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void onLoad() {
@Override
public void onEnable() {
// Don't log enabling, Spigot does that for you automatically!
MessageUtils.init();

SpigotPlugin.setPhase(HGPhase.WAITING_FOR_HG);

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/lar/minecraft/hg/commands/TestCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

import lar.minecraft.hg.MessageUtils;
import lar.minecraft.hg.ServerSchedulers;
import lar.minecraft.hg.SpigotPlugin;
import lar.minecraft.hg.managers.ServerManager;
Expand All @@ -21,7 +22,8 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
String cmdName = cmd.getName().toLowerCase();

if (cmdName.equals("test")) {
new ServerSchedulers(plugin).waitingPhase();
sender.sendMessage(MessageUtils.getMessage(args[0], sender.getName()));
//new ServerSchedulers(plugin).waitingPhase();
}

if (cmdName.equals("restart-hg-server")) {
Expand All @@ -39,5 +41,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
return true;
}




}
9 changes: 9 additions & 0 deletions src/main/java/lar/minecraft/hg/enums/MessageKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lar.minecraft.hg.enums;

public enum MessageKey {
class_selected,
class_selection_lobby,
class_wrong,
supply_drop_alert,
supply_drop
}
9 changes: 9 additions & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#Use this site to color code generator: https://minecraftitemids.com/color-codes#generator
class_selection_lobby: "Class selection is available only in lobby"
class_selected:
- "You will be a &6{0}&7!"
- "You choose to be a &6{0}&7!"
- "&6{0}&7 class selected!"
class_wrong: "There's no &6&m{0}&7 class"
supply_drop_alert: "&fA &6supply drop chest &fwill be spawned in {0} seconds"
supply_drop: "Supply chest dropped at XYZ:&2&o {0} / {1} / {2}"

0 comments on commit ea6cf6a

Please sign in to comment.