First commit
This commit is contained in:
commit
4564d11aa1
113
.gitignore
vendored
Normal file
113
.gitignore
vendored
Normal file
@ -0,0 +1,113 @@
|
||||
# User-specific stuff
|
||||
.idea/
|
||||
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.nar
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
*~
|
||||
|
||||
# temporary files which can be created if a process still has a handle open of a deleted file
|
||||
.fuse_hidden*
|
||||
|
||||
# KDE directory preferences
|
||||
.directory
|
||||
|
||||
# Linux trash folder which might appear on any partition or disk
|
||||
.Trash-*
|
||||
|
||||
# .nfs files are created when an open file is removed but is still being accessed
|
||||
.nfs*
|
||||
|
||||
# General
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
.com.apple.timemachine.donotpresent
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
# Windows thumbnail cache files
|
||||
Thumbs.db
|
||||
Thumbs.db:encryptable
|
||||
ehthumbs.db
|
||||
ehthumbs_vista.db
|
||||
|
||||
# Dump file
|
||||
*.stackdump
|
||||
|
||||
# Folder config file
|
||||
[Dd]esktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
target/
|
||||
|
||||
pom.xml.tag
|
||||
pom.xml.releaseBackup
|
||||
pom.xml.versionsBackup
|
||||
pom.xml.next
|
||||
|
||||
release.properties
|
||||
dependency-reduced-pom.xml
|
||||
buildNumber.properties
|
||||
.mvn/timing.properties
|
||||
.mvn/wrapper/maven-wrapper.jar
|
||||
.flattened-pom.xml
|
||||
|
||||
# Common working directory
|
||||
run/
|
||||
79
pom.xml
Normal file
79
pom.xml
Normal file
@ -0,0 +1,79 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>fr.molzonas</groupId>
|
||||
<artifactId>PainfulLoss</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>PainfulLoss</name>
|
||||
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<defaultGoal>clean package</defaultGoal>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.13.0</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.5.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spigotmc-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.20.6-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.38</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>26.0.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
100
src/main/java/fr/molzonas/painfulloss/PainfulLoss.java
Normal file
100
src/main/java/fr/molzonas/painfulloss/PainfulLoss.java
Normal file
@ -0,0 +1,100 @@
|
||||
package fr.molzonas.painfulloss;
|
||||
|
||||
import fr.molzonas.painfulloss.enums.PropertiesEnum;
|
||||
import fr.molzonas.painfulloss.listeners.DeathListener;
|
||||
import fr.molzonas.painfulloss.utils.Message;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public final class PainfulLoss extends JavaPlugin {
|
||||
public static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
|
||||
public static final String DEFAULT_LOCALE_TAG = "en-US";
|
||||
@Getter private static PainfulLoss instance = null;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
PainfulLoss.instance = this;
|
||||
Message.init(DEFAULT_LOCALE);
|
||||
configurationInit();
|
||||
langInit();
|
||||
registerListeners();
|
||||
Bukkit.getLogger().info("[PainfulLoss] Plugin has been enabled!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
Bukkit.getLogger().info("[PainfulLoss] Plugin has been disabled!");
|
||||
}
|
||||
|
||||
private void configurationInit() {
|
||||
saveDefaultConfig();
|
||||
saveResource("lang/messages_en.properties", false);
|
||||
saveResource("lang/messages_fr.properties", false);
|
||||
}
|
||||
|
||||
private void langInit() {
|
||||
Locale locale = Locale.forLanguageTag(PropertiesEnum.LOCALE.getValue());
|
||||
Message.init(locale);
|
||||
}
|
||||
|
||||
private void registerListeners() {
|
||||
Bukkit.getPluginManager().registerEvents(new DeathListener(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, Command command, @NotNull String label, String[] args) {
|
||||
return switch (command.getName()) {
|
||||
case "painfulloss" -> defaultCommand(sender, args);
|
||||
case "itesten" -> commandLang(sender, Locale.ENGLISH);
|
||||
case "itestfr" -> commandLang(sender, Locale.FRENCH);
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
private boolean commandLang(CommandSender sender, Locale locale) {
|
||||
if (sender instanceof Player p) {
|
||||
p.sendMessage(Message.of("death.summary", locale, p.getName(), 12345));
|
||||
p.sendMessage(Message.of("death.topitem", locale, "Netherite Chestplate", 7890));
|
||||
} else {
|
||||
var text = Message.of("death.summary.first", locale, "Console", 42);
|
||||
sender.sendMessage(text);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean defaultCommand(@NotNull CommandSender sender, String[] args) {
|
||||
if (args.length > 0) {
|
||||
return switch (args[0]) {
|
||||
case "help" -> commandHelp(sender);
|
||||
case "reload" -> commandReload(sender);
|
||||
default -> commandUnknown(sender);
|
||||
};
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean commandHelp(CommandSender sender) {
|
||||
sender.sendMessage("&f&lGit gud");
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean commandReload(CommandSender sender) {
|
||||
reloadConfig();
|
||||
Message.clearCache();
|
||||
langInit();
|
||||
sender.sendMessage(Message.of("plugin.reload"));
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean commandUnknown(CommandSender sender) {
|
||||
sender.sendMessage(Message.of("plugin.unknown"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package fr.molzonas.painfulloss.enums;
|
||||
|
||||
import fr.molzonas.painfulloss.PainfulLoss;
|
||||
import lombok.Getter;
|
||||
|
||||
public enum PropertiesEnum {
|
||||
LOCALE("locale", PainfulLoss.DEFAULT_LOCALE_TAG),
|
||||
DEBUG("debug", "false");
|
||||
|
||||
@Getter private final String path;
|
||||
@Getter private String fallback = null;
|
||||
|
||||
PropertiesEnum(String path, String fallback) {
|
||||
this.path = path;
|
||||
this.fallback = fallback;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
String value = PainfulLoss.getInstance().getConfig().getString(this.getPath());
|
||||
return value == null ? fallback : value;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package fr.molzonas.painfulloss.listeners;
|
||||
|
||||
import fr.molzonas.painfulloss.utils.Message;
|
||||
import org.bukkit.GameRule;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DeathListener implements Listener {
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onPlayerDeath(PlayerDeathEvent event) {
|
||||
Player player = event.getEntity();
|
||||
|
||||
if (Boolean.TRUE.equals(player.getWorld().getGameRuleValue(GameRule.KEEP_INVENTORY))) return;
|
||||
|
||||
List<ItemStack> lostItems = new ArrayList<>();
|
||||
for (ItemStack is : event.getDrops()) {
|
||||
if (is != null && is.getType() != Material.AIR) lostItems.add(is.clone());
|
||||
}
|
||||
|
||||
if (lostItems.isEmpty()) return;
|
||||
|
||||
int cnt = lostItems.stream().mapToInt(ItemStack::getAmount).sum();
|
||||
|
||||
player.sendMessage(Message.of("death.summary", player.getName(), cnt));
|
||||
}
|
||||
}
|
||||
110
src/main/java/fr/molzonas/painfulloss/utils/Message.java
Normal file
110
src/main/java/fr/molzonas/painfulloss/utils/Message.java
Normal file
@ -0,0 +1,110 @@
|
||||
package fr.molzonas.painfulloss.utils;
|
||||
|
||||
import fr.molzonas.painfulloss.PainfulLoss;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class Message {
|
||||
private static final String DIRECTORY = "lang";
|
||||
private static final String BUNDLE_NAME = "messages";
|
||||
private static final ConcurrentHashMap<Locale, ResourceBundle> CACHE = new ConcurrentHashMap<>();
|
||||
private static Locale currentLocale = Locale.ENGLISH;
|
||||
|
||||
|
||||
private Message() {}
|
||||
|
||||
public static void init(Locale locale) {
|
||||
if (locale == null) locale = PainfulLoss.DEFAULT_LOCALE;
|
||||
Bukkit.getLogger().info("[Painfullost] Locale used : " + locale.getDisplayName());
|
||||
Message.currentLocale = locale;
|
||||
clearCache();
|
||||
}
|
||||
|
||||
public static String of(String key, Object... args) {
|
||||
return of(key, currentLocale, args);
|
||||
}
|
||||
|
||||
public static String of(CommandSender sender, String key, Object... args) {
|
||||
return of(key, senderLocale(sender), args);
|
||||
}
|
||||
|
||||
public static void send(CommandSender sender, String key, Object... args) {
|
||||
sender.sendMessage(of(sender, key, args));
|
||||
}
|
||||
|
||||
public static String of(String key, Locale locale, Object... args) {
|
||||
Locale loc = (locale != null) ? locale : currentLocale;
|
||||
ResourceBundle bundle = getBundle(loc);
|
||||
String pattern = bundle.containsKey(key) ? bundle.getString(key) : ResourceBundle.getBundle(BUNDLE_NAME, loc).getString(key);
|
||||
MessageFormat mf = new MessageFormat(pattern, loc);
|
||||
String formatted = mf.format(args == null ? new Object[0] : args);
|
||||
return ChatColor.translateAlternateColorCodes('&', formatted);
|
||||
}
|
||||
|
||||
public static Locale senderLocale(CommandSender sender) {
|
||||
if (sender instanceof Player p) {
|
||||
String r = p.getLocale();
|
||||
if (!r.isEmpty()) {
|
||||
return fromMinecraftLocale(r);
|
||||
}
|
||||
}
|
||||
return PainfulLoss.DEFAULT_LOCALE;
|
||||
}
|
||||
|
||||
public static void clearCache() {
|
||||
CACHE.clear();
|
||||
ResourceBundle.clearCache(Message.class.getClassLoader());
|
||||
}
|
||||
|
||||
private static Locale fromMinecraftLocale(String mc) {
|
||||
String tag = mc.replace('_', '-');
|
||||
try {
|
||||
return Locale.forLanguageTag(tag);
|
||||
} catch (Exception ignored) {
|
||||
return PainfulLoss.DEFAULT_LOCALE;
|
||||
}
|
||||
}
|
||||
|
||||
private static ResourceBundle getBundle(Locale loc) {
|
||||
return CACHE.computeIfAbsent(loc, l ->
|
||||
ResourceBundle.getBundle(BUNDLE_NAME, l, new Utf8Control()));
|
||||
}
|
||||
|
||||
private static class Utf8Control extends ResourceBundle.Control {
|
||||
@Override
|
||||
public ResourceBundle newBundle(String baseName, Locale locale, String format,
|
||||
ClassLoader loader, boolean reload) throws IOException {
|
||||
String bundleName = toBundleName(baseName, locale);
|
||||
String resourceName = toResourceName(bundleName, "properties");
|
||||
File file = new File(PainfulLoss.getInstance().getDataFolder(), DIRECTORY + File.separator + resourceName);
|
||||
|
||||
try (FileInputStream fis = new FileInputStream(file)) {
|
||||
try (InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
|
||||
Properties prop = new Properties();
|
||||
prop.load(isr);
|
||||
return new PropertiesResourceBundle(prop);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class PropertiesResourceBundle extends ResourceBundle {
|
||||
private final Properties props;
|
||||
PropertiesResourceBundle(Properties props) { this.props = props; }
|
||||
@Override protected Object handleGetObject(@NotNull String key) { return props.getProperty(key); }
|
||||
@NotNull
|
||||
@Override public Enumeration<String> getKeys() {
|
||||
return Collections.enumeration(props.stringPropertyNames());
|
||||
}
|
||||
@Override public boolean containsKey(@NotNull String key) { return props.containsKey(key); }
|
||||
}
|
||||
}
|
||||
5
src/main/resources/config.yml
Normal file
5
src/main/resources/config.yml
Normal file
@ -0,0 +1,5 @@
|
||||
# Available : en-US, fr-FR
|
||||
locale: en-US
|
||||
|
||||
# Dev option, you won't have to enable it, trust me
|
||||
debug: false
|
||||
4
src/main/resources/lang/messages_en.properties
Normal file
4
src/main/resources/lang/messages_en.properties
Normal file
@ -0,0 +1,4 @@
|
||||
death.summary=&c{0} lost &e{1}&c.
|
||||
death.topitem=&7Most valuable: &6{0} &7(&e{1}&7)
|
||||
plugin.reload=&aPainfulLoss has successfully reloaded.
|
||||
plugin.unknown=&4Unknown command. Please use &c/painfullost help &4to get some help.
|
||||
4
src/main/resources/lang/messages_fr.properties
Normal file
4
src/main/resources/lang/messages_fr.properties
Normal file
@ -0,0 +1,4 @@
|
||||
death.summary=&c{0} a perdu &e{1}&c.
|
||||
death.topitem=&7Objet le plus cher : &6{0} &7(&e{1}&7)
|
||||
plugin.reload=&aPainfulLoss a été rechargé avec succès.
|
||||
plugin.unknown=&4Commande inconnue. Merci d'utiliser &c/painfulloss help &4pour obtenir de l'aide.
|
||||
15
src/main/resources/plugin.yml
Normal file
15
src/main/resources/plugin.yml
Normal file
@ -0,0 +1,15 @@
|
||||
name: PainfulLoss
|
||||
version: '1.0'
|
||||
main: fr.molzonas.painfulloss.PainfulLoss
|
||||
api-version: '1.20'
|
||||
author: Molzonas
|
||||
commands:
|
||||
itestfr:
|
||||
description: Test i18n
|
||||
usage: /itestfr
|
||||
itesten:
|
||||
description: Test i18n
|
||||
usage: /itesten
|
||||
painfulloss:
|
||||
description: Base command for Painfulloss
|
||||
usage: /painfulloss
|
||||
Loading…
x
Reference in New Issue
Block a user