Войти или зарегистрироваться

Помогите Помогите упростить код

Статус
Закрыто для дальнейших ответов.

TheMrQuake

Активный участник
Пользователь
Баллы
66
Имя в Minecraft
TheMrQuake
Помогите упростить код:
Код:
package com.themrquake.RPG.weapon;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Wolf;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.*********ventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;

import com.themrquake.RPG.other.SpawnSystem;

import de.slikey.effectlib.util.ParticleEffect;

public class IceArrow implements Listener {


    private com.themrquake.RPG.Main main;
    public IceArrow(com.themrquake.RPG.Main main) {
            this.main = main;
    }
   ;

      private List<Entity> getEntitiesByLocation(Location loc, float r)
      {
        List<Entity> ent = new ArrayList();
        for (Entity e : loc.getWorld().getEntities()) {
          if (e.getLocation().distanceSquared(loc) <= r) {
            ent.add(e);
          }
        }
        return ent;
      }



    ArrayList<Player> IA = new ArrayList<Player>();
  
    @EventHandler
      public void IA1(final PlayerInteractEvent event){
          final Player p = event.getPlayer();
          File f = new File(main.getDataFolder() + File.separator + "Class" + File.separator + p.getName() + ".yml");
          YamlConfiguration c = YamlConfiguration.loadConfiguration(f);
            if (event.getAction().equals(Action.RIGHT_CLICK_AIR) && p.isSneaking() == false || event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && p.isSneaking() == false) {
                if (event.getItem() !=null) {
                if (event.getItem().getType() != null && event.getItem().getItemMeta().getDisplayName() != null) {
                if (event.getItem().getItemMeta().getDisplayName().equals("§aПосох волшебника")
                        && event.getItem().getType() == Material.WOOD_HOE) {
                        if (p.getFoodLevel() <= 2 && c.get("Class." + p.getName() + ".S" + ".2").equals("Ледяная стрела") || !(p.getFoodLevel() >= 5) && c.get("Class." + p.getName() + ".S" + ".2").equals("Ледяная стрела")) {
                            p.sendMessage("§bRPG §7» §cДля использования заклинания вам необходимо иметь 5 маны!");
                        return;
                    }
                    
            if (event.getAction().equals(Action.RIGHT_CLICK_AIR)&& p.isSneaking() == false || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)&& p.isSneaking() == false) {
                    if (event.getItem() !=null) {
                    if (event.getItem().getType() != null && event.getItem().getItemMeta().getDisplayName() != null) {
                    if (event.getItem().getItemMeta().getDisplayName().equals("§aПосох волшебника")
                            && event.getItem().getType() == Material.WOOD_HOE) {
                        if (IA.contains(p) && c.get("Class." + p.getName() + ".S" + ".2").equals("Ледяная стрела")) {
                            p.sendMessage("§bRPG §7» §cВы можете использовать удар грома раз в 15 секунд.");
                        return;
                            }
                    
            if (event.getAction().equals(Action.RIGHT_CLICK_AIR)&& p.isSneaking() == false || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)&& p.isSneaking() == false) {
                if (event.getAction().equals(Action.RIGHT_CLICK_AIR)&& p.isSneaking() == false || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)&& p.isSneaking() == false) {
                    if (event.getItem() !=null) {
                    if (event.getItem().getType() != null && event.getItem().getItemMeta().getDisplayName() != null) {
                    if (event.getItem().getItemMeta().getDisplayName().equals("§aПосох волшебника")
                            && event.getItem().getType() == Material.WOOD_HOE) {
                        if (!IA.contains(p)) {
                            if (c.getString("Class." + p.getName() + ".S" + ".2").equals("Ледяная стрела"))  {
                                IA.add(p);
                                p.setFoodLevel(p.getFoodLevel() - 5);
                      main.sendTitle(p, Integer.valueOf(30), Integer.valueOf(100), Integer.valueOf(30), "§aПременено заклинание", "§6§oЛедяная стрела");

                      Location loc = p.getLocation().add(0.0D, 1.5D, 0.0D);
                      for (int i = 0; i <= 15; i++)
                      {
                        loc = loc.add(loc.getDirection().getX(), loc.getDirection().getY() - 0.07D,
                          loc.getDirection().getZ());
                        for (Entity ent : getEntitiesByLocation(loc, 1.2F)) {
                            for (final Player ar : Bukkit.getOnlinePlayers()) {
                                Player pl = (Player) Bukkit.getPlayer(ar.getName());
                          if (((ent instanceof LivingEntity)) && (ent != p)) {
                                    Random r2 = new Random();
                                 int ra2 = r2.nextInt(10);
                                 LivingEntity wf = (LivingEntity) ent;
                             
                            ((LivingEntity)ent).damage(10.0D + ra2);
                            wf.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 20 * 3, 50));
                            wf.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 20 * 1, 50));
                            ((LivingEntity)wf).getKiller();
                            main.map.put(wf.getUniqueId(), p.getName());
                        
                            p.getLocation().getWorld().playSound(ent.getLocation(), Sound.ORB_PICKUP, 2.0F, 2.0F);
                          }
                        }
                        if (loc.getBlock().getType().isSolid()) {
                          break;
                        }
                      }
                    }
                  Location start = p.getEyeLocation();
                  Vector increase = start.getDirection();
                  for (int guncount = 0; guncount < 20; guncount++)
                  {
                    Location point = start.add(increase);
                    ParticleEffect.FIREWORKS_SPARK.display(0.2F, 0.1F, 0.2F, 0.1F, 20, point, 400);
                  }
                  
                  
                      Bukkit.getScheduler().scheduleSyncDelayedTask(this.main, new Runnable() {
                        @Override
                        public void run() {
                            IA.remove(p);
                            }
                
                  }, 20 * 15);
                  }
                }
        
            }
        }
        }
        }
            }
                    }
                    }
                
        
            
            
            
            }
                }
                }
                }
            }
            }
    }



