diff --git a/src/main/kotlin/org/neoflock/neocomputers/NeoComputers.kt b/src/main/kotlin/org/neoflock/neocomputers/NeoComputers.kt index 90ac23f..f4350d4 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/NeoComputers.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/NeoComputers.kt @@ -31,6 +31,7 @@ object NeoComputers { BlockEntities.BLOCKENTITIES.register() BlockEntities.registerPowerBlocks() Menus.MENUS.register() + Menus.registerScreens() Tabs.TABS.register() ClientLifecycleEvent.CLIENT_SETUP.register { diff --git a/src/main/kotlin/org/neoflock/neocomputers/block/Generators.kt b/src/main/kotlin/org/neoflock/neocomputers/block/Generators.kt index 38eec00..09371a3 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/block/Generators.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/block/Generators.kt @@ -1,5 +1,6 @@ package org.neoflock.neocomputers.block +import dev.architectury.registry.menu.MenuRegistry import net.minecraft.core.BlockPos import net.minecraft.network.chat.ChatType import net.minecraft.network.chat.OutgoingChatMessage @@ -33,13 +34,8 @@ class CombustionGeneratorBlock : NodeBlock(), EntityBlock { ): InteractionResult { if(!level.isClientSide()) { val sp = player as ServerPlayer - val ent = level.getBlockEntity(blockPos, BlockEntities.COMBUSTGEN_ENTITY.get()) - if(ent.isPresent) { - val bust = ent.get() - val fuel = bust.stacks[0] - val msg = PlayerChatMessage.system("${fuel.displayName.string} x ${fuel.count} (${bust.node.getEnergy()} / ${bust.node.getEnergyCapacity()} J)") - sp.sendChatMessage(OutgoingChatMessage.create(msg), false, ChatType.bind(ChatType.CHAT, player)) - } + val ent = level.getBlockEntity(blockPos, BlockEntities.COMBUSTGEN_ENTITY.get()).get() + MenuRegistry.openMenu(sp, ent) } return InteractionResult.SUCCESS } diff --git a/src/main/kotlin/org/neoflock/neocomputers/entity/BlockEntities.kt b/src/main/kotlin/org/neoflock/neocomputers/entity/BlockEntities.kt index e735c53..ec699c1 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/entity/BlockEntities.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/entity/BlockEntities.kt @@ -60,6 +60,7 @@ object BlockEntities { fun registerPowerBlocks() { PowerManager.registerPowerBlockEntity(CAPACITOR_ENTITY.get()) + PowerManager.registerPowerBlockEntity(SOLARGEN_ENTITY.get()) PowerManager.registerPowerBlockEntity(COMBUSTGEN_ENTITY.get()) } } \ No newline at end of file diff --git a/src/main/kotlin/org/neoflock/neocomputers/entity/CombustionGeneratorBlockEntity.kt b/src/main/kotlin/org/neoflock/neocomputers/entity/CombustionGeneratorBlockEntity.kt index 2dbb334..f2c0655 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/entity/CombustionGeneratorBlockEntity.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/entity/CombustionGeneratorBlockEntity.kt @@ -2,17 +2,22 @@ package org.neoflock.neocomputers.entity import net.minecraft.core.BlockPos import net.minecraft.core.NonNullList +import net.minecraft.network.chat.Component +import net.minecraft.world.MenuProvider +import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Player +import net.minecraft.world.inventory.AbstractContainerMenu import net.minecraft.world.item.ItemStack import net.minecraft.world.level.block.state.BlockState import org.neoflock.neocomputers.block.NodeBlockEntity +import org.neoflock.neocomputers.gui.menu.CombustionGeneratorMenu import org.neoflock.neocomputers.network.Networking import org.neoflock.neocomputers.network.PowerRole import org.neoflock.neocomputers.utils.GenericContainer import org.neoflock.neocomputers.utils.ContainerUtils import kotlin.math.min -class CombustionGeneratorBlockEntity(blockPos: BlockPos, blockState: BlockState) : NodeBlockEntity(BlockEntities.COMBUSTGEN_ENTITY.get(), blockPos, blockState), GenericContainer { +class CombustionGeneratorBlockEntity(blockPos: BlockPos, blockState: BlockState) : NodeBlockEntity(BlockEntities.COMBUSTGEN_ENTITY.get(), blockPos, blockState), GenericContainer, MenuProvider { val energyPerTick: Long = 50 var energy: Long = 0 @@ -70,4 +75,8 @@ class CombustionGeneratorBlockEntity(blockPos: BlockPos, blockState: BlockState) burningTimeRemaining = ContainerUtils.getBurningTime(fuel) ?: 0 fuel.count-- } + + override fun getDisplayName(): Component? = Component.translatable("block.neocomputers.combustgen") + + override fun createMenu(i: Int, inventory: Inventory, player: Player) = CombustionGeneratorMenu(i, inventory, this) } \ No newline at end of file diff --git a/src/main/kotlin/org/neoflock/neocomputers/gui/menu/CombustionGeneratorMenu.kt b/src/main/kotlin/org/neoflock/neocomputers/gui/menu/CombustionGeneratorMenu.kt new file mode 100644 index 0000000..15e66a9 --- /dev/null +++ b/src/main/kotlin/org/neoflock/neocomputers/gui/menu/CombustionGeneratorMenu.kt @@ -0,0 +1,67 @@ +package org.neoflock.neocomputers.gui.menu + +import net.minecraft.world.Container +import net.minecraft.world.SimpleContainer +import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.entity.player.Player +import net.minecraft.world.inventory.AbstractContainerMenu +import net.minecraft.world.inventory.Slot +import net.minecraft.world.item.ItemStack + +class CombustionGeneratorMenu: AbstractContainerMenu { + var container: Container + + // Client-side constructor, idk forge tells me to do this + constructor(id: Int, inventory: Inventory): this(id, inventory, SimpleContainer(1)) + + // Server-side constructor + constructor(id: Int, inventory: Inventory, container: Container): super(Menus.COMBUSTGEN_MENU.get(), id) { + this.container = container + + container.startOpen(inventory.player) + + this.addSlot(Slot(container, 0, 80, 35)) + + // Based off the code in ChestMenu + for (l in 0..2) { + for (m in 0..8) { + this.addSlot(Slot(inventory, m + l * 9 + 9, 8 + m * 18, 84 + l * 18)) + } + } + + for (l in 0..8) { + this.addSlot(Slot(inventory, l, 8 + l * 18, 84 + 3 * 18 + 4)) + } + } + + // taken from https://docs.fabricmc.net/develop/blocks/container-menus + override fun quickMoveStack(player: Player, i: Int): ItemStack? { + val slot = slots[i] + + if(!slot.hasItem()) return ItemStack.EMPTY + + val stack = slot.item + val copied = stack.copy() + val contSize = container.containerSize + + if(i < contSize) { + if(!this.moveItemStackTo(stack, contSize, slots.size, true)) { + return ItemStack.EMPTY + } + } else if(!this.moveItemStackTo(stack, 0, contSize, false)) { + return ItemStack.EMPTY + } + + if(stack.isEmpty) { + slot.setByPlayer(ItemStack.EMPTY) + } else { + slot.setChanged() + } + + return copied + } + + override fun stillValid(player: Player): Boolean { + return container.stillValid(player) + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/neoflock/neocomputers/gui/menu/Menus.kt b/src/main/kotlin/org/neoflock/neocomputers/gui/menu/Menus.kt index ee34739..e20439a 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/gui/menu/Menus.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/gui/menu/Menus.kt @@ -3,13 +3,22 @@ package org.neoflock.neocomputers.gui.menu; import dev.architectury.registry.menu.MenuRegistry import dev.architectury.registry.registries.DeferredRegister import dev.architectury.registry.registries.RegistrySupplier +import net.minecraft.client.gui.screens.MenuScreens import net.minecraft.core.registries.Registries +import net.minecraft.world.flag.FeatureFlagSet +import net.minecraft.world.flag.FeatureFlags import net.minecraft.world.inventory.MenuType import org.neoflock.neocomputers.NeoComputers import org.neoflock.neocomputers.gui.menu.ScreenMenu +import org.neoflock.neocomputers.gui.screen.CombustionGeneratorScreen object Menus { val MENUS: DeferredRegister> = DeferredRegister.create(NeoComputers.MODID, Registries.MENU) - val SCREEN_MENU: RegistrySupplier> = MENUS.register("screen_menu") { MenuRegistry.of(::ScreenMenu)} // "deprecated" my ass + val SCREEN_MENU: RegistrySupplier> = MENUS.register("screen_menu") { MenuType(::ScreenMenu, FeatureFlagSet.of()) } + val COMBUSTGEN_MENU: RegistrySupplier> = MENUS.register("combustgen_menu") { MenuType(::CombustionGeneratorMenu, FeatureFlagSet.of() ) } + + fun registerScreens() { + MenuScreens.register(Menus.COMBUSTGEN_MENU.get(), {m: CombustionGeneratorMenu, u, comp -> CombustionGeneratorScreen(m, u, comp)}) + } } \ No newline at end of file diff --git a/src/main/kotlin/org/neoflock/neocomputers/gui/screen/CombustionGeneratorScreen.kt b/src/main/kotlin/org/neoflock/neocomputers/gui/screen/CombustionGeneratorScreen.kt new file mode 100644 index 0000000..07d372b --- /dev/null +++ b/src/main/kotlin/org/neoflock/neocomputers/gui/screen/CombustionGeneratorScreen.kt @@ -0,0 +1,29 @@ +package org.neoflock.neocomputers.gui.screen +import net.minecraft.client.gui.GuiGraphics +import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen +import net.minecraft.network.chat.Component +import net.minecraft.resources.ResourceLocation +import net.minecraft.world.entity.player.Inventory +import org.neoflock.neocomputers.NeoComputers +import org.neoflock.neocomputers.gui.menu.CombustionGeneratorMenu + +class CombustionGeneratorScreen(abstractContainerMenu: CombustionGeneratorMenu, inventory: Inventory, component: Component) : AbstractContainerScreen(abstractContainerMenu, inventory, component) { + override fun init() { + super.init() + + this.titleLabelX = (this.imageWidth - this.font.width(this.title)) / 2 + } + + override fun renderBg(guiGraphics: GuiGraphics, f: Float, i: Int, j: Int) { + val cx = (width - imageWidth) / 2 + val cy = (height - imageHeight) / 2 + + val containerTexture: ResourceLocation = ResourceLocation.fromNamespaceAndPath(NeoComputers.MODID, "textures/gui/linux.png") + guiGraphics.blitSprite(containerTexture, cx, cy, imageWidth, imageHeight) + } + + override fun render(graphics: GuiGraphics, mouseX: Int, mouseY: Int, something: Float) { + super.render(graphics, mouseX, mouseY, something) + super.renderTooltip(graphics, mouseX, mouseY) + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/neoflock/neocomputers/utils/ContainerUtils.kt b/src/main/kotlin/org/neoflock/neocomputers/utils/ContainerUtils.kt index 892ba92..689d05d 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/utils/ContainerUtils.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/utils/ContainerUtils.kt @@ -1,6 +1,7 @@ package org.neoflock.neocomputers.utils import dev.architectury.registry.fuel.FuelRegistry +import net.minecraft.world.inventory.AbstractContainerMenu import net.minecraft.world.item.ItemStack // mewhenthe, aka e, will have me publicly executed for this code diff --git a/src/main/resources/assets/neocomputers/lang/en_us.json b/src/main/resources/assets/neocomputers/lang/en_us.json index 42ba6b1..4bc3f36 100644 --- a/src/main/resources/assets/neocomputers/lang/en_us.json +++ b/src/main/resources/assets/neocomputers/lang/en_us.json @@ -1,4 +1,5 @@ { "neocomputers.confirm": "Confirm", - "neocomputers.cancel": "Cancel" + "neocomputers.cancel": "Cancel", + "block.neocomputers.combustgen": "Combustion Generator" } \ No newline at end of file diff --git a/src/main/resources/pack.mcmeta b/src/main/resources/pack.mcmeta index d6c6861..1958e72 100644 --- a/src/main/resources/pack.mcmeta +++ b/src/main/resources/pack.mcmeta @@ -1,6 +1,6 @@ { "pack": { "description": "${name}", - "pack_format": 15 + "pack_format": 48 } } \ No newline at end of file