Files
src
build.gradle
README.md
Code
14 2,130
src/main/java/com/levelssmp/remake/LevelsSMP.java
package com.levelssmp.remake;
import com.levelssmp.remake.command.DeathbanCommand;
import com.levelssmp.remake.command.GiveMultiplierCommand;
import com.levelssmp.remake.command.MultiplierCommand;
import com.levelssmp.remake.command.XPCoreCommand;
import com.levelssmp.remake.data.PlayerDataStore;
import com.levelssmp.remake.item.ItemFactory;
import com.levelssmp.remake.listener.CommandAccessListener;
import com.levelssmp.remake.listener.GameplayListener;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.plugin.java.JavaPlugin;
import java.math.BigDecimal;
public final class LevelsSMP extends JavaPlugin {
private PlayerDataStore dataStore;
private ItemFactory itemFactory;
private NamespacedKey itemTypeKey;
private NamespacedKey itemTokenKey;
private NamespacedKey itemValueKey;
@Override
public void onEnable() {
saveDefaultConfig();
itemTypeKey = new NamespacedKey(this, "item_type");
itemTokenKey = new NamespacedKey(this, "item_token");
itemValueKey = new NamespacedKey(this, "item_value");
dataStore = new PlayerDataStore(this);
itemFactory = new ItemFactory(this, dataStore);
registerCommands();
Bukkit.getPluginManager().registerEvents(new GameplayListener(this, dataStore, itemFactory), this);
Bukkit.getPluginManager().registerEvents(new CommandAccessListener(this), this);
Bukkit.getScheduler().runTaskTimer(this, () -> {
if (!getConfig().getBoolean("actionbar.enabled", true)) {
return;
}
String format = getConfig().getString("actionbar.format", "&aMultiplier: &f%multiplier%x");
for (Player player : Bukkit.getOnlinePlayers()) {
String multiplier = BigDecimal.valueOf(dataStore.getMultiplier(player.getUniqueId()))
.stripTrailingZeros().toPlainString();
player.sendActionBar(ChatColor.translateAlternateColorCodes('&',
format.replace("%multiplier%", multiplier)));
}
}, 0L, 20L);
registerMultiplierRecipe();
registerBeaconRecipe();
getLogger().info("LevelsSMP Remake enabled.");
}
@Override
public void onDisable() {
if (dataStore != null) {
dataStore.save();
}
}
public void reloadPluginConfig() {
reloadConfig();
registerMultiplierRecipe();
registerBeaconRecipe();
}
private void registerCommands() {
MultiplierCommand multiplierCommand = new MultiplierCommand(this, dataStore, itemFactory);
getCommand("multiplier").setExecutor(multiplierCommand);
getCommand("multiplier").setTabCompleter(multiplierCommand);
DeathbanCommand deathbanCommand = new DeathbanCommand(this, dataStore, itemFactory);
getCommand("deathban").setExecutor(deathbanCommand);
getCommand("deathban").setTabCompleter(deathbanCommand);
XPCoreCommand xpCoreCommand = new XPCoreCommand(this, itemFactory);
getCommand("xpcore").setExecutor(xpCoreCommand);
getCommand("xpcore").setTabCompleter(xpCoreCommand);
GiveMultiplierCommand giveMultiplierCommand = new GiveMultiplierCommand(this, itemFactory);
getCommand("givemultiplier").setExecutor(giveMultiplierCommand);
getCommand("givemultiplier").setTabCompleter(giveMultiplierCommand);
}
private void registerMultiplierRecipe() {
NamespacedKey recipeKey = new NamespacedKey(this, "multiplier_shard");
Bukkit.removeRecipe(recipeKey);
if (!getConfig().getBoolean("crafting.enabled", true)) {
return;
}
ItemStack result = itemFactory.createMultiplier(getConfig().getDouble("crafting.item-value", 0.5), "CRAFT_TEMPLATE");
ShapedRecipe recipe = new ShapedRecipe(recipeKey, result);
java.util.List<String> shape = getConfig().getStringList("crafting.recipe.shape");
if (shape.isEmpty() || shape.size() > 3) {
getLogger().warning("crafting.recipe.shape must contain 1 to 3 rows; multiplier recipe was not registered.");
return;
}
recipe.shape(shape.toArray(new String[0]));
org.bukkit.configuration.ConfigurationSection ingredients = getConfig().getConfigurationSection("crafting.recipe.ingredients");
if (ingredients != null) {
for (String symbol : ingredients.getKeys(false)) {
XMaterial.matchXMaterial(ingredients.getString(symbol, "AIR"))
.map(XMaterial::parseMaterial)
.filter(material -> material != null)
.ifPresent(material -> recipe.setIngredient(symbol.charAt(0), material));
}
}
try {
Bukkit.addRecipe(recipe);
} catch (IllegalArgumentException exception) {
getLogger().warning("Invalid multiplier recipe configuration: " + exception.getMessage());
}
}
private void registerBeaconRecipe() {
NamespacedKey recipeKey = new NamespacedKey(this, "revive_beacon");
Bukkit.removeRecipe(recipeKey);
if (!getConfig().getBoolean("revive-beacon.recipe.enabled", true)) {
return;
}
ItemStack result = itemFactory.createBeaconCraftTemplate();
if (result == null) {
return;
}
ShapedRecipe recipe = new ShapedRecipe(recipeKey, result);
java.util.List<String> shape = getConfig().getStringList("revive-beacon.recipe.shape");
if (shape.isEmpty()) {
shape = java.util.List.of("GGG", "GNG", "GOG");
}
if (shape.size() > 3) {
getLogger().warning("revive-beacon.recipe.shape must contain 1 to 3 rows; revive beacon recipe was not registered.");
return;
}
recipe.shape(shape.toArray(new String[0]));
org.bukkit.configuration.ConfigurationSection ingredients = getConfig().getConfigurationSection("revive-beacon.recipe.ingredients");
java.util.Set<Character> symbols = new java.util.LinkedHashSet<>();
for (String row : shape) {
for (char symbol : row.toCharArray()) {
if (symbol != ' ') {
symbols.add(symbol);
}
}
}
for (char symbol : symbols) {
String symbolName = Character.toString(symbol);
String materialName = ingredients == null ? null : ingredients.getString(symbolName);
if (materialName == null) {
materialName = switch (symbol) {
case 'G' -> "GOLD_INGOT";
case 'N' -> "NETHER_STAR";
case 'O' -> "OBSIDIAN";
default -> null;
};
}
org.bukkit.Material material = materialName == null ? null
: XMaterial.matchXMaterial(materialName).map(XMaterial::parseMaterial).orElse(null);
if (material == null) {
getLogger().warning("Missing or invalid material for revive beacon recipe symbol: " + symbolName);
return;
}
recipe.setIngredient(symbol, material);
}
try {
Bukkit.addRecipe(recipe);
} catch (IllegalArgumentException exception) {
getLogger().warning("Invalid revive beacon recipe configuration: " + exception.getMessage());
}
}
public PlayerDataStore getDataStore() {
return dataStore;
}
public ItemFactory getItemFactory() {
return itemFactory;
}
public NamespacedKey getItemTypeKey() {
return itemTypeKey;
}
public NamespacedKey getItemTokenKey() {
return itemTokenKey;
}
public NamespacedKey getItemValueKey() {
return itemValueKey;
}
}