@@ -2,7 +2,6 @@ package net.knarcraft.paidsigns.manager;
|
||||
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* A manager that performs all Economy tasks
|
||||
@@ -55,7 +54,7 @@ public final class EconomyManager {
|
||||
* @param player <p>The player to withdraw money from</p>
|
||||
* @param cost <p>The amount of money to withdraw</p>
|
||||
*/
|
||||
public static void withdraw(Player player, double cost) {
|
||||
public static void withdraw(OfflinePlayer player, double cost) {
|
||||
economy.withdrawPlayer(player, cost);
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,128 @@
|
||||
package net.knarcraft.paidsigns.manager;
|
||||
|
||||
import net.knarcraft.paidsigns.PaidSigns;
|
||||
import net.knarcraft.paidsigns.container.TrackedSign;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* A manager for keeping track of plugin-signs created by players
|
||||
*/
|
||||
public class TrackedSignManager {
|
||||
|
||||
private static Map<Location, TrackedSign> trackedSigns = new HashMap<>();
|
||||
private static final File signsFile = new File(PaidSigns.getInstance().getDataFolder(), "data.yml");
|
||||
|
||||
/**
|
||||
* Adds a tracked sign to the manager
|
||||
*
|
||||
* @param signLocation <p>The location the sign was created at</p>
|
||||
* @param playerId <p>The unique id of the player that created the sign</p>
|
||||
* @throws IOException <p>If unable to save the tracked signs</p>
|
||||
*/
|
||||
public static void addTrackedSign(Location signLocation, UUID playerId, double cost) throws IOException {
|
||||
trackedSigns.put(signLocation, new TrackedSign(playerId, cost));
|
||||
saveTrackedSigns();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a tracked sign from the manager
|
||||
*
|
||||
* @param signLocation <p>The location the sign was removed from</p>
|
||||
* @throws IOException <p>If unable to save the tracked signs</p>
|
||||
*/
|
||||
public static void removeTrackedSign(Location signLocation) throws IOException {
|
||||
if (!trackedSigns.containsKey(signLocation)) {
|
||||
return;
|
||||
}
|
||||
trackedSigns.remove(signLocation);
|
||||
saveTrackedSigns();
|
||||
if (!PaidSigns.getInstance().areRefundsEnabled()) {
|
||||
return;
|
||||
}
|
||||
TrackedSign trackedSign = trackedSigns.get(signLocation);
|
||||
OfflinePlayer player = Bukkit.getOfflinePlayer(trackedSign.getPlayerId());
|
||||
double refundSum = trackedSign.getCost() / 100 * PaidSigns.getInstance().getRefundPercentage();
|
||||
EconomyManager.withdraw(player, refundSum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all tracked signs from the data file
|
||||
*/
|
||||
public static void loadTrackedSigns() {
|
||||
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
|
||||
ConfigurationSection signSection = configuration.getConfigurationSection("trackedSigns");
|
||||
trackedSigns = new HashMap<>();
|
||||
|
||||
if (signSection == null) {
|
||||
PaidSigns.getInstance().getLogger().log(Level.WARNING, "Signs section not found in data.yml");
|
||||
return;
|
||||
}
|
||||
|
||||
for (String key : signSection.getKeys(false)) {
|
||||
try {
|
||||
loadSign(signSection, key);
|
||||
} catch (InvalidConfigurationException e) {
|
||||
PaidSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to load sign " + key + ": " +
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a sign from the save file
|
||||
*
|
||||
* @param signSection <p>The configuration section containing signs</p>
|
||||
* @param key <p>The sign key which is also the sign's location</p>
|
||||
* @throws InvalidConfigurationException <p>If unable to load the sign</p>
|
||||
*/
|
||||
private static void loadSign(ConfigurationSection signSection, String key) throws InvalidConfigurationException {
|
||||
String[] locationParts = key.split(",");
|
||||
Location signLocation;
|
||||
try {
|
||||
signLocation = new Location(Bukkit.getWorld(UUID.fromString(locationParts[0])),
|
||||
Double.parseDouble(locationParts[1]), Double.parseDouble(locationParts[2]),
|
||||
Double.parseDouble(locationParts[3]));
|
||||
} catch (NumberFormatException exception) {
|
||||
throw new InvalidConfigurationException("Invalid sign coordinates");
|
||||
}
|
||||
|
||||
double cost = signSection.getDouble(key + ".cost");
|
||||
UUID playerId = UUID.fromString(Objects.requireNonNull(signSection.getString(key + ".playerId")));
|
||||
|
||||
TrackedSign trackedSign = new TrackedSign(playerId, cost);
|
||||
trackedSigns.put(signLocation, trackedSign);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the managed tracked signs to the data file
|
||||
*
|
||||
* @throws IOException <p>If unable to write to the data file</p>
|
||||
*/
|
||||
private static void saveTrackedSigns() throws IOException {
|
||||
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
|
||||
ConfigurationSection signSection = configuration.createSection("trackedSigns");
|
||||
|
||||
for (Location signLocation : trackedSigns.keySet()) {
|
||||
TrackedSign sign = trackedSigns.get(signLocation);
|
||||
String locationString = Objects.requireNonNull(signLocation.getWorld()).getUID() + "," +
|
||||
signLocation.getBlockX() + "," + signLocation.getBlockY() + "," + signLocation.getBlockZ();
|
||||
signSection.set(locationString + ".cost", sign.getCost());
|
||||
signSection.set(locationString + ".playerId", sign.getPlayerId());
|
||||
}
|
||||
configuration.save(signsFile);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user