Skip to content

Commit

Permalink
Refactored code to use fully qualified name only for Bukkit classes
Browse files Browse the repository at this point in the history
  • Loading branch information
fulminazzo committed Apr 3, 2024
1 parent cbf3055 commit 69d74a1
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import it.angrybear.yagl.items.BukkitItem;
import it.angrybear.yagl.items.Item;
import it.angrybear.yagl.items.fields.ItemFlag;
import it.angrybear.yagl.items.recipes.FurnaceRecipe;
import it.angrybear.yagl.items.recipes.Recipe;
import it.angrybear.yagl.items.recipes.ShapedRecipe;
import it.angrybear.yagl.items.recipes.ShapelessRecipe;
import it.fulminazzo.fulmicollection.objects.Refl;
import it.fulminazzo.jbukkit.BukkitUtils;
import org.bukkit.Material;
import org.bukkit.inventory.FurnaceRecipe;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -41,14 +42,14 @@ void testItemConversion() {

@Test
void testShapedRecipeConversion() {
ShapedRecipe expected = new ShapedRecipe(new ItemStack(Material.STONE));
org.bukkit.inventory.ShapedRecipe expected = new org.bukkit.inventory.ShapedRecipe(new ItemStack(Material.STONE));
expected.shape("ABC", "DEF");
Material[] materials = new Material[]{Material.IRON_INGOT, Material.GOLD_INGOT, Material.REDSTONE,
Material.DIAMOND, Material.EMERALD, Material.INK_SACK};
for (int i = 0; i < materials.length; i++)
expected.setIngredient((char) ('A' + i), new ItemStack(materials[i]).getData());

it.angrybear.yagl.items.recipes.ShapedRecipe recipe = new it.angrybear.yagl.items.recipes.ShapedRecipe("test")
ShapedRecipe recipe = new ShapedRecipe("test")
.setOutput(Item.newItem("STONE")).setShape(2, 3);
for (int i = 0; i < materials.length; i++) recipe.setIngredient(i, BukkitItem.newItem(materials[i]));

Expand All @@ -66,10 +67,10 @@ void testShapedRecipeConversion() {

@Test
void testShapelessRecipeConversion() {
ShapelessRecipe expected = new ShapelessRecipe(new ItemStack(Material.STONE));
org.bukkit.inventory.ShapelessRecipe expected = new org.bukkit.inventory.ShapelessRecipe(new ItemStack(Material.STONE));
expected.addIngredient(new ItemStack(Material.GRASS).getData());

it.angrybear.yagl.items.recipes.ShapelessRecipe recipe = new it.angrybear.yagl.items.recipes.ShapelessRecipe("test")
ShapelessRecipe recipe = new ShapelessRecipe("test")
.setOutput(Item.newItem("STONE")).addIngredient(Item.newItem("GRASS"));

Refl<?> r1 = new Refl<>(expected);
Expand All @@ -81,10 +82,10 @@ void testShapelessRecipeConversion() {

@Test
void testFurnaceRecipeConversion() {
FurnaceRecipe expected = new FurnaceRecipe(new ItemStack(Material.STONE), Material.COAL);
org.bukkit.inventory.FurnaceRecipe expected = new org.bukkit.inventory.FurnaceRecipe(new ItemStack(Material.STONE), Material.COAL);
new Refl<>(expected).setFieldObject("ingredient", new ItemStack(Material.COAL));

it.angrybear.yagl.items.recipes.FurnaceRecipe recipe = new it.angrybear.yagl.items.recipes.FurnaceRecipe("test")
FurnaceRecipe recipe = new FurnaceRecipe("test")
.setOutput(Item.newItem("STONE")).setIngredient(Item.newItem("COAL"))
.setExperience(10).setCookingTime(20);

Expand All @@ -102,11 +103,11 @@ void testResizeRecipe() {
final BukkitItem returnItem = BukkitItem.newItem(Material.REDSTONE_BLOCK);
final int size = 4;

ShapedRecipe expected = new ShapedRecipe(returnItem.create());
org.bukkit.inventory.ShapedRecipe expected = new org.bukkit.inventory.ShapedRecipe(returnItem.create());
expected.shape("AB", "CD");
for (int i = 0; i < size; i++) expected.setIngredient((char) ('A' + i), new ItemStack(craftMaterial).getData());

it.angrybear.yagl.items.recipes.ShapedRecipe recipe = new it.angrybear.yagl.items.recipes.ShapedRecipe(id)
ShapedRecipe recipe = new ShapedRecipe(id)
.setOutput(returnItem).setShape(3, 3);
for (int i = 0; i < 9; i++) recipe.setIngredient(i, BukkitItem.newItem(Material.COBBLESTONE));

Expand All @@ -131,6 +132,6 @@ void testResizeRecipe() {

@Test
void testInvalidRecipeType() {
assertThrowsExactly(IllegalArgumentException.class, () -> ItemAdapter.recipeToMinecraft(mock(it.angrybear.yagl.items.recipes.Recipe.class)));
assertThrowsExactly(IllegalArgumentException.class, () -> ItemAdapter.recipeToMinecraft(mock(Recipe.class)));
}
}
50 changes: 27 additions & 23 deletions item/bukkit/src/main/java/it/angrybear/yagl/ItemAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import it.angrybear.yagl.items.BukkitItem;
import it.angrybear.yagl.items.Item;
import it.angrybear.yagl.items.fields.ItemFlag;
import it.angrybear.yagl.items.recipes.FurnaceRecipe;
import it.angrybear.yagl.items.recipes.Recipe;
import it.angrybear.yagl.items.recipes.ShapedRecipe;
import it.angrybear.yagl.items.recipes.ShapelessRecipe;
import it.angrybear.yagl.utils.EnumUtils;
import it.fulminazzo.fulmicollection.objects.Refl;
import it.fulminazzo.fulmicollection.structures.Tuple;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -110,39 +114,39 @@ private static void invokeNoSuchMethod(final @NotNull Runnable get, final @Nulla
}

/**
* Converts the {@link it.angrybear.yagl.items.recipes.Recipe} to a Minecraft {@link Recipe}.
* Converts the {@link Recipe} to a Minecraft {@link org.bukkit.inventory.Recipe}.
*
* @param recipe the recipe
* @return the recipe
*/
public static Recipe recipeToMinecraft(final @Nullable it.angrybear.yagl.items.recipes.Recipe recipe) {
public static org.bukkit.inventory.Recipe recipeToMinecraft(final @Nullable Recipe recipe) {
if (recipe == null) return null;
Recipe result;
if (recipe instanceof it.angrybear.yagl.items.recipes.ShapedRecipe)
result = recipeToMinecraft((it.angrybear.yagl.items.recipes.ShapedRecipe) recipe);
else if (recipe instanceof it.angrybear.yagl.items.recipes.ShapelessRecipe)
result = recipeToMinecraft((it.angrybear.yagl.items.recipes.ShapelessRecipe) recipe);
else if (recipe instanceof it.angrybear.yagl.items.recipes.FurnaceRecipe)
result = recipeToMinecraft((it.angrybear.yagl.items.recipes.FurnaceRecipe) recipe);
org.bukkit.inventory.Recipe result;
if (recipe instanceof ShapedRecipe)
result = recipeToMinecraft((ShapedRecipe) recipe);
else if (recipe instanceof ShapelessRecipe)
result = recipeToMinecraft((ShapelessRecipe) recipe);
else if (recipe instanceof FurnaceRecipe)
result = recipeToMinecraft((FurnaceRecipe) recipe);
else throw new IllegalArgumentException("Unrecognized recipe type: " + recipe.getClass());
return result;
}

private static Recipe recipeToMinecraft(final @Nullable it.angrybear.yagl.items.recipes.ShapedRecipe recipe) {
private static org.bukkit.inventory.Recipe recipeToMinecraft(final @Nullable ShapedRecipe recipe) {
if (recipe == null) return null;

final ItemStack output = recipe.getOutput().copy(BukkitItem.class).create();

Refl<ShapedRecipe> r;
Refl<org.bukkit.inventory.ShapedRecipe> r;
try {
final Object namespacedKey = WrappersAdapter.getNamespacedKey(ID_KEY, recipe.getId());
r = new Refl<>(ShapedRecipe.class, namespacedKey, output);
r = new Refl<>(org.bukkit.inventory.ShapedRecipe.class, namespacedKey, output);
} catch (IllegalStateException e) {
r = new Refl<>(ShapedRecipe.class, output);
r = new Refl<>(org.bukkit.inventory.ShapedRecipe.class, output);
}

StringBuilder charShape = new StringBuilder();
it.angrybear.yagl.items.recipes.ShapedRecipe.Shape shape = recipe.getShape();
ShapedRecipe.Shape shape = recipe.getShape();
char c = 'A';
for (int i = 0; i < shape.getRows(); i++) {
for (int j = 0; j < shape.getColumns(); j++) {
Expand All @@ -162,41 +166,41 @@ private static Recipe recipeToMinecraft(final @Nullable it.angrybear.yagl.items.
return r.getObject();
}

private static Recipe recipeToMinecraft(final @Nullable it.angrybear.yagl.items.recipes.ShapelessRecipe recipe) {
private static org.bukkit.inventory.Recipe recipeToMinecraft(final @Nullable ShapelessRecipe recipe) {
if (recipe == null) return null;

final List<Object> ingredients = recipe.getIngredients().stream()
.map(ItemAdapter::getItemOrRecipeChoice)
.collect(Collectors.toList());
final ItemStack output = recipe.getOutput().copy(BukkitItem.class).create();

Refl<ShapelessRecipe> r;
Refl<org.bukkit.inventory.ShapelessRecipe> r;
try {
final Object namespacedKey = WrappersAdapter.getNamespacedKey(ID_KEY, recipe.getId());
r = new Refl<>(ShapelessRecipe.class, namespacedKey, output);
r = new Refl<>(org.bukkit.inventory.ShapelessRecipe.class, namespacedKey, output);
} catch (IllegalStateException e) {
r = new Refl<>(ShapelessRecipe.class, output);
r = new Refl<>(org.bukkit.inventory.ShapelessRecipe.class, output);
}

r.setFieldObject("ingredients", ingredients);

return r.getObject();
}

private static Recipe recipeToMinecraft(final @Nullable it.angrybear.yagl.items.recipes.FurnaceRecipe recipe) {
private static org.bukkit.inventory.Recipe recipeToMinecraft(final @Nullable FurnaceRecipe recipe) {
if (recipe == null) return null;

final Object ingredient = getItemOrRecipeChoice(recipe.getIngredients().get(0));
final ItemStack output = recipe.getOutput().copy(BukkitItem.class).create();
final int cookingTime = recipe.getCookingTime();
final float experience = recipe.getExperience();

Refl<FurnaceRecipe> r;
Refl<org.bukkit.inventory.FurnaceRecipe> r;
try {
final Object namespacedKey = WrappersAdapter.getNamespacedKey(ID_KEY, recipe.getId());
r = new Refl<>(FurnaceRecipe.class, namespacedKey, output, Material.STONE, experience, cookingTime);
r = new Refl<>(org.bukkit.inventory.FurnaceRecipe.class, namespacedKey, output, Material.STONE, experience, cookingTime);
} catch (IllegalStateException e) {
r = new Refl<>(FurnaceRecipe.class, output, Material.STONE);
r = new Refl<>(org.bukkit.inventory.FurnaceRecipe.class, output, Material.STONE);
}

r.setFieldObject("ingredient", ingredient);
Expand Down
26 changes: 16 additions & 10 deletions item/bukkit/src/test/java/it/angrybear/yagl/ItemAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import it.angrybear.yagl.items.BukkitItem;
import it.angrybear.yagl.items.Item;
import it.angrybear.yagl.items.fields.ItemFlag;
import it.angrybear.yagl.items.recipes.FurnaceRecipe;
import it.angrybear.yagl.items.recipes.Recipe;
import it.angrybear.yagl.items.recipes.ShapedRecipe;
import it.angrybear.yagl.items.recipes.ShapelessRecipe;
import it.fulminazzo.fulmicollection.objects.Refl;
import it.fulminazzo.jbukkit.BukkitUtils;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -40,15 +45,15 @@ void testItemConversion() {

@Test
void testShapedRecipeConversion() {
ShapedRecipe expected = new ShapedRecipe(new NamespacedKey("yagl", "test"),
org.bukkit.inventory.ShapedRecipe expected = new org.bukkit.inventory.ShapedRecipe(new NamespacedKey("yagl", "test"),
new ItemStack(Material.STONE));
expected.shape("ABC", "DEF");
Material[] materials = new Material[]{Material.IRON_INGOT, Material.GOLD_INGOT, Material.REDSTONE,
Material.DIAMOND, Material.EMERALD, Material.LAPIS_LAZULI};
for (int i = 0; i < materials.length; i++)
expected.setIngredient((char) ('A' + i), new RecipeChoice.ExactChoice(new ItemStack(materials[i])));

it.angrybear.yagl.items.recipes.ShapedRecipe recipe = new it.angrybear.yagl.items.recipes.ShapedRecipe("test")
ShapedRecipe recipe = new ShapedRecipe("test")
.setOutput(Item.newItem("STONE")).setShape(2, 3);
for (int i = 0; i < materials.length; i++) recipe.setIngredient(i, BukkitItem.newItem(materials[i]));

Expand All @@ -66,11 +71,11 @@ void testShapedRecipeConversion() {

@Test
void testShapelessRecipeConversion() {
ShapelessRecipe expected = new ShapelessRecipe(new NamespacedKey("yagl", "test"),
org.bukkit.inventory.ShapelessRecipe expected = new org.bukkit.inventory.ShapelessRecipe(new NamespacedKey("yagl", "test"),
new ItemStack(Material.STONE));
expected.addIngredient(new RecipeChoice.ExactChoice(new ItemStack(Material.GRASS)));

it.angrybear.yagl.items.recipes.ShapelessRecipe recipe = new it.angrybear.yagl.items.recipes.ShapelessRecipe("test")
ShapelessRecipe recipe = new ShapelessRecipe("test")
.setOutput(Item.newItem("STONE")).addIngredient(Item.newItem("GRASS"));

Refl<?> r1 = new Refl<>(expected);
Expand All @@ -82,11 +87,11 @@ void testShapelessRecipeConversion() {

@Test
void testFurnaceRecipeConversion() {
FurnaceRecipe expected = new FurnaceRecipe(new NamespacedKey("yagl", "test"),
org.bukkit.inventory.FurnaceRecipe expected = new org.bukkit.inventory.FurnaceRecipe(new NamespacedKey("yagl", "test"),
new ItemStack(Material.STONE), Material.COAL, 10, 20);
new Refl<>(expected).setFieldObject("ingredient", new RecipeChoice.ExactChoice(new ItemStack(Material.COAL)));

it.angrybear.yagl.items.recipes.FurnaceRecipe recipe = new it.angrybear.yagl.items.recipes.FurnaceRecipe("test")
FurnaceRecipe recipe = new FurnaceRecipe("test")
.setOutput(Item.newItem("STONE")).setIngredient(Item.newItem("COAL"))
.setExperience(10).setCookingTime(20);

Expand All @@ -104,11 +109,12 @@ void testResizeRecipe() {
final BukkitItem returnItem = BukkitItem.newItem(Material.REDSTONE_BLOCK);
final int size = 4;

ShapedRecipe expected = new ShapedRecipe(new NamespacedKey("yagl", id), returnItem.create());
org.bukkit.inventory.ShapedRecipe expected = new org.bukkit.inventory.ShapedRecipe(new NamespacedKey("yagl", id),
returnItem.create());
expected.shape("AB", "CD");
for (int i = 0; i < size; i++) expected.setIngredient((char) ('A' + i), new RecipeChoice.ExactChoice(new ItemStack(craftMaterial)));

it.angrybear.yagl.items.recipes.ShapedRecipe recipe = new it.angrybear.yagl.items.recipes.ShapedRecipe(id)
ShapedRecipe recipe = new ShapedRecipe(id)
.setOutput(returnItem).setShape(3, 3);
for (int i = 0; i < 9; i++) recipe.setIngredient(i, BukkitItem.newItem(Material.COBBLESTONE));

Expand All @@ -133,6 +139,6 @@ void testResizeRecipe() {

@Test
void testInvalidRecipeType() {
assertThrowsExactly(IllegalArgumentException.class, () -> ItemAdapter.recipeToMinecraft(mock(it.angrybear.yagl.items.recipes.Recipe.class)));
assertThrowsExactly(IllegalArgumentException.class, () -> ItemAdapter.recipeToMinecraft(mock(Recipe.class)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

public class LegacyWrappersAdapterTest {

private static it.angrybear.yagl.Color[] getColors() {
return it.angrybear.yagl.Color.values();
private static Color[] getColors() {
return Color.values();
}

private static Particle[] getTestLegacyParticles() {
Expand Down Expand Up @@ -90,7 +90,7 @@ void testPotionConversion() {

@ParameterizedTest
@MethodSource("getColors")
void testColorConversion(it.angrybear.yagl.Color expected) {
void testColorConversion(Color expected) {
org.bukkit.Color color = WrappersAdapter.wColorToColor(expected);
assertEquals(expected, WrappersAdapter.colorToWColor(color));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.angrybear.yagl;

import it.angrybear.yagl.wrappers.Sound;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
Expand All @@ -14,15 +14,14 @@ public class ObsoleteWrappersAdapterTest {

@Test
void testPlaySound() {
it.angrybear.yagl.wrappers.Sound sound = new it.angrybear.yagl.wrappers.Sound(
Sound.AMBIENCE_CAVE.name(),10, 2, "BLOCKS");
Sound sound = new Sound(org.bukkit.Sound.AMBIENCE_CAVE.name(),10, 2, "BLOCKS");
Player player = mock(Player.class);

when(player.getLocation()).thenReturn(new Location(null, 0, 1, 0));

WrappersAdapter.playSound(player, sound);

ArgumentCaptor<Sound> soundArg = ArgumentCaptor.forClass(Sound.class);
ArgumentCaptor<org.bukkit.Sound> soundArg = ArgumentCaptor.forClass(org.bukkit.Sound.class);
ArgumentCaptor<Float> volumeArg = ArgumentCaptor.forClass(Float.class);
ArgumentCaptor<Float> pitchArg = ArgumentCaptor.forClass(Float.class);
verify(player).playSound(any(Location.class), soundArg.capture(), volumeArg.capture(), pitchArg.capture());
Expand Down
Loading

0 comments on commit 69d74a1

Please sign in to comment.