-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added messages management from messages.yml config file
- Loading branch information
1 parent
e2f9309
commit 4f04c4a
Showing
6 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
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,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); | ||
} | ||
} |
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
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
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
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,9 @@ | ||
package lar.minecraft.hg.enums; | ||
|
||
public enum MessageKey { | ||
class_selected, | ||
class_selection_lobby, | ||
class_wrong, | ||
supply_drop_alert, | ||
supply_drop | ||
} |
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,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}" |