Skip to content

Commit

Permalink
Update to current AE2 with deferred registration
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Jul 13, 2024
1 parent 1fdd586 commit 4c8b3dd
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 105 deletions.
5 changes: 0 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ java.toolchain.languageVersion = JavaLanguageVersion.of(21)
dependencies {
implementation(libs.ae2)
implementation(libs.ae2wtlib)

implementation(libs.appmek)
compileOnly(libs.mekanism)
compileOnly(variantOf(libs.mekanism) { classifier("generators") })
runtimeOnly(variantOf(libs.mekanism) { classifier("all") })
}

sourceSets {
Expand Down
10 changes: 3 additions & 7 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,13 @@ run {
versionCatalogs {
create("libs") {
version("minecraft", "1.21")
version("neoforge", "21.0.13-beta")
version("neoforge", "21.0.87-beta")
version("parchment", "2024.06.23")

version("ae2", "19.0.8-alpha")
version("ae2", "19.0.12-alpha")
library("ae2", "appeng", "appliedenergistics2").versionRef("ae2")

library("ae2wtlib", "maven.modrinth", "applied-energistics-2-wireless-terminals").version("jqaoHVCd")

val minecraftVersion = "1.20.4"
library("mekanism", "mekanism", "Mekanism").version("$minecraftVersion-10.5.10.32")
library("appmek", "maven.modrinth", "applied-mekanistics").version("BG93ZC9u")
library("ae2wtlib", "maven.modrinth", "applied-energistics-2-wireless-terminals").version("WyPbb8sE")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ private static void cell(
private static void portable(
RecipeOutput output, ItemDefinition<?> cell, ItemDefinition<?> component, ItemDefinition<?> housing) {
ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, cell)
.requires(AEBlocks.CHEST)
.requires(AEBlocks.ME_CHEST)
.requires(component)
.requires(AEBlocks.DENSE_ENERGY_CELL)
.requires(housing)
Expand Down
29 changes: 13 additions & 16 deletions src/main/java/gripe/_90/megacells/MEGACells.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public class MEGACells {
public static final String MODID = "megacells";

public MEGACells(ModContainer container, IEventBus modEventBus) {
MEGABlocks.DR.register(modEventBus);
MEGAItems.DR.register(modEventBus);
MEGABlockEntities.DR.register(modEventBus);

modEventBus.addListener(MEGACells::registerAll);
modEventBus.addListener(MEGACells::initUpgrades);
modEventBus.addListener(MEGACells::initStorageCells);
Expand All @@ -109,15 +113,6 @@ public static ResourceLocation makeId(String path) {
}

private static void registerAll(RegisterEvent event) {
MEGABlocks.getBlocks().forEach(block -> {
event.register(Registries.BLOCK, block.id(), block::block);
event.register(Registries.ITEM, block.id(), block::asItem);
});

MEGAItems.getItems().forEach(item -> event.register(Registries.ITEM, item.id(), item::asItem));

event.register(Registries.BLOCK_ENTITY_TYPE, helper -> MEGABlockEntities.getBEs()
.forEach(helper::register));
event.register(Registries.MENU, helper -> MEGAMenus.getMenuTypes().forEach(helper::register));

event.register(Registries.CREATIVE_MODE_TAB, MEGACreativeTab.ID, () -> MEGACreativeTab.TAB);
Expand Down Expand Up @@ -245,22 +240,23 @@ private static void initLavaTransform() {

@SuppressWarnings("UnstableApiUsage")
private static void initCapabilities(RegisterCapabilitiesEvent event) {
for (var type : MEGABlockEntities.getBEs().values()) {
for (var type : MEGABlockEntities.DR.getEntries()) {
event.registerBlockEntity(
AECapabilities.IN_WORLD_GRID_NODE_HOST, type, (be, context) -> (IInWorldGridNodeHost) be);
AECapabilities.IN_WORLD_GRID_NODE_HOST, type.get(), (be, context) -> (IInWorldGridNodeHost) be);
}

event.registerBlockEntity(
AECapabilities.GENERIC_INTERNAL_INV,
MEGABlockEntities.MEGA_INTERFACE,
MEGABlockEntities.MEGA_INTERFACE.get(),
(be, context) -> be.getInterfaceLogic().getStorage());
event.registerBlockEntity(
AECapabilities.ME_STORAGE, MEGABlockEntities.MEGA_INTERFACE, (be, context) -> be.getInterfaceLogic()
.getInventory());
AECapabilities.ME_STORAGE,
MEGABlockEntities.MEGA_INTERFACE.get(),
(be, context) -> be.getInterfaceLogic().getInventory());

event.registerBlockEntity(
AECapabilities.GENERIC_INTERNAL_INV,
MEGABlockEntities.MEGA_PATTERN_PROVIDER,
MEGABlockEntities.MEGA_PATTERN_PROVIDER.get(),
(be, context) -> be.getLogic().getReturnInv());

MEGAItems.getItemPortables().forEach(portable -> registerPoweredItemCapability(event, portable));
Expand Down Expand Up @@ -309,7 +305,8 @@ private static void initCraftingUnitModels(FMLClientSetupEvent event) {
ItemBlockRenderTypes.setRenderLayer(type.getDefinition().block(), RenderType.cutout());
}

BlockEntityRenderers.register(MEGABlockEntities.MEGA_CRAFTING_MONITOR, CraftingMonitorRenderer::new);
BlockEntityRenderers.register(
MEGABlockEntities.MEGA_CRAFTING_MONITOR.get(), CraftingMonitorRenderer::new);
});
}

Expand Down
56 changes: 26 additions & 30 deletions src/main/java/gripe/_90/megacells/definition/MEGABlockEntities.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package gripe._90.megacells.definition;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.registries.DeferredRegister;

import appeng.block.AEBaseEntityBlock;
import appeng.blockentity.AEBaseBlockEntity;
Expand All @@ -24,22 +23,19 @@

@SuppressWarnings("unused")
public final class MEGABlockEntities {
private static final Map<ResourceLocation, BlockEntityType<?>> BLOCK_ENTITY_TYPES = new HashMap<>();
public static final DeferredRegister<BlockEntityType<?>> DR =
DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, MEGACells.MODID);

public static Map<ResourceLocation, BlockEntityType<?>> getBEs() {
return Collections.unmodifiableMap(BLOCK_ENTITY_TYPES);
}

public static final BlockEntityType<EnergyCellBlockEntity> MEGA_ENERGY_CELL = create(
public static final Supplier<BlockEntityType<EnergyCellBlockEntity>> MEGA_ENERGY_CELL = create(
"mega_energy_cell", EnergyCellBlockEntity.class, EnergyCellBlockEntity::new, MEGABlocks.MEGA_ENERGY_CELL);

public static final BlockEntityType<CraftingBlockEntity> MEGA_CRAFTING_UNIT = create(
public static final Supplier<BlockEntityType<CraftingBlockEntity>> MEGA_CRAFTING_UNIT = create(
"mega_crafting_unit",
CraftingBlockEntity.class,
CraftingBlockEntity::new,
MEGABlocks.MEGA_CRAFTING_UNIT,
MEGABlocks.CRAFTING_ACCELERATOR);
public static final BlockEntityType<CraftingBlockEntity> MEGA_CRAFTING_STORAGE = create(
public static final Supplier<BlockEntityType<CraftingBlockEntity>> MEGA_CRAFTING_STORAGE = create(
"mega_crafting_storage",
CraftingBlockEntity.class,
CraftingBlockEntity::new,
Expand All @@ -48,47 +44,47 @@ public static Map<ResourceLocation, BlockEntityType<?>> getBEs() {
MEGABlocks.CRAFTING_STORAGE_16M,
MEGABlocks.CRAFTING_STORAGE_64M,
MEGABlocks.CRAFTING_STORAGE_256M);
public static final BlockEntityType<CraftingMonitorBlockEntity> MEGA_CRAFTING_MONITOR = create(
public static final Supplier<BlockEntityType<CraftingMonitorBlockEntity>> MEGA_CRAFTING_MONITOR = create(
"mega_crafting_monitor",
CraftingMonitorBlockEntity.class,
CraftingMonitorBlockEntity::new,
MEGABlocks.CRAFTING_MONITOR);

public static final BlockEntityType<MEGAInterfaceBlockEntity> MEGA_INTERFACE = create(
public static final Supplier<BlockEntityType<MEGAInterfaceBlockEntity>> MEGA_INTERFACE = create(
"mega_interface", MEGAInterfaceBlockEntity.class, MEGAInterfaceBlockEntity::new, MEGABlocks.MEGA_INTERFACE);
public static final BlockEntityType<MEGAPatternProviderBlockEntity> MEGA_PATTERN_PROVIDER = create(
public static final Supplier<BlockEntityType<MEGAPatternProviderBlockEntity>> MEGA_PATTERN_PROVIDER = create(
"mega_pattern_provider",
MEGAPatternProviderBlockEntity.class,
MEGAPatternProviderBlockEntity::new,
MEGABlocks.MEGA_PATTERN_PROVIDER);

@SuppressWarnings({"DataFlowIssue", "unchecked"})
@SafeVarargs
private static <T extends AEBaseBlockEntity> BlockEntityType<T> create(
private static <T extends AEBaseBlockEntity> Supplier<BlockEntityType<T>> create(
String id,
Class<T> entityClass,
BlockEntityFactory<T> factory,
BlockDefinition<? extends AEBaseEntityBlock<?>>... blockDefinitions) {
if (blockDefinitions.length == 0) {
BlockDefinition<? extends AEBaseEntityBlock<?>>... blockDefs) {
if (blockDefs.length == 0) {
throw new IllegalArgumentException();
}

var blocks = Arrays.stream(blockDefinitions).map(BlockDefinition::block).toArray(AEBaseEntityBlock[]::new);
return DR.register(id, () -> {
var blocks = Arrays.stream(blockDefs).map(BlockDefinition::block).toArray(AEBaseEntityBlock[]::new);

var typeHolder = new AtomicReference<BlockEntityType<T>>();
var type = BlockEntityType.Builder.of((pos, state) -> factory.create(typeHolder.get(), pos, state), blocks)
.build(null);
typeHolder.set(type);
BLOCK_ENTITY_TYPES.put(MEGACells.makeId(id), type);
var typeHolder = new AtomicReference<BlockEntityType<T>>();
var type = BlockEntityType.Builder.of((pos, state) -> factory.create(typeHolder.get(), pos, state), blocks)
.build(null);
typeHolder.set(type);

AEBaseBlockEntity.registerBlockEntityItem(type, blockDefinitions[0].asItem());
AEBaseBlockEntity.registerBlockEntityItem(type, blockDefs[0].asItem());

for (var block : blocks) {
var baseBlock = (AEBaseEntityBlock<T>) block;
baseBlock.setBlockEntity(entityClass, type, null, null);
}
for (var block : blocks) {
block.setBlockEntity(entityClass, type, null, null);
}

return type;
return type;
});
}

private interface BlockEntityFactory<T extends AEBaseBlockEntity> {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/gripe/_90/megacells/definition/MEGABlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.MapColor;
import net.neoforged.neoforge.registries.DeferredRegister;

import appeng.block.AEBaseBlockItem;
import appeng.block.crafting.CraftingMonitorBlock;
Expand All @@ -22,6 +23,7 @@
import appeng.core.definitions.AEItems;
import appeng.core.definitions.AEParts;
import appeng.core.definitions.BlockDefinition;
import appeng.core.definitions.ItemDefinition;
import appeng.decorative.AEDecorativeBlock;

import gripe._90.megacells.MEGACells;
Expand All @@ -32,6 +34,8 @@
import gripe._90.megacells.block.MEGAPatternProviderBlockItem;

public final class MEGABlocks {
public static final DeferredRegister.Blocks DR = DeferredRegister.createBlocks(MEGACells.MODID);

private static final List<BlockDefinition<?>> BLOCKS = new ArrayList<>();

public static List<BlockDefinition<?>> getBlocks() {
Expand Down Expand Up @@ -117,10 +121,10 @@ private static <T extends Block> BlockDefinition<T> block(
String id,
Supplier<T> blockSupplier,
BiFunction<Block, Item.Properties, BlockItem> itemFactory) {
var block = blockSupplier.get();
var item = itemFactory.apply(block, new Item.Properties());
var block = DR.register(id, blockSupplier);
var item = MEGAItems.DR.register(id, () -> itemFactory.apply(block.get(), new Item.Properties()));

var definition = new BlockDefinition<>(englishName, MEGACells.makeId(id), block, item);
var definition = new BlockDefinition<>(englishName, block, new ItemDefinition<>(englishName, item));
BLOCKS.add(definition);
return definition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import appeng.block.AEBaseBlock;
import appeng.block.AEBaseBlockItem;
import appeng.core.definitions.BlockDefinition;
import appeng.core.definitions.ItemDefinition;
import appeng.items.AEBaseItem;

Expand All @@ -24,7 +25,8 @@ public final class MEGACreativeTab {
private static void populateTab(CreativeModeTab.ItemDisplayParameters params, CreativeModeTab.Output output) {
var itemDefs = new ArrayList<ItemDefinition<?>>();
itemDefs.addAll(MEGAItems.getItems());
itemDefs.addAll(MEGABlocks.getBlocks());
itemDefs.addAll(
MEGABlocks.getBlocks().stream().map(BlockDefinition::item).toList());

for (var itemDef : itemDefs) {
var item = itemDef.asItem();
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/gripe/_90/megacells/definition/MEGAItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import net.minecraft.Util;
import net.minecraft.world.item.Item;
import net.neoforged.neoforge.registries.DeferredRegister;

import appeng.api.crafting.PatternDetailsHelper;
import appeng.api.parts.IPart;
Expand Down Expand Up @@ -35,6 +36,8 @@
import gripe._90.megacells.misc.DecompressionPattern;

public final class MEGAItems {
public static final DeferredRegister.Items DR = DeferredRegister.createItems(MEGACells.MODID);

private static final List<ItemDefinition<?>> ITEMS = new ArrayList<>();

public static List<ItemDefinition<?>> getItems() {
Expand Down Expand Up @@ -210,7 +213,7 @@ private static <T extends IPart> ItemDefinition<PartItem<T>> part(

public static <T extends Item> ItemDefinition<T> item(
String englishName, String id, Function<Item.Properties, T> factory) {
var definition = new ItemDefinition<>(englishName, MEGACells.makeId(id), factory.apply(new Item.Properties()));
var definition = new ItemDefinition<>(englishName, DR.registerItem(id, factory));
ITEMS.add(definition);
return definition;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/gripe/_90/megacells/definition/MEGAMenus.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.MenuType;

import appeng.core.AppEng;
import appeng.helpers.InterfaceLogicHost;
import appeng.helpers.patternprovider.PatternProviderLogicHost;
import appeng.menu.AEBaseMenu;
import appeng.menu.implementations.MenuTypeBuilder;

import gripe._90.megacells.MEGACells;
import gripe._90.megacells.menu.MEGAInterfaceMenu;
import gripe._90.megacells.menu.MEGAPatternProviderMenu;

Expand All @@ -30,8 +30,8 @@ public static Map<ResourceLocation, MenuType<?>> getMenuTypes() {

private static <C extends AEBaseMenu, I> MenuType<C> create(
String id, MenuTypeBuilder.MenuFactory<C, I> factory, Class<I> host) {
var menu = MenuTypeBuilder.create(factory, host).build(id);
MENU_TYPES.put(AppEng.makeId(id), menu);
var menu = MenuTypeBuilder.create(factory, host).build(MEGACells.makeId(id));
MENU_TYPES.put(MEGACells.makeId(id), menu);
return menu;
}
}
29 changes: 3 additions & 26 deletions src/main/java/gripe/_90/megacells/definition/MEGATranslations.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,14 @@ public enum MEGATranslations implements LocalizationEnum {
PartitionedFor("Partitioned for: %s", Type.TOOLTIP),
ProcessingOnly("Supports processing patterns only.", Type.TOOLTIP),
Quantity("Quantity: %s", Type.TOOLTIP),
NotPartitioned("Not Partitioned", Type.TOOLTIP),

CompressionChainLimit("Bulk Compression chain limit", Type.CONFIG_OPTION),
CompressionChainLimitTooltip(
"The maximum number of variants that a compression-enabled Bulk Cell may report as being stored.",
Type.CONFIG_TOOLTIP,
CompressionChainLimit),
AllowSpentWaste("(AppMek) Allow Spent Nuclear Waste", Type.CONFIG_OPTION),
AllowSpentWasteTooltip(
"Whether the MEGA Radioactive Cell should be able to store Spent Nuclear Waste.",
Type.CONFIG_TOOLTIP,
AllowSpentWaste);
NotPartitioned("Not Partitioned", Type.TOOLTIP);

private final String englishText;
private final Type type;
private final MEGATranslations associated;

MEGATranslations(String englishText, Type type) {
this(englishText, type, null);
}

MEGATranslations(String englishText, Type type, MEGATranslations associated) {
this.englishText = englishText;
this.type = type;
this.associated = associated;
}

@Override
Expand All @@ -52,18 +35,12 @@ public String getEnglishText() {

@Override
public String getTranslationKey() {
return switch (type) {
case CONFIG_OPTION -> type.root.formatted(MEGACells.MODID) + "." + name();
case CONFIG_TOOLTIP -> type.root.formatted(MEGACells.MODID) + "." + associated.name() + ".@Tooltip";
default -> String.format("%s.%s.%s", type.root, MEGACells.MODID, name());
};
return String.format("%s.%s.%s", type.root, MEGACells.MODID, name());
}

private enum Type {
GUI("gui"),
TOOLTIP("gui.tooltips"),
CONFIG_OPTION("text.autoconfig.%s.option"),
CONFIG_TOOLTIP("text.autoconfig.%s.option");
TOOLTIP("gui.tooltips");

private final String root;

Expand Down
Loading

0 comments on commit 4c8b3dd

Please sign in to comment.