There was no save system???? Also added proper voucher management.

This commit is contained in:
Brianna 2019-10-11 15:56:08 -04:00
parent 03af751c1a
commit 3470a75ea7
9 changed files with 132 additions and 65 deletions

View File

@ -17,14 +17,13 @@ import com.songoda.epicvouchers.settings.Settings;
import com.songoda.epicvouchers.voucher.CoolDownManager;
import com.songoda.epicvouchers.voucher.Voucher;
import com.songoda.epicvouchers.voucher.VoucherExecutor;
import com.songoda.epicvouchers.voucher.VoucherManager;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.PluginManager;
import java.io.File;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
public class EpicVouchers extends SongodaPlugin {
@ -32,12 +31,13 @@ public class EpicVouchers extends SongodaPlugin {
private static EpicVouchers INSTANCE;
private final GuiManager guiManager = new GuiManager(this);
private CommandManager commandManager;
private VoucherManager voucherManager;
private Connections connections;
private CoolDownManager coolDowns;
private VoucherExecutor voucherExecutor;
private CommandManager commandManager;
private Config vouchersConfig = new Config(this, "vouchers.yml");
private LinkedHashMap<String, Voucher> vouchers;
public static EpicVouchers getInstance() {
return INSTANCE;
@ -51,6 +51,7 @@ public class EpicVouchers extends SongodaPlugin {
@Override
public void onPluginDisable() {
connections.closeMySQL();
saveVouchers();
}
@Override
@ -79,11 +80,10 @@ public class EpicVouchers extends SongodaPlugin {
IconInv.init(this);
BountifulAPI.init(this);
this.vouchers = new LinkedHashMap<>();
this.connections = new Connections(this);
this.coolDowns = new CoolDownManager(this);
this.voucherExecutor = new VoucherExecutor(this);
this.voucherManager = new VoucherManager();
PluginManager manager = Bukkit.getServer().getPluginManager();
@ -92,21 +92,18 @@ public class EpicVouchers extends SongodaPlugin {
manager.registerEvents(new PlayerInteractListener(this), this);
manager.registerEvents(new PlayerCommandListener(), this);
File folder = getDataFolder();
File voucherFile = new File(folder, "vouchers.yml");
if (!voucherFile.exists()) {
saveResource("vouchers.yml", true);
}
saveResource("vouchers.yml", false);
vouchersConfig.load();
loadVouchersFromFile();
connections.openMySQL();
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this::saveVouchers, 6000, 6000);
}
private void loadVouchersFromFile() {
vouchers.clear();
vouchersConfig.load();
voucherManager.clearVouchers();
if (vouchersConfig.contains("vouchers")) {
for (String key : vouchersConfig.getConfigurationSection("vouchers").getKeys(false)) {
@ -124,41 +121,82 @@ public class EpicVouchers extends SongodaPlugin {
if (material == null) material = Material.PAPER;
}
voucher.setPermission(cs.getString("permission", ""));
voucher.setMaterial(material);
voucher.setData((short) cs.getInt("data", 0));
voucher.setName(cs.getString("name", "default"));
voucher.setLore(cs.getStringList("lore"));
voucher.setGlow(cs.getBoolean("glow", false));
voucher.setConfirm(cs.getBoolean("confirm", true));
voucher.setUnbreakable(cs.getBoolean("unbreakable", false));
voucher.setHideAttributes(cs.getBoolean("hide-attributes", false));
voucher.setRemoveItem(cs.getBoolean("remove-item", true));
voucher.setHealPlayer(cs.getBoolean("heal-player", false));
voucher.setSmiteEffect(cs.getBoolean("smite-effect", false));
voucher.setCoolDown(cs.getInt("coolDown", 0));
voucher.setBroadcasts(cs.getStringList("broadcasts"));
voucher.setMessages(cs.getStringList("messages"));
voucher.setCommands(cs.getStringList("commands"));
voucher.setActionBar(cs.getString("actionbar"));
voucher.setTitle(cs.getString("titles.title"));
voucher.setSubTitle(cs.getString("titles.subtitle"));
voucher.setTitleFadeIn(cs.getInt("titles.fade-in", 0));
voucher.setTitleStay(cs.getInt("titles.stay", 0));
voucher.setTitleFadeOut(cs.getInt("titles.fade-out", 0));
voucher.setSound(cs.getString("sounds.sound"));
voucher.setSoundPitch(cs.getInt("sounds.pitch", 0));
voucher.setParticle(cs.getString("particles.particle"));
voucher.setParticleAmount(cs.getInt("particles.amount", 0));
voucher.setEffect(cs.getString("effects.effect"));
voucher.setEffectAmplifier(cs.getInt("effects.amplifier"));
voucher.setItemStack(cs.getItemStack("itemstack", null));
voucher.setPermission(cs.getString("permission", ""))
.setMaterial(material)
.setData((short) cs.getInt("data", 0))
.setName(cs.getString("name", "default"))
.setLore(cs.getStringList("lore"))
.setGlow(cs.getBoolean("glow", false))
.setConfirm(cs.getBoolean("confirm", true))
.setUnbreakable(cs.getBoolean("unbreakable", false))
.setHideAttributes(cs.getBoolean("hide-attributes", false))
.setRemoveItem(cs.getBoolean("remove-item", true))
.setHealPlayer(cs.getBoolean("heal-player", false))
.setSmiteEffect(cs.getBoolean("smite-effect", false))
.setCoolDown(cs.getInt("coolDown", 0))
.setBroadcasts(cs.getStringList("broadcasts"))
.setMessages(cs.getStringList("messages"))
.setCommands(cs.getStringList("commands"))
.setActionBar(cs.getString("actionbar"))
.setTitle(cs.getString("titles.title"))
.setSubTitle(cs.getString("titles.subtitle"))
.setTitleFadeIn(cs.getInt("titles.fade-in", 0))
.setTitleStay(cs.getInt("titles.stay", 0))
.setTitleFadeOut(cs.getInt("titles.fade-out", 0))
.setSound(cs.getString("sounds.sound"))
.setSoundPitch(cs.getInt("sounds.pitch", 0))
.setParticle(cs.getString("particles.particle"))
.setParticleAmount(cs.getInt("particles.amount", 0))
.setEffect(cs.getString("effects.effect"))
.setEffectAmplifier(cs.getInt("effects.amplifier"))
.setItemStack(cs.getItemStack("itemstack", null));
vouchers.put(key, voucher);
voucherManager.addVoucher(voucher);
}
}
}
private void saveVouchers() {
for (String voucherName : vouchersConfig.getConfigurationSection("vouchers").getKeys(false)) {
if (voucherManager.getVouchers().stream().noneMatch(voucher -> voucher.getKey().equals(voucherName)))
vouchersConfig.set("vouchers." + voucherName, null);
}
for (Voucher voucher : voucherManager.getVouchers()) {
String prefix = "vouchers." + voucher.getKey() + ".";
vouchersConfig.set(prefix + "permission", voucher.getPermission());
vouchersConfig.set(prefix + "material", voucher.getMaterial().name());
vouchersConfig.set(prefix + "data", voucher.getData());
vouchersConfig.set(prefix + "name", voucher.getName());
vouchersConfig.set(prefix + "lore", voucher.getLore());
vouchersConfig.set(prefix + "glow", voucher.isGlow());
vouchersConfig.set(prefix + "confirm", voucher.isConfirm());
vouchersConfig.set(prefix + "unbreakable", voucher.isUnbreakable());
vouchersConfig.set(prefix + "hide-attributes", voucher.isHideAttributes());
vouchersConfig.set(prefix + "remove-item", voucher.isRemoveItem());
vouchersConfig.set(prefix + "heal-player", voucher.isHealPlayer());
vouchersConfig.set(prefix + "smite-effect", voucher.isSmiteEffect());
vouchersConfig.set(prefix + "coolDown", voucher.getCoolDown());
vouchersConfig.set(prefix + "broadcasts", voucher.getBroadcasts());
vouchersConfig.set(prefix + "messages", voucher.getMessages());
vouchersConfig.set(prefix + "commands", voucher.getCommands());
vouchersConfig.set(prefix + "actionbar", voucher.getActionBar());
vouchersConfig.set(prefix + "titles.title", voucher.getTitle());
vouchersConfig.set(prefix + "titles.subtitle", voucher.getSubTitle());
vouchersConfig.set(prefix + "titles.fade-in", voucher.getTitleFadeIn());
vouchersConfig.set(prefix + "titles.stay", voucher.getTitleStay());
vouchersConfig.set(prefix + "titles.fade-out", voucher.getTitleFadeOut());
vouchersConfig.set(prefix + "sounds.sound", voucher.getSound());
vouchersConfig.set(prefix + "sounds.pitch", voucher.getSoundPitch());
vouchersConfig.set(prefix + "particles.particle", voucher.getParticle());
vouchersConfig.set(prefix + "particles.amount", voucher.getParticleAmount());
vouchersConfig.set(prefix + "effects.effect", voucher.getEffect());
vouchersConfig.set(prefix + "effects.amplifier", voucher.getEffectAmplifier());
vouchersConfig.set(prefix + "itemstack", voucher.getItemStack());
}
vouchersConfig.saveChanges();
}
@Override
public void onConfigReload() {
vouchersConfig.load();
@ -188,14 +226,14 @@ public class EpicVouchers extends SongodaPlugin {
return this.vouchersConfig;
}
public LinkedHashMap<String, Voucher> getVouchers() {
return this.vouchers;
}
public CommandManager getCommandManager() {
return commandManager;
}
public VoucherManager getVoucherManager() {
return voucherManager;
}
public GuiManager getGuiManager() {
return guiManager;
}

View File

@ -34,7 +34,7 @@ public class CommandForce extends AbstractCommand {
return ReturnType.FAILURE;
}
Voucher voucher = instance.getVouchers().get(args[1]);
Voucher voucher = instance.getVoucherManager().getVoucher(args[1]);
if (voucher == null) {
sender.sendMessage("Unknown voucher...");
return ReturnType.FAILURE;
@ -54,7 +54,7 @@ public class CommandForce extends AbstractCommand {
if (args.length == 1) {
return Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName).collect(Collectors.toList());
} else if (args.length == 2) {
return new ArrayList<>(instance.getVouchers().keySet());
return instance.getVoucherManager().getVouchers().stream().map(Voucher::getKey).collect(Collectors.toList());
} else if (args.length == 3) {
return Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
}

View File

@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class CommandForceAll extends AbstractCommand {
@ -24,7 +25,7 @@ public class CommandForceAll extends AbstractCommand {
if (args.length != 2)
return ReturnType.SYNTAX_ERROR;
Voucher voucher = instance.getVouchers().get(args[0]);
Voucher voucher = instance.getVoucherManager().getVoucher(args[0]);
if (voucher == null) {
sender.sendMessage("Unknown voucher...");
return ReturnType.FAILURE;
@ -42,7 +43,7 @@ public class CommandForceAll extends AbstractCommand {
@Override
protected List<String> onTab(CommandSender sender, String... args) {
if (args.length == 1) {
return new ArrayList<>(instance.getVouchers().keySet());
return instance.getVoucherManager().getVouchers().stream().map(Voucher::getKey).collect(Collectors.toList());
} else if (args.length == 2) {
return Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
}

View File

@ -35,7 +35,7 @@ public class CommandGive extends AbstractCommand {
return ReturnType.FAILURE;
}
Voucher voucher = instance.getVouchers().get(args[1]);
Voucher voucher = instance.getVoucherManager().getVoucher(args[1]);
if (voucher == null) {
sender.sendMessage("Unknown voucher...");
return ReturnType.FAILURE;
@ -50,7 +50,7 @@ public class CommandGive extends AbstractCommand {
if (args.length == 1) {
return Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName).collect(Collectors.toList());
} else if (args.length == 2) {
return new ArrayList<>(instance.getVouchers().keySet());
return instance.getVoucherManager().getVouchers().stream().map(Voucher::getKey).collect(Collectors.toList());
} else if (args.length == 3) {
return Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
}

View File

@ -28,7 +28,7 @@ public class CommandGiveAll extends AbstractCommand {
if (args.length != 2)
return ReturnType.SYNTAX_ERROR;
Voucher voucher = instance.getVouchers().get(args[0]);
Voucher voucher = instance.getVoucherManager().getVoucher(args[0]);
if (voucher == null) {
sender.sendMessage("Unknown voucher...");
return ReturnType.FAILURE;
@ -41,7 +41,7 @@ public class CommandGiveAll extends AbstractCommand {
@Override
protected List<String> onTab(CommandSender sender, String... args) {
if (args.length == 1) {
return new ArrayList<>(instance.getVouchers().keySet());
return instance.getVoucherManager().getVouchers().stream().map(Voucher::getKey).collect(Collectors.toList());
} else if (args.length == 2) {
return Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
}

View File

@ -3,10 +3,12 @@ package com.songoda.epicvouchers.commands;
import com.songoda.core.commands.AbstractCommand;
import com.songoda.epicvouchers.EpicVouchers;
import com.songoda.epicvouchers.menus.VoucherMenu;
import com.songoda.epicvouchers.voucher.Voucher;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.stream.Collectors;
public class CommandList extends AbstractCommand {
@ -20,7 +22,8 @@ public class CommandList extends AbstractCommand {
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
instance.getLocale().getMessage("command.list.list")
.processPlaceholder("list", String.join(", ", instance.getVouchers().keySet()))
.processPlaceholder("list",
instance.getVoucherManager().getVouchers().stream().map(Voucher::getKey).collect(Collectors.joining(", ")))
.sendPrefixedMessage(sender);
return ReturnType.SUCCESS;
}

View File

@ -30,7 +30,7 @@ public class PlayerInteractListener implements Listener {
return;
final Player player = event.getPlayer();
for (Voucher voucher : instance.getVouchers().values()) {
for (Voucher voucher : instance.getVoucherManager().getVouchers()) {
final ItemStack voucherItem = voucher.toItemStack();
// does the item they're holding match this voucher?

View File

@ -17,9 +17,9 @@ import static org.bukkit.Material.PAPER;
public class VoucherMenu extends IconInv {
public VoucherMenu(EpicVouchers instance) {
super((int) (Math.ceil(instance.getVouchers().size() / 9.0) * 9 + (instance.getVouchers().size() % 9 == 0 ? 9 : 0)), "Vouchers");
super((int) (Math.ceil(instance.getVoucherManager().getVouchers().size() / 9.0) * 9 + (instance.getVoucherManager().getVouchers().size() % 9 == 0 ? 9 : 0)), "Vouchers");
for (Voucher voucher : instance.getVouchers().values()) {
for (Voucher voucher : instance.getVoucherManager().getVouchers()) {
if (getInventory().firstEmpty() != -1) {
addIcon(getInventory().firstEmpty(), voucher.toItemStack(), event -> new OptionMenu(instance, voucher).open(event.getPlayer()));
}
@ -32,7 +32,7 @@ public class VoucherMenu extends IconInv {
gui.setTitle("Insert id");
gui.setAction(aevent -> {
final String msg = gui.getInputText().trim();
if (instance.getVouchers().containsKey(msg)) {
if (instance.getVoucherManager().getVoucher(msg) != null) {
event.getPlayer().sendMessage(TextUtils.formatText("&cAlready a voucher registered with the id: " + msg));
new VoucherMenu(instance).open(event.getPlayer());
return;
@ -42,10 +42,7 @@ public class VoucherMenu extends IconInv {
voucher.setMaterial(PAPER);
voucher.setName("&f" + msg);
instance.getVouchers().put(msg, voucher);
instance.getConfig().set("vouchers." + msg + ".material", voucher.getMaterial().toString());
instance.getConfig().set("vouchers." + msg + ".name", voucher.getName(false));
instance.getVouchersConfig().save();
instance.getVoucherManager().addVoucher(voucher);
event.getPlayer().sendMessage(TextUtils.formatText("&7Successfully created voucher with id &r" + msg + "&7."));
new VoucherMenu(instance).open(event.getPlayer());
});

View File

@ -0,0 +1,28 @@
package com.songoda.epicvouchers.voucher;
import java.util.*;
public class VoucherManager {
private final Map<String, Voucher> registeredVouchers = new HashMap<>();
public Voucher addVoucher(Voucher voucher) {
return registeredVouchers.put(voucher.getKey(), voucher);
}
public Voucher removeVoucher(Voucher voucher) {
return registeredVouchers.remove(voucher);
}
public Voucher getVoucher(String key) {
return registeredVouchers.get(key);
}
public Collection<Voucher> getVouchers() {
return registeredVouchers.values();
}
public void clearVouchers() {
registeredVouchers.clear();
}
}