Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
16 kB
2
Indexable
MAIN CLASS  
package org.chinmay.custommob;

import org.bukkit.plugin.java.JavaPlugin;

public class CustomMob extends JavaPlugin {

    private MobSpawner mobSpawner;

    @Override
    public void onEnable() {
        MobSpawner mobSpawner = new MobSpawner(this);
        CustomDrops customDrops = new CustomDrops();
        this.getCommand("adminevent").setExecutor(new AdminEventCommand(this, mobSpawner, customDrops));
        getServer().getPluginManager().registerEvents(new MobDeathHandler(this), this);
        getConfig().options().copyDefaults(true);
        saveDefaultConfig();
        mobSpawner = new MobSpawner(this);
        mobSpawner.startSpawning();
        getLogger().info("CustomMob has been enabled");
        getLogger().info("Made @ plugins.chinmay.lol");

    }

    @Override
    public void onDisable() {
        saveConfig();
        mobSpawner.stopSpawning();
        getLogger().info("CustomMob has been disabled");
        getLogger().info("Made @ plugins.chinmay.lol");
    }
}






MOB SPAWNER CLASS
package org.chinmay.custommob;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.ArrayList;
import java.util.List;

public class MobSpawner implements Listener {
    private final CustomMob customMob;
    private final List<LivingEntity> spawnedMobs = new ArrayList<>();
    private int spawnInterval;
    private int despawnTime;
    private int zombieCount;
    private int skeletonCount;
    private int vexCount;

    public MobSpawner(CustomMob customMob) {
        this.customMob = customMob;
        loadConfig();
    }
    public void loadConfig() {
        spawnInterval = customMob.getConfig().getInt("spawnInterval", 30); 
        despawnTime = customMob.getConfig().getInt("despawnTime", 300); 
        zombieCount = customMob.getConfig().getInt("zombieCount", 15);
        skeletonCount = customMob.getConfig().getInt("skeletonCount", 15);
        vexCount = customMob.getConfig().getInt("vexCount", 15);
    }
    public void startSpawning() {
        new BukkitRunnable() {
            @Override
            public void run() {
                World eventWorld = Bukkit.getWorld("event");
                if (eventWorld != null && !Bukkit.getOnlinePlayers().isEmpty()) {
                    spawnMobs(eventWorld);
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            for (LivingEntity mob : spawnedMobs) {
                                if (!mob.isDead()) {
                                    mob.remove();
                                }
                            }
                            spawnedMobs.clear();
                            for (Player player : eventWorld.getPlayers()) {
                                player.sendMessage(ChatColor.RED + "The mobs have been cleared! New ones will appear in 30 seconds.");
                            }
                            new BukkitRunnable() {
                                @Override
                                public void run() {
                                    spawnMobs(eventWorld);
                                }
                            }.runTaskLater(customMob, 30 * 20L); 
                        }
                    }.runTaskLater(customMob, despawnTime * 20L); 
                }
            }
        }.runTaskTimer(customMob, 0, (despawnTime + 30) * 20L);
    }
    private void spawnMobs(World world) {
        for (int i = 0; i < zombieCount; i++) {
            spawnedMobs.add(CustomMobFactory.spawnCustomMob(world, "ZOMBIE"));
        }
        for (int i = 0; i < skeletonCount; i++) {
            spawnedMobs.add(CustomMobFactory.spawnCustomMob(world, "SKELETON"));
        }
        for (int i = 0; i < vexCount; i++) {
            spawnedMobs.add(CustomMobFactory.spawnCustomMob(world, "VEX"));
        }
    }
    public void manualDespawnAndRespawn() {
        World eventWorld = Bukkit.getWorld("event");
        if (eventWorld != null) {
            for (LivingEntity mob : spawnedMobs) {
                if (!mob.isDead()) {
                    mob.remove();
                }
            }
            spawnedMobs.clear();

            for (Player player : eventWorld.getPlayers()) {
                player.sendMessage(ChatColor.RED + "The mobs have been cleared! New ones will appear in 30 seconds.");
            }
            new BukkitRunnable() {
                @Override
                public void run() {
                    spawnMobs(eventWorld);
                }
            }.runTaskLater(customMob, 30 * 20L);
        }
    }


    public void stopSpawning() {
        Bukkit.getScheduler().cancelTasks(customMob);
    }
}





CUSTOM DROPS CLASS
package org.chinmay.custommob;

import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;

public class CustomDrops {

    public static ItemStack getZombieDrop() {
        ItemStack item = new ItemStack(Material.ROTTEN_FLESH);
        ItemMeta meta = item.getItemMeta();
        assert meta != null;
            meta.setDisplayName("§cZombie's Flesh");
         List<String> lore = new ArrayList<>();
         lore.add("§cThis item is found in the event world by killing zombies");
         lore.add("§cYou can trade this for various items.");
         meta.setLore(lore);
         meta.addEnchant(Enchantment.DURABILITY, 1, true);
         meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
        item.setItemMeta(meta);
        return item;
    }

