Skip to content

Commit

Permalink
docs: add javadoc for recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Sep 20, 2024
1 parent 3e018d8 commit 09853a3
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static final class Getter {
private final NbtMapBuilder nbtBuilder = NbtMap.builder();
private NbtMapBuilder stateBuilder;

public Getter(String name) {
private Getter(String name) {
if (name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.UUID;

/**
* Represents a crafting recipe.
*
* @author daoge_cmd
*/
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.cloudburstmc.protocol.bedrock.data.inventory.crafting.recipe.RecipeData;

/**
* Represents a furnace recipe.
*
* @author daoge_cmd
*/
public class FurnaceRecipe implements Recipe, TaggedRecipe, IdentifiedRecipe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import org.allaymc.api.utils.Identified;

/**
* <p>
* Recipe with an identifier.
* Represents an identified recipe.
*
* @author daoge_cmd
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.concurrent.atomic.AtomicInteger;

/**
* Represents a network recipe.
*
* @author daoge_cmd
*/
public interface NetworkRecipe extends Recipe {
Expand All @@ -13,6 +15,8 @@ static int assignNetworkId() {
}

/**
* Get the unique network ID of the recipe, assigned using {@link #assignNetworkId()} internally.
*
* @return The unique network ID of the recipe, assigned internally.
*/
int getNetworkId();
Expand Down
21 changes: 18 additions & 3 deletions Allay-API/src/main/java/org/allaymc/api/item/recipe/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,37 @@
import org.cloudburstmc.protocol.bedrock.data.inventory.crafting.recipe.RecipeData;

/**
* Represents a recipe.
*
* @author daoge_cmd
*/
public interface Recipe {
/**
* @param input Input
* Check if the input matches the recipe.
*
* @return Whether the input matches the recipe
* @param input the input to check.
* @return {@code true} if the input matches the recipe, {@code false} otherwise.
*/
boolean match(Input input);

/**
* @return The outputs of this recipe
* Get the outputs of this recipe.
*
* @return the outputs of this recipe.
*/
ItemStack[] getOutputs();

/**
* Get the type of this recipe.
*
* @return the type of this recipe.
*/
CraftingDataType getType();

/**
* Create the network data of this recipe.
*
* @return the network data of this recipe.
*/
RecipeData toNetworkRecipeData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
import static org.allaymc.api.item.type.ItemTypes.AIR;

/**
* Represents a shaped recipe.
*
* @author daoge_cmd
*/
@Getter
public class ShapedRecipe extends CraftingRecipe {

/**
* The character that represents an empty key in the pattern.
*/
public static final char EMPTY_KEY_CHAR = ' ';

protected static final ItemStack[][] EMPTY_ITEM_STACK_ARRAY = new ItemStack[0][0];
Expand Down Expand Up @@ -95,7 +100,7 @@ public boolean match(Input input) {

// @formatter:off
/**
* Remove useless row and column
* Remove useless row and column
*
* @param inputs <pre>
* {@code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static org.allaymc.api.item.type.ItemTypes.AIR;

/**
* Represents a shapeless recipe.
*
* @author daoge_cmd
*/
public class ShapelessRecipe extends CraftingRecipe {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.allaymc.api.item.recipe;

/**
* Represents a tagged recipe.
*
* @author daoge_cmd
*/
public interface TaggedRecipe extends Recipe {
/**
* @return The tag indicating the crafting type applicable to this recipe.
* Get the tag indicating the crafting type applicable to this recipe.
* <p>
* The tag indicating the crafting type applicable to this recipe.
* For example, a crafting table recipe should have the "crafting_table" tag.
*
* @return the tag indicating the crafting type applicable to this recipe.
*/
String getTag();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import java.util.UUID;

/**
* Represents a unique recipe.
*
* @author daoge_cmd
*/
public interface UniqueRecipe extends Recipe {
/**
* @return The UUID of the recipe
* Get the UUID of the recipe.
*
* @return the UUID of the recipe.
*/
UUID getUuid();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import static org.allaymc.api.item.type.ItemTypes.AIR;

/**
* Represents a crafting input.
*
* @author daoge_cmd
*/
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.allaymc.api.item.ItemStack;

/**
* Represents a furnace input.
*
* @author daoge_cmd
*/
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.allaymc.api.item.recipe.input;

/**
* Represents an input for a recipe.
*
* @author daoge_cmd
*/
public interface Input {
Expand Down
2 changes: 2 additions & 0 deletions Allay-API/src/main/java/org/allaymc/api/item/tag/ItemTag.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.allaymc.api.item.tag;

/**
* Represents an item tag.
*
* @author daoge_cmd
*/
public record ItemTag(String name) {
Expand Down
60 changes: 55 additions & 5 deletions Allay-API/src/main/java/org/allaymc/api/item/type/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,89 @@
import java.util.Set;

/**
* Represents a type of item.
*
* @author daoge_cmd
*/
public interface ItemType<T extends ItemStack> extends Identified {
/**
* Create an {@link ItemStack} with the given {@link ItemStackInitInfo}.
*
* @param info The {@link ItemStackInitInfo} to create the {@link ItemStack} with.
* @return The created {@link ItemStack}.
*/
T createItemStack(ItemStackInitInfo info);

/**
* Create an {@link ItemStack} with a count of 1 and a meta of 0.
*
* @return The created {@link ItemStack}.
*/
default T createItemStack() {
return createItemStack(1);
}

/**
* Create an {@link ItemStack} with the given count and a meta of 0.
*
* @param count The count of the {@link ItemStack}.
* @return The created {@link ItemStack}.
*/
default T createItemStack(int count) {
return createItemStack(count, 0);
}

/**
* Create an {@link ItemStack} with the given count and meta.
*
* @param count The count of the {@link ItemStack}.
* @param meta The meta of the {@link ItemStack}.
* @return The created {@link ItemStack}.
*/
default T createItemStack(int count, int meta) {
return createItemStack(SimpleItemStackInitInfo.builder().count(count).meta(meta).build());
}

/**
* Get the runtime ID of the item.
* <p>
* The runtime id of an item is mutable and will change
* between different versions of the game.
*
* @return The runtime ID of the item.
*/
int getRuntimeId();

default Identifier getBlockIdentifier() {
var blockType = getBlockType();
return blockType == null ? null : blockType.getIdentifier();
}

/**
* Get the block type of the item.
*
* @return The block type of the item.
*/
BlockType<?> getBlockType();

/**
* Create the network definition of the item.
*
* @return The network definition of the item.
*/
default ItemDefinition toNetworkDefinition() {
return new SimpleItemDefinition(getIdentifier().toString(), getRuntimeId(), false);
}

/**
* Get the item tags of the item.
*
* @return The item tags of the item.
*/
@Unmodifiable
Set<ItemTag> getItemTags();

/**
* Check if the item has the given item tag.
*
* @param itemTag The item tag to check.
* @return {@code true} if the item has the item tag, {@code false} otherwise.
*/
default boolean hasItemTag(ItemTag itemTag) {
return getItemTags().contains(itemTag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,65 @@
import org.cloudburstmc.nbt.NbtMapBuilder;

/**
* ItemTypeSafeGetter is used to get an item type safely, which means that
* the plugin developers can get an item type without worrying about being broken
* in the next minecraft version compared to using {@link ItemTypes} directly.
*
* @author daoge_cmd
*/
@UtilityClass
public class ItemTypeSafeGetter {

/**
* Get an item type by its name.
*
* @param name The name of the item type.
* @return The {@link Getter} object.
*/
public static Getter name(String name) {
return new Getter(name);
}

/**
* Get an item type by its id.
*
* @param id The id of the item type.
* @return The {@link Getter} object.
*/
public static Getter id(int id) {
return new Getter(id);
}

public static final class Getter {
private final NbtMapBuilder nbtMapBuilder = NbtMap.builder();

public Getter(String name) {
private Getter(String name) {
if (name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
nbtMapBuilder.putString("Name", name);
}

public Getter(int id) {
private Getter(int id) {
nbtMapBuilder.putString("Id", String.valueOf(id));
}

/**
* Set the meta of the item.
*
* @param meta The meta of the item.
* @return The {@link Getter} object.
*/
public Getter meta(int meta) {
nbtMapBuilder.putInt("Damage", meta);
return this;
}

/**
* Try to get the item type.
*
* @return The item type.
*/
public ItemType<?> itemType() {
var updatedNbt = ItemStateUpdaters.updateItemState(nbtMapBuilder.build(), ItemStateUpdaters.LATEST_VERSION);
var itemType = Registries.ITEMS.get(new Identifier(updatedNbt.getString("Name")));
Expand Down

0 comments on commit 09853a3

Please sign in to comment.