From 575a96b9ca474daaedd21e8bec6f251623998260 Mon Sep 17 00:00:00 2001 From: Koopa <115321970+KoopaCode@users.noreply.github.com> Date: Fri, 31 Jan 2025 20:28:28 -0500 Subject: [PATCH] Source Code Uplodation --- pom.xml | 71 +++++++++++++++++++ .../koopalabs/nobullying/BullyCommand.java | 31 ++++++++ .../nobullying/BullyTabCompleter.java | 22 ++++++ .../koopalabs/nobullying/ConfigManager.java | 29 ++++++++ .../nobullying/MobTargetListener.java | 46 ++++++++++++ .../com/koopalabs/nobullying/NoBullying.java | 39 ++++++++++ src/main/resources/config.yml | 13 ++++ src/main/resources/plugin.yml | 11 +++ 8 files changed, 262 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/koopalabs/nobullying/BullyCommand.java create mode 100644 src/main/java/com/koopalabs/nobullying/BullyTabCompleter.java create mode 100644 src/main/java/com/koopalabs/nobullying/ConfigManager.java create mode 100644 src/main/java/com/koopalabs/nobullying/MobTargetListener.java create mode 100644 src/main/java/com/koopalabs/nobullying/NoBullying.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/plugin.yml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4b00880 --- /dev/null +++ b/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + + com.koopalabs + nobullying + 1.0.0 + jar + + NoBullying + Limits the number of mobs that can target a player at once + + + 17 + UTF-8 + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + + + org.spigotmc + spigot-api + 1.21.4-R0.1-SNAPSHOT + provided + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.5.1 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + \ No newline at end of file diff --git a/src/main/java/com/koopalabs/nobullying/BullyCommand.java b/src/main/java/com/koopalabs/nobullying/BullyCommand.java new file mode 100644 index 0000000..bef479c --- /dev/null +++ b/src/main/java/com/koopalabs/nobullying/BullyCommand.java @@ -0,0 +1,31 @@ +package com.koopalabs.nobullying; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class BullyCommand implements CommandExecutor { + private final NoBullying plugin; + + public BullyCommand(NoBullying plugin) { + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!sender.hasPermission("nobullying.reload")) { + sender.sendMessage(ChatColor.RED + "You don't have permission to use this command!"); + return true; + } + + if (args.length == 1 && args[0].equalsIgnoreCase("reload")) { + plugin.getConfigManager().loadConfig(); + sender.sendMessage(ChatColor.GREEN + "NoBullying configuration reloaded!"); + return true; + } + + sender.sendMessage(ChatColor.RED + "Usage: /bully reload"); + return true; + } +} \ No newline at end of file diff --git a/src/main/java/com/koopalabs/nobullying/BullyTabCompleter.java b/src/main/java/com/koopalabs/nobullying/BullyTabCompleter.java new file mode 100644 index 0000000..a533626 --- /dev/null +++ b/src/main/java/com/koopalabs/nobullying/BullyTabCompleter.java @@ -0,0 +1,22 @@ +package com.koopalabs.nobullying; + +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 BullyTabCompleter implements TabCompleter { + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + List completions = new ArrayList<>(); + + if (args.length == 1) { + if (sender.hasPermission("nobullying.reload")) { + completions.add("reload"); + } + } + + return completions; + } +} \ No newline at end of file diff --git a/src/main/java/com/koopalabs/nobullying/ConfigManager.java b/src/main/java/com/koopalabs/nobullying/ConfigManager.java new file mode 100644 index 0000000..c64c331 --- /dev/null +++ b/src/main/java/com/koopalabs/nobullying/ConfigManager.java @@ -0,0 +1,29 @@ +package com.koopalabs.nobullying; + +import org.bukkit.World; +import java.util.List; + +public class ConfigManager { + private final NoBullying plugin; + private int maxTargetingMobs; + private List enabledWorlds; + + public ConfigManager(NoBullying plugin) { + this.plugin = plugin; + loadConfig(); + } + + public void loadConfig() { + plugin.reloadConfig(); + maxTargetingMobs = plugin.getConfig().getInt("max-targeting-mobs", 5); + enabledWorlds = plugin.getConfig().getStringList("enabled-worlds"); + } + + public int getMaxTargetingMobs() { + return maxTargetingMobs; + } + + public boolean isWorldEnabled(World world) { + return enabledWorlds.contains(world.getName()); + } +} \ No newline at end of file diff --git a/src/main/java/com/koopalabs/nobullying/MobTargetListener.java b/src/main/java/com/koopalabs/nobullying/MobTargetListener.java new file mode 100644 index 0000000..eb9c43b --- /dev/null +++ b/src/main/java/com/koopalabs/nobullying/MobTargetListener.java @@ -0,0 +1,46 @@ +package com.koopalabs.nobullying; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Monster; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityTargetLivingEntityEvent; + +import java.util.List; +import java.util.stream.Collectors; + +public class MobTargetListener implements Listener { + private final NoBullying plugin; + + public MobTargetListener(NoBullying plugin) { + this.plugin = plugin; + } + + @EventHandler + public void onEntityTarget(EntityTargetLivingEntityEvent event) { + if (event.getTarget() instanceof Player && event.getEntity() instanceof Monster) { + Player player = (Player) event.getTarget(); + + // Check if the world is enabled + if (!plugin.getConfigManager().isWorldEnabled(player.getWorld())) { + return; + } + + // Get all monsters currently targeting the player + List targetingMobs = player.getWorld().getEntities().stream() + .filter(e -> e instanceof Monster) + .filter(e -> { + LivingEntity target = ((Monster) e).getTarget(); + return target != null && target.equals(player); + }) + .collect(Collectors.toList()); + + // If we already have max mobs targeting, cancel the new targeting + if (targetingMobs.size() >= plugin.getConfigManager().getMaxTargetingMobs()) { + event.setCancelled(true); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/koopalabs/nobullying/NoBullying.java b/src/main/java/com/koopalabs/nobullying/NoBullying.java new file mode 100644 index 0000000..7bf0af5 --- /dev/null +++ b/src/main/java/com/koopalabs/nobullying/NoBullying.java @@ -0,0 +1,39 @@ +package com.koopalabs.nobullying; + +import org.bukkit.plugin.java.JavaPlugin; + +public class NoBullying extends JavaPlugin { + private static NoBullying instance; + private ConfigManager configManager; + + @Override + public void onEnable() { + instance = this; + + // Initialize config + saveDefaultConfig(); + configManager = new ConfigManager(this); + + // Register events + getServer().getPluginManager().registerEvents(new MobTargetListener(this), this); + + // Register commands and tab completer + getCommand("bully").setExecutor(new BullyCommand(this)); + getCommand("bully").setTabCompleter(new BullyTabCompleter()); + + getLogger().info("NoBullying plugin has been enabled!"); + } + + @Override + public void onDisable() { + getLogger().info("NoBullying plugin has been disabled!"); + } + + public static NoBullying getInstance() { + return instance; + } + + public ConfigManager getConfigManager() { + return configManager; + } +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..be096ed --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,13 @@ +# NoBullying Plugin Configuration +# Created by Koopa (KoopaLabs) + +# Maximum number of mobs that can target a player at once +max-targeting-mobs: 5 + +# Worlds where the plugin is enabled +enabled-worlds: + - world + - world_nether + - world_the_end + +# Support server: https://discord.gg/KmHGjaHWct \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..f3c2544 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,11 @@ +name: NoBullying +version: 1.0.0 +main: com.koopalabs.nobullying.NoBullying +api-version: '1.21' +author: Koopa +description: Limits the number of mobs that can target a player at once +commands: + bully: + description: Reload the plugin configuration + usage: /bully reload + permission: nobullying.reload \ No newline at end of file