From f6d676b1bc2e861e32978235c8659966b0bc2141 Mon Sep 17 00:00:00 2001 From: Mille12313 Date: Sun, 8 Sep 2024 14:57:35 +0200 Subject: [PATCH] Initial commit of ShearMaster plugin --- .gitignore | 113 ++++++++++++++++ pom.xml | 72 ++++++++++ .../devmules/shearMaster/ShearMaster.java | 35 +++++ .../shearMaster/ShearMasterCommand.java | 111 ++++++++++++++++ .../shearMaster/ShearMasterTabCompleter.java | 37 ++++++ .../shearMaster/config/ConfigManager.java | 109 +++++++++++++++ .../shearMaster/listeners/ShearListener.java | 124 ++++++++++++++++++ src/main/resources/config.yml | 64 +++++++++ src/main/resources/plugin.yml | 23 ++++ 9 files changed, 688 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/bloody/devmules/shearMaster/ShearMaster.java create mode 100644 src/main/java/bloody/devmules/shearMaster/ShearMasterCommand.java create mode 100644 src/main/java/bloody/devmules/shearMaster/ShearMasterTabCompleter.java create mode 100644 src/main/java/bloody/devmules/shearMaster/config/ConfigManager.java create mode 100644 src/main/java/bloody/devmules/shearMaster/listeners/ShearListener.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4788b4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,113 @@ +# User-specific stuff +.idea/ + +*.iml +*.ipr +*.iws + +# IntelliJ +out/ + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +target/ + +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next + +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +# Common working directory +run/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8e274b9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + + bloody.devmules + ShearMaster + 0.01-SNAPSHOT + jar + + ShearMaster + + + 21 + UTF-8 + + + + clean package + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.5.3 + + + package + + shade + + + + + + + + src/main/resources + true + + + + + + + spigotmc-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + + + + org.spigotmc + spigot-api + 1.21.1-R0.1-SNAPSHOT + provided + + + diff --git a/src/main/java/bloody/devmules/shearMaster/ShearMaster.java b/src/main/java/bloody/devmules/shearMaster/ShearMaster.java new file mode 100644 index 0000000..4979acf --- /dev/null +++ b/src/main/java/bloody/devmules/shearMaster/ShearMaster.java @@ -0,0 +1,35 @@ +package bloody.devmules.shearMaster; + +import bloody.devmules.shearMaster.config.ConfigManager; +import bloody.devmules.shearMaster.listeners.ShearListener; +import org.bukkit.plugin.java.JavaPlugin; + +public class ShearMaster extends JavaPlugin { + + private ConfigManager configManager; + + @Override + public void onEnable() { + // Config laden en aanmaken als deze niet bestaat + saveDefaultConfig(); + configManager = new ConfigManager(this); + + // Event listener registreren + getServer().getPluginManager().registerEvents(new ShearListener(this), this); + + // Command executor en tab-completer registreren + getCommand("shearmaster").setExecutor(new ShearMasterCommand(this)); + getCommand("shearmaster").setTabCompleter(new ShearMasterTabCompleter(this)); + + getLogger().info("ShearMaster plugin enabled!"); + } + + @Override + public void onDisable() { + getLogger().info("ShearMaster plugin disabled."); + } + + public ConfigManager getConfigManager() { + return configManager; + } +} diff --git a/src/main/java/bloody/devmules/shearMaster/ShearMasterCommand.java b/src/main/java/bloody/devmules/shearMaster/ShearMasterCommand.java new file mode 100644 index 0000000..ea04728 --- /dev/null +++ b/src/main/java/bloody/devmules/shearMaster/ShearMasterCommand.java @@ -0,0 +1,111 @@ +package bloody.devmules.shearMaster; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class ShearMasterCommand implements CommandExecutor { + + private ShearMaster plugin; + + public ShearMasterCommand(ShearMaster plugin) { + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage("This command can only be used by players."); + return true; + } + + Player player = (Player) sender; + + if (command.getName().equalsIgnoreCase("shearmaster")) { + if (args.length == 0) { + player.sendMessage("Use /shearmaster reload, /shearmaster toggle , or /shearmaster status."); + return true; + } + + if (args[0].equalsIgnoreCase("reload")) { + if (!player.hasPermission("shearmaster.admin")) { + player.sendMessage("You do not have permission to use this command."); + return true; + } + // Config herladen en opnieuw initialiseren + plugin.reloadConfig(); + plugin.getConfigManager().reloadConfig(); + player.sendMessage("ShearMaster configuration reloaded!"); + return true; + } + + if (args[0].equalsIgnoreCase("toggle")) { + if (!player.hasPermission("shearmaster.toggle")) { + player.sendMessage("You do not have permission to use this command."); + return true; + } + if (args.length < 3) { + player.sendMessage("Usage: /shearmaster toggle "); + return true; + } + String animal = args[1].toLowerCase(); + String type = args[2].toLowerCase(); + + // Check if the animal is in the config + if (!plugin.getConfigManager().isAnimalConfigured(animal)) { + player.sendMessage(ChatColor.RED + "Error: " + animal + " is not configured in the plugin."); + return true; + } + + // Toggle shearable or sliceable + if (type.equals("shear")) { + if (!plugin.getConfigManager().canBeSheared(animal)) { + player.sendMessage(ChatColor.RED + animal + " cannot be sheared."); + } else if (!plugin.getConfigManager().hasShearableOption(animal)) { + player.sendMessage(ChatColor.RED + animal + " cannot be toggled as shearable."); + } else { + plugin.getConfigManager().toggleShearable(animal); + player.sendMessage("Toggled shearable status for " + animal + "."); + } + } else if (type.equals("slice")) { + if (!plugin.getConfigManager().hasSliceableOption(animal)) { + player.sendMessage(ChatColor.RED + animal + " cannot be toggled as sliceable."); + } else { + plugin.getConfigManager().toggleSliceable(animal); + player.sendMessage("Toggled sliceable status for " + animal + "."); + } + } else { + player.sendMessage(ChatColor.RED + "Error: Invalid type. Use shear or slice."); + } + return true; + } + + if (args[0].equalsIgnoreCase("status")) { + if (!player.hasPermission("shearmaster.admin")) { + player.sendMessage("You do not have permission to use this command."); + return true; + } + + // Stijl en kleur toevoegen aan de status-output + player.sendMessage(ChatColor.BLUE + "" + ChatColor.BOLD + "ShearMaster Status:"); + + for (String animal : plugin.getConfigManager().getAnimals()) { + boolean shearable = plugin.getConfigManager().isShearable(animal); + boolean sliceable = plugin.getConfigManager().isSliceable(animal); + ChatColor shearableColor = shearable ? ChatColor.GREEN : ChatColor.RED; + ChatColor sliceableColor = sliceable ? ChatColor.GREEN : ChatColor.RED; + + // Bericht voor elk dier + player.sendMessage(ChatColor.GOLD + animal + ": " + + ChatColor.WHITE + " Shearable: " + shearableColor + shearable + + ChatColor.WHITE + ", Sliceable: " + sliceableColor + sliceable); + } + return true; + } + } + + return false; + } +} diff --git a/src/main/java/bloody/devmules/shearMaster/ShearMasterTabCompleter.java b/src/main/java/bloody/devmules/shearMaster/ShearMasterTabCompleter.java new file mode 100644 index 0000000..4661b5f --- /dev/null +++ b/src/main/java/bloody/devmules/shearMaster/ShearMasterTabCompleter.java @@ -0,0 +1,37 @@ +package bloody.devmules.shearMaster; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; + +import java.util.ArrayList; +import java.util.List; + +public class ShearMasterTabCompleter implements TabCompleter { + + private ShearMaster plugin; + + public ShearMasterTabCompleter(ShearMaster plugin) { + this.plugin = plugin; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + List completions = new ArrayList<>(); + + if (command.getName().equalsIgnoreCase("shearmaster")) { + if (args.length == 1) { + completions.add("reload"); + completions.add("toggle"); + completions.add("status"); + } else if (args.length == 2 && args[0].equalsIgnoreCase("toggle")) { + completions.addAll(plugin.getConfigManager().getAnimals()); // Suggestie voor dieren + } else if (args.length == 3 && args[0].equalsIgnoreCase("toggle")) { + completions.add("shear"); + completions.add("slice"); + } + } + + return completions; + } +} diff --git a/src/main/java/bloody/devmules/shearMaster/config/ConfigManager.java b/src/main/java/bloody/devmules/shearMaster/config/ConfigManager.java new file mode 100644 index 0000000..f987d20 --- /dev/null +++ b/src/main/java/bloody/devmules/shearMaster/config/ConfigManager.java @@ -0,0 +1,109 @@ +package bloody.devmules.shearMaster.config; + +import bloody.devmules.shearMaster.ShearMaster; +import org.bukkit.configuration.file.FileConfiguration; + +import java.util.Set; +import java.util.HashSet; + +public class ConfigManager { + + private ShearMaster plugin; + private FileConfiguration config; + + // Set of animals that cannot be sheared + private static final Set UNSHEARABLE_ANIMALS = new HashSet() {{ + add("pig"); + add("wolf"); + add("cat"); + // Voeg hier andere dieren toe die niet geschoren mogen worden + }}; + + public ConfigManager(ShearMaster plugin) { + this.plugin = plugin; + this.config = plugin.getConfig(); + } + + public void reloadConfig() { + plugin.reloadConfig(); + this.config = plugin.getConfig(); + } + + // Methode om te controleren of een dier shearable is + public boolean isShearable(String animal) { + return config.getBoolean("shearable-animals." + animal + ".shearable", false); + } + + // Methode om te controleren of een dier sliceable is + public boolean isSliceable(String animal) { + return config.getBoolean("shearable-animals." + animal + ".sliceable", false); + } + + // Controle of een dier shearable kan zijn (dus dieren zoals pig blokkeren) + public boolean canBeSheared(String animal) { + return !UNSHEARABLE_ANIMALS.contains(animal); // Controleer of het dier in de set van niet-shearable dieren zit + } + + // Methode om de loot op te halen voor scheren + public String getLoot(String animal) { + return config.getString("shearable-animals." + animal + ".loot.material", "LEATHER"); + } + + // Methode om de hoeveelheid loot op te halen voor scheren + public int getLootAmount(String animal) { + return config.getInt("shearable-animals." + animal + ".loot.amount", 1); + } + + // Methode om de loot op te halen voor slicen + public String getSliceLoot(String animal) { + return config.getString("shearable-animals." + animal + ".slice.loot.material", "PORKCHOP"); + } + + // Methode om de hoeveelheid loot op te halen voor slicen + public int getSliceLootAmount(String animal) { + return config.getInt("shearable-animals." + animal + ".slice.loot.amount", 1); + } + + // Methode om het aantal EXP op te halen (standaard 1) + public int getExpAmount(String animal) { + return config.getInt("shearable-animals." + animal + ".exp", 1); + } + + // Toggle shearable status van een dier + public void toggleShearable(String animal) { + // Controleer eerst of het dier shearable kan zijn + if (!canBeSheared(animal)) { + return; // Blokkeer toggling als het dier niet shearable is + } + boolean currentStatus = config.getBoolean("shearable-animals." + animal + ".shearable", false); + config.set("shearable-animals." + animal + ".shearable", !currentStatus); + plugin.saveConfig(); + } + + // Toggle sliceable status van een dier + public void toggleSliceable(String animal) { + boolean currentStatus = config.getBoolean("shearable-animals." + animal + ".sliceable", false); + config.set("shearable-animals." + animal + ".sliceable", !currentStatus); + plugin.saveConfig(); + } + + // Methode om te controleren of een dier is geconfigureerd + public boolean isAnimalConfigured(String animal) { + return config.contains("shearable-animals." + animal); + } + + // Methode om te controleren of een dier een shearable optie heeft + public boolean hasShearableOption(String animal) { + return config.contains("shearable-animals." + animal + ".shearable"); + } + + // Methode om te controleren of een dier een sliceable optie heeft + public boolean hasSliceableOption(String animal) { + return config.contains("shearable-animals." + animal + ".sliceable"); + } + + // Methode om alle geconfigureerde dieren te krijgen + public Set getAnimals() { + return config.getConfigurationSection("shearable-animals").getKeys(false); + } +} diff --git a/src/main/java/bloody/devmules/shearMaster/listeners/ShearListener.java b/src/main/java/bloody/devmules/shearMaster/listeners/ShearListener.java new file mode 100644 index 0000000..937c691 --- /dev/null +++ b/src/main/java/bloody/devmules/shearMaster/listeners/ShearListener.java @@ -0,0 +1,124 @@ +package bloody.devmules.shearMaster.listeners; + +import bloody.devmules.shearMaster.ShearMaster; +import org.bukkit.Material; +import org.bukkit.entity.*; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; + +public class ShearListener implements Listener { + + private ShearMaster plugin; + + public ShearListener(ShearMaster plugin) { + this.plugin = plugin; + } + + @EventHandler + public void onInteractEntity(PlayerInteractEntityEvent event) { + Player player = event.getPlayer(); + Entity entity = event.getRightClicked(); + + // Controleren of het een dier is en of het een volwassene is + if (!(entity instanceof Ageable)) return; + Ageable animal = (Ageable) entity; + + ItemStack itemInHand = player.getInventory().getItemInMainHand(); + + // Stop de interactie als het dier een baby is + if (animal.getAge() < 0) { + return; + } + + // Scheren met schaar + if (itemInHand.getType() == Material.SHEARS) { + shearAnimal(player, animal, itemInHand); + } + // Oogsten met een zwaard + else if (isSword(itemInHand.getType())) { + harvestMeat(player, animal, itemInHand); + } + } + + private void shearAnimal(Player player, Ageable animal, ItemStack shears) { + String animalType = getAnimalType(animal); + if (animalType == null || !plugin.getConfigManager().isShearable(animalType)) return; + + // Geef loot voor het scheren + Material lootMaterial; + try { + lootMaterial = Material.valueOf(plugin.getConfigManager().getLoot(animalType).toUpperCase()); + } catch (IllegalArgumentException e) { + return; + } + + int lootAmount = plugin.getConfigManager().getLootAmount(animalType); + ItemStack loot = new ItemStack(lootMaterial, lootAmount); + + // Laat de items op de grond vallen + animal.getWorld().dropItemNaturally(animal.getLocation(), loot); + + // Verlies van duurzaamheid voor de schaar + Damageable shearsMeta = (Damageable) shears.getItemMeta(); + shearsMeta.setDamage(shearsMeta.getDamage() + 1); // Schaar verliest 1 duurzaamheid + shears.setItemMeta(shearsMeta); + + // Laat de EXP orbs op de grond vallen + int expAmount = plugin.getConfigManager().getExpAmount(animalType); + animal.getWorld().spawn(animal.getLocation(), ExperienceOrb.class).setExperience(expAmount); + + // Verander het dier in een baby zonder bericht + animal.setBaby(); + } + + private void harvestMeat(Player player, Ageable animal, ItemStack sword) { + String animalType = getAnimalType(animal); + if (animalType == null || !plugin.getConfigManager().isSliceable(animalType)) return; + + // Geef vlees op basis van het type dier + Material meatMaterial; + try { + meatMaterial = Material.valueOf(plugin.getConfigManager().getSliceLoot(animalType).toUpperCase()); + } catch (IllegalArgumentException e) { + return; + } + + int meatAmount = plugin.getConfigManager().getSliceLootAmount(animalType); + ItemStack meat = new ItemStack(meatMaterial, meatAmount); + + // Laat de items op de grond vallen + animal.getWorld().dropItemNaturally(animal.getLocation(), meat); + + // Verlies van duurzaamheid voor het zwaard + Damageable swordMeta = (Damageable) sword.getItemMeta(); + swordMeta.setDamage(swordMeta.getDamage() + 1); // Zwaard verliest 1 duurzaamheid + sword.setItemMeta(swordMeta); + + // Laat de EXP orbs op de grond vallen + int expAmount = plugin.getConfigManager().getExpAmount(animalType); + animal.getWorld().spawn(animal.getLocation(), ExperienceOrb.class).setExperience(expAmount); + + // Verander het dier in een baby zonder bericht + animal.setBaby(); + } + + // Methode om te controleren of een item een zwaard is + private boolean isSword(Material material) { + return material.name().endsWith("_SWORD"); + } + + // Methode om het dierstype te bepalen + private String getAnimalType(Entity entity) { + if (entity instanceof Cow) return "cow"; + if (entity instanceof Chicken) return "chicken"; + if (entity instanceof Horse) return "horse"; + if (entity instanceof Rabbit) return "rabbit"; + if (entity instanceof Llama) return "llama"; + if (entity instanceof Strider) return "strider"; + if (entity instanceof Pig) return "pig"; + return null; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..2520ac8 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,64 @@ +shearable-animals: + cow: + shearable: true + sliceable: true + loot: + material: LEATHER + amount: 1 + slice: + loot: + material: BEEF + amount: 2 + exp: 2 + chicken: + shearable: true + sliceable: true + loot: + material: FEATHER + amount: 2 + slice: + loot: + material: CHICKEN + amount: 1 + exp: 1 + horse: + shearable: true + sliceable: true + loot: + material: LEATHER + amount: 2 + slice: + loot: + material: BEEF + amount: 3 + exp: 3 + rabbit: + shearable: true + sliceable: true + loot: + material: RABBIT_HIDE + amount: 1 + slice: + loot: + material: RABBIT + amount: 1 + exp: 1 + pig: + shearable: false + sliceable: true + slice: + loot: + material: PORKCHOP + amount: 2 + exp: 1 + llama: + shearable: true + sliceable: true + loot: + material: WOOL + amount: 3 + slice: + loot: + material: LEATHER + amount: 2 + exp: 2 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..95eedd3 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,23 @@ +name: ShearMaster +version: '0.01-SNAPSHOT' +main: bloody.devmules.shearMaster.ShearMaster +api-version: '1.21' + +commands: + shearmaster: + description: Main command for ShearMaster plugin + usage: / + aliases: [sm] + permission: shearmaster.admin + +permissions: + shearmaster.admin: + description: Grants full access to ShearMaster commands (such as reload) + default: op + shearmaster.use: + description: Grants permission to shear or slice animals + default: true + shearmaster.toggle: + description: Grants permission to toggle shearing/slicing of animals + default: op +