Hub Particles

Discussion in 'Resources' started by Corndogoz, Apr 18, 2015.

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

    Corndogoz

    ok, so many of you have asked for this, or seen this and want to make it, sometimes on servers, VIPS can open a menu, and select a particle effect to follow you, so i am going to show you how to make this: this is the main class:
    Code:
    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */
    package hubparticles;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    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.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    /**
    *
    * @author plankton
    */
    public class HubParticles extends JavaPlugin implements Listener {
    
      Inventory inv = Bukkit.createInventory(null, 9, "Particles");
    
      Thread doeffect;
    
      public void onEnable() {
      Bukkit.getPluginManager().registerEvents(this, this);
      ItemStack is = new ItemStack(Material.APPLE);
      ItemMeta im = is.getItemMeta();
      im.setDisplayName("largeexplode");
      is.setItemMeta(im);
      inv.setItem(0, is);
      im.setDisplayName("angryVillager");
      is.setItemMeta(im);
      inv.setItem(1, is);
      im.setDisplayName("happyVillager");
      is.setItemMeta(im);
      inv.setItem(2, is);
      im.setDisplayName("instantSpell");
      is.setItemMeta(im);
      inv.setItem(3, is);
      }
    
      public void onDisable() {
    
      }
    
      @EventHandler
      public void onClick(InventoryClickEvent e) {
      if (e.getInventory().equals(inv)) {
      if (!e.getCurrentItem().equals(Material.AIR)) {
      doeffect = new Thread(new DoEffect(Effect.getByName(e.getCurrentItem().getItemMeta().getDisplayName()), (Player) e.getWhoClicked()));
      doeffect.start();
      e.setCancled(true);
      }
      }
      }
    
      @EventHandler
      public void onInteractEvent(PlayerInteractEvent e) {
      if (e.getPlayer().getItemInHand().getType().equals(Material.COMPASS)) {
      e.getPlayer().openInventory(inv);
      }
      }
    
    }
    
    
    then the class DoEffect:
    Code:
    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */
    package hubparticles;
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.bukkit.Effect;
    import org.bukkit.entity.Player;
    
    /**
    *
    * @author plankton
    */
    public class DoEffect implements Runnable {
    
        Effect e;
    
        Player p;
    
        public DoEffect(Effect effect, Player pl) {
            e = effect;
            p = pl;
        }
    
        public void run() {
            for (;;) { //this makes a forever loop
                p.getWorld().playEffect(p.getLocation(), e, 0); //play effect
                try {
                    Thread.sleep(250); //playing the effect over and over causes lag, stopping for a quarter fixes that
                } catch (InterruptedException ex) {
                    Logger.getLogger(DoEffect.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    and their you have it! where it says angryVillager and stuff, you can switch it with this stuff:
    angryVillager bubble cloud crit depthsuspend dripWater dripLava enchantmenttable explode fireworksSpark flame footstep happyVillager heart hugeexplosion iconcrack_* instantSpell largeexplode largesmoke lava magicCrit mobSpell mobSpellAmbient note portal reddust slime smoke snowballpoof snowshovel spell splash suspended tilecrack__ townaura witchMagic
     
    Last edited: Apr 18, 2015
    ChipDev likes this.
  2. Offline

    ChipDev

    You don't really comment your code!
    what is 'for(;;){'?
     
  3. Offline

    Corndogoz

    kk, i will add it in
     
    ChipDev likes this.
  4. Offline

    RingOfStorms

    I feel like this is an incredibly expensive use of Thread creation as starting up new effects will happen on a regular basis. I would recommend using the built in schedulers that use thread pools already created, as well as have built in timers that allow you to not block an entire thread just for the use of one particle effect.
     
  5. Offline

    Corndogoz

    i know, i have thought of that execpt i have never used a built in scheduler, so, anyway, its works fine, so thats good :D
     
  6. Offline

    API_Tutorials

    @Corndogoz
    This is extremely bad practice, especially for Bukkit.

    Code:
    new BukkitRunnable() {
    
    public void run() {
    // play particle
    }
    
    }.runTaskTimer(<plugin>, <initalDelay>, <repeatDelay>);
    this works just as well, and is what Bukkit recommends.
     
  7. Offline

    xTrollxDudex

    Unbounded thread creation for tasks which do not die sounds like a very bad idea on its own
     
    teej107 likes this.
Thread Status:
Not open for further replies.

Share This Page