Beispiel #1
0
 public static void detonateFirework(Location loc, FireworkEffect effects) {
   World world = loc.getWorld();
   Firework firework = (Firework) world.spawnEntity(loc, EntityType.FIREWORK);
   // Credit to codename_B
   Object nms_world = null;
   Object nms_firework = null;
   if (world_getHandle == null) {
     world_getHandle = getMethod(world.getClass(), "getHandle");
     firework_getHandle = getMethod(firework.getClass(), "getHandle");
   }
   try {
     nms_world = world_getHandle.invoke(world, (Object[]) null);
     nms_firework = firework_getHandle.invoke(firework, (Object[]) null);
   } catch (InvocationTargetException e) {
     e.printStackTrace();
   } catch (IllegalAccessException e) {
     e.printStackTrace();
   }
   if (nms_world_broadcastEntityEffect == null) {
     nms_world_broadcastEntityEffect = getMethod(nms_world.getClass(), "broadcastEntityEffect");
   }
   FireworkMeta data = (FireworkMeta) firework.getFireworkMeta();
   data.clearEffects();
   data.setPower(1);
   data.addEffect(effects);
   firework.setFireworkMeta(data);
   try {
     nms_world_broadcastEntityEffect.invoke(nms_world, new Object[] {nms_firework, (byte) 17});
   } catch (IllegalAccessException e) {
     e.printStackTrace();
   } catch (InvocationTargetException e) {
     e.printStackTrace();
   }
   firework.remove();
 }
Beispiel #2
0
 public static void transferFireworkMetaEffects(
     FireworkMeta meta, JsonObject json, boolean meta2json) {
   if (meta2json) {
     if (!meta.hasEffects()) return;
     json.add(FIREWORK_EFFECTS, convertFireworkEffectList(meta.getEffects()));
   } else {
     JsonElement element = json.get(FIREWORK_EFFECTS);
     if (element == null) return;
     meta.clearEffects();
     meta.addEffects(convertFireworkEffectList(element));
   }
 }
 /**
  * Make a packet object
  *
  * @param location Location to play firework effect at
  * @param fireworkEffect FireworkEffect to play
  * @return Packet constructed by the parameters
  */
 private static Object makePacket(Location location, FireworkEffect fireworkEffect) {
   try {
     Firework firework = location.getWorld().spawn(location, Firework.class);
     FireworkMeta data = firework.getFireworkMeta();
     data.clearEffects();
     data.setPower(1);
     data.addEffect(fireworkEffect);
     firework.setFireworkMeta(data);
     Object nmsFirework = ReflectionUtil.getHandle(firework);
     firework.remove();
     return PACKET_PLAY_OUT_ENTITY_STATUS.newInstance(nmsFirework, (byte) 17);
   } catch (Exception e) {
     e.printStackTrace();
   }
   return null;
 }
 /**
  * Play a pretty firework at the location with the FireworkEffect when called
  *
  * @param world
  * @param loc
  * @param fe
  * @throws Exception
  */
 public static void playFirework(World world, Location loc, FireworkEffect fe) throws Exception {
   // Bukkity load (CraftFirework)
   Firework fw = (Firework) world.spawn(loc, Firework.class);
   // the net.minecraft.server.World
   Object nms_world = null;
   Object nms_firework = null;
   /*
    * The reflection part, this gives us access to funky ways of messing
    * around with things
    */
   if (world_getHandle == null) {
     // get the methods of the craftbukkit objects
     world_getHandle = getMethod(world.getClass(), "getHandle");
     firework_getHandle = getMethod(fw.getClass(), "getHandle");
   }
   // invoke with no arguments
   nms_world = world_getHandle.invoke(world, (Object[]) null);
   nms_firework = firework_getHandle.invoke(fw, (Object[]) null);
   // null checks are fast, so having this seperate is ok
   if (nms_world_broadcastEntityEffect == null) {
     // get the method of the nms_world
     nms_world_broadcastEntityEffect = getMethod(nms_world.getClass(), "broadcastEntityEffect");
   }
   /*
    * Now we mess with the metadata, allowing nice clean spawning of a
    * pretty firework (look, pretty lights!)
    */
   // metadata load
   FireworkMeta data = (FireworkMeta) fw.getFireworkMeta();
   // clear existing
   data.clearEffects();
   // power of one
   data.setPower(1);
   // add the effect
   data.addEffect(fe);
   // set the meta
   fw.setFireworkMeta(data);
   /*
    * Finally, we broadcast the entity effect then kill our fireworks
    * object
    */
   // invoke with arguments
   nms_world_broadcastEntityEffect.invoke(nms_world, new Object[] {nms_firework, (byte) 17});
   // remove from the game
   fw.remove();
 }
  @Override
  public void run() {
    if (complete) return;

    // Log.debug((NumberUtil.randomBoolean() ? "tick" : "tock"));
    Rotation rotation = Rotation.get();
    RotationSlot next = rotation.getNext();

    rotation.setRestart();
    if (!rotation.isRestarting()) {
      next.load(true);
    }

    this.next = next;
    String what = getMessage();
    ChatColor colour = ChatColor.GRAY;
    if (next != null) {
      colour = ChatColor.DARK_AQUA;
    }

    try {
      List<User> players = match.getMap().getWinner().getPlayers();
      if (players.size() > 0 && isFullSecond()) { // check for more than 1 player
        User player = players.get(NumberUtil.getRandom(0, players.size() - 1));

        Location location = player.getPlayer().getLocation();
        Firework firework =
            (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
        FireworkMeta meta = firework.getFireworkMeta();
        meta.clearEffects();
        meta.addEffect(getFirework());
        firework.setFireworkMeta(meta);

        try {
          firework.detonate();
        } catch (Exception e) {
          Log.warning(
              "Server isn't running a version of Bukkit which allows the use of Firework.detonate() - resorting to manual detonation.");
          try {
            Object craft = NMSUtil.getClassBukkit("entity.CraftFirework").cast(firework);
            Method method = craft.getClass().getMethod("getHandle");
            method.setAccessible(true);
            Object handle = method.invoke(craft);
            handle.getClass().getField("expectedLifespan").set(handle, 0);
          } catch (Exception e2) {
            e2.printStackTrace();
          }
        }
      }
    } catch (NullPointerException ignored) {

    }

    if (duration <= 0 && next == null) {
      for (User player : User.getUsers()) {
        player.getPlayer().kickPlayer(ChatColor.GREEN + "Server restarting!");
      }

      complete = true;
      Bukkit.getServer().shutdown();
      return;
    }

    if (duration <= 0) {
      broadcast(what + "!");
      match.stop();
      complete = true;
      rotation.cycle();
      return;
    }

    boolean show = false;
    if (getTicks() % 20 == 0) {
      if (getSeconds() % 30 == 0) show = true;
      else if (getSeconds() < 30 && getSeconds() % 15 == 0) show = true;
      else if (getSeconds() < 15 && getSeconds() % 5 == 0) show = true;
      else if (getSeconds() < 5) show = true;
    }

    if (show)
      broadcast(
          what
              + " in "
              + ChatColor.RED
              + getSeconds()
              + " second"
              + (getSeconds() != 1 ? "s" : "")
              + colour
              + "!");
    setTicks(getTicks() - 1);
  }