    public static ItemStack getSkeletonDrop() {
        ItemStack item = new ItemStack(Material.BONE_MEAL);
        ItemMeta skeletonmeta = item.getItemMeta();
        assert skeletonmeta != null;
        skeletonmeta.setDisplayName("§fSkeleton's Crushed Bones");
        List<String> lore = new ArrayList<>();
        lore.add("§fThis item is found in the event world by killing skeleton");
        lore.add("§fYou can trade this for various items.");
        skeletonmeta.setLore(lore);
        skeletonmeta.addEnchant(Enchantment.DURABILITY, 1, true);
        skeletonmeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
        item.setItemMeta(skeletonmeta);
        return item;
    }

    public static ItemStack getVexDrop() {
        ItemStack item = new ItemStack(Material.BLUE_DYE);
        ItemMeta vexmeta = item.getItemMeta();
        assert vexmeta != null;
        vexmeta.setDisplayName("§bVex's Soul");
        List<String> lore = new ArrayList<>();
        lore.add("§bThis item is found in the event world by killing vex's");
        lore.add("§bYou can trade this for various items.");
        vexmeta.setLore(lore);
       vexmeta.addEnchant(Enchantment.DURABILITY, 1, true);
       vexmeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
        item.setItemMeta(vexmeta);
        return item;
    }
}






MOB FACTORY CLASS
package org.chinmay.custommob;

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.Location;

public class CustomMobFactory {

    public static LivingEntity spawnCustomMob(World world, String mobType) {
        EntityType entityType = EntityType.valueOf(mobType);
        LivingEntity mob = (LivingEntity) world.spawnEntity(getRandomLocation(world), entityType);

        switch (entityType) {
            case ZOMBIE:
                equipZombie(mob);
                break;
            case SKELETON:
                equipSkeleton(mob);
                break;
            case VEX:
                equipVex(mob);
                break;
        }

        return mob;
    }
    private static void equipZombie(LivingEntity mob) {
        ItemStack helmet = createCustomItem(Material.LEATHER_HELMET, "Zombie's Iron Helmet", Enchantment.PROTECTION_ENVIRONMENTAL, 6);
        ItemStack chestplate = createCustomItem(Material.LEATHER_CHESTPLATE, "Zombie's Iron Chestplate", Enchantment.PROTECTION_ENVIRONMENTAL, 6);
        ItemStack leggings = createCustomItem(Material.LEATHER_LEGGINGS, "Zombie's Iron Leggings", Enchantment.PROTECTION_ENVIRONMENTAL, 6);
        ItemStack boots = createCustomItem(Material.LEATHER_BOOTS, "Zombie's Iron Boots", Enchantment.PROTECTION_ENVIRONMENTAL, 6);

        mob.getEquipment().setHelmet(helmet);
        mob.getEquipment().setChestplate(chestplate);
        mob.getEquipment().setLeggings(leggings);
        mob.getEquipment().setBoots(boots);
    }

    private static void equipSkeleton(LivingEntity mob) {
        ItemStack helmet = createCustomItem(Material.IRON_HELMET, "Skeleton's Iron Helmet", Enchantment.PROTECTION_ENVIRONMENTAL, 6);
        ItemStack chestplate = createCustomItem(Material.IRON_CHESTPLATE, "Skeleton's Iron Chestplate", Enchantment.PROTECTION_ENVIRONMENTAL, 6);
        ItemStack leggings = createCustomItem(Material.IRON_LEGGINGS, "Skeleton's Iron Leggings", Enchantment.PROTECTION_ENVIRONMENTAL, 6);
        ItemStack boots = createCustomItem(Material.IRON_BOOTS, "Skeleton's Iron Boots", Enchantment.PROTECTION_ENVIRONMENTAL, 6);
        ItemStack bow = createCustomItem(Material.BOW, "Skeleton's Fire Bow", Enchantment.ARROW_FIRE, 1, Enchantment.ARROW_DAMAGE, 6);

        mob.getEquipment().setHelmet(helmet);
        mob.getEquipment().setChestplate(chestplate);
        mob.getEquipment().setLeggings(leggings);
        mob.getEquipment().setBoots(boots);
        mob.getEquipment().setItemInMainHand(bow);
    }

    private static void equipVex(LivingEntity mob) {
        ItemStack sword = createCustomItem(Material.DIAMOND_SWORD, "Vex's Fire Sword", Enchantment.FIRE_ASPECT, 2, Enchantment.DAMAGE_ALL, 5);

        mob.getEquipment().setItemInMainHand(sword);
    }

