From ebc49b03c439d3677e0deb3ad4cadda432eefa20 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 7 Mar 2025 11:45:35 +0700 Subject: [PATCH] Replace shulker box gui --- .../otm/mixin/ShulkerBoxBlockEntityMixin.java | 16 +++++ .../mc/otm/OverdriveThatMatters.kt | 3 +- .../vanilla/AbstractVanillaChestMenu.kt | 30 ++++++++ .../mc/otm/compat/vanilla/MatteryChestMenu.kt | 72 ++++--------------- .../compat/vanilla/MatteryShulkerBoxMenu.kt | 32 +++++++++ ...ryChestScreen.kt => VanillaChestScreen.kt} | 4 +- .../mc/otm/compat/vanilla/VanillaMenuTypes.kt | 41 +++++++++++ .../overdrive_that_matters.mixins.json | 3 +- 8 files changed, 139 insertions(+), 62 deletions(-) create mode 100644 src/main/java/ru/dbotthepony/mc/otm/mixin/ShulkerBoxBlockEntityMixin.java create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/AbstractVanillaChestMenu.kt create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/MatteryShulkerBoxMenu.kt rename src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/{MatteryChestScreen.kt => VanillaChestScreen.kt} (85%) create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/VanillaMenuTypes.kt diff --git a/src/main/java/ru/dbotthepony/mc/otm/mixin/ShulkerBoxBlockEntityMixin.java b/src/main/java/ru/dbotthepony/mc/otm/mixin/ShulkerBoxBlockEntityMixin.java new file mode 100644 index 000000000..5e44c483b --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/mixin/ShulkerBoxBlockEntityMixin.java @@ -0,0 +1,16 @@ +package ru.dbotthepony.mc.otm.mixin; + +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import ru.dbotthepony.mc.otm.compat.vanilla.MatteryShulkerBoxMenu; + +@Mixin(ShulkerBoxBlockEntity.class) +public abstract class ShulkerBoxBlockEntityMixin { + @Overwrite(remap = false) + public AbstractContainerMenu createMenu(int p_59312_, Inventory p_59313_) { + return new MatteryShulkerBoxMenu(p_59312_, p_59313_, (ShulkerBoxBlockEntity) (Object) this); + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt index b020f9548..bcb33c4ef 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt @@ -44,6 +44,7 @@ import ru.dbotthepony.mc.otm.client.render.blockentity.MatterBatteryBankRenderer import ru.dbotthepony.mc.otm.compat.curios.isCuriosLoaded import ru.dbotthepony.mc.otm.compat.curios.onCuriosSlotModifiersUpdated import ru.dbotthepony.mc.otm.compat.vanilla.MatteryChestMenu +import ru.dbotthepony.mc.otm.compat.vanilla.VanillaMenuTypes import ru.dbotthepony.mc.otm.config.AndroidConfig import ru.dbotthepony.mc.otm.config.CablesConfig import ru.dbotthepony.mc.otm.config.ClientConfig @@ -127,7 +128,7 @@ object OverdriveThatMatters { MLootNumberProviders.register(MOD_BUS) StorageStack.register(MOD_BUS) - MatteryChestMenu.register(MOD_BUS) + VanillaMenuTypes.register(MOD_BUS) MCreativeTabs.initialize(MOD_BUS) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/AbstractVanillaChestMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/AbstractVanillaChestMenu.kt new file mode 100644 index 000000000..c67a3bc68 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/AbstractVanillaChestMenu.kt @@ -0,0 +1,30 @@ +package ru.dbotthepony.mc.otm.compat.vanilla + +import net.minecraft.world.Container +import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.entity.player.Player +import net.minecraft.world.inventory.MenuType +import ru.dbotthepony.mc.otm.menu.MatteryMenu +import ru.dbotthepony.mc.otm.menu.MatteryMenuSlot + +abstract class AbstractVanillaChestMenu( + type: MenuType<*>, + containerId: Int, + inventory: Inventory, + val container: Container +) : MatteryMenu(type, containerId, inventory) { + abstract val rows: Int + abstract val columns: Int + + abstract val containerSlots: List + val sort = SortInput(container, playerSortSettings) + + override fun stillValid(player: Player): Boolean { + return container.stillValid(player) + } + + override fun removed(player: Player) { + super.removed(player) + container.stopOpen(player) + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/MatteryChestMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/MatteryChestMenu.kt index 231ef450d..25bb68fee 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/MatteryChestMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/MatteryChestMenu.kt @@ -1,118 +1,74 @@ package ru.dbotthepony.mc.otm.compat.vanilla -import net.minecraft.core.registries.Registries 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.flag.FeatureFlags import net.minecraft.world.inventory.MenuType -import net.neoforged.bus.api.IEventBus -import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent -import ru.dbotthepony.mc.otm.OverdriveThatMatters -import ru.dbotthepony.mc.otm.menu.MatteryMenu +import ru.dbotthepony.mc.otm.container.EnhancedContainer import ru.dbotthepony.mc.otm.menu.MatteryMenuSlot import ru.dbotthepony.mc.otm.menu.makeSlots -import ru.dbotthepony.mc.otm.registry.MDeferredRegister class MatteryChestMenu( type: MenuType<*>, containerId: Int, - inventory: Inventory, val rows: Int, val columns: Int, - val container: Container = SimpleContainer(rows * columns) -) : MatteryMenu(type, containerId, inventory) { - val chestSlots = makeSlots(container, ::MatteryMenuSlot) - val sort = SortInput(container, playerSortSettings) + inventory: Inventory, override val rows: Int, override val columns: Int, + container: Container = EnhancedContainer(rows * columns) +) : AbstractVanillaChestMenu(type, containerId, inventory, container) { + override val containerSlots = makeSlots(container, ::MatteryMenuSlot) init { require(rows * columns == container.containerSize) { "Provided container $container has different dimensions than specified rows x columns: ${container.containerSize} vs $rows x $columns (${rows * columns})" } container.startOpen(player) - addStorageSlot(chestSlots) + addStorageSlot(containerSlots) addInventorySlots() } - override fun stillValid(player: Player): Boolean { - return container.stillValid(player) - } - - override fun removed(player: Player) { - super.removed(player) - container.stopOpen(player) - } - companion object { - private val registrar = MDeferredRegister(Registries.MENU, OverdriveThatMatters.MOD_ID) - - private val GENERIC_9x1 by registrar.register("generic_9x1") { MenuType(::c9x1, FeatureFlags.VANILLA_SET) } - private val GENERIC_9x2 by registrar.register("generic_9x2") { MenuType(::c9x2, FeatureFlags.VANILLA_SET) } - private val GENERIC_9x3 by registrar.register("generic_9x3") { MenuType(::c9x3, FeatureFlags.VANILLA_SET) } - private val GENERIC_9x4 by registrar.register("generic_9x4") { MenuType(::c9x4, FeatureFlags.VANILLA_SET) } - private val GENERIC_9x5 by registrar.register("generic_9x5") { MenuType(::c9x5, FeatureFlags.VANILLA_SET) } - private val GENERIC_9x6 by registrar.register("generic_9x6") { MenuType(::c9x6, FeatureFlags.VANILLA_SET) } - private val GENERIC_3x3 by registrar.register("generic_3x3") { MenuType(::c3x3, FeatureFlags.VANILLA_SET) } - private val HOPPER by registrar.register("hopper") { MenuType(::hopper, FeatureFlags.VANILLA_SET) } - @JvmStatic @JvmOverloads fun c9x1(containerId: Int, inventory: Inventory, container: Container = SimpleContainer(9)): MatteryChestMenu { - return MatteryChestMenu(GENERIC_9x1, containerId, inventory, 1, 9, container) + return MatteryChestMenu(VanillaMenuTypes.GENERIC_9x1, containerId, inventory, 1, 9, container) } @JvmStatic @JvmOverloads fun c9x2(containerId: Int, inventory: Inventory, container: Container = SimpleContainer(9 * 2)): MatteryChestMenu { - return MatteryChestMenu(GENERIC_9x2, containerId, inventory, 2, 9, container) + return MatteryChestMenu(VanillaMenuTypes.GENERIC_9x2, containerId, inventory, 2, 9, container) } @JvmStatic @JvmOverloads fun c9x3(containerId: Int, inventory: Inventory, container: Container = SimpleContainer(9 * 3)): MatteryChestMenu { - return MatteryChestMenu(GENERIC_9x3, containerId, inventory, 3, 9, container) + return MatteryChestMenu(VanillaMenuTypes.GENERIC_9x3, containerId, inventory, 3, 9, container) } @JvmStatic @JvmOverloads fun c9x4(containerId: Int, inventory: Inventory, container: Container = SimpleContainer(9 * 4)): MatteryChestMenu { - return MatteryChestMenu(GENERIC_9x4, containerId, inventory, 4, 9, container) + return MatteryChestMenu(VanillaMenuTypes.GENERIC_9x4, containerId, inventory, 4, 9, container) } @JvmStatic @JvmOverloads fun c9x5(containerId: Int, inventory: Inventory, container: Container = SimpleContainer(9 * 5)): MatteryChestMenu { - return MatteryChestMenu(GENERIC_9x5, containerId, inventory, 5, 9, container) + return MatteryChestMenu(VanillaMenuTypes.GENERIC_9x5, containerId, inventory, 5, 9, container) } @JvmStatic @JvmOverloads fun c9x6(containerId: Int, inventory: Inventory, container: Container = SimpleContainer(9 * 6)): MatteryChestMenu { - return MatteryChestMenu(GENERIC_9x6, containerId, inventory, 6, 9, container) + return MatteryChestMenu(VanillaMenuTypes.GENERIC_9x6, containerId, inventory, 6, 9, container) } @JvmStatic @JvmOverloads fun c3x3(containerId: Int, inventory: Inventory, container: Container = SimpleContainer(3 * 3)): MatteryChestMenu { - return MatteryChestMenu(GENERIC_3x3, containerId, inventory, 3, 3, container) + return MatteryChestMenu(VanillaMenuTypes.GENERIC_3x3, containerId, inventory, 3, 3, container) } @JvmStatic @JvmOverloads fun hopper(containerId: Int, inventory: Inventory, container: Container = SimpleContainer(5)): MatteryChestMenu { - return MatteryChestMenu(HOPPER, containerId, inventory, 1, 5, container) - } - - fun register(bus: IEventBus) { - registrar.register(bus) - bus.addListener(this::registerScreens) - } - - private fun registerScreens(event: RegisterMenuScreensEvent) { - event.register(GENERIC_9x1, ::MatteryChestScreen) - event.register(GENERIC_9x2, ::MatteryChestScreen) - event.register(GENERIC_9x3, ::MatteryChestScreen) - event.register(GENERIC_9x4, ::MatteryChestScreen) - event.register(GENERIC_9x5, ::MatteryChestScreen) - event.register(GENERIC_9x6, ::MatteryChestScreen) - event.register(GENERIC_3x3, ::MatteryChestScreen) - event.register(HOPPER, ::MatteryChestScreen) + return MatteryChestMenu(VanillaMenuTypes.HOPPER, containerId, inventory, 1, 5, container) } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/MatteryShulkerBoxMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/MatteryShulkerBoxMenu.kt new file mode 100644 index 000000000..01bd06a14 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/MatteryShulkerBoxMenu.kt @@ -0,0 +1,32 @@ +package ru.dbotthepony.mc.otm.compat.vanilla + +import net.minecraft.world.Container +import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.item.ItemStack +import ru.dbotthepony.mc.otm.container.EnhancedContainer +import ru.dbotthepony.mc.otm.menu.MatteryMenuSlot +import ru.dbotthepony.mc.otm.menu.makeSlots + +class MatteryShulkerBoxMenu( + containerId: Int, + inventory: Inventory, + container: Container = EnhancedContainer(27) +) : AbstractVanillaChestMenu(VanillaMenuTypes.SHULKER_BOX, containerId, inventory, container) { + override val containerSlots = makeSlots(container, ::Slot) + override val rows: Int + get() = 3 + override val columns: Int + get() = 9 + + init { + container.startOpen(player) + addStorageSlot(containerSlots) + addInventorySlots() + } + + class Slot(container: Container, slot: Int) : MatteryMenuSlot(container, slot) { + override fun mayPlace(stack: ItemStack): Boolean { + return super.mayPlace(stack) && stack.item.canFitInsideContainerItems() + } + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/MatteryChestScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/VanillaChestScreen.kt similarity index 85% rename from src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/MatteryChestScreen.kt rename to src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/VanillaChestScreen.kt index 316a948c9..eaa1f0976 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/MatteryChestScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/VanillaChestScreen.kt @@ -11,7 +11,7 @@ import ru.dbotthepony.mc.otm.client.screen.panels.slot.AbstractSlotPanel import ru.dbotthepony.mc.otm.client.screen.panels.slot.SlotPanel import ru.dbotthepony.mc.otm.client.screen.panels.util.GridPanel -class MatteryChestScreen(menu: MatteryChestMenu, inventory: Inventory, title: Component) : MatteryScreen(menu, inventory, title) { +class VanillaChestScreen(menu: AbstractVanillaChestMenu, inventory: Inventory, title: Component) : MatteryScreen(menu, inventory, title) { override fun makeMainFrame(): FramePanel> { val frame = FramePanel.padded(this, AbstractSlotPanel.SIZE * menu.columns.coerceAtLeast(9), AbstractSlotPanel.SIZE * menu.rows + 4f, title) @@ -22,7 +22,7 @@ class MatteryChestScreen(menu: MatteryChestMenu, inventory: Inventory, title: Co grid.dock = Dock.FILL grid.gravity = RenderGravity.BOTTOM_CENTER - for (slot in menu.chestSlots) + for (slot in menu.containerSlots) SlotPanel(this, grid, slot) val controls = DeviceControls(this, frame) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/VanillaMenuTypes.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/VanillaMenuTypes.kt new file mode 100644 index 000000000..8e7c52865 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/vanilla/VanillaMenuTypes.kt @@ -0,0 +1,41 @@ +package ru.dbotthepony.mc.otm.compat.vanilla + +import net.minecraft.core.registries.Registries +import net.minecraft.world.flag.FeatureFlags +import net.minecraft.world.inventory.MenuType +import net.neoforged.bus.api.IEventBus +import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent +import ru.dbotthepony.mc.otm.OverdriveThatMatters +import ru.dbotthepony.mc.otm.registry.MDeferredRegister + +object VanillaMenuTypes { + private val registrar = MDeferredRegister(Registries.MENU, OverdriveThatMatters.MOD_ID) + + val GENERIC_9x1 by registrar.register("generic_9x1") { MenuType(MatteryChestMenu::c9x1, FeatureFlags.VANILLA_SET) } + val GENERIC_9x2 by registrar.register("generic_9x2") { MenuType(MatteryChestMenu::c9x2, FeatureFlags.VANILLA_SET) } + val GENERIC_9x3 by registrar.register("generic_9x3") { MenuType(MatteryChestMenu::c9x3, FeatureFlags.VANILLA_SET) } + val GENERIC_9x4 by registrar.register("generic_9x4") { MenuType(MatteryChestMenu::c9x4, FeatureFlags.VANILLA_SET) } + val GENERIC_9x5 by registrar.register("generic_9x5") { MenuType(MatteryChestMenu::c9x5, FeatureFlags.VANILLA_SET) } + val GENERIC_9x6 by registrar.register("generic_9x6") { MenuType(MatteryChestMenu::c9x6, FeatureFlags.VANILLA_SET) } + val GENERIC_3x3 by registrar.register("generic_3x3") { MenuType(MatteryChestMenu::c3x3, FeatureFlags.VANILLA_SET) } + val HOPPER by registrar.register("hopper") { MenuType(MatteryChestMenu::hopper, FeatureFlags.VANILLA_SET) } + + val SHULKER_BOX by registrar.register("shulker_box") { MenuType(::MatteryShulkerBoxMenu, FeatureFlags.VANILLA_SET) } + + fun register(bus: IEventBus) { + registrar.register(bus) + bus.addListener(this::registerScreens) + } + + private fun registerScreens(event: RegisterMenuScreensEvent) { + event.register(GENERIC_9x1, ::VanillaChestScreen) + event.register(GENERIC_9x2, ::VanillaChestScreen) + event.register(GENERIC_9x3, ::VanillaChestScreen) + event.register(GENERIC_9x4, ::VanillaChestScreen) + event.register(GENERIC_9x5, ::VanillaChestScreen) + event.register(GENERIC_9x6, ::VanillaChestScreen) + event.register(GENERIC_3x3, ::VanillaChestScreen) + event.register(HOPPER, ::VanillaChestScreen) + event.register(SHULKER_BOX, ::VanillaChestScreen) + } +} diff --git a/src/main/resources/overdrive_that_matters.mixins.json b/src/main/resources/overdrive_that_matters.mixins.json index f317fa83a..8cc7a2020 100644 --- a/src/main/resources/overdrive_that_matters.mixins.json +++ b/src/main/resources/overdrive_that_matters.mixins.json @@ -19,7 +19,8 @@ "DispenserBlockEntityMixin", "GuiGraphicsMixin", "BlockStateBaseMixin", - "LevelMixin" + "LevelMixin", + "ShulkerBoxBlockEntityMixin" ], "client": [ "MixinGameRenderer",