Solved 1.7 to 1.8

Discussion in 'Plugin Help/Development/Requests' started by pjr8, Jun 22, 2015.

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

    pjr8

    Code:
    package me.pjr8;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    
    public enum ParticleEffect
    {
      HUGE_EXPLOSION("hugeexplosion"),  LARGE_EXPLODE("largeexplode"),  FIREWORKS_SPARK(
        "fireworksSpark"),  BUBBLE("bubble"),  SUSPEND("suspend"),  DEPTH_SUSPEND(
        "depthSuspend"),  TOWN_AURA("townaura"),  CRIT("crit"),  MAGIC_CRIT(
        "magicCrit"),  SMOKE("smoke"),  MOB_SPELL("mobSpell"),  MOB_SPELL_AMBIENT(
        "mobSpellAmbient"),  SPELL("spell"),  INSTANT_SPELL("instantSpell"),  WITCH_MAGIC(
        "witchMagic"),  NOTE("note"),  PORTAL("portal"),  ENCHANTMENT_TABLE(
        "enchantmenttable"),  EXPLODE("explode"),  FLAME("flame"),  LAVA(
        "lava"),  FOOTSTEP("footstep"),  SPLASH("splash"),  WAKE("wake"),  LARGE_SMOKE(
        "largesmoke"),  CLOUD("cloud"),  RED_DUST("reddust"),  SNOWBALL_POOF(
        "snowballpoof"),  DRIP_WATER("dripWater"),  DRIP_LAVA("dripLava"),  SNOW_SHOVEL(
        "snowshovel"),  SLIME("slime"),  HEART("heart"),  ANGRY_VILLAGER(
        "angryVillager"),  HAPPY_VILLAGER("happyVillager");
    
      private static final Map<String, ParticleEffect> NAME_MAP;
      private static Constructor<?> packetPlayOutWorldParticles;
      private static Method getHandle;
      private static Field playerConnection;
      private static Method sendPacket;
      private final String name;
    
      static
      {
        NAME_MAP = new HashMap();
        ParticleEffect[] arrayOfParticleEffect;
        int j = (arrayOfParticleEffect = values()).length;
        for (int i = 0; i < j; i++)
        {
          ParticleEffect p = arrayOfParticleEffect[i];
          NAME_MAP.put(p.name, p);
        }
        try
        {
          packetPlayOutWorldParticles = ReflectionHandler.getConstructor(
            ReflectionHandler.PacketType.PLAY_OUT_WORLD_PARTICLES
            .getPacket(), new Class[] { String.class,
            Float.TYPE, Float.TYPE, Float.TYPE, Float.TYPE,
            Float.TYPE, Float.TYPE, Float.TYPE, Integer.TYPE });
          getHandle = ReflectionHandler.getMethod("CraftPlayer",
            ReflectionHandler.SubPackageType.ENTITY, "getHandle",
            new Class[0]);
          playerConnection = ReflectionHandler.getField("EntityPlayer",
            ReflectionHandler.PackageType.MINECRAFT_SERVER,
            "playerConnection");
          sendPacket = ReflectionHandler.getMethod(
            playerConnection.getType(), "sendPacket",
            new Class[] {
            ReflectionHandler.getClass("Packet", ReflectionHandler.PackageType.MINECRAFT_SERVER) });
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    
      private ParticleEffect(String name)
      {
        this.name = name;
      }
    
      public String getName()
      {
        return this.name;
      }
    
      public static ParticleEffect fromName(String name)
      {
        if (name != null) {
          for (Map.Entry e : NAME_MAP.entrySet()) {
            if (((String)e.getKey()).equalsIgnoreCase(name)) {
              return (ParticleEffect)e.getValue();
            }
          }
        }
        return null;
      }
    
      private static List<Player> getPlayers(Location center, double range)
      {
        List<Player> players = new ArrayList();
        String name = center.getWorld().getName();
        double squared = range * range;
        Player[] arrayOfPlayer;
        int j = (arrayOfPlayer = Bukkit.getOnlinePlayers()).length;
        for (int i = 0; i < j; i++)
        {
          Player p = arrayOfPlayer[i];
          if ((p.getWorld().getName().equals(name)) &&
            (p.getLocation().distanceSquared(center) <= squared)) {
            players.add(p);
          }
        }
        return players;
      }
    
      private static Object instantiatePacket(String name, Location center, float offsetX, float offsetY, float offsetZ, float speed, int amount)
      {
        if (amount < 1) {
          throw new PacketInstantiationException(
            "Amount cannot be lower than 1");
        }
        try
        {
          return packetPlayOutWorldParticles.newInstance(new Object[] { name,
            Float.valueOf((float)center.getX()),
            Float.valueOf((float)center.getY()),
            Float.valueOf((float)center.getZ()),
            Float.valueOf(offsetX), Float.valueOf(offsetY),
            Float.valueOf(offsetZ), Float.valueOf(speed),
            Integer.valueOf(amount) });
        }
        catch (Exception e)
        {
          throw new PacketInstantiationException(
            "Packet instantiation failed", e);
        }
      }
    
      private static Object instantiateIconCrackPacket(int id, Location center, float offsetX, float offsetY, float offsetZ, float speed, int amount)
      {
        return instantiatePacket("iconcrack_" + id, center, offsetX, offsetY,
          offsetZ, speed, amount);
      }
    
      private static Object instantiateBlockCrackPacket(int id, byte data, Location center, float offsetX, float offsetY, float offsetZ, int amount)
      {
        return instantiatePacket("blockcrack_" + id + "_" + data, center,
          offsetX, offsetY, offsetZ, 0.0F, amount);
      }
    
      private static Object instantiateBlockDustPacket(int id, byte data, Location center, float offsetX, float offsetY, float offsetZ, float speed, int amount)
      {
        return instantiatePacket("blockdust_" + id + "_" + data, center,
          offsetX, offsetY, offsetZ, speed, amount);
      }
    
      private static void sendPacket(Player p, Object packet)
      {
        try
        {
          sendPacket.invoke(
            playerConnection.get(getHandle.invoke(p, new Object[0])),
            new Object[] { packet });
        }
        catch (Exception e)
        {
          throw new PacketSendingException(
            "Failed to send a packet to player '" + p.getName() + "'",
            e);
        }
      }
    
      private static void sendPacket(Collection<Player> players, Object packet)
      {
        for (Player p : players) {
          sendPacket(p, packet);
        }
      }
    
      public void display(Location center, float offsetX, float offsetY, float offsetZ, float speed, int amount, Player[] players)
      {
        sendPacket(
          Arrays.asList(players),
          instantiatePacket(this.name, center, offsetX, offsetY, offsetZ,
          speed, amount));
      }
    
      public void display(Location center, double range, float offsetX, float offsetY, float offsetZ, float speed, int amount)
      {
        if (range > 16.0D) {
          throw new IllegalArgumentException(
            "Range cannot exceed the maximum value of 16");
        }
        sendPacket(
          getPlayers(center, range),
          instantiatePacket(this.name, center, offsetX, offsetY, offsetZ,
          speed, amount));
      }
    
      public void display(Location center, float offsetX, float offsetY, float offsetZ, float speed, int amount)
      {
        display(center, 16.0D, offsetX, offsetY, offsetZ, speed, amount);
      }
    
      public static void displayIconCrack(Location center, int id, float offsetX, float offsetY, float offsetZ, float speed, int amount, Player[] players)
      {
        sendPacket(
          Arrays.asList(players),
          instantiateIconCrackPacket(id, center, offsetX, offsetY,
          offsetZ, speed, amount));
      }
    
      public static void displayIconCrack(Location center, double range, int id, float offsetX, float offsetY, float offsetZ, float speed, int amount)
      {
        if (range > 16.0D) {
          throw new IllegalArgumentException(
            "Range has to be lower/equal the maximum of 16");
        }
        sendPacket(
          getPlayers(center, range),
          instantiateIconCrackPacket(id, center, offsetX, offsetY,
          offsetZ, speed, amount));
      }
    
      public static void displayIconCrack(Location center, int id, float offsetX, float offsetY, float offsetZ, float speed, int amount)
      {
        displayIconCrack(center, 16.0D, id, offsetX, offsetY, offsetZ, speed,
          amount);
      }
    
      public static void displayBlockCrack(Location center, int id, byte data, float offsetX, float offsetY, float offsetZ, int amount, Player[] players)
      {
        sendPacket(
          Arrays.asList(players),
          instantiateBlockCrackPacket(id, data, center, offsetX, offsetY,
          offsetZ, amount));
      }
    
      public static void displayBlockCrack(Location center, double range, int id, byte data, float offsetX, float offsetY, float offsetZ, int amount)
      {
        if (range > 16.0D) {
          throw new IllegalArgumentException(
            "Range has to be lower/equal the maximum of 16");
        }
        sendPacket(
          getPlayers(center, range),
          instantiateBlockCrackPacket(id, data, center, offsetX, offsetY,
          offsetZ, amount));
      }
    
      public static void displayBlockCrack(Location center, int id, byte data, float offsetX, float offsetY, float offsetZ, int amount)
      {
        displayBlockCrack(center, 16.0D, id, data, offsetX, offsetY, offsetZ,
          amount);
      }
    
      public static void displayBlockDust(Location center, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int amount, Player[] players)
      {
        sendPacket(
          Arrays.asList(players),
          instantiateBlockDustPacket(id, data, center, offsetX, offsetY,
          offsetZ, speed, amount));
      }
    
      public static void displayBlockDust(Location center, double range, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int amount)
      {
        if (range > 16.0D) {
          throw new IllegalArgumentException(
            "Range has to be lower/equal the maximum of 16");
        }
        sendPacket(
          getPlayers(center, range),
          instantiateBlockDustPacket(id, data, center, offsetX, offsetY,
          offsetZ, speed, amount));
      }
    
      public static void displayBlockDust(Location center, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int amount)
      {
        displayBlockDust(center, 16.0D, id, data, offsetX, offsetY, offsetZ,
          speed, amount);
      }
    
      private static final class PacketInstantiationException
        extends RuntimeException
      {
        private static final long serialVersionUID = 3203085387160737484L;
     
        public PacketInstantiationException(String message) {}
     
        public PacketInstantiationException(String message, Throwable cause) {}
      }
    
      private static final class PacketSendingException
        extends RuntimeException
      {
        private static final long serialVersionUID = 3203085387160737484L;
     
        public PacketSendingException(String message, Throwable cause) {}
      }
    }
    
    Ok so that is my code, within my plugin and the error place is this:
    Code:
      private static List<Player> getPlayers(Location center, double range)
      {
        List<Player> players = new ArrayList();
        String name = center.getWorld().getName();
        double squared = range * range;
        Player[] arrayOfPlayer;
        int j = (arrayOfPlayer = Bukkit.getOnlinePlayers()).length;
        for (int i = 0; i < j; i++)
        {
          Player p = arrayOfPlayer[i];
          if ((p.getWorld().getName().equals(name)) &&
            (p.getLocation().distanceSquared(center) <= squared)) {
            players.add(p);
          }
        }
        return players;
      }
    It says "
    Type mismatch: cannot convert from Collection<capture#6-of ? extends Player> to Player[]" I have no idea what to do. If someone could put that as a new code and set it up here thanks.
     
    Last edited: Jun 22, 2015
  2. Offline

    meguy26

    @pjr8
    Ok wow. This is a very simple error, that is very simple to fix, if I am reading it right.

    Bukkit.getOnlinePlayers() no longer returns a Player[], instead it returns a List<Player>,
    so it would be:
    Code:
    List<Player> arrayOfPlayer = Bukkit.getOnlinePlayers();
    Full fixed code:
    (please make an effort to fix your own code and also learn some java, I mean dude, it's a frikin type mismatch error!
    Code (open)

    Code:
    private static List<Player> getPlayers(Location center, double range)
      {
        List<Player> players = new ArrayList<Player>(); //added parameter
        String name = center.getWorld().getName();
        double squared = range * range;
        //removed player list stuff, unnecessary due to advanced for loop
        for (Player p : Bukkit.getOnlinePlayers()) //changed to special for loop
        {
         //removed player variable, assigned in for loop
          if ((p.getWorld().getName().equals(name)) &&
            (p.getLocation().distanceSquared(center) <= squared)) {
            players.add(p);
          }
        }
        return players;
      }
     
  3. Offline

    pjr8

    EDIT: I did not see the thing to see the code.
     
    Last edited: Jun 23, 2015
  4. Offline

    timtower Moderator Moderator

    Moved to Bukkit alternatives
     
Thread Status:
Not open for further replies.

Share This Page