Skip to content

Commit

Permalink
Fix registries
Browse files Browse the repository at this point in the history
  • Loading branch information
WenXin20 committed Jun 9, 2024
1 parent 9ca10ae commit 6e1e3ec
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 82 deletions.
64 changes: 30 additions & 34 deletions src/main/java/com/wenxin2/warp_pipes/WarpPipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@
import com.wenxin2.warp_pipes.init.ModCreativeTabs;
import com.wenxin2.warp_pipes.init.ModRegistry;
import com.wenxin2.warp_pipes.init.SoundRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fluids.FluidType;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.fluids.FluidType;
import net.neoforged.neoforge.registries.DeferredRegister;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import org.slf4j.Logger;

// The value here should match an entry in the META-INF/mods.toml file
Expand All @@ -35,45 +34,42 @@ public class WarpPipes
// Directly reference a slf4j logger
private static final Logger LOGGER = LogUtils.getLogger();
// Create a Deferred Register to hold blocks/items which will all be registered under the "warp_pipes" namespace
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
public static final DeferredRegister<Fluid> FLUIDS = DeferredRegister.create(ForgeRegistries.FLUIDS, MODID);
public static final DeferredRegister<FluidType> FLUID_TYPES = DeferredRegister.create(ForgeRegistries.Keys.FLUID_TYPES, MODID);
public static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(MODID);
public static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(MODID);
// public static final DeferredRegister<Fluid> FLUIDS = DeferredRegister.create(Registries.FLUID, MODID);
// public static final DeferredRegister<FluidType> FLUID_TYPES = DeferredRegister.create(NeoForgeRegistries.FLUID_TYPES, MODID);

public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITIES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, WarpPipes.MODID);
public static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(ForgeRegistries.MENU_TYPES, WarpPipes.MODID);
public static final DeferredRegister<SoundEvent> SOUNDS = DeferredRegister.create(ForgeRegistries.SOUND_EVENTS, WarpPipes.MODID);
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITIES = DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, WarpPipes.MODID);
public static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(Registries.MENU, WarpPipes.MODID);
public static final DeferredRegister<SoundEvent> SOUNDS = DeferredRegister.create(Registries.SOUND_EVENT, WarpPipes.MODID);

// Bus for Forge Events
public static final IEventBus FORGE_BUS = MinecraftForge.EVENT_BUS;
public static final IEventBus FORGE_BUS = NeoForge.EVENT_BUS;

public WarpPipes()
public WarpPipes(IEventBus bus, Dist dist)
{
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();

// Register the Deferred Register to the mod event bus so blocks/items get registered
BLOCKS.register(modEventBus);
ITEMS.register(modEventBus);
FLUIDS.register(modEventBus);
FLUID_TYPES.register(modEventBus);
BLOCK_ENTITIES.register(modEventBus);
MENUS.register(modEventBus);
SOUNDS.register(modEventBus);
ModCreativeTabs.TABS.register(modEventBus);
BLOCKS.register(bus);
ITEMS.register(bus);
// FLUIDS.register(bus);
// FLUID_TYPES.register(bus);
BLOCK_ENTITIES.register(bus);
MENUS.register(bus);
SOUNDS.register(bus);
ModCreativeTabs.TABS.register(bus);

ModRegistry.init();
SoundRegistry.init();

DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
modEventBus.addListener(ClientSetupHandler::registerBlockEntityRenderers);
});
if (dist.isClient())
bus.addListener(ClientSetupHandler::registerBlockEntityRenderers);

WarpEventHandlers.register();
// PipeBubblesSoundHandler.init();

ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, Config.CONFIG);

// Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this);
NeoForge.EVENT_BUS.register(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import net.minecraft.world.phys.shapes.EntityCollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.server.ServerLifecycleHooks;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.jetbrains.annotations.NotNull;

public class ClearWarpPipeBlock extends WarpPipeBlock implements EntityBlock, SimpleWaterloggedBlock {
Expand Down
42 changes: 21 additions & 21 deletions src/main/java/com/wenxin2/warp_pipes/init/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@
import java.util.List;
import java.util.function.Predicate;
import net.minecraft.world.entity.Entity;
import net.minecraftforge.common.ForgeConfigSpec;
import net.neoforged.neoforge.common.ModConfigSpec;

public class Config
{
private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
public static ForgeConfigSpec CONFIG;
private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder();
public static ModConfigSpec CONFIG;

public static final String CATEGORY_DEBUG = "Debug";
public static final String CATEGORY_CLIENT = "Client";
public static final String CATEGORY_COMMON = "Common";

public static ForgeConfigSpec.BooleanValue ALLOW_FAST_TRAVEL;
public static ForgeConfigSpec.BooleanValue ALLOW_PIPE_UNWAXING;
public static ForgeConfigSpec.BooleanValue CREATIVE_BUBBLES;
public static ForgeConfigSpec.BooleanValue CREATIVE_CLOSE_PIPES;
public static ForgeConfigSpec.BooleanValue CREATIVE_WATER_SPOUT;
public static ForgeConfigSpec.BooleanValue CREATIVE_WRENCH_PIPE_LINKING;
public static ForgeConfigSpec.BooleanValue DEBUG_PIPE_BUBBLES_SELECTION_BOX;
public static ForgeConfigSpec.BooleanValue DEBUG_WATER_SPOUT_SELECTION_BOX;
public static ForgeConfigSpec.BooleanValue DEBUG_SELECTION_BOX;
public static ForgeConfigSpec.BooleanValue DEBUG_SELECTION_BOX_CREATIVE;
public static ForgeConfigSpec.BooleanValue DISABLE_TEXT;
public static ForgeConfigSpec.BooleanValue TELEPORT_MOBS;
public static ForgeConfigSpec.BooleanValue TELEPORT_NON_MOBS;
public static ForgeConfigSpec.BooleanValue TELEPORT_PLAYERS;
public static ForgeConfigSpec.IntValue WARP_COOLDOWN;
public static ForgeConfigSpec.BooleanValue WARP_COOLDOWN_MESSAGE;
public static ForgeConfigSpec.BooleanValue WARP_COOLDOWN_MESSAGE_TICKS;
public static ForgeConfigSpec.BooleanValue WATER_SPOUTS_BUCKETABLE;
public static ModConfigSpec.BooleanValue ALLOW_FAST_TRAVEL;
public static ModConfigSpec.BooleanValue ALLOW_PIPE_UNWAXING;
public static ModConfigSpec.BooleanValue CREATIVE_BUBBLES;
public static ModConfigSpec.BooleanValue CREATIVE_CLOSE_PIPES;
public static ModConfigSpec.BooleanValue CREATIVE_WATER_SPOUT;
public static ModConfigSpec.BooleanValue CREATIVE_WRENCH_PIPE_LINKING;
public static ModConfigSpec.BooleanValue DEBUG_PIPE_BUBBLES_SELECTION_BOX;
public static ModConfigSpec.BooleanValue DEBUG_WATER_SPOUT_SELECTION_BOX;
public static ModConfigSpec.BooleanValue DEBUG_SELECTION_BOX;
public static ModConfigSpec.BooleanValue DEBUG_SELECTION_BOX_CREATIVE;
public static ModConfigSpec.BooleanValue DISABLE_TEXT;
public static ModConfigSpec.BooleanValue TELEPORT_MOBS;
public static ModConfigSpec.BooleanValue TELEPORT_NON_MOBS;
public static ModConfigSpec.BooleanValue TELEPORT_PLAYERS;
public static ModConfigSpec.IntValue WARP_COOLDOWN;
public static ModConfigSpec.BooleanValue WARP_COOLDOWN_MESSAGE;
public static ModConfigSpec.BooleanValue WARP_COOLDOWN_MESSAGE_TICKS;
public static ModConfigSpec.BooleanValue WATER_SPOUTS_BUCKETABLE;

static
{
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/wenxin2/warp_pipes/init/ModCreativeTabs.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.wenxin2.warp_pipes.init;

import com.wenxin2.warp_pipes.WarpPipes;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.CreativeModeTab;
Expand All @@ -10,16 +11,15 @@
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.RegistryObject;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent;
import net.neoforged.neoforge.registries.DeferredRegister;

@Mod.EventBusSubscriber(modid = WarpPipes.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ModCreativeTabs {
public static final DeferredRegister<CreativeModeTab> TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, WarpPipes.MODID);
public static final RegistryObject<CreativeModeTab> WARP_PIPES_TAB = TABS.register("warp_pipes_tab", () -> CreativeModeTab.builder()
public static final Registry<CreativeModeTab> WARP_PIPES_TAB = TABS.register("warp_pipes_tab", () -> CreativeModeTab.builder()
.title(Component.translatable("itemGroup.warp_pipes"))
.icon(() -> new ItemStack(ModRegistry.WARP_PIPES.get(DyeColor.GREEN).get())).build());

Expand All @@ -30,8 +30,8 @@ public static void addCreative(BuildCreativeModeTabContentsEvent event) {

add(event, ModRegistry.CLEAR_WARP_PIPE.get());

for (RegistryObject<Block> pipe : ModRegistry.WARP_PIPES.values()) {
add(event, pipe.get());
for (Registry<Block> pipe : ModRegistry.WARP_PIPES.values()) {
add(event, pipe);
}
}
if (event.getTabKey() == CreativeModeTabs.TOOLS_AND_UTILITIES) {
Expand All @@ -50,15 +50,15 @@ public static void addCreative(BuildCreativeModeTabContentsEvent event) {
if (event.getTabKey() == CreativeModeTabs.COLORED_BLOCKS) {
add(event, ModRegistry.CLEAR_WARP_PIPE.get());

for (RegistryObject<Block> pipe : ModRegistry.WARP_PIPES.values()) {
for (Registry<Block> pipe : ModRegistry.WARP_PIPES.values()) {
add(event, pipe.get());
}
}

if (event.getTabKey() == CreativeModeTabs.FUNCTIONAL_BLOCKS) {
addAfter(event, Items.RESPAWN_ANCHOR, ModRegistry.CLEAR_WARP_PIPE.get());

for (RegistryObject<Block> pipe : ModRegistry.WARP_PIPES.values()) {
for (Registry<Block> pipe : ModRegistry.WARP_PIPES.values()) {
addAfter(event, ModRegistry.CLEAR_WARP_PIPE.get(), pipe.get());
}

Expand Down
40 changes: 24 additions & 16 deletions src/main/java/com/wenxin2/warp_pipes/init/ModRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import com.wenxin2.warp_pipes.blocks.entities.WarpPipeBlockEntity;
import com.wenxin2.warp_pipes.inventory.WarpPipeMenu;
import com.wenxin2.warp_pipes.items.WrenchItem;
import dev.architectury.registry.registries.RegistrarManager;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.function.Supplier;
import java.util.stream.Stream;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.resources.RegistryOps;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.DyeColor;
Expand All @@ -27,20 +31,23 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.level.material.PushReaction;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.RegistryObject;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.registries.DeferredBlock;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredItem;
import net.neoforged.neoforge.registries.DeferredRegister;

@Mod.EventBusSubscriber(modid = WarpPipes.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ModRegistry {
public static final EnumMap<DyeColor, RegistryObject<Block>> WARP_PIPES =
public static final EnumMap<DyeColor, DeferredBlock<Block>> WARP_PIPES =
new EnumMap<>(DyeColor.class);
public static final RegistryObject<Item> PIPE_WRENCH;
public static final RegistryObject<Block> CLEAR_WARP_PIPE;
public static final RegistryObject<Block> PIPE_BUBBLES;
public static final RegistryObject<Block> WATER_SPOUT;
public static final DeferredItem<Item> PIPE_WRENCH;
public static final DeferredBlock<Block> CLEAR_WARP_PIPE;
public static final DeferredBlock<Block> PIPE_BUBBLES;
public static final DeferredBlock<Block> WATER_SPOUT;

public static final RegistryObject<BlockEntityType<WarpPipeBlockEntity>> WARP_PIPE_BLOCK_ENTITY;
public static final RegistryObject<MenuType<WarpPipeMenu>> WARP_PIPE_MENU;
public static final DeferredHolder<BlockEntityType<?>, BlockEntityType<WarpPipeBlockEntity>> WARP_PIPE_BLOCK_ENTITY;
public static final DeferredHolder<MenuType<?>, MenuType<AbstractContainerMenu>> WARP_PIPE_MENU;

static
{
Expand Down Expand Up @@ -72,29 +79,30 @@ public class ModRegistry {

WARP_PIPE_BLOCK_ENTITY = WarpPipes.BLOCK_ENTITIES.register("warp_pipe",
() -> BlockEntityType.Builder.of(WarpPipeBlockEntity::new,
Stream.concat(WARP_PIPES.values().stream().map(RegistryObject::get),
Stream.concat(WARP_PIPES.values().stream().map(DeferredBlock::get),
Stream.of(CLEAR_WARP_PIPE.get())).toArray(Block[]::new))
.build(null));

WARP_PIPE_MENU = WarpPipes.MENUS.register("warp_pipe", () -> new MenuType<>(WarpPipeMenu::new, FeatureFlags.REGISTRY.allFlags()));
}

public static RegistryObject<Block> registerBlock(String name, Supplier<? extends Block> block)
public static <T extends Block> DeferredBlock<T> registerBlock(String name, Supplier<T> block)
{
RegistryObject<Block> blocks = WarpPipes.BLOCKS.register(name, block);
DeferredBlock<T> blocks = WarpPipes.BLOCKS.register(name, block);
WarpPipes.ITEMS.register(name, () -> new BlockItem(blocks.get(), new Item.Properties()));
return blocks;
}

public static RegistryObject<Block> registerNoItemBlock(String name, Supplier<? extends Block> block)
public static <T extends Block> DeferredBlock<T> registerNoItemBlock(String name, Supplier<T> block)
{
RegistryObject<Block> blocks = WarpPipes.BLOCKS.register(name, block);
DeferredBlock<T> blocks = WarpPipes.BLOCKS.register(name, block);
return blocks;
}

public static RegistryObject<Item> registerItem(String name, Supplier<? extends Item> item)
public static <T extends Item> DeferredItem registerItem(String name, Supplier<T> item)
{
return WarpPipes.ITEMS.register(name, item);
DeferredItem<T> items = WarpPipes.ITEMS.register(name, item);
return items;
}

private static boolean always(BlockState state, BlockGetter block, BlockPos pos)
Expand Down

0 comments on commit 6e1e3ec

Please sign in to comment.