v0.4.0 - Added config.yml. Removed BannedItemsConfig and replaced it with a new ConfigManager. Toggle option in config.yml to toggle AutoPicked up mined blocks for prisoners. Will use config.yml for future toggles as well.
This commit is contained in:
parent
0dd883536a
commit
0ef2a83b88
|
@ -1,59 +0,0 @@
|
||||||
package me.nvus.nvus_prison_setup.Configs;
|
|
||||||
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class BannedItemsConfig {
|
|
||||||
private final JavaPlugin plugin;
|
|
||||||
private FileConfiguration config;
|
|
||||||
private File configFile;
|
|
||||||
|
|
||||||
public BannedItemsConfig(JavaPlugin plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reloadConfig() {
|
|
||||||
if (configFile == null) {
|
|
||||||
configFile = new File(plugin.getDataFolder(), "banned_items.yml");
|
|
||||||
}
|
|
||||||
config = YamlConfiguration.loadConfiguration(configFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileConfiguration getConfig() {
|
|
||||||
if (config == null) {
|
|
||||||
reloadConfig();
|
|
||||||
}
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveConfig() {
|
|
||||||
if (config == null || configFile == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
getConfig().save(configFile);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveDefaultConfig() {
|
|
||||||
if (configFile == null) {
|
|
||||||
configFile = new File(plugin.getDataFolder(), "banned_items.yml");
|
|
||||||
}
|
|
||||||
if (!configFile.exists()) {
|
|
||||||
plugin.saveResource("banned_items.yml", false);
|
|
||||||
plugin.getLogger().info("banned_items.yml has been created.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public List<String> getBannedItems() {
|
|
||||||
return getConfig().getStringList("BannedItems");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
package me.nvus.nvus_prison_setup.Configs;
|
||||||
|
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ConfigManager {
|
||||||
|
private final JavaPlugin plugin;
|
||||||
|
private final Map<String, FileConfiguration> configs;
|
||||||
|
private final Map<String, File> configFiles;
|
||||||
|
|
||||||
|
public ConfigManager(JavaPlugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.configs = new HashMap<>();
|
||||||
|
this.configFiles = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reloadConfig(String configName) {
|
||||||
|
File configFile = getConfigFile(configName);
|
||||||
|
FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
|
||||||
|
configs.put(configName, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileConfiguration getConfig(String configName) {
|
||||||
|
if (!configs.containsKey(configName)) {
|
||||||
|
reloadConfig(configName);
|
||||||
|
}
|
||||||
|
return configs.get(configName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveConfig(String configName) {
|
||||||
|
FileConfiguration config = getConfig(configName);
|
||||||
|
File configFile = getConfigFile(configName);
|
||||||
|
try {
|
||||||
|
config.save(configFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveDefaultConfig(String configName) {
|
||||||
|
File configFile = getConfigFile(configName);
|
||||||
|
if (!configFile.exists()) {
|
||||||
|
plugin.saveResource(configName, false);
|
||||||
|
plugin.getLogger().info(configName + " has been created.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private File getConfigFile(String configName) {
|
||||||
|
if (!configFiles.containsKey(configName)) {
|
||||||
|
File configFile = new File(plugin.getDataFolder(), configName);
|
||||||
|
configFiles.put(configName, configFile);
|
||||||
|
}
|
||||||
|
return configFiles.get(configName);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package me.nvus.nvus_prison_setup.Listeners;
|
||||||
|
|
||||||
|
import me.nvus.nvus_prison_setup.PrisonSetup;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.block.Block;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.block.BlockBreakEvent;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
public class BlockListener implements Listener {
|
||||||
|
|
||||||
|
private final PrisonSetup plugin;
|
||||||
|
|
||||||
|
public BlockListener(PrisonSetup plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onBlockBreak(BlockBreakEvent event) {
|
||||||
|
Player player = event.getPlayer();
|
||||||
|
Block block = event.getBlock();
|
||||||
|
|
||||||
|
// Check if the player is a prisoner and auto-pickup is enabled
|
||||||
|
if (player.hasPermission("nvus.prisoner") && plugin.getConfig().getBoolean("AutoPickup")) {
|
||||||
|
ItemStack itemStack = new ItemStack(block.getType());
|
||||||
|
if (player.getInventory().addItem(itemStack).isEmpty()) {
|
||||||
|
// Inventory has enough space, remove the dropped item
|
||||||
|
event.setDropItems(false);
|
||||||
|
} else {
|
||||||
|
// Inventory is full, drop the item on the ground
|
||||||
|
player.getWorld().dropItemNaturally(block.getLocation(), itemStack);
|
||||||
|
player.sendMessage("Your inventory is currently full. The resource has been dropped on the ground!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,15 +1,14 @@
|
||||||
package me.nvus.nvus_prison_setup.Listeners;
|
package me.nvus.nvus_prison_setup.Listeners;
|
||||||
|
|
||||||
|
import me.nvus.nvus_prison_setup.Configs.ConfigManager;
|
||||||
import me.nvus.nvus_prison_setup.PrisonSetup;
|
import me.nvus.nvus_prison_setup.PrisonSetup;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
import org.bukkit.event.inventory.InventoryType;
|
|
||||||
import org.bukkit.event.player.PlayerItemHeldEvent;
|
import org.bukkit.event.player.PlayerItemHeldEvent;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
@ -17,10 +16,10 @@ import java.util.List;
|
||||||
|
|
||||||
public class PlayerItems implements Listener {
|
public class PlayerItems implements Listener {
|
||||||
|
|
||||||
private final PrisonSetup plugin;
|
private final ConfigManager configManager;
|
||||||
|
|
||||||
public PlayerItems(PrisonSetup plugin) {
|
public PlayerItems(ConfigManager configManager) {
|
||||||
this.plugin = plugin;
|
this.configManager = configManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
|
@ -54,41 +53,8 @@ public class PlayerItems implements Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isBannedItem(Material itemType) {
|
private boolean isBannedItem(Material itemType) {
|
||||||
FileConfiguration config = plugin.getBannedItemsConfig().getConfig();
|
List<String> bannedItems = configManager.getConfig("banned_items.yml").getStringList("BannedItems");
|
||||||
List<String> bannedItems = config.getStringList("BannedItems");
|
|
||||||
return bannedItems.contains(itemType.toString());
|
return bannedItems.contains(itemType.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
|
||||||
public void onInventoryClick(InventoryClickEvent event) {
|
|
||||||
if (event.getClickedInventory() != null && event.getClickedInventory().getType() == InventoryType.PLAYER) {
|
|
||||||
if (event.getSlotType() == InventoryType.SlotType.QUICKBAR) {
|
|
||||||
ItemStack item = event.getCurrentItem();
|
|
||||||
Player player = (Player) event.getWhoClicked();
|
|
||||||
|
|
||||||
// Check if the clicked item is a banned item
|
|
||||||
if (item != null && isBannedItem(item.getType())) {
|
|
||||||
// Check if the player is a prisoner
|
|
||||||
if (player.hasPermission("nvus.prisoner")) {
|
|
||||||
// Cancel the event to prevent the banned item from being moved to the quickbar
|
|
||||||
event.setCancelled(true);
|
|
||||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&',"&c&lSorry inmate! &cYou're a prisoner and cannot use this tool!"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private boolean isBannedItem(Material itemType) {
|
|
||||||
FileConfiguration config = plugin.getBannedItemsConfig().getConfig();
|
|
||||||
List<String> bannedItems = config.getStringList("BannedItems");
|
|
||||||
return bannedItems.contains(itemType.toString());
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,41 +1,46 @@
|
||||||
package me.nvus.nvus_prison_setup;
|
package me.nvus.nvus_prison_setup;
|
||||||
|
|
||||||
import me.nvus.nvus_prison_setup.Configs.BannedItemsConfig;
|
import me.nvus.nvus_prison_setup.Configs.ConfigManager;
|
||||||
import me.nvus.nvus_prison_setup.Listeners.PlayerArmor;
|
import me.nvus.nvus_prison_setup.Listeners.PlayerArmor;
|
||||||
import me.nvus.nvus_prison_setup.Listeners.PlayerItems;
|
import me.nvus.nvus_prison_setup.Listeners.PlayerItems;
|
||||||
import me.nvus.nvus_prison_setup.Listeners.PlayerSpawn;
|
import me.nvus.nvus_prison_setup.Listeners.PlayerSpawn;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
public final class PrisonSetup extends JavaPlugin {
|
public final class PrisonSetup extends JavaPlugin {
|
||||||
|
|
||||||
private BannedItemsConfig bannedItemsConfig;
|
private ConfigManager configManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
|
// Initialize the ConfigManager
|
||||||
|
configManager = new ConfigManager(this);
|
||||||
|
|
||||||
// Init the BannedItemsConfig
|
// Save the default config if it doesn't exist
|
||||||
bannedItemsConfig = new BannedItemsConfig(this);
|
configManager.saveDefaultConfig("banned_items.yml");
|
||||||
|
configManager.saveDefaultConfig("config.yml"); // Add this line
|
||||||
// Save banned_items.yml config if it doesn't exist
|
|
||||||
bannedItemsConfig.saveDefaultConfig();
|
|
||||||
|
|
||||||
|
// Register event listeners
|
||||||
getServer().getPluginManager().registerEvents(new PlayerSpawn(), this);
|
getServer().getPluginManager().registerEvents(new PlayerSpawn(), this);
|
||||||
getServer().getPluginManager().registerEvents(new PlayerArmor(), this);
|
getServer().getPluginManager().registerEvents(new PlayerArmor(), this);
|
||||||
getServer().getPluginManager().registerEvents(new PlayerItems(this), this);
|
getServer().getPluginManager().registerEvents(new PlayerItems(configManager), this);
|
||||||
getLogger().info(ChatColor.translateAlternateColorCodes('&',"&a&lNVus Prison Setup has been successfully enabled!"));
|
|
||||||
|
|
||||||
|
// Log a success message
|
||||||
|
getLogger().info(ChatColor.translateAlternateColorCodes('&',"&a&lNVus Prison Setup has been successfully enabled!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
// Saving config file, just in case it was changed prior to disabling plugin/shutdown?
|
// Save the config when disabling the plugin
|
||||||
bannedItemsConfig.saveConfig();
|
configManager.saveConfig("config.yml");
|
||||||
|
configManager.saveConfig("banned_items.yml");
|
||||||
|
|
||||||
|
// Log a success message
|
||||||
getLogger().info(ChatColor.translateAlternateColorCodes('&',"&c&lNVus Prison Setup has been successfully disabled!"));
|
getLogger().info(ChatColor.translateAlternateColorCodes('&',"&c&lNVus Prison Setup has been successfully disabled!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public BannedItemsConfig getBannedItemsConfig() {
|
public ConfigManager getConfigManager() {
|
||||||
return bannedItemsConfig;
|
return configManager;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
|
|
||||||
# Place Item(s) here that prisoners should never be able to equip in their hands or move into their quickbar
|
# Place Item(s) here that prisoners should never be able to equip in their hands or move into their quickbar
|
||||||
# Remember to give all prisoner ranks/groups that permission nvus.prisoner and negate that permission for any
|
# Remember to give all prisoner ranks/groups the permission ' nvus.prisoner ' and negate that permission for any
|
||||||
# players that are not or no longer considered a prisoner.
|
# players that are not or no longer considered a prisoner.
|
||||||
BannedItems:
|
BannedItems:
|
||||||
- DIAMOND_PICKAXE
|
- DIAMOND_PICKAXE
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
#======================================================================================#
|
||||||
|
# NVus PRISON SETUP #
|
||||||
|
# by never2nv #
|
||||||
|
# www.FNGnation.net #
|
||||||
|
# Discord: FNGnation.net/discord #
|
||||||
|
#======================================================================================#
|
||||||
|
# Remember to give all prisoner ranks/groups the permission ' nvus.prisoner ' and negate that permission for any
|
||||||
|
# players that are not or no longer considered a prisoner.
|
||||||
|
|
||||||
|
# Should prisoners auto pickup blocks they have mined?
|
||||||
|
AutoPickup: true
|
Loading…
Reference in New Issue