Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
17 kB
1
Indexable
Never
package org.lifestealsmp.itemskinner;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

public class main extends JavaPlugin implements Listener{
	
	private FileConfiguration data;
    private File dfile;
    
    private FileConfiguration furnitures;
    private File ffile;
    
    private FileConfiguration hats;
    private File hfile;
    
    private FileConfiguration armors;
    private File afile;
    
    private FileConfiguration tools;
    private File tfile;

    public void onEnable() {
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
        saveDefaultConfig();

        setupFiles();
    }

    public void setupFiles() {
        // data.yml setup
        dfile = new File(this.getDataFolder(), "data.yml");
        setupFile(dfile);
        data = YamlConfiguration.loadConfiguration(dfile);
        
        // furnitures.yml setup
        ffile = new File(this.getDataFolder(), "furnitures.yml");
        setupFile(ffile);
        furnitures = YamlConfiguration.loadConfiguration(ffile);

        // hats.yml setup
        hfile = new File(this.getDataFolder(), "hats.yml");
        setupFile(hfile);
        hats = YamlConfiguration.loadConfiguration(hfile);

        // armors.yml setup
        afile = new File(this.getDataFolder(), "armors.yml");
        setupFile(afile);
        armors = YamlConfiguration.loadConfiguration(afile);
        
        // tools.yml setup
        tfile = new File(this.getDataFolder(), "tools.yml");
        setupFile(tfile);
        tools = YamlConfiguration.loadConfiguration(tfile);
    }

