package net.brennholz.challenges;
import com.google.common.io.MoreFiles;
import com.google.common.io.RecursiveDeleteOption;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public class Challenges extends JavaPlugin {
private static Challenges plugin;
private File BackpackConfigFile;
private FileConfiguration BackpackConfig;
private File RndDropsConfigFile;
private FileConfiguration RndDropsConfig;
private Timer_Command Timer;
private Backpack backpack;
private SBManager sbManager;
private RandomDrops rndDrops;
public static Inventory SettingsGUI = Bukkit.createInventory(null, 45, "§6Settings");
public static String GUIName = "§6Settings";
public static Challenges getplugin() { return plugin; }
public void onEnable() {
plugin = this;
this.Timer = new Timer_Command();
this.backpack = new Backpack();
this.rndDrops = new RandomDrops();
getCommand("settings").setExecutor((CommandExecutor)this);
getCommand("hp").setExecutor(new HP_Command());
getCommand("coords").setExecutor(new Coords_Command());
getCommand("revive").setExecutor(new Revive_Command());
getCommand("timer").setExecutor(new Timer_Command());
getCommand("challenges").setExecutor(new Challenges_Command());
getCommand("backpack").setExecutor(new Backpack());
getCommand("reset").setExecutor(new Reset_Command());
Bukkit.getPluginManager().registerEvents(new EventListener(), (Plugin)this);
saveDefaultConfig();
reloadConfig();
createRndDropsConfig();
createBackpackConfig();
if (getConfig().getBoolean("World_Reset")) {
getLogger().info("/Reset Command was executed!");
getLogger().info("Preparing world reset!");
String worldname = getConfig().getString("World_Name");
String nethername = worldname + "_nether";
String endname = worldname + "_the_end";
File worldfolder = new File(Bukkit.getWorldContainer().getPath() + "/" + worldname);
File netherfolder = new File(Bukkit.getWorldContainer().getPath() + "/" + nethername);
File endfolder = new File(Bukkit.getWorldContainer().getPath() + "/" + endname);
try {
MoreFiles.deleteRecursively(worldfolder.toPath(), new RecursiveDeleteOption[] { RecursiveDeleteOption.ALLOW_INSECURE });
MoreFiles.deleteRecursively(netherfolder.toPath(), new RecursiveDeleteOption[] { RecursiveDeleteOption.ALLOW_INSECURE });
MoreFiles.deleteRecursively(endfolder.toPath(), new RecursiveDeleteOption[] { RecursiveDeleteOption.ALLOW_INSECURE });
getLogger().info("World reset successful!");
} catch (IOException e) {
getLogger().info("World reset failed!");
e.printStackTrace();
}
getConfig().set("World_Reset", Boolean.valueOf(false));
saveConfig();
this.backpack.clearConfig();
this.rndDrops.shuffleItems();
this.rndDrops.saveItems();
this.rndDrops.loadItems();
} else {
this.rndDrops.loadItems();
}
this.backpack.loadInventoryFromConfig();
this.Timer.Run_Timer();
getLogger().info("Plugin loaded!");
this.sbManager = new SBManager();
}
public void onDisable() {
this.backpack.saveInventoryToConfig();
try {
this.BackpackConfig.save(this.BackpackConfigFile);
this.RndDropsConfig.save(this.RndDropsConfigFile);
} catch (IOException e) {
e.printStackTrace();
}
getLogger().info("Plugin unloaded");
}
public RandomDrops getRandomDropsManager() {
return this.rndDrops;
}
public FileConfiguration getBackpackConfig() {
return this.BackpackConfig;
}
private void createBackpackConfig() {
this.BackpackConfigFile = new File(getDataFolder(), "backpack.yml");
if (!this.BackpackConfigFile.exists()) {
this.BackpackConfigFile.getParentFile().mkdirs();
saveResource("backpack.yml", false);
}
this.BackpackConfig = (FileConfiguration)new YamlConfiguration();
try {
this.BackpackConfig.load(this.BackpackConfigFile);
} catch (IOException|org.bukkit.configuration.InvalidConfigurationException e) {
e.printStackTrace();
}
}
public FileConfiguration getRndDropsConfig() {
return this.RndDropsConfig;
}
private void createRndDropsConfig() {
this.RndDropsConfigFile = new File(getDataFolder(), "rnddrops.yml");
if (!this.RndDropsConfigFile.exists()) {
this.RndDropsConfigFile.getParentFile().mkdirs();
saveResource("rnddrops.yml", false);
}
this.RndDropsConfig = (FileConfiguration)new YamlConfiguration();
try {
this.RndDropsConfig.load(this.RndDropsConfigFile);
} catch (IOException|org.bukkit.configuration.InvalidConfigurationException e) {
e.printStackTrace();
}
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player p = (Player)sender;
if (args.length == 0) {
if (p.hasPermission("challenges.settings.view")) {
createGUI(p);
} else {
p.sendMessage("hast hierfkeine Berechtigung");
}
} else if (args.length == 1) {
if (args[0].equalsIgnoreCase("get")) {
if (p.hasPermission("challenges.settings.get")) {
p.sendMessage("Einstellungen");
p.sendMessage("Crafting =" + getConfig().getBoolean("craftingtable"));
p.sendMessage("Handeln =" + getConfig().getBoolean("allowtrading"));
p.sendMessage("bei Fallschaden =" + getConfig().getBoolean("deathonfall"));
p.sendMessage("bei Schleichen =" + getConfig().getBoolean("damageonsneak"));
p.sendMessage("Schaden bei Schleichen =" + getConfig().getInt("sneakdmg"));
p.sendMessage("Drops =" + getConfig().getBoolean("rnddrops"));
p.sendMessage("Schaden =" + getConfig().getBoolean("sharedmg"));
p.sendMessage("Leben falle =" + getConfig().getBoolean("onelife"));
p.sendMessage("in Chat =" + getConfig().getBoolean("dmginchat"));
p.sendMessage("=" + getConfig().getBoolean("respawn"));
} else {
p.sendMessage("hast hierfkeine Berechtigung");
}
} else {
p.sendMessage("/settings Get/Crafting/Trading/DeathFall/DMGOnSneak/SneakDMG/RndDrops/ShareDMG/Respawn/OneLife/ChatDMG [True/False]");
}
} else if (args.length >= 2) {
if (p.hasPermission("challenges.settings.modify")) {
Boolean Bwert = Boolean.valueOf(Boolean.parseBoolean(args[1]));
if (args[0].equalsIgnoreCase("crafting")) {
getConfig().set("craftingtable", Bwert);
saveConfig();
p.sendMessage("Crafting =" + getConfig().getBoolean("craftingtable"));
} else if (args[0].equalsIgnoreCase("deathfall")) {
getConfig().set("deathonfall", Bwert);
saveConfig();
p.sendMessage("bei Fallschaden =" + getConfig().getBoolean("deathonfall"));
} else if (args[0].equalsIgnoreCase("trading")) {
getConfig().set("allowtrading", Bwert);
saveConfig();
p.sendMessage("Handeln =" + getConfig().getBoolean("allowtrading"));
} else if (args[0].equalsIgnoreCase("dmgonsneak")) {
getConfig().set("damageonsneak", Bwert);
saveConfig();
p.sendMessage("bei Schleichen =" + getConfig().getBoolean("damageonsneak"));
} else if (args[0].equalsIgnoreCase("sneakdmg")) {
Double Dwert = Double.valueOf(Double.parseDouble(args[1]));
getConfig().set("sneakdmg", Dwert);
saveConfig();
p.sendMessage("Schaden bei Schleichen =" + getConfig().getString("sneakdmg"));
} else if (args[0].equalsIgnoreCase("rnddrops")) {
getConfig().set("rnddrops", Bwert);
saveConfig();
p.sendMessage("Drops =" + getConfig().getBoolean("rnddrops"));
} else if (args[0].equalsIgnoreCase("sharedmg")) {
getConfig().set("sharehp", Bwert);
saveConfig();
p.sendMessage("Schaden =" + getConfig().getBoolean("sharedmg"));
} else if (args[0].equalsIgnoreCase("respawn")) {
getConfig().set("respawn", Bwert);
saveConfig();
p.sendMessage("=" + getConfig().getBoolean("respawn"));
} else if (args[0].equalsIgnoreCase("onelife")) {
getConfig().set("onelife", Bwert);
saveConfig();
p.sendMessage("Leben falle =" + getConfig().getBoolean("onelife"));
} else if (args[0].equalsIgnoreCase("chatdmg")) {
getConfig().set("dmginchat", Bwert);
saveConfig();
p.sendMessage("in Chat =" + getConfig().getBoolean("dmginchat"));
} else {
p.sendMessage("Parameter!");
}
} else {
p.sendMessage("hast hierfkeine Berechtigung");
}
} else {
p.sendMessage("/settings Get/Crafting/Trading/DeathFall/DMGOnSneak/SneakDMG/RndDrops/ShareDMG/Respawn/OneLife/ChatDMG [True/False]");
}
} else {
sender.sendMessage("Konsolenbefehl!");
}
return true;
}
public void createGUI(Player p) {
SettingsGUI.setItem(9, addGUIItem(Material.CRAFTING_TABLE, "§6Erlaube Crafting"));
SettingsGUI.setItem(10, addGUIItem(Material.EMERALD, "§6Erlaube Handeln"));
SettingsGUI.setItem(11, addGUIItem(Material.BONE, "§6Tod bei Fallschaden", "§cSpieler stirbt bei Fallschaden"));
SettingsGUI.setItem(12, addGUIItem(Material.MAGMA_BLOCK, "§6Schaden bei Schleichen","§cSpieler erhält §6" + getConfig().getInt("sneakdmg") + " §cSchaden bei Schleichen"));
SettingsGUI.setItem(13, addGUIItem(Material.DIAMOND_PICKAXE, "§6Drops", "§cRandom Drops"));
SettingsGUI.setItem(14, addGUIItem(Material.GLISTERING_MELON_SLICE, "§6Geteilter Schaden","§cAlle Spieler erhalten den gleichen Schaden"));
SettingsGUI.setItem(15, addGUIItem(Material.TOTEM_OF_UNDYING, "§6Respawn"));
SettingsGUI.setItem(16, addGUIItem(Material.POPPY, "§6Ein Leben für alle", "§cStirbt ein Spieler ist die","§cChallenge fehlgeschlagen"));
SettingsGUI.setItem(17, addGUIItem(Material.BOOK, "§6Schaden in Chat", "§cZeigt Spielerschaden im Chat an"));
SettingsGUI.setItem(36, addGUIItem(Material.CHEST, "§6KeepInventory", "§cStirbst du, beh§lst du dein Inventar"));
SettingsGUI.setItem(44, addGUIItem(Material.GOLDEN_APPLE, "§6Natürliche Regeneration", "§cSchalte natürliche Regeneration um"));
SettingsGUI.setItem(18, addGUIItem(getDye("craftingtable"), getBool("craftingtable")));
SettingsGUI.setItem(19, addGUIItem(getDye("allowtrading"), getBool("allowtrading")));
SettingsGUI.setItem(20, addGUIItem(getDye("deathonfall"), getBool("deathonfall")));
SettingsGUI.setItem(21, addGUIItem(getDye("damageonsneak"), getBool("damageonsneak")));
SettingsGUI.setItem(22, addGUIItem(getDye("rnddrops"), getBool("rnddrops")));
SettingsGUI.setItem(23, addGUIItem(getDye("sharedmg"), getBool("sharedmg")));
SettingsGUI.setItem(24, addGUIItem(getDye("respawn"), getBool("respawn")));
SettingsGUI.setItem(25, addGUIItem(getDye("onelife"), getBool("onelife")));
SettingsGUI.setItem(26, addGUIItem(getDye("dmginchat"), getBool("dmginchat")));
SettingsGUI.setItem(37, addGUIItem(getgrDye("keepInventory"), getgrBool("keepInventory")));
SettingsGUI.setItem(43, addGUIItem(getgrDye("naturalRegeneration"), getgrBool("naturalRegeneration")));
for (int i = 0; i < 45; i++) {
if (SettingsGUI.getItem(i) == null)
SettingsGUI.setItem(i, addGUIItem(Material.ORANGE_STAINED_GLASS_PANE, ""));
}
p.openInventory(SettingsGUI);
}
private Material getDye(String cnfpath) {
ItemStack mat = new ItemStack(Material.GRAY_DYE);
if (getConfig().getBoolean(cnfpath) == true) {
mat.setType(Material.LIME_DYE);
} else {
mat.setType(Material.RED_DYE);
}
return mat.getType();
}
private String getBool(String cnfpath) {
if (getConfig().getBoolean(cnfpath) == true)
return "§aTrue";
return "§cFalse";
}
@SuppressWarnings("deprecation")
private Material getgrDye(String gr) {
ItemStack mat = new ItemStack(Material.GRAY_DYE);
World wld = Bukkit.getWorlds().get(0);
if (wld.getGameRuleValue(gr).equals("true")) {
mat.setType(Material.LIME_DYE);
} else if (wld.getGameRuleValue(gr).equals("false")) {
mat.setType(Material.RED_DYE);
}
return mat.getType();
}
@SuppressWarnings("deprecation")
private String getgrBool(String gr) {
World wld = Bukkit.getWorlds().get(0);
if (wld.getGameRuleValue(gr).equals("true"))
return "§aTrue";
if (wld.getGameRuleValue(gr).equals("false"))
return "§cFalse";
return "§7Unknown";
}
private ItemStack addGUIItem(Material mat, String name, String... lore) {
ItemStack item = new ItemStack(mat, 1);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
ArrayList<String> metalore = new ArrayList<>();
if (lore != null)
for (String lorecomments : lore)
metalore.add(lorecomments);
meta.setLore(metalore);
item.setItemMeta(meta);
return item;
}
public SBManager getSBManager() {
return this.sbManager;
}
}