Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<dependency>
<groupId>studio.magemonkey</groupId>
<artifactId>divinity</artifactId>
<version>1.0.2-R0.47-SNAPSHOT</version>
<version>1.0.2-R0.57-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>studio.magemonkey</groupId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/studio/magemonkey/fusion/Fusion.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import studio.magemonkey.codex.util.ItemUtils;
import studio.magemonkey.fusion.api.FusionAPI;
import studio.magemonkey.fusion.cfg.*;
import studio.magemonkey.fusion.cfg.FuelManager;
import studio.magemonkey.fusion.cfg.editors.EditorRegistry;
import studio.magemonkey.fusion.cfg.hooks.HookManager;
import studio.magemonkey.fusion.cfg.hooks.HookType;
Expand Down Expand Up @@ -70,6 +71,7 @@ public void reloadConfig() {
hookManager = new HookManager();

Cfg.init();
FuelManager.init();
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
ProfessionsCfg.init();
EditorRegistry.reload();
Expand Down
55 changes: 53 additions & 2 deletions src/main/java/studio/magemonkey/fusion/cfg/Cfg.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import studio.magemonkey.fusion.Fusion;
import studio.magemonkey.fusion.api.FusionAPI;
import studio.magemonkey.fusion.cfg.sql.DatabaseType;
import studio.magemonkey.fusion.commands.CommandMechanics;
import studio.magemonkey.fusion.data.player.FusionPlayer;
Expand All @@ -17,6 +18,7 @@
import java.io.IOException;
import java.sql.Array;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -41,6 +43,11 @@ public final class Cfg {
public static List<NamespacedKey> disabledVanillaRecipes = new ArrayList<>();
public static List<String> autoJoinProfessions = new ArrayList<>();

// Fuel system
public static int fuelStart = 0;
public static int fuelMax = 1000;
public static List<FuelItem> fuelItems = new ArrayList<>();

// No usage inside of Cfg, just used for default values. The actual values are stored in SQLManager.class
private static final DatabaseType storageType = DatabaseType.LOCAL;
private static final String storageHost = "localhost";
Expand Down Expand Up @@ -103,6 +110,19 @@ private static void addDefs(FileConfiguration cfg) {
if (!cfg.isSet("useCustomFormula")) cfg.set("useCustomFormula", useCustomFormula);
if (!cfg.isSet("disabled_vanilla_recipes")) cfg.set("disabled_vanilla_recipes", disabledVanillaRecipes);
if (!cfg.isSet("auto_join_professions")) cfg.set("auto_join_professions", autoJoinProfessions);

// Fuel defaults
if (!cfg.isSet("fuel.start")) cfg.set("fuel.start", 0);
if (!cfg.isSet("fuel.max")) cfg.set("fuel.max", 1000);
if (!cfg.isSet("fuel.items.coal.material")) cfg.set("fuel.items.coal.material", "COAL");
if (!cfg.isSet("fuel.items.coal.amount")) cfg.set("fuel.items.coal.amount", 10);
if (!cfg.isSet("fuel.items.coal.return")) cfg.set("fuel.items.coal.return", "null");
if (!cfg.isSet("fuel.items.coal_block.material")) cfg.set("fuel.items.coal_block.material", "COAL_BLOCK");
if (!cfg.isSet("fuel.items.coal_block.amount")) cfg.set("fuel.items.coal_block.amount", 90);
if (!cfg.isSet("fuel.items.coal_block.return")) cfg.set("fuel.items.coal_block.return", "null");
if (!cfg.isSet("fuel.items.blaze_rod.material")) cfg.set("fuel.items.blaze_rod.material", "BLAZE_ROD");
if (!cfg.isSet("fuel.items.blaze_rod.amount")) cfg.set("fuel.items.blaze_rod.amount", 50);
if (!cfg.isSet("fuel.items.blaze_rod.return")) cfg.set("fuel.items.blaze_rod.return", "BLAZE_POWDER");
}

public static void init() {
Expand All @@ -129,6 +149,36 @@ public static void init() {
disabledVanillaRecipes = BukkitRecipeWrapper.getRecipeKeysForMaterials(materials);
autoJoinProfessions = cfg.getStringList("auto_join_professions");

// Load fuel config
fuelStart = cfg.getInt("fuel.start", 0);
fuelMax = cfg.getInt("fuel.max", 1000);
fuelItems = new ArrayList<>();
if (cfg.isConfigurationSection("fuel.items")) {
for (String key : cfg.getConfigurationSection("fuel.items").getKeys(false)) {
String path = "fuel.items." + key;
String matName = cfg.getString(path + ".material", "");
int fuelAmount = cfg.getInt(path + ".amount", 0);
String retName = cfg.getString(path + ".return", "null");

Material mat;
try {
mat = Material.valueOf(matName.toUpperCase());
} catch (IllegalArgumentException e) {
Fusion.getInstance().getLogger().warning("Unknown material '" + matName + "' in fuel.items." + key);
continue;
}
Material ret = null;
if (retName != null && !retName.equalsIgnoreCase("null") && !retName.isEmpty()) {
try {
ret = Material.valueOf(retName.toUpperCase());
} catch (IllegalArgumentException e) {
Fusion.getInstance().getLogger().warning("Unknown return material '" + retName + "' in fuel.items." + key);
}
}
fuelItems.add(new FuelItem(key, mat, fuelAmount, ret));
}
}

migrateOldTypes(cfg);
}

Expand Down Expand Up @@ -188,8 +238,9 @@ public static boolean setDatabaseType(DatabaseType type) {
public static void autoJoinProfessions(Player player) {
FusionPlayer fp = PlayerLoader.getPlayer(player);
for (String professionId : autoJoinProfessions) {
if(fp.hasProfession(professionId) && fp.hasJoined(professionId)) continue;
BrowseGUI.joinProfession(player, ProfessionsCfg.getGuiMap().get(professionId));
if (fp.hasProfession(professionId) && fp.hasJoined(professionId)) continue;
if (ProfessionsCfg.getTable(professionId) == null) continue;
FusionAPI.getEventServices().getProfessionService().joinProfession(professionId, player, 0.0, 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,17 @@ public static String getCanCraft(boolean fulfilled) {
return ChatUT.hexString(config.getString("recipes.canCraft." + (fulfilled ? "true" : "false"),
(fulfilled ? "&aYou can craft this item." : "&cYou can't craft this item.")));
}

public static String getStationLine(String path, boolean fulfilled, String stationName) {
String displayName = stationName;
// Jeśli to divinity, pokaż ładniejszą nazwę
if (stationName.toLowerCase().startsWith("divinity:")) {
displayName = "Divinity: " + stationName.substring("divinity:".length());
}
String line = config.getString(path + ".station." + (fulfilled ? "true" : "false"),
fulfilled ? "&6- &eStation: &7(&a" + displayName + "&7)"
: "&6- &eStation: &7(&c" + displayName + "&7)");
return ChatUT.hexString(line);
}
public static String getLimit(String path, int limit, int maxLimit) {
boolean fulfilled = limit < maxLimit;
String line = config.getString(path + ".limit." + (fulfilled ? "true" : "false"),
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/studio/magemonkey/fusion/cfg/FuelItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package studio.magemonkey.fusion.cfg;

import lombok.Getter;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.List;

/**
* Represents one type of item that can be used as fuel in the server fuel tank.
*/
@Getter
public class FuelItem {

private final String id; // config key (e.g. "coal")
private final Material material; // item material
private final int fuelAmount; // fuel added per item consumed
private final Material returnMaterial; // item returned after use (null = nothing)

public FuelItem(String id, Material material, int fuelAmount, Material returnMaterial) {
this.id = id;
this.material = material;
this.fuelAmount = fuelAmount;
this.returnMaterial = returnMaterial;
}

/**
* Creates a display ItemStack for use in the FuelGUI.
*/
public ItemStack createDisplayItem() {
ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(ChatColor.YELLOW + id);
meta.setLore(List.of(
ChatColor.GRAY + "Fuel per item: " + ChatColor.WHITE + fuelAmount,
returnMaterial != null
? ChatColor.GRAY + "Returns: " + ChatColor.WHITE + returnMaterial.name()
: ChatColor.DARK_GRAY + "No return item",
"",
ChatColor.GREEN + "Click to add fuel from inventory"
));
item.setItemMeta(meta);
}
return item;
}

/**
* Returns true if the given ItemStack matches this fuel item's material.
*/
public boolean matches(ItemStack stack) {
return stack != null && stack.getType() == material;
}
}
157 changes: 157 additions & 0 deletions src/main/java/studio/magemonkey/fusion/cfg/FuelManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package studio.magemonkey.fusion.cfg;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import studio.magemonkey.fusion.Fusion;

import java.io.File;
import java.io.IOException;

/**
* Global per-server fuel counter.
*
* Configuration (config.yml):
* fuel.start — initial fuel on first start
* fuel.max — maximum capacity
* fuel.items — items that can be used as fuel (loaded into Cfg.fuelItems)
*
* Persistence (fuel.yml):
* current — current fuel level (updated live)
*/
public final class FuelManager {

private static int current = 0;

private FuelManager() {}

// ──────────────────────────────────────────────
// Initialisation — called from Fusion.reloadConfig()
// ──────────────────────────────────────────────

public static void init() {
// Load current level from fuel.yml; use Cfg.fuelStart as default
FileConfiguration saved = loadFuelFile();
current = saved.getInt("current", Cfg.fuelStart);

// Clamp to valid range from config
if (current < 0) current = 0;
if (current > Cfg.fuelMax) current = Cfg.fuelMax;
}

// ──────────────────────────────────────────────
// fuel.yml helpers (stores only current level)
// ──────────────────────────────────────────────

private static File getFuelFile() {
return new File(Fusion.getInstance().getDataFolder(), "fuel.yml");
}

private static FileConfiguration loadFuelFile() {
File file = getFuelFile();
FileConfiguration cfg = new YamlConfiguration();
if (file.exists()) {
try {
cfg.load(file);
} catch (Exception e) {
Fusion.getInstance().getLogger().severe("Could not load fuel.yml: " + e.getMessage());
}
}
return cfg;
}

public static void save() {
File file = getFuelFile();
FileConfiguration cfg = new YamlConfiguration();
cfg.set("current", current);
try {
file.getParentFile().mkdirs();
cfg.save(file);
} catch (IOException e) {
Fusion.getInstance().getLogger().severe("Could not save fuel.yml: " + e.getMessage());
}
}

// ──────────────────────────────────────────────
// API
// ──────────────────────────────────────────────

public static int getFuel() {
return current;
}

public static int getMaxFuel() {
return Cfg.fuelMax;
}

public static boolean hasFuel(int cost) {
if (cost <= 0) return true;
return current >= cost;
}

/**
* Consumes {@code cost} fuel. Returns true if successful, false if not enough.
*/
public static boolean consumeFuel(int cost) {
if (cost <= 0) return true;
if (current < cost) return false;
current -= cost;
save();
return true;
}

/**
* Adds up to {@code amount} fuel (capped at max). Returns the actual amount added.
*/
public static int addFuel(int amount) {
if (amount <= 0) return 0;
int space = Cfg.fuelMax - current;
int added = Math.min(amount, space);
current += added;
save();
return added;
}

/**
* Sets fuel directly, clamped to [0, max].
*/
public static void setFuel(int amount) {
current = Math.max(0, Math.min(amount, Cfg.fuelMax));
save();
}

/**
* Attempts to add fuel using one of the configured fuel items from the player's inventory.
* Takes one matching item, adds its fuel amount, gives return item if configured.
* Returns the FuelItem used, or null if the player had none of the given item.
*/
public static FuelItem addFuelFromInventory(Player player, FuelItem fuelItem) {
// Check inventory for a matching item
ItemStack[] contents = player.getInventory().getContents();
for (int i = 0; i < contents.length; i++) {
ItemStack stack = contents[i];
if (fuelItem.matches(stack)) {
// Take 1 of the item
if (stack.getAmount() == 1) {
player.getInventory().setItem(i, null);
} else {
stack.setAmount(stack.getAmount() - 1);
}

// Add fuel
addFuel(fuelItem.getFuelAmount());

// Give return item
if (fuelItem.getReturnMaterial() != null) {
player.getInventory().addItem(new ItemStack(fuelItem.getReturnMaterial(), 1))
.values()
.forEach(drop -> player.getWorld().dropItemNaturally(player.getLocation(), drop));
}

return fuelItem;
}
}
return null; // player didn't have the item
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ProfessionLevelCfg(CraftingTable table, String filePath) {
}

public double getXP(int level) {
return levelMap.get(level);
return levelMap.getOrDefault(level, 0.0);
}

public int getLevel(double xp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import studio.magemonkey.fusion.gui.ProfessionGuiRegistry;
import studio.magemonkey.fusion.hook.NexoHook;
import studio.magemonkey.fusion.util.ChatUT;
import studio.magemonkey.fusion.util.TabCacher;
import studio.magemonkey.fusion.util.Utils;

import java.io.File;
Expand Down Expand Up @@ -73,6 +74,7 @@ public static boolean createNewProfession(String profession, String refProfessio
map.put(ct.getName(), ct);
cfgs.put(profession, cfg);
files.put(profession, file);
TabCacher.clearAllCaches("professions");
return true;
} else if (refProfession == null) {
files.put(profession,
Expand All @@ -92,6 +94,7 @@ public static boolean createNewProfession(String profession, String refProfessio
map.put(ct.getName(), ct);
files.put(profession, file);
cfgs.put(profession, cfg);
TabCacher.clearAllCaches("professions");
return true;
}
} catch (Exception e) {
Expand Down Expand Up @@ -138,6 +141,8 @@ private static void loadProfessions(File root) {
String key = entry.getKey();
guiMap.put(key, new ProfessionGuiRegistry(key));
}

TabCacher.clearAllCaches("professions");
}

public static CraftingTable getTable(String str) {
Expand Down
Loading