Files
src
build.gradle
manifest.kod
README.md
settings.gradle
Code
12 787
src/main/java/com/royaleffa/bounties/RoyaleFFABountiesPlugin.java
package com.royaleffa.bounties;
import com.royaleffa.bounties.command.BountyCommand;
import com.royaleffa.bounties.command.TokensCommand;
import com.royaleffa.bounties.data.SQLiteStorage;
import com.royaleffa.bounties.listener.BountyListener;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.io.BukkitObjectInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Objects;
import java.util.zip.GZIPInputStream;
public final class RoyaleFFABountiesPlugin extends JavaPlugin {
private SQLiteStorage storage;
private ItemStack tokenItem;
@Override
public void onEnable() {
saveDefaultConfig();
loadTokenItem();
try {
storage = new SQLiteStorage(this);
} catch (RuntimeException exception) {
getLogger().severe("Could not initialize SQLite storage: " + exception.getMessage());
Bukkit.getPluginManager().disablePlugin(this);
return;
}
Objects.requireNonNull(getCommand("tokens")).setExecutor(new TokensCommand(this));
Objects.requireNonNull(getCommand("tokens")).setTabCompleter(new TokensCommand(this));
BountyCommand bountyCommand = new BountyCommand(this);
Objects.requireNonNull(getCommand("bounty")).setExecutor(bountyCommand);
Objects.requireNonNull(getCommand("bounty")).setTabCompleter(bountyCommand);
Bukkit.getPluginManager().registerEvents(new BountyListener(this), this);
}
@Override
public void onDisable() {
if (storage != null) {
storage.close();
}
}
public SQLiteStorage getStorage() {
return storage;
}
public long getKillReward() {
return Math.max(0, getConfig().getLong("kill-reward", 1));
}
public long getMinimumBounty() {
return Math.max(1, getConfig().getLong("minimum-bounty", 1));
}
public ItemStack getTokenItem() {
return tokenItem == null ? null : tokenItem.clone();
}
public void sendMessage(org.bukkit.command.CommandSender sender, String key, String... replacements) {
sender.sendMessage(formatMessage(key, replacements));
}
public String formatMessage(String key, String... replacements) {
String message = getConfig().getString("messages." + key, "&cMissing message: " + key);
String prefix = getConfig().getString("prefix", "");
if (prefix != null && !prefix.isEmpty()) {
message = prefix + message;
}
for (int index = 0; index + 1 < replacements.length; index += 2) {
message = message.replace(replacements[index], replacements[index + 1]);
}
return ChatColor.translateAlternateColorCodes('&', message);
}
private void loadTokenItem() {
String encoded = getConfig().getString("token-item", "");
if (encoded == null || encoded.isBlank()) {
return;
}
try (ByteArrayInputStream bytes = new ByteArrayInputStream(Base64.getDecoder().decode(encoded));
GZIPInputStream gzip = new GZIPInputStream(bytes);
BukkitObjectInputStream input = new BukkitObjectInputStream(gzip)) {
Object item = input.readObject();
if (item instanceof ItemStack stack) {
tokenItem = stack;
} else {
getLogger().warning("The configured token-item does not contain an ItemStack.");
}
} catch (IOException | ClassNotFoundException | IllegalArgumentException exception) {
getLogger().warning("Could not load the configured token-item: " + exception.getMessage());
}
}
}