@SuppressWarnings({"null", "unchecked"}) public static final Collection<? extends Player> getOnlinePlayers() { if (hasCollecionGetOnlinePlayers) { return ImmutableList.copyOf(Bukkit.getOnlinePlayers()); } else { if (getOnlinePlayers == null) { try { getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers"); } catch (final NoSuchMethodException e) { Skript.outdatedError(e); } catch (final SecurityException e) { Skript.exception(e); } } try { final Object o = getOnlinePlayers.invoke(null); if (o instanceof Collection<?>) return ImmutableList.copyOf((Collection<? extends Player>) o); else return Arrays.asList(((Player[]) o).clone()); } catch (final IllegalAccessException e) { Skript.outdatedError(e); } catch (final IllegalArgumentException e) { Skript.outdatedError(e); } catch (final InvocationTargetException e) { Skript.exception(e); } return Collections.emptyList(); } }
@Nullable public E spawn(final Location loc) { assert loc != null; try { final E e = loc.getWorld().spawn(loc, getType()); if (e == null) throw new IllegalArgumentException(); if (baby.isTrue() && e instanceof Ageable) ((Ageable) e).setBaby(); set(e); return e; } catch (final IllegalArgumentException e) { if (Skript.testing()) Skript.error("Can't spawn " + getType().getName()); return null; } }
@Override public boolean init( final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) { switch (matchedPattern) { case 0: breakLevels = ScriptLoader.currentSections.size() + 1; type = EVERYTHING; break; case 1: case 2: breakLevels = matchedPattern == 1 ? 1 : Integer.parseInt(parser.regexes.get(0).group()); type = parser.mark; if (breakLevels > numLevels(type)) { if (numLevels(type) == 0) Skript.error( "can't stop any " + names[type] + " as there are no " + names[type] + " present", ErrorQuality.SEMANTIC_ERROR); else Skript.error( "can't stop " + breakLevels + " " + names[type] + " as there are only " + numLevels(type) + " " + names[type] + " present", ErrorQuality.SEMANTIC_ERROR); return false; } break; case 3: type = parser.mark; breakLevels = numLevels(type); if (breakLevels == 0) { Skript.error( "can't stop any " + names[type] + " as there are no " + names[type] + " present", ErrorQuality.SEMANTIC_ERROR); return false; } break; } return true; }
/** * @param c * @param property * @param type must be plural */ protected static void register( final Class<? extends PropertyCondition<?>> c, final String property, final String type) { Skript.registerCondition( c, "%" + type + "% (is|are) " + property, "%" + type + "% (isn't|is not|aren't|are not) " + property); }
static { Skript.registerExpression( ExprTool.class, Slot.class, ExpressionType.PROPERTY, "[the] (tool|held item) [of %players%]", "%player%'[s] (tool|held item)"); }
static { Skript.registerEffect( EffExit.class, "(exit|stop) [trigger]", "(exit|stop) [(1|a|the|this)] (0¦section|1¦loop|2¦conditional)", "(exit|stop) <\\d+> (0¦section|1¦loop|2¦conditional)s", "(exit|stop) all (0¦section|1¦loop|2¦conditional)s"); }
static { Skript.registerEffect( EffHealth.class, "damage %slots% by %integer%", "damage %livingentities% by %double% [heart[s]]", "heal %livingentities% [by %-double% [heart[s]]]", "repair %slots% [by %-integer%]"); }
@Override public String toString(final Event e, final boolean debug) { if (e == null) return "the " + (getTime() == 1 ? "future " : getTime() == -1 ? "former " : "") + "tool of " + players.toString(e, debug); return Skript.getDebugMessage(getSingle(e)); }
@Override protected EntityData deserialize(final Fields fields) throws StreamCorruptedException, NotSerializableException { final String codeName = fields.getAndRemoveObject("codeName", String.class); if (codeName == null) throw new StreamCorruptedException(); final EntityDataInfo<?> info = getInfo(codeName); if (info == null) throw new StreamCorruptedException("Invalid EntityData code name " + codeName); try { final EntityData<?> d = info.c.newInstance(); d.deserialize(fields); return d; } catch (final InstantiationException e) { Skript.exception(e); } catch (final IllegalAccessException e) { Skript.exception(e); } throw new StreamCorruptedException(); }
@SuppressWarnings("deprecation") @Override public void run() { try { for (final Player p : inviUpdate) p.updateInventory(); } catch ( final NullPointerException e) { // can happen on older CraftBukkit (Tekkit) builds if (Skript.debug()) e.printStackTrace(); } inviUpdate.clear(); }
// return getInfo((Class<? extends EntityData<?>>) d.getClass()).codeName + ":" + // d.serialize(); @SuppressWarnings("null") @Override @Deprecated @Nullable public EntityData deserialize(final String s) { final String[] split = s.split(":", 2); if (split.length != 2) return null; final EntityDataInfo<?> i = getInfo(split[0]); if (i == null) return null; EntityData<?> d; try { d = i.c.newInstance(); } catch (final Exception e) { Skript.exception(e, "Can't create an instance of " + i.c.getCanonicalName()); return null; } if (!d.deserialize(split[1])) return null; return d; }
private static <E extends Entity> EntityData<? super E> getData( final @Nullable Class<E> c, final @Nullable E e) { assert c == null ^ e == null; assert c == null || c.isInterface(); for (final EntityDataInfo<?> info : infos) { if (info.entityClass != Entity.class && (e == null ? info.entityClass.isAssignableFrom(c) : info.entityClass.isInstance(e))) { try { @SuppressWarnings("unchecked") final EntityData<E> d = (EntityData<E>) info.c.newInstance(); if (d.init(c, e)) return d; } catch (final Exception ex) { throw Skript.exception(ex); } } } if (e != null) { return new SimpleEntityData(e); } else { assert c != null; return new SimpleEntityData(c); } }
/** * TODO check all updates and find out which ones are not required * * @author Peter Güttinger */ public abstract class PlayerUtils { private PlayerUtils() {} static final Set<Player> inviUpdate = new HashSet<Player>(); public static final void updateInventory(final @Nullable Player p) { if (p != null) inviUpdate.add(p); } // created when first used static final Task task = new Task(Skript.getInstance(), 1, 1) { @SuppressWarnings("deprecation") @Override public void run() { try { for (final Player p : inviUpdate) p.updateInventory(); } catch ( final NullPointerException e) { // can happen on older CraftBukkit (Tekkit) builds if (Skript.debug()) e.printStackTrace(); } inviUpdate.clear(); } }; private static final boolean hasCollecionGetOnlinePlayers = Skript.methodExists(Bukkit.class, "getOnlinePlayers", new Class[0], Collection.class); @Nullable private static Method getOnlinePlayers = null; @SuppressWarnings({"null", "unchecked"}) public static final Collection<? extends Player> getOnlinePlayers() { if (hasCollecionGetOnlinePlayers) { return ImmutableList.copyOf(Bukkit.getOnlinePlayers()); } else { if (getOnlinePlayers == null) { try { getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers"); } catch (final NoSuchMethodException e) { Skript.outdatedError(e); } catch (final SecurityException e) { Skript.exception(e); } } try { final Object o = getOnlinePlayers.invoke(null); if (o instanceof Collection<?>) return ImmutableList.copyOf((Collection<? extends Player>) o); else return Arrays.asList(((Player[]) o).clone()); } catch (final IllegalAccessException e) { Skript.outdatedError(e); } catch (final IllegalArgumentException e) { Skript.outdatedError(e); } catch (final InvocationTargetException e) { Skript.exception(e); } return Collections.emptyList(); } } public static final boolean canEat(Player p, Material food) { GameMode gm = p.getGameMode(); if (gm == GameMode.CREATIVE || gm == GameMode.SPECTATOR) return false; // Can't eat anything in those gamemodes boolean edible = food.isEdible(); if (!edible) return false; boolean special; switch (food) { case GOLDEN_APPLE: case CHORUS_FRUIT: special = true; break; // $CASES-OMITTED$ default: special = false; } if (p.getFoodLevel() < 20 || special) return true; return false; } }
static { Skript.registerExpression( ExprParseError.class, String.class, ExpressionType.SIMPLE, "[the] [last] [parse] error"); }