Skip to content

Commit

Permalink
More api updates
Browse files Browse the repository at this point in the history
  • Loading branch information
UnRealDinnerbone committed Dec 12, 2024
1 parent cdd282f commit f6c1902
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 11 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
### 21.4.3
### 21.4.4

- Expose Block Entity creation for common api
- Add Block Registration Helper and other tabs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unrealdinnerbone.trenzalore.api.platform.services;

import com.unrealdinnerbone.trenzalore.lib.CreativeTabs;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
Expand All @@ -15,4 +16,8 @@ public interface ICreativeTabRegister {
default void addItemToCreativeTab(ResourceKey<CreativeModeTab> tabResourceKey, Supplier<? extends Item> item) {
addItemToCreativeTab(tabResourceKey, List.of(item));
}

default CreativeTabs tabs() {
return CreativeTabs.INSTANCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.unrealdinnerbone.trenzalore.api.registry;

import com.google.common.base.Suppliers;
import com.unrealdinnerbone.trenzalore.lib.RLUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;

import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

public class BlockRegistryObjects extends RegistryObjects<Block> {

public BlockRegistryObjects(String modID) {
super(modID, Registries.BLOCK);
}

public <A extends Block> RegistryEntry<A> register(String name, Function<Block.Properties, A> object, UnaryOperator<Block.Properties> blockProperties) {
ResourceLocation rl = RLUtils.rl(modID, name);
ResourceKey<Block> key = ResourceKey.create(registryKey, rl);
Supplier<A> memoize = Suppliers.memoize(() -> object.apply(blockProperties.apply(Block.Properties.of().setId(key))));
RegistryEntry<A> entry = new RegistryEntry<>(rl, memoize);
objects.add(entry);
return entry;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;

import java.util.function.Function;
import java.util.function.Supplier;
Expand All @@ -20,12 +22,15 @@ public ItemRegistryObjects(String modID) {
public <A extends Item> RegistryEntry<A> register(String name, Function<Item.Properties, A> object, UnaryOperator<Item.Properties> itemProperties) {
ResourceLocation rl = RLUtils.rl(modID, name);
ResourceKey<Item> key = ResourceKey.create(registryKey, rl);
Supplier<A> memoize = Suppliers.memoize(() -> object.apply(itemProperties.apply(new Item.Properties()
.setId(key))));
Supplier<A> memoize = Suppliers.memoize(() -> object.apply(itemProperties.apply(new Item.Properties().setId(key))));
RegistryEntry<A> entry = new RegistryEntry<>(rl, memoize);
objects.add(entry);
return entry;
}

public RegistryEntry<BlockItem> registerBlockItem(String name, Supplier<Block> block, UnaryOperator<Item.Properties> itemProperties) {
return register(name, (properties) -> new BlockItem(block.get(), properties), itemProperties);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ public static ItemRegistryObjects createItemRegistry(String modID) {
return new ItemRegistryObjects(modID);
}

public static BlockRegistryObjects createBlockRegistry(String modID) {
return new BlockRegistryObjects(modID);
}

@Deprecated(forRemoval = true)
@ApiStatus.ScheduledForRemoval(inVersion = "22.0.0")
public static void addItemToCreateTab(ResourceKey<CreativeModeTab> tabResourceKey, Supplier<? extends Item> item) {
Services.PLATFORM.addItemToCreativeTab(tabResourceKey, List.of(item));
}

@Deprecated(forRemoval = true)
@ApiStatus.ScheduledForRemoval(inVersion = "22.0.0")
public static void addItemsToCreateTab(ResourceKey<CreativeModeTab> tabResourceKey, List<Supplier<? extends Item>> item) {
Services.PLATFORM.addItemToCreativeTab(tabResourceKey, item);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.unrealdinnerbone.trenzalore.api.registry;

import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.ApiStatus;

Expand All @@ -27,19 +26,30 @@ public <B> void setHolder(Holder<B> register) {
}

public Holder<T> getHolder() {
if (holder == null) {
throw new IllegalStateException("Can't get holder before it is set");
}
return holder;
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "21.0.0")
@Override
public T get() {
return entry.get();
return getHolder().value();
}

public ResourceLocation getKey() {
return key;
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "21.0.0")
public Supplier<T> entry() {
return entry;
}

public Supplier<T> creator() {
return entry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

public class CreativeTabs {

public static final CreativeTabs INSTANCE = new CreativeTabs();

public static final ResourceKey<CreativeModeTab> BUILDING_BLOCKS = CreativeModeTabs.BUILDING_BLOCKS;
public static final ResourceKey<CreativeModeTab> COLORED_BLOCKS = CreativeModeTabs.COLORED_BLOCKS;
public static final ResourceKey<CreativeModeTab> NATURAL_BLOCKS = CreativeModeTabs.NATURAL_BLOCKS;
Expand All @@ -18,4 +20,49 @@ public class CreativeTabs {
public static final ResourceKey<CreativeModeTab> SPAWN_EGGS = CreativeModeTabs.SPAWN_EGGS;
public static final ResourceKey<CreativeModeTab> OP_BLOCKS = CreativeModeTabs.OP_BLOCKS;


public ResourceKey<CreativeModeTab> buildingBlocks() {
return BUILDING_BLOCKS;
}

public ResourceKey<CreativeModeTab> coloredBlocks() {
return COLORED_BLOCKS;
}

public ResourceKey<CreativeModeTab> naturalBlocks() {
return NATURAL_BLOCKS;
}

public ResourceKey<CreativeModeTab> functionalBlocks() {
return FUNCTIONAL_BLOCKS;
}

public ResourceKey<CreativeModeTab> redstoneBlocks() {
return REDSTONE_BLOCKS;
}

public ResourceKey<CreativeModeTab> toolsAndUtilities() {
return TOOLS_AND_UTILITIES;
}

public ResourceKey<CreativeModeTab> combat() {
return COMBAT;
}

public ResourceKey<CreativeModeTab> foodAndDrinks() {
return FOOD_AND_DRINKS;
}

public ResourceKey<CreativeModeTab> ingredients() {
return INGREDIENTS;
}

public ResourceKey<CreativeModeTab> spawnEggs() {
return SPAWN_EGGS;
}

public ResourceKey<CreativeModeTab> opBlocks() {
return OP_BLOCKS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ public void addItemToCreativeTab(ResourceKey<CreativeModeTab> tabResourceKey, Li
public <T> void registryRegistryObjects(String modId, RegistryObjects<T> registryObjects) {
ResourceKey<Registry<T>> registryKey = registryObjects.registryKey();
BuiltInRegistries.REGISTRY.get(registryKey.location()).ifPresentOrElse(registry -> {
Registry<?> value = registry.value();
Registry<T> theRegistry = (Registry<T>) value;
Registry<T> theRegistry = (Registry<T>) registry.value();
for (RegistryEntry<? extends T> object : registryObjects.objects()) {
Holder.Reference<T> register = Registry.registerForHolder(theRegistry, object.getKey(), object.get());
object.setHolder(register);
object.setHolder(Registry.registerForHolder(theRegistry, object.getKey(), object.creator().get()));
}
}, () -> LOGGER.error("Failed to find registry: {}", registryKey.location()));

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Project
mod_version=21.4.3
mod_version=21.4.4
maven_group=com.unrealdinnerbone
mod_name=Trenzalore
mod_author=UnRealDinnerbone
Expand Down

0 comments on commit f6c1902

Please sign in to comment.