@EventHandler
public void IA2(final PlayerInteractEvent event){
      final Player p = event.getPlayer();
      File f = new File(main.getDataFolder() + File.separator + "Class" + File.separator + p.getName() + ".yml");
      YamlConfiguration c = YamlConfiguration.loadConfiguration(f);
        if (event.getAction().equals(Action.RIGHT_CLICK_AIR) && p.isSneaking() == true || event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && p.isSneaking() == true) {
            if (event.getItem() !=null) {
            if (event.getItem().getType() != null && event.getItem().getItemMeta().getDisplayName() != null) {
            if (event.getItem().getItemMeta().getDisplayName().equals("§aПосох волшебника")
                    && event.getItem().getType() == Material.WOOD_HOE) {
                    if (p.getFoodLevel() <= 2 && c.get("Class." + p.getName() + ".S" + ".3").equals("Ледяная стрела") || !(p.getFoodLevel() >= 5) && c.get("Class." + p.getName() + ".S" + ".3").equals("Деленная стрела")) {
                        p.sendMessage("§bRPG §7» §cДля использования заклинания вам необходимо иметь 5 маны!");
                    return;
                }
                
        if (event.getAction().equals(Action.RIGHT_CLICK_AIR)&& p.isSneaking() == true || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)&& p.isSneaking() == true) {
                if (event.getItem() !=null) {
                if (event.getItem().getType() != null && event.getItem().getItemMeta().getDisplayName() != null) {
                if (event.getItem().getItemMeta().getDisplayName().equals("§aПосох волшебника")
                        && event.getItem().getType() == Material.WOOD_HOE) {
                    if (IA.contains(p) && c.get("Class." + p.getName() + ".S" + ".3").equals("Ледяная стрела")) {
                        p.sendMessage("§bRPG §7» §cВы можете использовать удар грома раз в 15 секунд.");
                    return;
                        }
                
        if (event.getAction().equals(Action.RIGHT_CLICK_AIR)&& p.isSneaking() == true || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)&& p.isSneaking() == true) {
                if (event.getItem() !=null) {
                if (event.getItem().getType() != null && event.getItem().getItemMeta().getDisplayName() != null) {
                if (event.getItem().getItemMeta().getDisplayName().equals("§aПосох волшебника")
                        && event.getItem().getType() == Material.WOOD_HOE) {
                    if (!IA.contains(p)) {
                        if (c.getString("Class." + p.getName() + ".S" + ".3").equals("Ледяная стрела"))  {
                            IA.add(p);
                            p.setFoodLevel(p.getFoodLevel() - 5);
                  main.sendTitle(p, Integer.valueOf(30), Integer.valueOf(100), Integer.valueOf(30), "§aПременено заклинание", "§6§oЛедяная стрела");

                  Location loc = p.getLocation().add(0.0D, 1.5D, 0.0D);
                  for (int i = 0; i <= 15; i++)
                  {
                    loc = loc.add(loc.getDirection().getX(), loc.getDirection().getY() - 0.07D,
                      loc.getDirection().getZ());
                    for (Entity ent : getEntitiesByLocation(loc, 1.2F)) {
                        for (final Player ar : Bukkit.getOnlinePlayers()) {
                            Player pl = (Player) Bukkit.getPlayer(ar.getName());
                      if (((ent instanceof LivingEntity)) && (ent != p)) {
                                Random r2 = new Random();
                             int ra2 = r2.nextInt(10);
                             LivingEntity wf = (LivingEntity) ent;
                         
                        ((LivingEntity)ent).damage(10.0D + ra2);
                        wf.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 20 * 3, 50));
                        wf.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 20 * 1, 50));
                        main.map.put(wf.getUniqueId(), p.getName());
                    
                        p.getLocation().getWorld().playSound(ent.getLocation(), Sound.ORB_PICKUP, 2.0F, 2.0F);
                      }
                    }
                    if (loc.getBlock().getType().isSolid()) {
                      break;
                    }
                  }
                }
              Location start = p.getEyeLocation();
              Vector increase = start.getDirection();
              for (int guncount = 0; guncount < 20; guncount++)
              {
                Location point = start.add(increase);
                ParticleEffect.FIREWORKS_SPARK.display(0.2F, 0.1F, 0.2F, 0.1F, 20, point, 400);
              }
              
              
                Bukkit.getScheduler().scheduleSyncDelayedTask(this.main, new Runnable() {
                  @Override
                  public void run() {
                      IA.remove(p);
                      }
          
            }, 20 * 15);
              }
            }
    
        }
    }
    }
    }
        }
                }
                }
            
    
        
        
        
        }
            }
            }
            }
        }

}
}

Код выполняет следующие действия:
После того как я нажимаю на мотыгу с именем "Посох волшебника" он проверяет что есть ли в конфиге "Ледяная стрела". После чего он проверяет чтобы у игрока не было ArrayList IA, и уже после этого стреляет лучом, наносит урон и накладывает эффекты. Ифент IA1 проверяет что у нас в конфиге ".S" + ".2" есть "Ледяная стрела" и тогда он позволяет стрелять на пкм и снимается 5 голода, если они есть, а если есть в ".S" + ".3" есть "Ледяная стрела" тогда он стреляет Shift + Пкм и снимается 5 голода, если они есть. Также он может и стрелять одновременно Пкм и Shift + Пкм.
При этом он заносит в HashMap моба ифну что игрок с ником "Который выстрелил" нанес урон и после смерти наносит выполняется ещё 1 ивент (Его нет в примере). Прошу помочь упростить код, а то я совсем рак )
 
Последнее редактирование:
http://puu.sh/mTgNF/1955ceba14.java

Куча повторов. Куча кривоты (наносим урона ровно столько, сколько игроков на сервере). Загрузка конфига из файла прям в слушателе без разбора причин.
Этого теперь нет :D
 
Статус
Закрыто для дальнейших ответов.
Назад
Верх