Skip to content

Commit

Permalink
First optimization for convertOption method
Browse files Browse the repository at this point in the history
  • Loading branch information
fulminazzo committed Apr 7, 2024
1 parent 733c498 commit 5c83ba4
Showing 1 changed file with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,19 +282,18 @@ public static void spawnEffect(final @NotNull Player player, final @NotNull Part
@SuppressWarnings("unchecked")
static @Nullable Object convertOption(@NotNull Class<?> dataType, @NotNull Object option) {
if (option instanceof AbstractItem) return itemToItemStack((AbstractItem) option);
if (option instanceof Potion) return wPotionToPotion((Potion) option);
if (dataType.isEnum()) return EnumUtils.valueOf(dataType, option.toString());
if (dataType.equals(MaterialData.class)) {
else if (option instanceof Potion) return wPotionToPotion((Potion) option);
else if (dataType.isEnum()) return EnumUtils.valueOf(dataType, option.toString());
else if (dataType.equals(MaterialData.class)) {
if (!(option instanceof Tuple))
throw new IllegalArgumentException(String.format("Expected %s but got %s",
Tuple.class.getSimpleName(), option.getClass().getSimpleName()));
Tuple<String, Integer> tuple = (Tuple<String, Integer>) option;
Material material = EnumUtils.valueOf(Material.class, tuple.getKey());
Integer data = tuple.getValue();
return material.getNewData((byte) (data == null ? 0 : data));
}
if (dataType.getCanonicalName().equalsIgnoreCase("org.bukkit.Vibration")) return option;
if (dataType.getSimpleName().equals("BlockData")) {
} else if (dataType.getCanonicalName().equalsIgnoreCase("org.bukkit.Vibration")) return option;
else if (dataType.getSimpleName().equals("BlockData")) {
String raw = option.toString();
BlockDataOption blockDataOption = new BlockDataOption(raw);
Material material = EnumUtils.valueOf(Material.class, blockDataOption.getMaterial());
Expand All @@ -303,18 +302,20 @@ public static void spawnEffect(final @NotNull Player player, final @NotNull Part
String nbt = blockDataOption.getNBT().trim();
return nbt.isEmpty() ? material.createBlockData() : material.createBlockData(String.format("[%s]", nbt));
}
if (option instanceof Color) return wColorToColor((Color) option);
final Object finalOption;
Constructor<?> constructor = dataType.getDeclaredConstructors()[0];
int size = constructor.getParameterCount();
if (size == 2) {
Tuple<?, ?> t = (Tuple<?, ?>) option;
finalOption = new Refl<>(dataType, prepareParameters(t.getKey(), t.getValue())).getObject();
} else if (size == 3) {
Triple<?, ?, ?> t = (Triple<?, ?, ?>) option;
finalOption = new Refl<>(dataType, prepareParameters(t.getFirst(), t.getSecond(), t.getThird())).getObject();
} else throw new IllegalArgumentException("Cannot create option from constructor: " + constructor);
return finalOption;
else if (option instanceof Color) return wColorToColor((Color) option);
else {
final Object finalOption;
Constructor<?> constructor = dataType.getDeclaredConstructors()[0];
int size = constructor.getParameterCount();
if (size == 2) {
Tuple<?, ?> t = (Tuple<?, ?>) option;
finalOption = new Refl<>(dataType, prepareParameters(t.getKey(), t.getValue())).getObject();
} else if (size == 3) {
Triple<?, ?, ?> t = (Triple<?, ?, ?>) option;
finalOption = new Refl<>(dataType, prepareParameters(t.getFirst(), t.getSecond(), t.getThird())).getObject();
} else throw new IllegalArgumentException("Cannot create option from constructor: " + constructor);
return finalOption;
}
}

private static Object @NotNull [] prepareParameters(final Object @NotNull ... parameters) {
Expand Down

0 comments on commit 5c83ba4

Please sign in to comment.