/** * Creates a new {@link Tuple} with the desired {@code first} and * {@code second} objects. * * @param first The first object * @param second The second object * @param <K> The type of first object * @param <V> The type of second object * @return The new Tuple */ public static <K, V> Tuple<K, V> of(K first, V second) { return new Tuple<>(first, second); }
@Override public final double getOriginalModifierDamage(DamageModifier damageModifier) { checkArgument(damageModifier != null, "The damage modifier cannot be null!"); for (Tuple<DamageModifier, Double> tuple : this.originalModifiers) { if (tuple.getFirst().equals(damageModifier)) { return tuple.getSecond(); } } throw new IllegalArgumentException("The provided damage modifier is not applicable: " + damageModifier.toString()); }
public final List<Tuple<T, Function<? super Double, Double>>> getModifiers() { ImmutableList.Builder<Tuple<T, Function<? super Double, Double>>> builder = ImmutableList.builder(); for (Tuple<T, Function<? super Double, Double>> entry : this.modifierFunctions) { builder.add(new Tuple<>(entry.getFirst(), entry.getSecond())); } return builder.build(); } }
@Override public void addModifierAfter(DamageModifier damageModifier, Function<? super Double, Double> function, Set<DamageModifierType> after) { checkNotNull(damageModifier, "Damage modifier was null!"); checkNotNull(function, "Function was null!"); int indexToAddAfter = -1; int index = 0; for (Tuple<DamageModifier, Function<? super Double, Double>> tuple : this.modifierFunctions) { checkArgument(!tuple.getFirst().equals(damageModifier), "Cannot add a duplicate modifier!"); if (after.contains(tuple.getFirst().getType())) { indexToAddAfter = index; } index++; } if (indexToAddAfter == -1) { this.modifierFunctions.add(new Tuple<>(damageModifier, function)); } else { this.modifierFunctions.add(indexToAddAfter + 1, new Tuple<>(damageModifier, function)); } this.recalculateDamages(this.baseDamage); }
protected double getFinalAmount(double baseAmount) { double damage = baseAmount; for (Tuple<T, Function<? super Double, Double>> entry : this.modifierFunctions) { damage += checkNotNull(entry.getSecond().apply(damage)); } return damage; }
private void testKeysAreNotParents(String bundle) throws Exception { // Get the resource ResourceBundle rb = ResourceBundle.getBundle(bundle, Locale.getDefault()); Enumeration<String> keys = rb.getKeys(); Set<String> s = new HashSet<>(); while (keys.hasMoreElements()) { s.add(keys.nextElement()); } Map<String, List<String>> filter = s.parallelStream() .map(x -> Tuple.of(x.toLowerCase(), s.stream().filter(y -> x.toLowerCase().startsWith(y.toLowerCase() + ".") && !x.equalsIgnoreCase(y)).collect(Collectors.toList()))) .filter(x -> !x.getSecond().isEmpty()) .sorted(Comparator.comparing(Tuple::getFirst)) .collect(Collectors.toMap(Tuple::getFirst, Tuple::getSecond)); if (!filter.isEmpty()) { StringBuilder sb = new StringBuilder("Some keys are parents of others!").append(System.lineSeparator()); filter.forEach((x, y) -> sb.append(x).append("->").append(y).append(System.lineSeparator())); Assert.fail(sb.toString()); } }
/** * Trigger an event WebHook for the specified event. * @param clazz The class of event for which the WebHooks are triggered. * @param data The data that is sent to the endpoints. */ public void notifyHooks(Class<? extends Event> clazz, Object data) { Timings.WEBHOOK_NOTIFY.startTimingIfSync(); List<WebHook> notifyHooks = new ArrayList<>(customHooks.get(clazz).getFirst()); for (WebHook hook : notifyHooks) { notifyHook(hook, WebHookType.CUSTOM_EVENT, null, data); } Timings.WEBHOOK_NOTIFY.stopTimingIfSync(); }
protected ImmutableList<Tuple<T, Function<? super Double, Double>>> init(double originalValue, List<Tuple<T, Function<? super Double, Double>>> originalFunctions) { final ImmutableList.Builder<Tuple<T, Double>> modifierMapBuilder = ImmutableList.builder(); final ImmutableList.Builder<Tuple<T, Function<? super Double, Double>>> functionListBuilder = ImmutableList.builder(); final ImmutableMap.Builder<T, Double> mapBuilder = ImmutableMap.builder(); double finalDamage = originalValue; for (Tuple<T, Function<? super Double, Double>> tuple : originalFunctions) { this.modifierFunctions.add(new Tuple<>(tuple.getFirst(), tuple.getSecond())); double tempDamage = checkNotNull(tuple.getSecond().apply(finalDamage)); finalDamage += tempDamage; modifierMapBuilder.add(new Tuple<>(tuple.getFirst(), tempDamage)); mapBuilder.put(tuple.getFirst(), tempDamage); this.modifiers.put(tuple.getFirst(), tempDamage); functionListBuilder.add(tuple); } this.originalFinalAmount = finalDamage; this.originalModifiers = modifierMapBuilder.build(); this.originalModifierMap = mapBuilder.build(); return functionListBuilder.build(); }
@Override public void addDamageModifierAfter(DamageModifier damageModifier, Function<? super Double, Double> function, Set<DamageModifierType> after) { checkNotNull(damageModifier, "Damage modifier was null!"); checkNotNull(function, "Function was null!"); int indexToAddAfter = -1; int index = 0; for (Tuple<DamageModifier, Function<? super Double, Double>> tuple : this.modifierFunctions) { checkArgument(!tuple.getFirst().equals(damageModifier), "Cannot add a duplicate modifier!"); if (after.contains(tuple.getFirst().getType())) { indexToAddAfter = index; } index++; } if (indexToAddAfter == -1) { this.modifierFunctions.add(new Tuple<>(damageModifier, function)); } else { this.modifierFunctions.add(indexToAddAfter + 1, new Tuple<>(damageModifier, function)); } this.recalculateDamages(this.baseDamage); }
@Override public final double getOriginalModifierDamage(DamageModifier damageModifier) { checkArgument(damageModifier != null, "The damage modifier cannot be null!"); for (Tuple<DamageModifier, Double> tuple : this.originalModifiers) { if (tuple.getFirst().equals(damageModifier)) { return tuple.getSecond(); } } throw new IllegalArgumentException("The provided damage modifier is not applicable: " + damageModifier.toString()); }
@JsonIgnore @ApiModelProperty(hidden = true) public Tuple<Class[], Object[]> getParsedParameters() { Class[] paramTypes = new Class[parameters.size()]; Object[] paramValues = new Object[parameters.size()]; try { for (int i = 0; i < parameters.size(); i++) { Tuple<Class, Object> tup = parameters.get(i).toObject(); paramTypes[i] = tup.getFirst(); paramValues[i] = tup.getSecond(); } } catch (ClassNotFoundException e) { throw new NotFoundException("Class could not be found: " + e.getMessage()); } catch (NoSuchFieldException e) { throw new NotFoundException("Field could not be found: " + e.getMessage()); } catch (IllegalAccessException e) { throw new ForbiddenException("Access not allowed: " + e.getMessage()); } return new Tuple<>(paramTypes, paramValues); } }
@Override public void addDamageModifierBefore(DamageModifier damageModifier, Function<? super Double, Double> function, Set<DamageModifierType> before) { checkNotNull(damageModifier, "Damage modifier was null!"); checkNotNull(function, "Function was null!"); int indexToAddBefore = -1; int index = 0; for (Tuple<DamageModifier, Function<? super Double, Double>> tuple : this.modifierFunctions) { checkArgument(!tuple.getFirst().equals(damageModifier), "Cannot add a duplicate modifier!"); if (before.contains(tuple.getFirst().getType())) { indexToAddBefore = index; } index++; } if (indexToAddBefore == -1) { this.modifierFunctions.add(new Tuple<>(damageModifier, function)); } else { this.modifierFunctions.add(indexToAddBefore, new Tuple<>(damageModifier, function)); } this.recalculateDamages(this.baseDamage); }
/** * Creates a new {@link BiConsumer} that takes the provided * {@link EnchantmentType} and applies it to the generated {@link ItemStack}. * The enchantmentType level is defined by the variable amount provided. * * @param level The variance in enchantmentType level * @param enchantmentType The enchantmentType to add * @return The new biconsumer to apply to an itemstack builder */ public static BiConsumer<ItemStack.Builder, Random> enchantment(VariableAmount level, EnchantmentType enchantmentType) { checkNotNull(level, "VariableAmount cannot be null!"); checkNotNull(enchantmentType, "EnchantmentType cannot be null!"); return enchantments(fixed(1), ImmutableList.of(new Tuple<>(enchantmentType, level))); }
/** * Creates a new {@link BiConsumer} that takes the provided * {@link Collection} of coupled {@link EnchantmentType} and * {@link VariableAmount} to apply varying enchantments of varying amounts * to the generated {@link ItemStack}. * * @param amount The varying amount of enchantments * @param enchantments The collection of enchantment tuples combining the * enchantment and the variable amount of level to apply * @return The new biconsumer to apply to an itemstack builder */ public static BiConsumer<ItemStack.Builder, Random> enchantments(VariableAmount amount, Collection<Tuple<EnchantmentType, VariableAmount>> enchantments) { checkNotNull(amount, "VariableAmount cannot be null!"); final WeightedTable<Function<Random, Enchantment>> suppliers = new WeightedTable<>(amount); for (Tuple<EnchantmentType, VariableAmount> enchantment : enchantments) { suppliers.add(random -> Enchantment.builder().type(enchantment.getFirst()).level(enchantment.getSecond().getFlooredAmount(random)).build(), 1); } return listValueSuppliers(Keys.ITEM_ENCHANTMENTS, suppliers); }
@Override public void addDamageModifierBefore(DamageModifier damageModifier, Function<? super Double, Double> function, Set<DamageModifierType> before) { checkNotNull(damageModifier, "Damage modifier was null!"); checkNotNull(function, "Function was null!"); int indexToAddBefore = -1; int index = 0; for (Tuple<DamageModifier, Function<? super Double, Double>> tuple : this.modifierFunctions) { checkArgument(!tuple.getFirst().equals(damageModifier), "Cannot add a duplicate modifier!"); if (before.contains(tuple.getFirst().getType())) { indexToAddBefore = index; } index++; } if (indexToAddBefore == -1) { this.modifierFunctions.add(new Tuple<>(damageModifier, function)); } else { this.modifierFunctions.add(indexToAddBefore, new Tuple<>(damageModifier, function)); } this.recalculateDamages(this.baseDamage); }
/** * Creates a new {@link BiConsumer} that takes the provided * {@link Collection} of {@link EnchantmentType}s and applies a varying amount * of generated enchantments to the generated {@link ItemStack}. * * @param amount The varying amount of enchantments to use * @param itemEnchantmentTypes The enchantment pool to use * @return The new biconsumer to apply to an itemstack builder */ public static BiConsumer<ItemStack.Builder, Random> enchantmentsWithVanillaLevelVariance(VariableAmount amount, Collection<EnchantmentType> itemEnchantmentTypes) { checkNotNull(amount, "Variable amount cannot be null!"); checkNotNull(itemEnchantmentTypes, "EnchantmentType collection cannot be null!"); List<Tuple<EnchantmentType, VariableAmount>> list = itemEnchantmentTypes.stream() .map(enchantment -> { checkNotNull(enchantment, "EnchantmentType cannot be null!"); final int minimum = enchantment.getMinimumLevel(); final int maximum = enchantment.getMaximumLevel(); return new Tuple<>(enchantment, baseWithRandomAddition(minimum, maximum - minimum)); }) .collect(Collectors.toList()); return enchantments(amount, list); }
@Override public final void setOutputDamage(DamageModifier damageModifier, Function<? super Double, Double> function) { checkNotNull(damageModifier, "Damage modifier was null!"); checkNotNull(function, "Function was null!"); int indexToAddTo = 0; boolean addAtEnd = true; for (Iterator<Tuple<DamageModifier, Function<? super Double, Double>>> iterator = this.modifierFunctions.iterator(); iterator.hasNext(); ) { Tuple<DamageModifier, Function<? super Double, Double>> tuple = iterator.next(); if (tuple.getFirst().equals(damageModifier)) { iterator.remove(); addAtEnd = false; break; } indexToAddTo++; } if (addAtEnd) { this.modifierFunctions.add(new Tuple<>(damageModifier, function)); } else { this.modifierFunctions.add(indexToAddTo, new Tuple<>(damageModifier, function)); } this.recalculateDamages(this.baseDamage); }