[Solved] Effects not displaying at a location

Discussion in 'Spigot Plugin Development' started by GamerBah, May 17, 2015.

  1. Hi there. I've been working on a Spell plugin and I've come across a slight problem while trying to get effects to display automagically. I'm not quite sure where the issue is.

    Code (Text):
    public void onEnable() {


            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                public void run() {
                    for (int i = 0; i <= Events.tables.size(); i++) {
                        if (Events.tables.size() == 0) {
                            return;
                        } else {
                            for(World w : Bukkit.getServer().getWorlds()) {
                            Location loc = Events.unpackageLocation(Events.tables
                                    .get(i));
                            ParticleEffect.FLAME.display(0, 0, 0, 0.05F, 30, loc,
                                    100);
                            ParticleEffect.SPELL_INSTANT.display(0, 0, 0, 0.05F,
                                    30, loc, 100);
                            w.playEffect(loc, Effect.FLAME, 2);
                            }
                        }
                    }
                }
            }, 10);

    Code (Text):


    public class Events implements Listener {
     
        @SuppressWarnings("unused")
        private Spellbound plugin;
        Files files = new Files();
     
        public Events(Spellbound plugin) {
            this.plugin = plugin;
        }

        public static ArrayList<String> tables = new ArrayList<String>();
     
        public ArrayList<String> tables() {
            return tables;
        }
     
        /**
         * Formats a location to world,x,y,z for saving *
         */
        public static String packageLocation(Location l)
        {
            if (l == null)
                return null;
            return l.getWorld().getName() + "," + l.getBlockX() + "," + l.getBlockY() + "," + l.getBlockZ();
        }

        /**
         * Returns a location from a location in the format world,x,y,z *
         */
        public static Location unpackageLocation(String s)
        {
            if (s == null)
                return null;
            String[] split = s.split(",");
            return new Location(Bukkit.getWorld(split[0]), Double.parseDouble(split[1]), Double.parseDouble(split[2]), Double.parseDouble(split[3]));
        }
     
        public ItemStack spelltable() {
            ItemStack spelltable = new ItemStack(Material.ENCHANTMENT_TABLE);
            ItemMeta im = spelltable.getItemMeta();
            im.setDisplayName(AQUA + "Spellbook Enchantment Table");
            spelltable.setItemMeta(im);
            return spelltable;
        }
     
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent event) {
            Player player = event.getPlayer();
            if (player.getItemInHand().equals(spelltable()) && player.getItemInHand().getAmount() >= 1) {
                if (event.getBlock().getType().equals(Material.ENCHANTMENT_TABLE)) {
                    Block table = event.getBlockPlaced();
                    if (table.getRelative(BlockFace.UP).getType().equals(Material.AIR)
                            && table.getRelative(BlockFace.WEST).getType().equals(Material.AIR)
                            && table.getRelative(BlockFace.NORTH).getType().equals(Material.AIR)
                            && table.getRelative(BlockFace.EAST).getType().equals(Material.AIR)
                            && table.getRelative(BlockFace.SOUTH).getType().equals(Material.AIR)) {
    player.getWorld().getBlockAt(table.getLocation()).setType(Material.DIAMOND_BLOCK);
    player.getWorld().getBlockAt(table.getRelative(BlockFace.NORTH).getLocation()).setType(Material.COBBLE_WALL);
    player.getWorld().getBlockAt(table.getRelative(BlockFace.SOUTH).getLocation()).setType(Material.COBBLE_WALL);
    player.getWorld().getBlockAt(table.getRelative(BlockFace.EAST).getLocation()).setType(Material.COBBLE_WALL);
    player.getWorld().getBlockAt(table.getRelative(BlockFace.WEST).getLocation()).setType(Material.COBBLE_WALL);
    player.getWorld().getBlockAt(table.getRelative(BlockFace.UP).getLocation()).setType(Material.ENCHANTMENT_TABLE);
                     
                        player.getWorld().strikeLightningEffect(table.getLocation());
                        player.getWorld().playSound(table.getLocation(), Sound.AMBIENCE_THUNDER, 1, 1);
                     
                        player.sendMessage(GRAY + "«" + RED + "Spellbound"
                                + GRAY + "» " + AQUA
                                + "You've just placed a Spellbook Enchanting Table!");
                        player.playSound(player.getLocation(), Sound.WITHER_DEATH, 1, 1);
                        files.addTable(packageLocation(table.getRelative(BlockFace.UP).getLocation()));
                    } else {
                        event.setCancelled(true);
                        player.sendMessage(GRAY + "«" + RED + "Spellbound"
                                + GRAY + "" + "» " + DARK_RED
                                + "You can't place a Spellbook Enchanting Table there!"
                                + " You need a 3x3x2 area in order for the table to fit!");
                        player.playSound(player.getLocation(), Sound.ANVIL_LAND, 1, 2);
                    }
                }
            }
        }

    Code (Text):
    public void addTable(String location) {

            if (get("Tables") != null) {
                Events.tables.add(location);
                set("Tables", Events.tables);
            } else {
                if (Events.tables.contains(location)) {
                    return;
                } else {
                    Events.tables.add(location);
                    set("Tables", Events.tables);
                }
            }
        }

        public void removeTable(String location) {
            if (get("Tables") != null) {
                if (Events.tables.contains(location)) {
                    Events.tables.remove(location);
                    set("Tables", Events.tables);
                } else {
                    return;
                }
            } else {
                return;
            }
        }

        public boolean isTable(String location) {
            if (Events.tables.isEmpty()) {
                return false;
            } else {
                if (Events.tables.contains(location)) {
                    return true;
                }
            }
            return false;
        }
     

    I'm guessing that the issue lies somewhere with the location or the packaging of the location to the YML file.

    Help is greatly appreciated!
     
  2. Have you tried debugging it?
    Without actually testing it myself I cant find the error(s).
    Does any of it work, for example when you place the enchamnet table do you get the message?
    Are their any erros?
     
  3. Yes, the messages do come up, and there are no errors in the console.
     
  4. You schedule a delayed task, meaning it will only run once (10 ticks after enable).
    Schedule a repeating task instead
     
  5. Ok, can you put some debug lines in the scheduler so that every time it runs it prints out, and if the if stament is false, then put another debug line saying its been triggered, and what the location was
     
  6. Ahh woops.... Thanks for pointing that out! :p