Give advice please

Discussion in 'Plugin Development' started by Milkyc, Jun 12, 2022.

  1. Offline

    Milkyc

    Hello, I understand how to write plugins, but it seems to me that I constantly do something wrong in terms of optimizing the plugin, can you give some advice on how to do it right?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Milkyc Did you write all the code already?
     
  3. Offline

    Milkyc

    almost all, it is in working condition
     
  4. Offline

    timtower Administrator Administrator Moderator

    Is the server having issues with your code running?
     
  5. Offline

    Milkyc

    no, but sometimes the RAM is heavily loaded and a certain percentage is loaded in timings
     
  6. Offline

    timtower Administrator Administrator Moderator

    Then what are you storing in memory in the plugin?
     
  7. Offline

    Milkyc

    A plugin to spawn mobs in a specific area when the player enters it. The memory mainly stores data on territories
     
  8. Offline

    timtower Administrator Administrator Moderator

    Can you post the data that is getting stored?
     
  9. Offline

    Milkyc

    I have the .yml file that store data of territories
    Code:
    test:
      position:
        region: 143, 68, -50 - 147, 75, -47
        world: world
      mobs:
      - CHICKEN_SPAWN_EGG: 4;100;random
      triggers:
        position:
          region: 145, 70, -48 - 146, 68, -47
    and the dictionary of type <String, Territory> String is name of territory and class that represents this data
    maybe in that class something wrong with code but i tried to make it right

    Class (open)

    Code:
    package mlk.core.mngrs;
    
    import com.sk89q.worldedit.bukkit.BukkitAdapter;
    import com.sk89q.worldedit.math.BlockVector3;
    import com.sk89q.worldedit.math.Vector3;
    import com.sk89q.worldedit.regions.CuboidRegion;
    import com.sk89q.worldedit.world.World;
    import io.lumine.xikage.mythicmobs.MythicMobs;
    import io.lumine.xikage.mythicmobs.mobs.ActiveMob;
    import io.lumine.xikage.mythicmobs.mobs.MythicMob;
    import mlk.core.DungeonCore;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.Sound;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    
    import java.util.*;
    
    public class Territory {
        private ArrayList<Entity> alive_mobs = new ArrayList<>();
        private ArrayList<ActiveMob> alive_activemobs = new ArrayList<>();
        public Map<String, List<String>> settings = new LinkedHashMap<>();
        public Set<String> players = new LinkedHashSet<>();
        public ArrayList<Sound> sound = new ArrayList<>();
        public boolean spawn;
    
        public HashMap<String, Boolean> checks;
    
        public CuboidRegion tregion;
        public CuboidRegion region;
        public String message;
        public String name;
        public World world;
        public int radius;
    
        public Territory(int radius, World world, ArrayList<String> str, List<?> sound, List<Map<?, ?>> mobs, HashMap<String, Boolean> checks) {
            this.name = str.get(0);
            this.radius = radius;
            ArrayList<BlockVector3> arl = coords(str.get(1));
            this.tregion = createRadius(str.get(2));
            this.message = str.get(3);
            this.world = world;
            this.settings = mng(mobs);
            this.checks = checks;
            this.spawn = checks.get("spawn_if_exist");
            this.region = new CuboidRegion(world, arl.get(0), arl.get(1));
    
            if (sound != null) {
                for (Object i : sound) {
                    this.sound.add(Sound.valueOf(i.toString()));
                }
            }
        }
    
        public void sendMessage(Player player) {
            if (message != null) {
                String[] ms = message.split("\\\\n");
                for (String j : ms) {
                    player.sendMessage(j + "\n");
                }
            }
        }
        public void playSound(Player player) {
            if (sound != null) {
                for (Sound j : sound) {
                    player.playSound(player.getLocation(), j, 100, 2);
                }
            }
    
        }
        public void SpawnMob(Player player) {
            Random rand = new Random();
            if (!spawn) {
                if (alive_mobs != null) {
                    for (Entity entity : alive_mobs)
                        if (!entity.isDead()) return;
                }
                if (alive_activemobs != null)
                    for (ActiveMob activeMob : alive_activemobs)
                        if (!activeMob.isDead()) return;
            }
            sendMessage(player);
            playSound(player);
            for (String str : settings.keySet()) {
                    int mob_count = Integer.parseInt(settings.get(str).get(0));
                    double mob_percent = Double.parseDouble(settings.get(str).get(1)) / 100;
                    String pos = settings.get(str).get(2);
                    float num = rand.nextFloat();
                    if (mob_percent != 1.0) {
                        if (num > mob_percent)
                            continue;
                    }
                    for (int i = 0; i < mob_count; i++) {
                        Location loc = PositionSpawn(pos);
                        while (!region.contains(BlockVector3.at(loc.getX(), loc.getY(), loc.getZ())) && region.getWalls().contains(BlockVector3.at(loc.getX(), loc.getY()-2, loc.getZ()))) {
                            loc = PositionSpawn(pos);
                        }
                        if (!str.contains("_MythicMob")) {
                            Material mat = Material.valueOf(str);
                            EntityType mob = (EntityType) Mobs.getEggs().get(0).get(mat);
                            alive_mobs.add(BukkitAdapter.adapt(world).spawnEntity(loc, mob));
                        } else {
                            Object test = DungeonCore.getMyth();
                            if (test == null) return;
                            MythicMobs mythic = (MythicMobs) test;
                            String mob = str.replace("_MythicMob", "");
                            alive_activemobs.add(mythic.getMobManager().spawnMob(mob, loc));
                        }
                    }
                }
            }
        public void teleport(Player player) {
            Bukkit.getScheduler().runTask(DungeonCore.ins, new Runnable() {
                @Override
                public void run() {
                    org.bukkit.World world = BukkitAdapter.adapt(region.getWorld());
                    Vector3 center = region.getCenter();
                    Location location = new Location(world, center.getX(), center.getY(), center.getZ());
                    player.teleport(location);
                }
            });
        }
        public void killMobs() {
                if (alive_mobs != null) {
                    for (Entity i : alive_mobs)
                        i.remove();
                    alive_mobs.clear();
                }
                if (alive_activemobs != null) {
                    for (ActiveMob i : alive_activemobs)
                        i.remove();
                    alive_activemobs.clear();
                }
        }
    
    
        private Location PositionSpawn(String pos) {
            Location location;
            //get position by spawn
            org.bukkit.World world = BukkitAdapter.adapt(region.getWorld());
            if (pos.equalsIgnoreCase("center")) {
                Vector3 vector3 = region.getCenter();
                double x = vector3.getX();
                double y = region.getMinimumY();
                double z = vector3.getZ();
                location = new Location(world, x, y, z);
            } else {
                Random rand = new Random();
                ArrayList<BlockVector3> vector3 = new ArrayList<>();
                for (BlockVector3 point : region) vector3.add(point);
                int random = rand.nextInt(vector3.size());
                double x = vector3.get(random).getBlockX();
                double y = region.getMinimumY();
                double z = vector3.get(random).getBlockZ();
                location = new Location(world, x, y, z);
                }
    
            //Some fixes
    
            double x = location.getX();
            double y = location.getY();
            double z = location.getZ();
    
    
            location = new Location(world, x < 0 ? x-0.500 : x+0.500, y, z < 0 ? z-0.500 : z+0.500);
            x = location.getX();
            y = location.getY();
            z = location.getZ();
            while (location.getBlock().getType().equals(Material.AIR) && new Location(world, x, y-1, z).getBlock().getType().equals(Material.AIR)) {
                x = location.getX();
                y = location.getY();
                z = location.getZ();
                location = new Location(world, x, y-1, z);
            }
            while (!new Location(world, x, y+2, z).getBlock().getType().equals(Material.AIR)) {
                x = location.getX();
                y = location.getY();
                z = location.getZ();
                location = new Location(world, x, y+2, z);
            }
            while (!new Location(world, x, y+2, z).getBlock().getType().equals(Material.AIR)) {
                x = location.getX();
                y = location.getY();
                z = location.getZ();
                location = new Location(world, x, y+2, z);
            }
            location = new Location(world, x, y+1, z);
            return location;
        }
        private ArrayList<BlockVector3> coords(String lst) {
            if (lst.contains(" - ")) {
                ArrayList<BlockVector3> bl = new ArrayList<>();
                String[] trans = lst.split(" - ");
                for (String i : trans) {
                    List<Integer> list = new ArrayList<>();
                    for (String j : i.split(", ")) {
                        int c = Integer.parseInt(j);
                        list.add(c);
                    }
                    bl.add(BlockVector3.at(list.get(0), list.get(1), list.get(2)));
                }
                return bl;
            } else {
                List<Integer> list = new ArrayList<>();
                for (String j : lst.split(", ")) {
                    int c = Integer.parseInt(j);
                    list.add(c);
                }
                ArrayList<BlockVector3> bl = new ArrayList<>();
                bl.add(BlockVector3.at(list.get(0), list.get(1), list.get(2)));
                return bl;
            }
        }
        private CuboidRegion createRadius(String rg) {
            if (rg == null) return null;
            ArrayList<BlockVector3> ar = coords(rg);
            if (ar.size() == 1) {
                BlockVector3 loc1 = ar.get(0);
                BlockVector3 loc2 = BlockVector3.at(radius, radius, radius);
                BlockVector3 loc3 = BlockVector3.at(-radius, -radius, -radius);
                CuboidRegion region = new CuboidRegion(world, loc1, loc1);
                region.expand(loc2,loc3);
                return region;
            } else {
                return new CuboidRegion(world, ar.get(0), ar.get(1));
            }
        }
        private Map<String, List<String>> mng(List<Map<?,?>> lst) {
            Map<String, List<String>> m = new LinkedHashMap<>();
            for (Map<?,?> map : lst) {
                for (Object i : map.keySet()) {
                    String[] str = ((String) map.get(i)).split(";");
                    List<String> s = new ArrayList<>(Arrays.asList(str));
                    m.put((String) i, s);
                }
            }
            return m;
        }
    }
    
     

Share This Page