    public void setupFile(File file) {
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create " + file.getName() + "!");
            }
        }
    }

    public FileConfiguration getData() {
        return data;
    }

    public FileConfiguration getFurnitures() {
        return furnitures;
    }

    public FileConfiguration getHats() {
        return hats;
    }

    public FileConfiguration getArmors() {
        return armors;
    }

    public FileConfiguration getTools() {
        return tools;
    }

    public void saveData() {
        saveFile(data, dfile);
    }

    public void saveFurnitures() {
        saveFile(furnitures, ffile);
    }

    public void saveHats() {
        saveFile(hats, hfile);
    }

    public void saveArmors() {
        saveFile(armors, afile);
    }

    public void saveTools() {
        saveFile(tools, tfile);
    }

    public void saveFile(FileConfiguration config, File file) {
        try {
            config.save(file);
        } catch (IOException e) {
            Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save " + file.getName() + "!");
        }
    }

    public void reloadData() {
        data = YamlConfiguration.loadConfiguration(dfile);
    }

    public void reloadFurnitures() {
        furnitures = YamlConfiguration.loadConfiguration(ffile);
    }

    public void reloadHats() {
        hats = YamlConfiguration.loadConfiguration(hfile);
    }

    public void reloadArmors() {
        armors = YamlConfiguration.loadConfiguration(afile);
    }

    public void reloadTools() {
        tools = YamlConfiguration.loadConfiguration(tfile);
    }
	
	@Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		if(cmd.getName().equalsIgnoreCase("skin") || cmd.getName().equalsIgnoreCase("skins")) {
			if (sender instanceof Player) {
				Player player = (Player) sender;
				SkinStationInventory(player);
			} else {
	            sender.sendMessage("This command can only be run by a player!");
	        }
		}
		return false;
	}
	
	public void SkinStationInventory(Player player) {
		Inventory inv = Bukkit.createInventory(null, 27, "Skin Station");
		ItemStack blank = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);

		// Fill empty slots with the blank item
	    for (int i = 0; i < inv.getSize(); i++) {
	        if (inv.getItem(i) == null) {
	            inv.setItem(i, blank);
	        }
	    }
	    // Create the Furniture item
	    ItemStack furniture = new ItemStack(Material.PAPER);
	    ItemMeta furnitureMeta = furniture.getItemMeta();
	    furnitureMeta.setDisplayName("Furniture");
	    furnitureMeta.setCustomModelData(10029);
	    furnitureMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    furniture.setItemMeta(furnitureMeta);

	    // Create the Hats item
	    ItemStack hats = new ItemStack(Material.LEATHER_HELMET);
	    ItemMeta hatsMeta = hats.getItemMeta();
	    hatsMeta.setDisplayName("Hats");
	    hatsMeta.setCustomModelData(10005);
	    hatsMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    hats.setItemMeta(hatsMeta);

	    // Create the Armor item
	    ItemStack armor = new ItemStack(Material.LEATHER_CHESTPLATE);
	    ItemMeta armorMeta = armor.getItemMeta();
	    armorMeta.setDisplayName("Armor");
	    armorMeta.setCustomModelData(10121);
	    armorMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    armor.setItemMeta(armorMeta);

	    // Create the Tools item
	    ItemStack tools = new ItemStack(Material.DIAMOND_SWORD);
	    ItemMeta toolsMeta = tools.getItemMeta();
	    toolsMeta.setDisplayName("Tools");
	    toolsMeta.addEnchant(Enchantment.DURABILITY, 1, true);
	    toolsMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
	    toolsMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    tools.setItemMeta(toolsMeta);

	    // Add the items to the specified slots (0-based index)
	    inv.setItem(10, furniture); // Slot 11
	    inv.setItem(12, hats);      // Slot 13
	    inv.setItem(14, armor);     // Slot 15
	    inv.setItem(16, tools);     // Slot 17
	    player.openInventory(inv);
	}
	
	public void FurnitureInventory(Player player) {
	    // Set up inventory with 27 slots
	    Inventory inv = Bukkit.createInventory(null, 27, "Furniture Skins");
	    ItemStack blank = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);
	    
	    // Back and forward button setup
	    ItemStack backButton = new ItemStack(Material.RED_WOOL);
	    ItemMeta backMeta = backButton.getItemMeta();
	    backMeta.setDisplayName("Back Page");
	    backButton.setItemMeta(backMeta);
	    
	    ItemStack forwardButton = new ItemStack(Material.GREEN_WOOL);
	    ItemMeta forwardMeta = forwardButton.getItemMeta();
	    forwardMeta.setDisplayName("Next Page");
	    forwardButton.setItemMeta(forwardMeta);
	    
	    inv.setItem(21, backButton);
	    inv.setItem(23, forwardButton);

	    // Player's UUID
	    String uuid = player.getUniqueId().toString();

	    // Load data.yml file
	    FileConfiguration dataConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "data.yml"));

	    // Check if the player's UUID exists in the data.yml
	    if (dataConfig.contains(uuid)) {
	        List<String> playerFurnitures = dataConfig.getStringList(uuid + ".Furnitures");

	        for (String furniture : playerFurnitures) {
	            if (getFurnitures().contains(furniture)) {
	                String itemType = getFurnitures().getString(furniture + ".Item_Type");
	                int customModelData = getFurnitures().getInt(furniture + ".Custom Model Data");
	                String displayName = getFurnitures().getString(furniture + ".Display_Name");

	                ItemStack itemStack = new ItemStack(Material.valueOf(itemType));
	                ItemMeta meta = itemStack.getItemMeta();

	                meta.setCustomModelData(customModelData);
	                meta.setDisplayName(displayName);
	                itemStack.setItemMeta(meta);

	                inv.addItem(itemStack);
	            }
	        }
	    }
	    
	    // Fill empty slots with the blank item
	    for (int i = 0; i < inv.getSize(); i++) {
	        if (inv.getItem(i) == null) {
	            inv.setItem(i, blank);
	        }
	    }

	    player.openInventory(inv);
	}

	
	public void HatsInventory(Player player) {
		Inventory inv = Bukkit.createInventory(null, 27, "Hat Skins");
		ItemStack blank = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);

		// Fill empty slots with the blank item
	    for (int i = 0; i < inv.getSize(); i++) {
	        if (inv.getItem(i) == null) {
	            inv.setItem(i, blank);
	        }
	    }
	    ItemStack backButton = new ItemStack(Material.RED_WOOL);
	    ItemMeta backMeta = backButton.getItemMeta();
	    backMeta.setDisplayName("Back Page");
	    backButton.setItemMeta(backMeta);
	    

	    ItemStack forwardButton = new ItemStack(Material.GREEN_WOOL);
	    ItemMeta forwardMeta = forwardButton.getItemMeta();
	    forwardMeta.setDisplayName("Next Page");
	    forwardButton.setItemMeta(forwardMeta);
	    
	    inv.setItem(21, backButton);
	    inv.setItem(23, forwardButton);
	    player.openInventory(inv);
	}
	
	public void ArmorInventory(Player player) {
		Inventory inv = Bukkit.createInventory(null, 27, "Armor Skins");
		ItemStack blank = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);

		// Fill empty slots with the blank item
	    for (int i = 0; i < inv.getSize(); i++) {
	        if (inv.getItem(i) == null) {
	            inv.setItem(i, blank);
	        }
	    }
	    ItemStack backButton = new ItemStack(Material.RED_WOOL);
	    ItemMeta backMeta = backButton.getItemMeta();
	    backMeta.setDisplayName("Back");
	    backButton.setItemMeta(backMeta);
	    
	    inv.setItem(21, backButton);
	    player.openInventory(inv);
	}
	
	public void ToolInventory(Player player) {
		Inventory inv = Bukkit.createInventory(null, 27, "Tool Skins");
		ItemStack blank = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);

		// Fill empty slots with the blank item
	    for (int i = 0; i < inv.getSize(); i++) {
	        if (inv.getItem(i) == null) {
	            inv.setItem(i, blank);
	        }
	    }
	    ItemStack backButton = new ItemStack(Material.RED_WOOL);
	    ItemMeta backMeta = backButton.getItemMeta();
	    backMeta.setDisplayName("Back");
	    backButton.setItemMeta(backMeta);
	    
	    inv.setItem(21, backButton);
	    player.openInventory(inv);
	}
	
	public void ArmorCategoriesInventory(Player player) {
	    Inventory inv = Bukkit.createInventory(null, 27, "Armor Categories");
	    ItemStack blank = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);

	    // Fill empty slots with the blank item
	    for (int i = 0; i < inv.getSize(); i++) {
	        if (inv.getItem(i) == null) {
	            inv.setItem(i, blank);
	        }
	    }

	    // Creating the Diamond Helmet item
	    ItemStack helmet = new ItemStack(Material.DIAMOND_HELMET);
	    ItemMeta helmetMeta = helmet.getItemMeta();
	    helmetMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    helmet.setItemMeta(helmetMeta);

	    // Creating the Diamond Chestplate item
	    ItemStack chestplate = new ItemStack(Material.DIAMOND_CHESTPLATE);
	    ItemMeta chestplateMeta = chestplate.getItemMeta();
	    chestplateMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    chestplate.setItemMeta(chestplateMeta);

	    // Creating the Diamond Leggings item
	    ItemStack leggings = new ItemStack(Material.DIAMOND_LEGGINGS);
	    ItemMeta leggingsMeta = leggings.getItemMeta();
	    leggingsMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    leggings.setItemMeta(leggingsMeta);

	    // Creating the Diamond Boots item
	    ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS);
	    ItemMeta bootsMeta = boots.getItemMeta();
	    bootsMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    boots.setItemMeta(bootsMeta);

	    // Setting the items to the respective slots in the inventory
	    inv.setItem(10, helmet);      // Slot 10 (0-based index)
	    inv.setItem(12, chestplate); // Slot 12 (0-based index)
	    inv.setItem(14, leggings);   // Slot 14 (0-based index)
	    inv.setItem(16, boots);      // Slot 16 (0-based index)

	    // Adding the back button
	    ItemStack backButton = new ItemStack(Material.RED_WOOL);
	    ItemMeta backMeta = backButton.getItemMeta();
	    backMeta.setDisplayName("Back");
	    backButton.setItemMeta(backMeta);

	    inv.setItem(22, backButton); // Slot 21 (0-based index)

	    player.openInventory(inv);
	}
	
	public void ToolCategoriesInventory(Player player) {
	    Inventory inv = Bukkit.createInventory(null, 27, "Tool Categories");
	    ItemStack blank = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);

	    // Fill empty slots with the blank item
	    for (int i = 0; i < inv.getSize(); i++) {
	        if (inv.getItem(i) == null) {
	            inv.setItem(i, blank);
	        }
	    }

	    // Creating the Diamond Pickaxe item
	    ItemStack pickaxe = new ItemStack(Material.DIAMOND_PICKAXE);
	    ItemMeta pickaxeMeta = pickaxe.getItemMeta();
	    pickaxeMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    pickaxe.setItemMeta(pickaxeMeta);

	    // Creating the Diamond Sword item
	    ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
	    ItemMeta swordMeta = sword.getItemMeta();
	    swordMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    sword.setItemMeta(swordMeta);

	    // Creating the Diamond Axe item
	    ItemStack axe = new ItemStack(Material.DIAMOND_AXE);
	    ItemMeta axeMeta = axe.getItemMeta();
	    axeMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    axe.setItemMeta(axeMeta);

	    // Creating the Diamond Shovel item
	    ItemStack shovel = new ItemStack(Material.DIAMOND_SHOVEL);
	    ItemMeta shovelMeta = shovel.getItemMeta();
	    shovelMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
	    shovel.setItemMeta(shovelMeta);

	    // Setting the items to the respective slots in the inventory
	    inv.setItem(10, pickaxe);  // Slot 10 (0-based index)
	    inv.setItem(12, sword);   // Slot 12 (0-based index)
	    inv.setItem(14, axe);     // Slot 14 (0-based index)
	    inv.setItem(16, shovel);  // Slot 16 (0-based index)

	    // Adding the back button
	    ItemStack backButton = new ItemStack(Material.RED_WOOL);
	    ItemMeta backMeta = backButton.getItemMeta();
	    backMeta.setDisplayName("Back");
	    backButton.setItemMeta(backMeta);

	    inv.setItem(22, backButton); // Slot 21 (0-based index)

	    player.openInventory(inv);
	}

	
	@EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
		Player player = (Player) event.getWhoClicked();
		if(event.getView().getTitle().startsWith("Skin Station")) {
			event.setCancelled(true);
			if(event.getSlot() == 10) {
				FurnitureInventory(player);
			}
			if(event.getSlot() == 12) {
				HatsInventory(player);
			}
			if(event.getSlot() == 14) {
				ArmorCategoriesInventory(player);
				
			}
			if(event.getSlot() == 16) {
				ToolCategoriesInventory(player);
			}
		}
		if(event.getView().getTitle().startsWith("Furniture Skins")) {
			event.setCancelled(true);
			if(event.getSlot() == 21) {
				SkinStationInventory(player);
			}
		}
		if(event.getView().getTitle().startsWith("Hat Skins")) {
			event.setCancelled(true);
			if(event.getSlot() == 21) {
				SkinStationInventory(player);
			}
		}
		if(event.getView().getTitle().startsWith("Armor Skins")) {
			event.setCancelled(true);
			if(event.getSlot() == 21) {
				SkinStationInventory(player);
			}
		}
		if(event.getView().getTitle().startsWith("Tool Skins")) {
			event.setCancelled(true);
			if(event.getSlot() == 21) {
				SkinStationInventory(player);
			}
		}
		if(event.getView().getTitle().startsWith("Armor Categories")) {
			event.setCancelled(true);
			if(event.getSlot() == 22) {
				SkinStationInventory(player);
			}
		}
		if(event.getView().getTitle().startsWith("Tool Categories")) {
			event.setCancelled(true);
			if(event.getSlot() == 22) {
				SkinStationInventory(player);
			}
		}
	}
}