    private static ItemStack createCustomItem(Material material, String name, Enchantment enchantment, int level) {
        ItemStack item = new ItemStack(material);
        ItemMeta meta = item.getItemMeta();
        if (meta != null) {
            meta.setDisplayName(name);
            meta.addEnchant(enchantment, level, true);
        }
        item.setItemMeta(meta);
        return item;
    }

    private static ItemStack createCustomItem(Material material, String name, Enchantment enchantment1, int level1, Enchantment enchantment2, int level2) {
        ItemStack item = new ItemStack(material);
        ItemMeta meta = item.getItemMeta();
        if (meta != null) {
            meta.setDisplayName(name);
            meta.addEnchant(enchantment1, level1, true);
            meta.addEnchant(enchantment2, level2, true);
        }
        item.setItemMeta(meta);
        return item;
    }

    private static Location getRandomLocation(World world) {
        int x = (int) (Math.random() * 100) - 50;
        int y = 64;
        int z = (int) (Math.random() * 100) - 50;
        return new Location(world, x, y, z);
    }
}



MOB DEATH HANDLER CLASS
package org.chinmay.custommob;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;

public class MobDeathHandler implements Listener {
    private final CustomMob customMob;
    public MobDeathHandler(CustomMob customMob) {
        this.customMob = customMob;
    }
    @EventHandler
    public void onMobDeath(EntityDeathEvent event) {
        if (event.getEntity().getKiller() != null) {
            Player killer = event.getEntity().getKiller();
            World eventWorld = Bukkit.getWorld("event");
            if (killer != null && killer.getWorld().equals(eventWorld)) {
                EntityType entityType = event.getEntityType();
                ItemStack customDrop = null;

                switch (entityType) {
                    case ZOMBIE:
                        customDrop = CustomDrops.getZombieDrop();
                        break;
                    case SKELETON:
                        customDrop = CustomDrops.getSkeletonDrop();
                        break;
                    case VEX:
                        customDrop = CustomDrops.getVexDrop();
                        break;
                    default:
                        break;
                }

                if (customDrop != null) {
                    event.getDrops().clear();
                    event.getDrops().add(customDrop);
                }
            }
        }
    }
}



LAST, ADMIN EVENT COMMAND CLASS
package org.chinmay.custommob;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.ChatColor;
import org.chinmay.custommob.CustomMob;
import org.chinmay.custommob.MobSpawner;
import org.chinmay.custommob.CustomDrops;

public class AdminEventCommand implements CommandExecutor {
    private final CustomMob customMob;
    private final MobSpawner mobSpawner;
    private final CustomDrops customDrops;
    public AdminEventCommand(CustomMob customMob, MobSpawner mobSpawner, CustomDrops customDrops) {
        this.customMob = customMob;
        this.mobSpawner = mobSpawner;
        this.customDrops = customDrops;
    }
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("§cOnly players can run this command.");
            return true;
        }

        Player player = (Player) sender;

        if (args.length < 1) {
            player.sendMessage(ChatColor.RED + "§cUsage: /adminevents <givedrops/reload/despawn>");
            return true;
        }

        switch (args[0].toLowerCase()) {
            case "givedrops":
                if (args.length < 4) {
                    player.sendMessage(ChatColor.RED + "§cUsage: /adminevents givedrops <player> <drop> <amount>");
                    return true;
                }
                giveDropsCommand(player, args);
                break;

            case "reload":
                customMob.reloadConfig();
                mobSpawner.loadConfig();
                player.sendMessage(ChatColor.GREEN + "§aConfiguration reloaded successfully.");
                break;

            case "despawn":
                mobSpawner.manualDespawnAndRespawn();
                player.sendMessage(ChatColor.GREEN + "Done!");
                break;

            default:
                player.sendMessage(ChatColor.RED + "Unknown subcommand. Usage: /adminevent <givedrops/reload/despawn>");
                break;
        }

        return true;
    }
    private void giveDropsCommand(Player player, String[] args) {
        Player target = Bukkit.getPlayer(args[1]);
        if (target == null) {
            player.sendMessage(ChatColor.RED + "Player not found.");
            return;
        }

        String dropType = args[2].toLowerCase();
        int amount;

        try {
            amount = Integer.parseInt(args[3]);
        } catch (NumberFormatException e) {
            player.sendMessage(ChatColor.RED + "Invalid amount.");
            return;
        }

        ItemStack item;

        switch (dropType) {
            case "zombie":
                item = customDrops.getZombieDrop();
                break;
            case "skeleton":
                item = customDrops.getSkeletonDrop();
                break;
            case "vex":
                item = customDrops.getVexDrop();
                break;
            default:
                player.sendMessage(ChatColor.RED + "Invalid drop type. Use 'zombie', 'skeleton', or 'vex'.");
                return;
        }
        item.setAmount(amount);
        target.getInventory().addItem(item);
        player.sendMessage(ChatColor.GREEN + "Gave " + amount + " " + dropType + " drop(s) to " + target.getName());
    }
}
Leave a Comment