package xp10d3.corelia.main; import java.io.File; import java.io.IOException; import java.util.ArrayList; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Item; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.java.JavaPlugin; import net.md_5.bungee.api.ChatColor; // Note: Right now all commented out code is MySQL stuff. /* Helpful stuff: https://www.spigotmc.org/wiki/connecting-to-databases-mysql/#getting-data https://dev.mysql.com/downloads/connector/j/ https://www.spigotmc.org/threads/saving-player-data-to-a-config-file.232790/ https://bukkit.org/threads/storing-player-data.168636/ https://www.spigotmc.org/threads/config-adddefault.357226/ https://www.spigotmc.org/wiki/config-files/ https://www.spigotmc.org/threads/creating-config-file.246894/ https://www.spigotmc.org/wiki/creating-a-config-file/ https://www.spigotmc.org/threads/create-a-database.357617/ https://www.spigotmc.org/threads/storing-data.226211/#post-2307586 */ public class Core extends JavaPlugin implements Listener { public static Core instance; FileConfiguration config = getConfig(); private File customConfigFile; private FileConfiguration customConfig; @Override public void onEnable() { createCustomConfig(); getServer().getPluginManager().registerEvents(this, this); getLogger().info(" has started!"); } @Override public void onDisable() { getLogger().info(" is disabled."); } public static Core getInstance() { return instance; } public FileConfiguration getCustomConfig() { return this.customConfig; } private void createCustomConfig() { customConfigFile = new File(getDataFolder(), "kills.yml"); if (!customConfigFile.exists()) { customConfigFile.getParentFile().mkdirs(); saveResource("kills.yml", false); getConfig().options().copyDefaults(true); saveDefaultConfig(); } customConfig = new YamlConfiguration(); try { customConfig.load(customConfigFile); } catch (IOException | InvalidConfigurationException e) { e.printStackTrace(); } } @EventHandler public void onDeath(PlayerDeathEvent e) { Player p = e.getEntity(); Player k = p.getKiller(); int amount = customConfig.getInt("Data." + k.getUniqueId().toString() + ".Kills"); customConfig.set("Data." + k.getUniqueId().toString() + ".Kills", amount + 0.5); saveConfig(); double gold = config.getDouble("path.to.gold" + k.getUniqueId().toString()); this.getConfig().set("path.to.gold" + k.getUniqueId().toString(), gold + 0.5); saveConfig(); final ItemStack nugget = new ItemStack(Material.GOLD_NUGGET); final ItemMeta nuggetMeta = nugget.getItemMeta(); ArrayList nuggetLore = new ArrayList<>(); nuggetLore.add("Right Click to display your balance."); nuggetLore.add("You get more money for every kill you get."); nuggetMeta.setLore(nuggetLore); nuggetMeta.setDisplayName(ChatColor.GOLD + "Balance"); nugget.setItemMeta(nuggetMeta); p.getInventory().addItem(new ItemStack(Material.GOLD_NUGGET)); } public Inventory goldGUI(Player player) { double gold = config.getDouble("path.to.gold" + player.getUniqueId().toString()); final Inventory inventory = Bukkit.createInventory(null, 9, ChatColor.GOLD + "Balance"); final ItemStack balance = new ItemStack(Material.GOLD_BLOCK); final ItemMeta balanceMeta = balance.getItemMeta(); final ItemStack close = new ItemStack(Material.RED_STAINED_GLASS_PANE); final ItemMeta closeMeta = close.getItemMeta(); ArrayList balanceLore = new ArrayList<>(); balanceLore.add(ChatColor.LIGHT_PURPLE + "Your balance is: " + gold + " gold."); balanceMeta.setLore(balanceLore); balanceMeta.setDisplayName(ChatColor.WHITE + player.getName() + "'s Balance"); balance.setItemMeta(balanceMeta); closeMeta.setDisplayName(ChatColor.DARK_RED + "Close"); close.setItemMeta(closeMeta); inventory.setItem(4, balance); inventory.setItem(8, close); return inventory; } @EventHandler public void onRightClickGoldNugget(PlayerInteractEvent event) { Player player = event.getPlayer(); if (event.getItem() != null && event.getItem().getType() == Material.GOLD_NUGGET && (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR))) { player.openInventory(goldGUI(player)); } } @EventHandler public void click(InventoryClickEvent event) { Player player = (Player) event.getWhoClicked(); if ((event.getCurrentItem().getType() == Material.RED_STAINED_GLASS_PANE) && event.getRawSlot() < event.getInventory().getSize()) { player.closeInventory(); } } @EventHandler public void onJoinBefore(PlayerJoinEvent player) { Player p = player.getPlayer(); boolean hasPlayed = p.hasPlayedBefore(); if (!hasPlayed) { final ItemStack nugget = new ItemStack(Material.GOLD_NUGGET); final ItemMeta nuggetMeta = nugget.getItemMeta(); ArrayList nuggetLore = new ArrayList<>(); nuggetLore.add("Right Click to display your balance."); nuggetLore.add("You get more money for every kill you get."); nuggetMeta.setLore(nuggetLore); nuggetMeta.setDisplayName(ChatColor.GOLD + "Balance"); nugget.setItemMeta(nuggetMeta); p.getInventory().addItem(new ItemStack(Material.GOLD_NUGGET)); } } @EventHandler public void onBlockBreak(BlockBreakEvent event) { Player player = (Player) event.getPlayer(); double gold = config.getDouble("path.to.gold" + player.getUniqueId().toString()); this.getConfig().set("path.to.gold" + player.getUniqueId().toString(), gold + 0.05); saveConfig(); } @EventHandler public void onItemPickup(InventoryClickEvent event) { Player player = (Player) event.getWhoClicked(); if (event.getCurrentItem().getType() == Material.GOLD_BLOCK) { event.setCancelled(true); } player.sendMessage(ChatColor.RED + "Hey! You can't steal that Gold Block. Stop trying!"); } }