Skip to content

Commit

Permalink
Merge pull request #5 from KartoffelChipss/feature-language-manager
Browse files Browse the repository at this point in the history
Feature: Language Manager
  • Loading branch information
KartoffelChipss authored May 8, 2024
2 parents 3de7fca + 659acfe commit 6be3c9e
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 66 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.strassburger</groupId>
<artifactId>LifeStealZ</artifactId>
<name>LifeStealZ</name>
<version>1.1.10</version>
<version>1.1.11</version>
<description>A LifeSteal SMP plugin providing you all the features you need!</description>
<build>
<resources>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.strassburger</groupId>
<artifactId>LifeStealZ</artifactId>
<version>1.1.10</version>
<version>1.1.11</version>
<packaging>jar</packaging>

<name>LifeStealZ</name>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/strassburger/lifestealz/LifeStealZ.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public final class LifeStealZ extends JavaPlugin {
private VersionChecker versionChecker;
private PlayerDataStorage playerDataStorage;
private WorldGuardManager worldGuardManager;
private LanguageManager languageManager;
private final boolean hasWorldGuard = Bukkit.getPluginManager().getPlugin("WorldGuard") != null;
private final boolean hasPlaceholderApi = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;

Expand All @@ -38,6 +39,8 @@ public void onEnable() {
getConfig().options().copyDefaults(true);
saveDefaultConfig();

languageManager = new LanguageManager();

CommandManager.registerCommands();

EventManager.registerListeners();
Expand Down Expand Up @@ -90,6 +93,10 @@ public boolean hasPlaceholderApi() {
return hasPlaceholderApi;
}

public LanguageManager getLanguageManager() {
return languageManager;
}

private PlayerDataStorage createPlayerDataStorage() {
String option = getConfig().getString("storage.type");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

LifeStealZ.getInstance().reloadConfig();
config = LifeStealZ.getInstance().getConfig();
LifeStealZ.getInstance().getLanguageManager().reload();
sender.sendMessage(MessageUtils.getAndFormatMsg(true, "messages.reloadMsg", "&7Successfully reloaded the plugin!"));
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void onPlayerLogin(PlayerLoginEvent event) {
double minHearts = LifeStealZ.getInstance().getConfig().getInt("minHearts") * 2;
if (playerData.getMaxhp() <= minHearts && !disabledBanOnDeath) {
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
Component kickmsg = MessageUtils.getAndFormatMsg(false, "messages.eliminatedjoin", "&cYou don't have any hearts left!");
Component kickmsg = MessageUtils.getAndFormatMsg(false, "eliminatedJoin", "&cYou don't have any hearts left!");
event.kickMessage(kickmsg);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.strassburger.lifestealz.util;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.strassburger.lifestealz.LifeStealZ;

import java.io.File;
import java.util.HashMap;
import java.util.List;

public class LanguageManager {
private final JavaPlugin plugin = LifeStealZ.getInstance();
public static final List<String> defaultLangs = List.of("en_US", "de_DE");

private HashMap<String, String> translationMap;
private FileConfiguration langConfig;

public LanguageManager() {
loadLanguageConfig();
}

public void reload() {
loadLanguageConfig();
}

private void loadLanguageConfig() {
File languageDirectory = new File(plugin.getDataFolder(), "lang/");
if (!languageDirectory.exists() || !languageDirectory.isDirectory()) languageDirectory.mkdir();

for (String langString : defaultLangs) {
File langFile = new File("lang/", langString + ".yml");
if (!new File(languageDirectory, langString + ".yml").exists()) {
plugin.getLogger().info("Saving file " + langFile.getPath());
plugin.saveResource(langFile.getPath(), false);
}
}

String langOption = plugin.getConfig().getString("lang") != null ? plugin.getConfig().getString("lang") : "en_US";
File selectedLangFile = new File(languageDirectory, langOption + ".yml");

if (!selectedLangFile.exists()) {
selectedLangFile = new File(languageDirectory, "en_US.yml");
plugin.getLogger().warning("Language file " + langOption + ".yml (" + selectedLangFile.getPath() + ") not found! Using fallback en_US.yml.");
}

plugin.getLogger().info("Using language file: " + selectedLangFile.getPath());
langConfig = YamlConfiguration.loadConfiguration(selectedLangFile);
}

public String getString(String key) {
return langConfig.getString(key);
}

public String getString(String key, String fallback) {
return langConfig.getString(key) != null ? langConfig.getString(key) : fallback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ public static Component formatMsg(String msg, Replaceable... replaceables) {
* @return The formatted message
*/
public static Component getAndFormatMsg(boolean addPrefix, String path, String fallback, Replaceable... replaceables) {
if (path.startsWith("messages.")) path = path.substring("messages.".length());

MiniMessage mm = MiniMessage.miniMessage();
String msg = "<!i>" + (LifeStealZ.getInstance().getConfig().getString(path) != null ? LifeStealZ.getInstance().getConfig().getString(path) : fallback);
String prefix = LifeStealZ.getInstance().getConfig().getString("messages.prefix") != null ? LifeStealZ.getInstance().getConfig().getString("messages.prefix") : "&8[&cLifeStealZ&8]";
String msg = "<!i>" + LifeStealZ.getInstance().getLanguageManager().getString(path, fallback);
String prefix = LifeStealZ.getInstance().getLanguageManager().getString("prefix", "&8[&cLifeStealZ&8]");
if (addPrefix) {
msg = prefix + " " + msg;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public CustomItem setName(String name) {
return this;
}

public CustomItem setName(Component name) {
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.displayName(name);
itemStack.setItemMeta(itemMeta);

return this;
}

public CustomItem setCustomModelID(int customModelID) {
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setCustomModelData(customModelID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public static ItemStack createRevive() {

public static ItemStack createCloseItem() {
return new CustomItem(Material.BARRIER)
.setName("&cClose")
.setName(MessageUtils.getAndFormatMsg(false, "closeBtn", "&cClose"))
.setCustomModelID(999)
.addFlag(ItemFlag.HIDE_ATTRIBUTES)
.getItemStack();
}

public static ItemStack createBackItem(int page) {
CustomItem ci = new CustomItem(Material.ARROW)
.setName("&cBack")
.setName(MessageUtils.getAndFormatMsg(false, "backBtn", "&cBack"))
.setCustomModelID(998)
.addFlag(ItemFlag.HIDE_ATTRIBUTES);

Expand All @@ -90,7 +90,7 @@ public static ItemStack createBackItem(int page) {

public static ItemStack createNextItem(int page) {
CustomItem ci = new CustomItem(Material.ARROW)
.setName("&cNext")
.setName(MessageUtils.getAndFormatMsg(false, "nextBtn", "&cNext"))
.setCustomModelID(997)
.addFlag(ItemFlag.HIDE_ATTRIBUTES);

Expand Down
84 changes: 26 additions & 58 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,55 @@
# https://docs.advntr.dev/minimessage/format.html
# With these, you can also add HEX colors, gradients, hover and click events, etc

# If set to true, LifeStealZ will check for updates and let you know if there's a newer version
checkForUpdates: true

#A list of worlds, where the plugin should take effect
# Set the language to any code found in the "lang" folder (don't add the .yml extension)
# You can add your own language files. Use https://github.com/KartoffelChipss/LifeStealZ/tree/main/src/main/resources/lang/en_US.yml as a template
lang: "en_US"

# A list of worlds, where the plugin should take effect
worlds:
- "world"
- "world_nether"
- "world_the_end"

#The amount of hearts a player has, when joining for the first time
# The amount of hearts a player has, when joining for the first time
startHearts: 10
#The maximal amount of hearts, a player can have
# The maximal amount of hearts, a player can have
maxHearts: 20
# The minimal amount of hearts. If a player gets to this amount of hearts, they will be eliminated.
# PLEASE ONLY CHANGE IF YOU KNOW WHAT YOU ARE DOING!
minHearts: 0
# This option will enforce the heart limit on admin commands like /lifestealz hearts <add, set> <player> <amount>
enforceMaxHeartsOnAdminCommands: false

#If hearts should be dropped instead of directly added to the killer
# If hearts should be dropped instead of directly added to the killer
dropHearts: false
#If a heart should be dropped, when the killer already has the max amount of hearts
# If a heart should be dropped, when the killer already has the max amount of hearts
dropHeartsIfMax: true
#If a player should lose a heart, when dying to hostile mobs or falldamage, lava, etc
# If a player should lose a heart, when dying to hostile mobs or falldamage, lava, etc
looseHeartsToNature: true
#If a player should lose a heart, when being killed by another player
# If a player should lose a heart, when being killed by another player
looseHeartsToPlayer: true
#Whether it should be announced, when a player got eliminated (has no more hearts)
# Whether it should be announced, when a player got eliminated (has no more hearts)
announceElimination: true

#Allows players to withdraw a heart, even if they only have one left
# Allows players to withdraw a heart, even if they only have one left
allowDyingFromWithdraw: true
#If the totem effect should be played, when you use a heart
# If the totem effect should be played, when you use a heart
playTotemEffect: false
#The time you have to wait, before you can use another heart in Milliseconds
# The time you have to wait, before you can use another heart in Milliseconds
heartCooldown: 0
#How many times a player can be revived. Set to -1 to make it infinite
# How many times a player can be revived. Set to -1 to make it infinite
maxRevives: -1

#If the use of totems of undying should be prevented
# If the use of totems of undying should be prevented
preventTotems: false
#If crystalpvp should be disabled
# If crystalpvp should be disabled
preventCrystalPVP: false

#Only disable this option if you want to add custom commands on elimination and don't want the player to get banned
# Only disable this option if you want to add custom commands on elimination and don't want the player to get banned
disablePlayerBanOnElimination: false
# The amount of hp a player should have after getting eliminated
respawnHP: 10
Expand Down Expand Up @@ -99,7 +104,7 @@ storage:
username: "root"
password: "password"

#Here you can modify everything about the custom items
# Here you can modify everything about the custom items
items:
# DONT DELETE THE defaultheart ITEM!!!
defaultheart:
Expand All @@ -118,9 +123,9 @@ items:
# true if this item should be craftable
craftable: true
recipe:
#Every item represents one slot in the crafting table
#The first item in a row is the left most item in the crafting table
#If you want a slot to be blant, use 'AIR'
# Every item represents one slot in the crafting table
# The first item in a row is the left most item in the crafting table
# If you want a slot to be blank, use 'AIR'
rowOne:
- "GOLD_BLOCK"
- "GOLD_BLOCK"
Expand Down Expand Up @@ -164,45 +169,8 @@ items:
- "AMETHYST_SHARD"
sound:
enabled: false
sound: ENTITY_PLAYER_LEVELUP # Find all sounds here: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Sound.html
sound: ENTITY_PLAYER_LEVELUP
volume: 1.0
pitch: 1.0

# You can add as many custom items as you want

#You can modify all messages here
messages:
prefix: "&8[&cLifeStealZ&8]"
newVersionAvailable: "&7A new version of LifeStealZ is available!\n&c<click:OPEN_URL:https://modrinth.com/plugin/lifestealz/versions>https://modrinth.com/plugin/lifestealz/versions</click>"
usageError: "&cUsage: %usage%"
noPermsError: "<red>You do not have permission to execute this command!"
playerNotFound: "&cPlayer not found!"
worldNotWhitelisted: "&cThis world is not whitelisted for LifeStealZ!"
specifyPlayerOrBePlayer: "&cYou need to either specify a player or be a player yourself!"
noPermissionError: "&cYou don't have permission to use this!"
noPlayerData: "&cThis player has not played on this server yet!"
eliminateSuccess: "&7You successfully eliminated &c%player%&7!"
reviveSuccess: "&7You successfully revived &c%player%!"
reviveMaxReached: "&cThis player has already been revived %amount% times!"
onlyReviveElimPlayers: "&cYou can only revive eliminated players!"
eliminatedJoin: "&cYou don't have any hearts left!"
eliminationAnnouncement: "&c%player% &7has been eliminated by &c%killer%&7!"
eliminateionAnnouncementNature: "&c%player% &7has been eliminated!"
setHeartsConfirm: "&7Successfully set &c%player%&7's hearts to &c%amount%"
getHearts: "&c%player% &7currently has &c%amount% &7hearts!"
reloadMsg: "&7Successfully reloaded the plugin!"
versionMsg: "&7You are using version <red>%version%"
noWithdraw: "&cYou would be eliminated, if you withdraw a heart!"
withdrawConfirmmsg: "&8&oUse <underlined><click:SUGGEST_COMMAND:/withdrawheart %amount% confirm>/withdrawheart %amount% confirm</click></underlined> if you really want to withdraw a heart"
maxHeartLimitReached: "&cYou already reached the limit of %limit% hearts!"
closeBtn: "&cClose"
reviveTitle: "&8Revive a player"
revivePlayerDesc: "&7Click to revive this player"
viewheartsYou: "&7You currently have &c%amount% &7hearts!"
viewheartsOther: "&c%player% &7currently has &c%amount% &7hearts!"
heartconsume: "&7You got &c%amount% &7hearts!"
heartconsumeCooldown: "&cYou have to wait before using another heart!"
recipeNotCraftable: "&cThis item is not craftable!"
altKill: "&cPlease don't kill alts! This attempt has been logged!"
withdrawMin: "&cYou can't withdraw less than 1 heart!"
giveItem: "&7You received %amount%x &c%item%&7!"
# You can add as many custom items as you want
39 changes: 39 additions & 0 deletions src/main/resources/lang/de_DE.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# LifeStealZ language file
# de_DE - by Kartoffelchipss

prefix: "&8[&cLifeStealZ&8]"
newVersionAvailable: "&7Eine neue Version von LifeStealZ ist verfügbar!\n&c<click:OPEN_URL:https://modrinth.com/plugin/lifestealz/versions>https://modrinth.com/plugin/lifestealz/versions</click>"
usageError: "&cVerwendung: %usage%"
noPermsError: "<red>Du hast keine Berechtigung, diesen Befehl auszuführen!"
playerNotFound: "&cSpieler nicht gefunden!"
worldNotWhitelisted: "&cDiese Welt ist nicht auf der LifeStealZ Whitelist!"
specifyPlayerOrBePlayer: "&cDu musst entweder einen Spieler angeben oder selbst ein Spieler sein!"
noPermissionError: "&cDu hast keine Berechtigung, das zu benutzen!"
noPlayerData: "&cDieser Spieler hat noch nicht auf diesem Server gespielt!"
eliminateSuccess: "&7Du hast &c%player%&7 erfolgreich eliminiert!"
reviveSuccess: "&7Du hast &c%player%&7 erfolgreich wiederbelebt!"
reviveMaxReached: "&cDieser Spieler wurde bereits %amount% mal wiederbelebt!"
onlyReviveElimPlayers: "&cDu kannst nur eliminierte Spieler wiederbeleben!"
eliminatedJoin: "&cDu hast keine Herzen mehr!"
eliminationAnnouncement: "&c%player% &7wurde von &c%killer%&7 eliminiert!"
eliminateionAnnouncementNature: "&c%player% &7wurde eliminiert!"
setHeartsConfirm: "&7Die Herzen von &c%player%&7 wurden erfolgreich auf &c%amount%&7 gesetzt!"
getHearts: "&c%player% &7hat derzeit &c%amount% &7Herzen!"
reloadMsg: "&7Das Plugin wurde erfolgreich neu geladen!"
versionMsg: "&7Du benutzt Version <red>%version%"
noWithdraw: "&cDu würdest eliminiert werden, wenn du so viele Herzen auszahlst!"
withdrawConfirmmsg: "&8&oVerwende <underlined><click:SUGGEST_COMMAND:/withdrawheart %amount% confirm>/withdrawheart %amount% confirm</click></underlined>, wenn du wirklich Herzen auszahlen möchtest."
maxHeartLimitReached: "&cDu hast bereits das Limit von %limit% Herzen erreicht!"
closeBtn: "&cSchließen"
backBtn: "&cZurück"
nextBtn: "&cNächste Seite"
reviveTitle: "&8Spieler wiederbeleben"
revivePlayerDesc: "&7Klicke hier, um diesen Spieler wiederzubeleben"
viewheartsYou: "&7Du hast derzeit &c%amount% &7Herzen!"
viewheartsOther: "&c%player% &7hat derzeit &c%amount% &7Herzen!"
heartconsume: "&7Du hast &c%amount% &7Herzen erhalten!"
heartconsumeCooldown: "&cDu musst warten, bevor du ein weiteres Herz benutzen kannst!"
recipeNotCraftable: "&cDieser Gegenstand kann nicht hergestellt werden!"
altKill: "&cBitte töte keine Alts! Dieser Versuch wurde protokolliert!"
withdrawMin: "&cDu kannst nicht weniger als 1 Herz auszahlen!"
giveItem: "&7Du hast %amount%x &c%item%&7 erhalten!"
Loading

0 comments on commit 6be3c9e

Please sign in to comment.