Skip to content

Commit

Permalink
Begin work on ME EMC Interface
Browse files Browse the repository at this point in the history
FIXME: screen won't fucking open for some reason
  • Loading branch information
62832 committed Apr 29, 2024
1 parent 6daeb68 commit 961d1fe
Show file tree
Hide file tree
Showing 22 changed files with 1,158 additions and 18 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ repositories {
minecraft {
mappings("official", "1.20.1")
copyIdeResources.set(true)
accessTransformer(file("src/main/resources/META-INF/accesstransformer.cfg"))

runs {
configureEach {
workingDirectory(file("run"))
property("forge.logging.console.level", "info")
property("forge.logging.console.level", "debug")

mods {
create(modId) {
Expand Down
78 changes: 63 additions & 15 deletions src/main/java/gripe/_90/appliede/AppliedE.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package gripe._90.appliede;

import java.math.BigInteger;
import java.util.function.Function;

import net.minecraft.Util;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.loading.FMLEnvironment;
Expand All @@ -16,15 +20,24 @@
import net.minecraftforge.registries.RegistryObject;

import appeng.api.networking.GridServices;
import appeng.api.parts.IPart;
import appeng.api.parts.IPartItem;
import appeng.api.parts.PartModels;
import appeng.blockentity.AEBaseBlockEntity;
import appeng.core.AppEng;
import appeng.items.parts.PartItem;
import appeng.items.parts.PartModelsHelper;

import gripe._90.appliede.iface.EMCInterfaceBlock;
import gripe._90.appliede.iface.EMCInterfaceBlockEntity;
import gripe._90.appliede.iface.EMCInterfaceMenu;
import gripe._90.appliede.iface.EMCInterfacePart;
import gripe._90.appliede.iface.EMCInterfaceScreen;
import gripe._90.appliede.key.EMCKeyType;
import gripe._90.appliede.key.EMCRenderer;
import gripe._90.appliede.module.EMCModulePart;
import gripe._90.appliede.module.KnowledgeService;
import gripe._90.appliede.pattern.TransmutationPatternItem;
import gripe._90.appliede.module.TransmutationPatternItem;

@Mod(AppliedE.MODID)
public final class AppliedE {
Expand All @@ -37,35 +50,70 @@ public static ResourceLocation id(String path) {
public static final BigInteger TIER_LIMIT = BigInteger.valueOf((long) Math.pow(2, 42));

private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);
private static final DeferredRegister<BlockEntityType<?>> BE_TYPES =
DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, MODID);
private static final DeferredRegister<MenuType<?>> MENU_TYPES =
DeferredRegister.create(ForgeRegistries.MENU_TYPES, AppEng.MOD_ID);
private static final DeferredRegister<CreativeModeTab> TABS =
DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MODID);

public static final RegistryObject<Item> EMC_MODULE = ITEMS.register(
"emc_module",
() -> Util.make(() -> {
PartModels.registerModels(PartModelsHelper.createModels(EMCModulePart.class));
return new PartItem<>(new Item.Properties(), EMCModulePart.class, EMCModulePart::new);
}));
public static final RegistryObject<Item> EMC_MODULE =
ITEMS.register("emc_module", () -> part(EMCModulePart.class, EMCModulePart::new));
public static final RegistryObject<Item> TRANSMUTATION_PATTERN =
ITEMS.register("transmutation_pattern", TransmutationPatternItem::new);

@SuppressWarnings("unused")
private static final RegistryObject<CreativeModeTab> TAB = TABS.register(MODID, () -> CreativeModeTab.builder()
.title(Component.translatable("mod." + MODID))
.icon(() -> EMC_MODULE.get().getDefaultInstance())
.displayItems((params, output) -> output.accept(EMC_MODULE.get()))
.build());
public static final RegistryObject<EMCInterfaceBlock> EMC_INTERFACE = BLOCKS.register("emc_interface", () -> {
var block = new EMCInterfaceBlock();
ITEMS.register("emc_interface", () -> new BlockItem(block, new Item.Properties()));
return block;
});
public static final RegistryObject<Item> CABLE_EMC_INTERFACE =
ITEMS.register("cable_emc_interface", () -> part(EMCInterfacePart.class, EMCInterfacePart::new));

@SuppressWarnings("DataFlowIssue")
public static final RegistryObject<BlockEntityType<EMCInterfaceBlockEntity>> EMC_INTERFACE_BE =
BE_TYPES.register("emc_interface", () -> {
var type = BlockEntityType.Builder.of(EMCInterfaceBlockEntity::new, EMC_INTERFACE.get())
.build(null);
EMC_INTERFACE.get().setBlockEntity(EMCInterfaceBlockEntity.class, type, null, null);
AEBaseBlockEntity.registerBlockEntityItem(
type, EMC_INTERFACE.get().asItem());
return type;
});

static {
MENU_TYPES.register("emc_interface", () -> EMCInterfaceMenu.TYPE);
TABS.register(MODID, () -> CreativeModeTab.builder()
.title(Component.translatable("mod." + MODID))
.icon(() -> EMC_MODULE.get().getDefaultInstance())
.displayItems((params, output) -> {
output.accept(EMC_MODULE.get());
output.accept(EMC_INTERFACE.get());
output.accept(CABLE_EMC_INTERFACE.get());
})
.build());
}

public AppliedE() {
var bus = FMLJavaModLoadingContext.get().getModEventBus();
ITEMS.register(bus);
BLOCKS.register(bus);
MENU_TYPES.register(bus);
BE_TYPES.register(bus);
TABS.register(bus);
bus.addListener(EMCKeyType::register);

GridServices.register(KnowledgeService.class, KnowledgeService.class);

if (FMLEnvironment.dist.isClient()) {
EMCRenderer.register();
bus.addListener(EMCRenderer::register);
bus.addListener(EMCInterfaceScreen::register);
}
}

private static <P extends IPart> Item part(Class<P> partClass, Function<IPartItem<P>, P> factory) {
PartModels.registerModels(PartModelsHelper.createModels(partClass));
return new PartItem<>(new Item.Properties(), partClass, factory);
}
}
46 changes: 46 additions & 0 deletions src/main/java/gripe/_90/appliede/iface/EMCInterfaceBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package gripe._90.appliede.iface;

import org.jetbrains.annotations.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;

import appeng.block.AEBaseEntityBlock;
import appeng.menu.locator.MenuLocators;
import appeng.util.InteractionUtil;

public class EMCInterfaceBlock extends AEBaseEntityBlock<EMCInterfaceBlockEntity> {
public EMCInterfaceBlock() {
super(metalProps());
}

@Override
public InteractionResult onActivated(
Level level,
BlockPos pos,
Player player,
InteractionHand hand,
@Nullable ItemStack heldItem,
BlockHitResult hit) {
if (InteractionUtil.isInAlternateUseMode(player)) {
return InteractionResult.PASS;
}

var be = this.getBlockEntity(level, pos);

if (be != null) {
if (!level.isClientSide()) {
be.openMenu(player, MenuLocators.forBlockEntity(be));
}

return InteractionResult.sidedSuccess(level.isClientSide());
}

return InteractionResult.PASS;
}
}
102 changes: 102 additions & 0 deletions src/main/java/gripe/_90/appliede/iface/EMCInterfaceBlockEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package gripe._90.appliede.iface;

import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;

import appeng.api.networking.GridHelper;
import appeng.api.networking.IGridNode;
import appeng.api.networking.IGridNodeListener;
import appeng.api.networking.IManagedGridNode;
import appeng.blockentity.grid.AENetworkBlockEntity;
import appeng.me.helpers.BlockEntityNodeListener;

import gripe._90.appliede.AppliedE;

public class EMCInterfaceBlockEntity extends AENetworkBlockEntity implements EMCInterfaceLogicHost {
private static final IGridNodeListener<EMCInterfaceBlockEntity> NODE_LISTENER = new BlockEntityNodeListener<>() {
@Override
public void onGridChanged(EMCInterfaceBlockEntity nodeOwner, IGridNode node) {
nodeOwner.logic.notifyNeighbours();
}
};

private final EMCInterfaceLogic logic = createLogic();

public EMCInterfaceBlockEntity(BlockPos pos, BlockState blockState) {
super(AppliedE.EMC_INTERFACE_BE.get(), pos, blockState);
}

protected EMCInterfaceLogic createLogic() {
return new EMCInterfaceLogic(getMainNode(), this);
}

@Override
protected IManagedGridNode createMainNode() {
return GridHelper.createManagedNode(this, NODE_LISTENER);
}

@Override
public EMCInterfaceLogic getInterfaceLogic() {
return logic;
}

@Override
public void onMainNodeStateChanged(IGridNodeListener.State reason) {
if (getMainNode().hasGridBooted()) {
logic.notifyNeighbours();
}
}

@Override
public void saveAdditional(CompoundTag data) {
super.saveAdditional(data);
logic.writeToNBT(data);
}

@Override
public void loadTag(CompoundTag data) {
super.loadTag(data);
logic.readFromNBT(data);
}

@Override
public void addAdditionalDrops(Level level, BlockPos pos, List<ItemStack> drops) {
super.addAdditionalDrops(level, pos, drops);
logic.addDrops(drops);
}

@Override
public void clearContent() {
super.clearContent();
getStorage().clear();
}

@Override
public ItemStack getMainMenuIcon() {
return AppliedE.EMC_INTERFACE.get().asItem().getDefaultInstance();
}

@NotNull
@Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
var capability = logic.getCapability(cap);
return capability.isPresent() ? capability : super.getCapability(cap, side);
}

@Override
public void invalidateCaps() {
super.invalidateCaps();
logic.invalidateCaps();
}
}
Loading

0 comments on commit 961d1fe

Please sign in to comment.