Skip to content

Commit

Permalink
Add IdAwareObject and use for a new recipe helper utility
Browse files Browse the repository at this point in the history
IdAwareObject just represents an object that can return its own ID, similarly to RegistryObject. Simplifies some utilities as they can automatically work with ItemObject and FluidObject at no extra cost
  • Loading branch information
KnightMiner committed Mar 14, 2024
1 parent b2f8009 commit c44fa63
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
35 changes: 35 additions & 0 deletions src/main/java/slimeknights/mantle/recipe/data/IRecipeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraftforge.common.crafting.conditions.TagEmptyCondition;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.ApiStatus;
import slimeknights.mantle.registration.object.IdAwareObject;
import slimeknights.mantle.util.IdExtender.LocationExtender;

import java.util.Objects;
Expand Down Expand Up @@ -107,6 +108,40 @@ default ResourceLocation suffix(RegistryObject<?> location, String suffix) {
}


/* Other named object location helpers */

/**
* Wraps the registry object ID in the given prefix and suffix
* @param location Object to use for location
* @param prefix Path prefix
* @param suffix Path suffix
* @return Location with the given prefix and suffix
*/
default ResourceLocation wrap(IdAwareObject location, String prefix, String suffix) {
return wrap(location.getId(), prefix, suffix);
}

/**
* Prefixes the registry object ID
* @param location Object to use for location
* @param prefix Path prefix
* @return Location with the given prefix
*/
default ResourceLocation prefix(IdAwareObject location, String prefix) {
return prefix(location.getId(), prefix);
}

/**
* Suffixes the registry object ID
* @param location Object to use for location
* @param suffix Path suffix
* @return Location with the given suffix
*/
default ResourceLocation suffix(IdAwareObject location, String suffix) {
return suffix(location.getId(), suffix);
}


/* Tags and conditions */

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @see FlowingFluidObject
*/
@SuppressWarnings("WeakerAccess")
public class FluidObject<F extends Fluid> implements Supplier<F>, ItemLike {
public class FluidObject<F extends Fluid> implements Supplier<F>, ItemLike, IdAwareObject {
/** Fluid name, used for tag creation */
@Getter @Nonnull
protected final ResourceLocation id;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package slimeknights.mantle.registration.object;

import net.minecraft.resources.ResourceLocation;

/** Interface for an object that holds its own name, used to simplify some utilities */
public interface IdAwareObject {
/** Gets the ID for this object */
ResourceLocation getId();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slimeknights.mantle.registration.object;

import lombok.AllArgsConstructor;
import lombok.Getter;
import net.minecraft.core.DefaultedRegistry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
Expand All @@ -18,19 +19,20 @@
*/
@SuppressWarnings({"unused", "WeakerAccess"})
@AllArgsConstructor
public class ItemObject<I extends ItemLike> implements Supplier<I>, ItemLike {
public class ItemObject<I extends ItemLike> implements Supplier<I>, ItemLike, IdAwareObject {
/** Supplier to the registry entry */
private final Supplier<? extends I> entry;
/** Supplier to the registry name for this entry, allows fetching the name before the entry resolves if registry object is used */
private final ResourceLocation name;
/** Registry name for this entry, allows fetching the name before the entry resolves if registry object is used */
@Getter
private final ResourceLocation id;

/**
* Creates a new item object from a supplier instance. Registry name will be fetched from the supplier entry, so the entry must be present during construction
* @param entry Existing registry entry, typically a vanilla block or a registered block
*/
public ItemObject(DefaultedRegistry<I> registry, I entry) {
this.entry = RegistryHelper.getHolder(registry, entry);
this.name = registry.getKey(entry);
this.id = registry.getKey(entry);
}

/**
Expand All @@ -39,7 +41,7 @@ public ItemObject(DefaultedRegistry<I> registry, I entry) {
*/
public ItemObject(RegistryObject<? extends I> object) {
this.entry = object;
this.name = object.getId();
this.id = object.getId();
}

/**
Expand All @@ -48,7 +50,7 @@ public ItemObject(RegistryObject<? extends I> object) {
*/
protected ItemObject(ItemObject<? extends I> object) {
this.entry = object.entry;
this.name = object.name;
this.id = object.id;
}

/**
Expand All @@ -58,7 +60,7 @@ protected ItemObject(ItemObject<? extends I> object) {
*/
@Override
public I get() {
return Objects.requireNonNull(entry.get(), () -> "Item Object not present " + name);
return Objects.requireNonNull(entry.get(), () -> "Item Object not present " + id);
}

/**
Expand All @@ -79,12 +81,4 @@ public I getOrNull() {
public Item asItem() {
return get().asItem();
}

/**
* Gets the resource location for the given item
* @return Resource location for the given item
*/
public ResourceLocation getRegistryName() {
return name;
}
}

0 comments on commit c44fa63

Please sign in to comment.