Skip to content

Commit 5c83ba4

Browse files
committed
First optimization for convertOption method
1 parent 733c498 commit 5c83ba4

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

wrappers/bukkit/src/main/java/it/angrybear/yagl/WrappersAdapter.java

+19-18
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,18 @@ public static void spawnEffect(final @NotNull Player player, final @NotNull Part
282282
@SuppressWarnings("unchecked")
283283
static @Nullable Object convertOption(@NotNull Class<?> dataType, @NotNull Object option) {
284284
if (option instanceof AbstractItem) return itemToItemStack((AbstractItem) option);
285-
if (option instanceof Potion) return wPotionToPotion((Potion) option);
286-
if (dataType.isEnum()) return EnumUtils.valueOf(dataType, option.toString());
287-
if (dataType.equals(MaterialData.class)) {
285+
else if (option instanceof Potion) return wPotionToPotion((Potion) option);
286+
else if (dataType.isEnum()) return EnumUtils.valueOf(dataType, option.toString());
287+
else if (dataType.equals(MaterialData.class)) {
288288
if (!(option instanceof Tuple))
289289
throw new IllegalArgumentException(String.format("Expected %s but got %s",
290290
Tuple.class.getSimpleName(), option.getClass().getSimpleName()));
291291
Tuple<String, Integer> tuple = (Tuple<String, Integer>) option;
292292
Material material = EnumUtils.valueOf(Material.class, tuple.getKey());
293293
Integer data = tuple.getValue();
294294
return material.getNewData((byte) (data == null ? 0 : data));
295-
}
296-
if (dataType.getCanonicalName().equalsIgnoreCase("org.bukkit.Vibration")) return option;
297-
if (dataType.getSimpleName().equals("BlockData")) {
295+
} else if (dataType.getCanonicalName().equalsIgnoreCase("org.bukkit.Vibration")) return option;
296+
else if (dataType.getSimpleName().equals("BlockData")) {
298297
String raw = option.toString();
299298
BlockDataOption blockDataOption = new BlockDataOption(raw);
300299
Material material = EnumUtils.valueOf(Material.class, blockDataOption.getMaterial());
@@ -303,18 +302,20 @@ public static void spawnEffect(final @NotNull Player player, final @NotNull Part
303302
String nbt = blockDataOption.getNBT().trim();
304303
return nbt.isEmpty() ? material.createBlockData() : material.createBlockData(String.format("[%s]", nbt));
305304
}
306-
if (option instanceof Color) return wColorToColor((Color) option);
307-
final Object finalOption;
308-
Constructor<?> constructor = dataType.getDeclaredConstructors()[0];
309-
int size = constructor.getParameterCount();
310-
if (size == 2) {
311-
Tuple<?, ?> t = (Tuple<?, ?>) option;
312-
finalOption = new Refl<>(dataType, prepareParameters(t.getKey(), t.getValue())).getObject();
313-
} else if (size == 3) {
314-
Triple<?, ?, ?> t = (Triple<?, ?, ?>) option;
315-
finalOption = new Refl<>(dataType, prepareParameters(t.getFirst(), t.getSecond(), t.getThird())).getObject();
316-
} else throw new IllegalArgumentException("Cannot create option from constructor: " + constructor);
317-
return finalOption;
305+
else if (option instanceof Color) return wColorToColor((Color) option);
306+
else {
307+
final Object finalOption;
308+
Constructor<?> constructor = dataType.getDeclaredConstructors()[0];
309+
int size = constructor.getParameterCount();
310+
if (size == 2) {
311+
Tuple<?, ?> t = (Tuple<?, ?>) option;
312+
finalOption = new Refl<>(dataType, prepareParameters(t.getKey(), t.getValue())).getObject();
313+
} else if (size == 3) {
314+
Triple<?, ?, ?> t = (Triple<?, ?, ?>) option;
315+
finalOption = new Refl<>(dataType, prepareParameters(t.getFirst(), t.getSecond(), t.getThird())).getObject();
316+
} else throw new IllegalArgumentException("Cannot create option from constructor: " + constructor);
317+
return finalOption;
318+
}
318319
}
319320

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

0 commit comments

Comments
 (0)