Config randomly switching

Discussion in 'Plugin Development' started by maxecito, Aug 28, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    maxecito

    Hey guys!
    Im trying to do a little "Harry-Potter-Spells" plugin, were you will always use the spell with left-click which was selected via right-click (and saved into the config), but currently I have a weird bug, which seems to happen when the spell hits a player. It then sometimes (1/10 spells or something like that) switches to another spell and also uses its effect.

    Would be glad if someone could help me out!

    Code: (Main)
    Code:
    package at.skyblock.main;
    
    import java.util.HashSet;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.FireworkEffect;
    import org.bukkit.FireworkEffect.Type;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Firework;
    import org.bukkit.entity.LightningStrike;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.FireworkMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;
    
    public class Main extends JavaPlugin implements Listener {
    
       public void onEnable() {
    
         getServer().getPluginManager().registerEvents(this, this);
    
       }
    
       public void onDisable() {
         saveConfig();
       }
    
       @EventHandler
       public void chooseSpell(InventoryClickEvent event) {
         Player player = (Player) event.getWhoClicked();
         ItemStack clicked = event.getCurrentItem();
         Inventory inventory = event.getInventory();
    
         if (inventory.getName().equals(SpellInventory.spells.getName())) {
    
           if (clicked.getType() == Material.BEDROCK) {
             event.setCancelled(true);
             player.closeInventory();
             getConfig().set("MSPotter.currentSpell", "AvadaKedavra");
             saveConfig();
           } else if (clicked.getType() == Material.FIREWORK_CHARGE) {
             event.setCancelled(true);
             player.closeInventory();
             getConfig().set("MSPotter.currentSpell", "Stupefy");
             saveConfig();
           } else if (clicked.getType() == Material.REDSTONE) {
             event.setCancelled(true);
             player.closeInventory();
             getConfig().set("MSPotter.currentSpell", "Expelliarmus");
             saveConfig();
           }
         }
    
       }
    
       @SuppressWarnings({ "deprecation", "unused" })
       @EventHandler
       public void useSpell(PlayerInteractEvent e) {
    
         Player p = e.getPlayer();
         if (e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
           if (p.getItemInHand().getType().equals(Material.STICK)) {
             Location loc = p.getTargetBlock((HashSet<Byte>) null, 20).getLocation();
             final Firework f = (Firework) p.getWorld().spawn(loc.add(0, 1.5, 0), Firework.class);
             FireworkMeta fm = f.getFireworkMeta();
    
             if (getConfig().getString("MSPotter.currentSpell").equals("AvadaKedavra")) {
    
               WandCreator wandCreator = new WandCreator();
               p.getInventory().addItem(wandCreator.receiveWand(2, p));
    
               fm.addEffect(FireworkEffect.builder().flicker(false).trail(true).with(Type.BURST)
                   .withColor(Color.GREEN).withFade(Color.BLACK).build());
               fm.setPower(5);
               f.setFireworkMeta(fm);
    
               this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
    
                 public void run() {
    
                   f.detonate();
    
                 }
               }, 1L);
    
               for (Entity en : f.getNearbyEntities(1, 1, 1)) {
    
                 if (en instanceof LivingEntity) {
                   LivingEntity le = (LivingEntity) en;
                   LightningStrike ls = (LightningStrike) le.getWorld().spawn(le.getLocation(),
                       LightningStrike.class);
                   le.setHealth(0);
    
                 }
    
               }
    
             } else if (getConfig().getString("MSPotter.currentSpell").equals("Stupefy")) {
    
               fm.addEffect(FireworkEffect.builder().flicker(false).trail(true).with(Type.BALL)
                   .withColor(Color.AQUA).withFade(Color.BLUE).build());
               fm.setPower(3);
               f.setFireworkMeta(fm);
               this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
    
                 public void run() {
    
                   f.detonate();
    
                 }
               }, 1L);
    
               for (Entity en : f.getNearbyEntities(1.5, 1.5, 1.5)) {
    
                 if (en instanceof LivingEntity) {
                   LivingEntity le = (LivingEntity) en;
                   le.setVelocity(p.getLocation().getDirection().multiply(3));
                   le.setVelocity(new Vector(le.getVelocity().getX(), 1.0D, le.getVelocity().getZ()));
                   if (le.getHealth() > 3) {
                     le.setHealth(le.getHealth() - 1.5);
                   }
                 }
    
               }
    
             } else if (getConfig().getString("MSPotter.currentSpell").equals("Expelliarmus")) {
    
               fm.addEffect(FireworkEffect.builder().flicker(false).trail(true).with(Type.STAR)
                   .withColor(Color.RED).build());
               fm.setPower(1);
               f.setFireworkMeta(fm);
               this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
    
                 public void run() {
    
                   f.detonate();
    
                 }
               }, 1L);
    
               for (Entity en : f.getNearbyEntities(1, 1, 1)) {
    
                 if (en instanceof Player) {
    
                   Player target = (Player) en;
    
                   if (target.getInventory().getItemInHand().getType().equals(Material.STICK)) {
                     if (target.getInventory().getItemInHand() != null
                         && target.getInventory().getItemInHand().getType() != Material.AIR) {
    
                       ItemStack wand = target.getInventory().getItemInHand();
                       p.getWorld().dropItemNaturally(p.getLocation(), wand);
    
                       target.getInventory().remove(Material.STICK);
    
                     }
                   } else {
                     p.sendMessage(ChatColor.RED + "Dein Gegner hat keinen Zauberstab in der Hand!");
                   }
    
                 }
    
               }
    
             }
           }
    
         } else if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
           if (p.getItemInHand().getType().equals(Material.STICK)) {
             p.openInventory(SpellInventory.spells);
           }
    
         }
    
       }
    
    }
    
    and the SpellInventory: (the last spell is not done yet, you can ignore it)

    Code:
    package at.skyblock.main;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class SpellInventory {
    
       public static Inventory spells = Bukkit.createInventory(null, 9,
           ChatColor.BOLD + "" + ChatColor.WHITE + "Choose your Spell");
    
       public static void createDisplay(Material material, Inventory inv, int Slot, String name, String lore) {
         ItemStack item = new ItemStack(material);
         ItemMeta meta = item.getItemMeta();
         meta.setDisplayName(name);
         ArrayList<String> Lore = new ArrayList<String>();
         Lore.add(lore);
         meta.setLore(Lore);
         item.setItemMeta(meta);
    
         inv.setItem(Slot, item);
    
       }
    
       static {
    
         createDisplay(Material.BEDROCK, spells, 0, "Avada Kedavra", "Unverzeilicher Fluch");
         createDisplay(Material.FIREWORK_CHARGE, spells, 1, "Stupefy", "Schockzauber");
         createDisplay(Material.REDSTONE, spells, 2, "Expelliarmus", "Entwaffnungszauber");
         createDisplay(Material.MOSSY_COBBLESTONE, spells, 3, "Expelliarmus", "Versteinert den Gegner");
       }
    
    }
    
     
  2. Offline

    au2001

    Why do I feel like "little" and "spell" are two incompatible words...

    Anyway, you should create your config in the onEnable.
    Also, move the event.setCancelled(true) in your inventory check, so that it will cancel it for any clicked item.

    I think your problem is this:
    You save the same spell for EVERYBODY on the server. You should do something like this:
    Code:
    MSPotter:
      <uuid>: <spell>
      <uuid>: <spell>
      <uuid>: <spell>
      <uuid>: <spell>
      <uuid>: <spell>
     
    mine-care likes this.
  3. Offline

    maxecito

    Wa... Thank you Sir! I do not know how this could happen... Now Im kind of looking stupid xD
    Nevermind, thank u very much
     
Thread Status:
Not open for further replies.

Share This Page