Skip to content

Commit

Permalink
clean up init again
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Jan 25, 2023
1 parent fc2f9c2 commit 265d899
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 122 deletions.
35 changes: 26 additions & 9 deletions fabric/src/main/java/gripe/_90/megacells/MEGACells.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
public class MEGACells implements IAEAddonEntrypoint {
@Override
public void onAe2Initialized() {
initAll();
registerAll();

InitStorageCells.init();
InitUpgrades.init();

initCompression();
}

private void initAll() {
MEGABlocks.init();
MEGAItems.init();
MEGAParts.init();
Expand All @@ -28,19 +38,26 @@ public void onAe2Initialized() {
if (Utils.PLATFORM.isModLoaded("appbot")) {
AppBotItems.init();
}
}

MEGABlocks.getBlocks().forEach(b -> {
Registry.register(Registry.BLOCK, b.id(), b.block());
Registry.register(Registry.ITEM, b.id(), b.asItem());
});
MEGAItems.getItems().forEach(i -> Registry.register(Registry.ITEM, i.id(), i.asItem()));
MEGABlockEntities.getBlockEntityTypes().forEach((k, v) -> Registry.register(Registry.BLOCK_ENTITY_TYPE, k, v));
private void registerAll() {
for (var block : MEGABlocks.getBlocks()) {
Registry.register(Registry.BLOCK, block.id(), block.block());
Registry.register(Registry.ITEM, block.id(), block.asItem());
}

Registry.register(Registry.MENU, AppEng.makeId("mega_pattern_provider"), MEGAPatternProviderMenu.TYPE);
for (var item : MEGAItems.getItems()) {
Registry.register(Registry.ITEM, item.id(), item.asItem());
}

InitStorageCells.init();
InitUpgrades.init();
for (var blockEntity : MEGABlockEntities.getBlockEntityTypes().entrySet()) {
Registry.register(Registry.BLOCK_ENTITY_TYPE, blockEntity.getKey(), blockEntity.getValue());
}

Registry.register(Registry.MENU, AppEng.makeId("mega_pattern_provider"), MEGAPatternProviderMenu.TYPE);
}

private void initCompression() {
ServerLifecycleEvents.SERVER_STARTED.register(server -> CompressionService.INSTANCE.load());
ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, success) -> {
if (success) {
Expand Down
63 changes: 38 additions & 25 deletions fabric/src/main/java/gripe/_90/megacells/MEGACellsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,20 @@
public class MEGACellsClient implements IAEAddonEntrypoint {
@Override
public void onAe2Initialized() {
initScreens();
initBlockModels();
initItemModels();
initItemColors();
}

private void initScreens() {
ClientLifecycleEvents.CLIENT_STARTED.register(client -> {
InitScreens.register(MEGAPatternProviderMenu.TYPE, PatternProviderScreen<MEGAPatternProviderMenu>::new,
"/screens/megacells/mega_pattern_provider.json");
});
}

private void initBlockModels() {
for (var type : MEGACraftingUnitType.values()) {
ModelLoadingRegistry.INSTANCE.registerResourceProvider(resourceManager -> new SimpleModelLoader<>(
Utils.makeId("block/crafting/" + type.getAffix() + "_formed"),
Expand All @@ -60,31 +69,6 @@ public void onAe2Initialized() {

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

var cells = new ArrayList<>(MEGAItems.getItemCells());
cells.addAll(MEGAItems.getFluidCells());
cells.add(MEGAItems.BULK_ITEM_CELL);

var portables = new ArrayList<>(MEGAItems.getItemPortables());
portables.addAll(MEGAItems.getFluidPortables());

if (Utils.PLATFORM.isModLoaded("appbot")) {
cells.addAll(AppBotItems.getCells());
portables.addAll(AppBotItems.getPortables());
}

ColorProviderRegistry.ITEM.register(BasicStorageCell::getColor, cells.toArray(new ItemLike[0]));
ColorProviderRegistry.ITEM.register(PortableCellItem::getColor, portables.toArray(new ItemLike[0]));

ItemProperties.register(MEGABlocks.MEGA_ENERGY_CELL.asItem(), AppEng.makeId("fill_level"),
(is, level, entity, seed) -> {
var energyCell = (EnergyCellBlockItem) MEGABlocks.MEGA_ENERGY_CELL.asItem();

double curPower = energyCell.getAECurrentPower(is);
double maxPower = energyCell.getAEMaxPower(is);

return (float) (curPower / maxPower);
});

ModelsReloadCallback.EVENT.register(modelRegistry -> {
var customizers = new HashMap<ResourceLocation, Function<BakedModel, BakedModel>>();
customizers.put(MEGABlocks.CRAFTING_MONITOR.id(), model -> model instanceof MonitorBakedModel
Expand Down Expand Up @@ -115,4 +99,33 @@ public void onAe2Initialized() {
}
});
}

private void initItemModels() {
ItemProperties.register(MEGABlocks.MEGA_ENERGY_CELL.asItem(), AppEng.makeId("fill_level"),
(is, level, entity, seed) -> {
var energyCell = (EnergyCellBlockItem) MEGABlocks.MEGA_ENERGY_CELL.asItem();

double curPower = energyCell.getAECurrentPower(is);
double maxPower = energyCell.getAEMaxPower(is);

return (float) (curPower / maxPower);
});
}

private void initItemColors() {
var cells = new ArrayList<>(MEGAItems.getItemCells());
cells.addAll(MEGAItems.getFluidCells());
cells.add(MEGAItems.BULK_ITEM_CELL);

var portables = new ArrayList<>(MEGAItems.getItemPortables());
portables.addAll(MEGAItems.getFluidPortables());

if (Utils.PLATFORM.isModLoaded("appbot")) {
cells.addAll(AppBotItems.getCells());
portables.addAll(AppBotItems.getPortables());
}

ColorProviderRegistry.ITEM.register(BasicStorageCell::getColor, cells.toArray(new ItemLike[0]));
ColorProviderRegistry.ITEM.register(PortableCellItem::getColor, portables.toArray(new ItemLike[0]));
}
}
68 changes: 39 additions & 29 deletions forge/src/main/java/gripe/_90/megacells/forge/MEGACells.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,18 @@
@Mod(Utils.MODID)
public class MEGACells {
public MEGACells() {
initAll();

var bus = FMLJavaModLoadingContext.get().getModEventBus();
bus.addListener(this::registerAll);
bus.addListener(this::initCells);

initCompression();

DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> MEGACellsClient::new);
}

private void initAll() {
MEGABlocks.init();
MEGAItems.init();
MEGAParts.init();
Expand All @@ -44,44 +54,44 @@ public MEGACells() {
if (Utils.PLATFORM.isModLoaded("appbot")) {
AppBotItems.init();
}
}

bus.addListener((RegisterEvent event) -> {
if (event.getRegistryKey().equals(Registry.BLOCK_REGISTRY)) {
MEGABlocks.getBlocks().forEach(b -> {
ForgeRegistries.BLOCKS.register(b.id(), b.block());
ForgeRegistries.ITEMS.register(b.id(), b.asItem());
});
}
private void registerAll(RegisterEvent event) {
if (event.getRegistryKey().equals(Registry.BLOCK_REGISTRY)) {
MEGABlocks.getBlocks().forEach(b -> {
ForgeRegistries.BLOCKS.register(b.id(), b.block());
ForgeRegistries.ITEMS.register(b.id(), b.asItem());
});
}

if (event.getRegistryKey().equals(Registry.ITEM_REGISTRY)) {
MEGAItems.getItems().forEach(i -> ForgeRegistries.ITEMS.register(i.id(), i.asItem()));
}
if (event.getRegistryKey().equals(Registry.ITEM_REGISTRY)) {
MEGAItems.getItems().forEach(i -> ForgeRegistries.ITEMS.register(i.id(), i.asItem()));
}

if (event.getRegistryKey().equals(Registry.BLOCK_ENTITY_TYPE_REGISTRY)) {
MEGABlockEntities.getBlockEntityTypes().forEach(ForgeRegistries.BLOCK_ENTITY_TYPES::register);
}
if (event.getRegistryKey().equals(Registry.BLOCK_ENTITY_TYPE_REGISTRY)) {
MEGABlockEntities.getBlockEntityTypes().forEach(ForgeRegistries.BLOCK_ENTITY_TYPES::register);
}

if (event.getRegistryKey().equals(Registry.MENU_REGISTRY)) {
ForgeRegistries.MENU_TYPES.register(AppEng.makeId("mega_pattern_provider"),
MEGAPatternProviderMenu.TYPE);
}
});
if (event.getRegistryKey().equals(Registry.MENU_REGISTRY)) {
ForgeRegistries.MENU_TYPES.register(AppEng.makeId("mega_pattern_provider"),
MEGAPatternProviderMenu.TYPE);
}
}

bus.addListener((FMLCommonSetupEvent event) -> {
event.enqueueWork(InitStorageCells::init);
event.enqueueWork(InitUpgrades::init);
private void initCells(FMLCommonSetupEvent event) {
event.enqueueWork(InitStorageCells::init);
event.enqueueWork(InitUpgrades::init);

event.enqueueWork(() -> {
if (Utils.PLATFORM.isModLoaded("appmek")) {
AppMekIntegration.initUpgrades();
AppMekIntegration.initStorageCells();
}
});
event.enqueueWork(() -> {
if (Utils.PLATFORM.isModLoaded("appmek")) {
AppMekIntegration.initUpgrades();
AppMekIntegration.initStorageCells();
}
});
}

private void initCompression() {
MinecraftForge.EVENT_BUS.addListener((ServerStartedEvent event) -> CompressionService.INSTANCE.load());
MinecraftForge.EVENT_BUS.addListener((AddReloadListenerEvent event) -> CompressionService.INSTANCE.load());

DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> MEGACellsClient::new);
}
}
122 changes: 63 additions & 59 deletions forge/src/main/java/gripe/_90/megacells/forge/MEGACellsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,85 +42,89 @@
public class MEGACellsClient {
public MEGACellsClient() {
var bus = FMLJavaModLoadingContext.get().getModEventBus();
bus.addListener(this::initScreens);
bus.addListener(this::initModels);
bus.addListener(this::initModelRotation);
bus.addListener(this::initItemColors);
}

bus.addListener((FMLClientSetupEvent event) -> {
InitScreens.register(MEGAPatternProviderMenu.TYPE, PatternProviderScreen<MEGAPatternProviderMenu>::new,
"/screens/megacells/mega_pattern_provider.json");
});
private void initScreens(FMLClientSetupEvent ignoredEvent) {
InitScreens.register(MEGAPatternProviderMenu.TYPE, PatternProviderScreen<MEGAPatternProviderMenu>::new,
"/screens/megacells/mega_pattern_provider.json");
}

bus.addListener((ModelEvent.RegisterGeometryLoaders event) -> {
for (var type : MEGACraftingUnitType.values()) {
event.register("block/crafting/" + type.getAffix() + "_formed",
new SimpleModelLoader<>(() -> new CraftingCubeModel(new MEGACraftingUnitModelProvider(type))));
private void initModels(ModelEvent.RegisterGeometryLoaders event) {
for (var type : MEGACraftingUnitType.values()) {
event.register("block/crafting/" + type.getAffix() + "_formed",
new SimpleModelLoader<>(() -> new CraftingCubeModel(new MEGACraftingUnitModelProvider(type))));

ItemBlockRenderTypes.setRenderLayer(type.getDefinition().block(), RenderType.cutout());
}
ItemBlockRenderTypes.setRenderLayer(type.getDefinition().block(), RenderType.cutout());
}

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

ItemProperties.register(MEGABlocks.MEGA_ENERGY_CELL.asItem(), AppEng.makeId("fill_level"),
(is, level, entity, seed) -> {
var energyCell = (EnergyCellBlockItem) MEGABlocks.MEGA_ENERGY_CELL.asItem();
ItemProperties.register(MEGABlocks.MEGA_ENERGY_CELL.asItem(), AppEng.makeId("fill_level"),
(is, level, entity, seed) -> {
var energyCell = (EnergyCellBlockItem) MEGABlocks.MEGA_ENERGY_CELL.asItem();

double curPower = energyCell.getAECurrentPower(is);
double maxPower = energyCell.getAEMaxPower(is);
double curPower = energyCell.getAECurrentPower(is);
double maxPower = energyCell.getAEMaxPower(is);

return (float) (curPower / maxPower);
});
});

bus.addListener((RegisterColorHandlersEvent.Item event) -> {
var cells = new ArrayList<>(MEGAItems.getItemCells());
cells.addAll(MEGAItems.getFluidCells());
cells.add(MEGAItems.BULK_ITEM_CELL);
return (float) (curPower / maxPower);
});
}

var portables = new ArrayList<>(MEGAItems.getItemPortables());
portables.addAll(MEGAItems.getFluidPortables());
private void initModelRotation(ModelEvent.BakingCompleted event) {
var modelRegistry = event.getModels();
var customizers = new HashMap<ResourceLocation, Function<BakedModel, BakedModel>>();
customizers.put(MEGABlocks.CRAFTING_MONITOR.id(), model -> model instanceof MonitorBakedModel
? model
: new AutoRotatingBakedModel(model));

if (Utils.PLATFORM.isModLoaded("appmek")) {
cells.addAll(AppMekItems.getCells());
portables.addAll(AppMekItems.getPortables());
cells.add(AppMekItems.RADIOACTIVE_CHEMICAL_CELL);
for (var block : MEGABlocks.getBlocks()) {
if (!(block.block() instanceof CraftingUnitBlock)) {
customizers.put(block.id(), AutoRotatingBakedModel::new);
}
}

if (Utils.PLATFORM.isModLoaded("appbot")) {
cells.addAll(AppBotItems.getCells());
portables.addAll(AppBotItems.getPortables());
for (var location : modelRegistry.keySet()) {
if (!location.getNamespace().equals(Utils.MODID)) {
continue;
}

event.register(BasicStorageCell::getColor, cells.toArray(new ItemLike[0]));
event.register(PortableCellItem::getColor, portables.toArray(new ItemLike[0]));
});
var originalModel = modelRegistry.get(location);
var customizer = customizers.get(location);

bus.addListener((ModelEvent.BakingCompleted event) -> {
var modelRegistry = event.getModels();
var customizers = new HashMap<ResourceLocation, Function<BakedModel, BakedModel>>();
customizers.put(MEGABlocks.CRAFTING_MONITOR.id(), model -> model instanceof MonitorBakedModel
? model
: new AutoRotatingBakedModel(model));
if (customizer != null) {
var newModel = customizer.apply(originalModel);

for (var block : MEGABlocks.getBlocks()) {
if (!(block.block() instanceof CraftingUnitBlock)) {
customizers.put(block.id(), AutoRotatingBakedModel::new);
if (newModel != originalModel) {
modelRegistry.put(location, newModel);
}
}
}
}

for (var location : modelRegistry.keySet()) {
if (!location.getNamespace().equals(Utils.MODID)) {
continue;
}
private void initItemColors(RegisterColorHandlersEvent.Item event) {
var cells = new ArrayList<>(MEGAItems.getItemCells());
cells.addAll(MEGAItems.getFluidCells());
cells.add(MEGAItems.BULK_ITEM_CELL);

var originalModel = modelRegistry.get(location);
var customizer = customizers.get(location);
var portables = new ArrayList<>(MEGAItems.getItemPortables());
portables.addAll(MEGAItems.getFluidPortables());

if (customizer != null) {
var newModel = customizer.apply(originalModel);
if (Utils.PLATFORM.isModLoaded("appmek")) {
cells.addAll(AppMekItems.getCells());
portables.addAll(AppMekItems.getPortables());
cells.add(AppMekItems.RADIOACTIVE_CHEMICAL_CELL);
}

if (newModel != originalModel) {
modelRegistry.put(location, newModel);
}
}
}
});
if (Utils.PLATFORM.isModLoaded("appbot")) {
cells.addAll(AppBotItems.getCells());
portables.addAll(AppBotItems.getPortables());
}

event.register(BasicStorageCell::getColor, cells.toArray(new ItemLike[0]));
event.register(PortableCellItem::getColor, portables.toArray(new ItemLike[0]));
}
}

0 comments on commit 265d899

Please sign in to comment.