From f7ca3a1e1176c4454d4b3419492619b045b712d5 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 16 Aug 2022 17:41:45 +0700 Subject: [PATCH] Instead of iterating slot indexes iterate slots themselves --- .../otm/client/screen/MatterPanelScreen.java | 48 +++---- .../mc/otm/client/screen/MatteryScreen.java | 4 +- src/main/kotlin/ru/dbotthepony/mc/otm/Ext.kt | 17 +++ .../mc/otm/client/screen/BatteryBankScreen.kt | 4 +- .../mc/otm/client/screen/CargoCrateScreen.kt | 2 +- .../mc/otm/client/screen/DriveRackScreen.kt | 8 +- .../otm/client/screen/MatterBottlerScreen.kt | 4 +- .../screen/MatterCapacitorBankScreen.kt | 4 +- .../client/screen/MatterReplicatorScreen.kt | 6 +- .../otm/client/screen/PatternStorageScreen.kt | 4 +- .../mc/otm/menu/AndroidStationMenu.kt | 14 +- .../mc/otm/menu/BatteryBankMenu.kt | 18 +-- .../dbotthepony/mc/otm/menu/CargoCrateMenu.kt | 15 +-- .../mc/otm/menu/ChemicalGeneratorMenu.kt | 18 +-- .../dbotthepony/mc/otm/menu/DriveRackMenu.kt | 22 +-- .../mc/otm/menu/DriveViewerMenu.kt | 18 +-- .../mc/otm/menu/EnergyCounterMenu.kt | 8 +- .../mc/otm/menu/ItemMonitorMenu.kt | 40 +++--- .../mc/otm/menu/MatterBottlerMenu.kt | 19 +-- .../mc/otm/menu/MatterCapacitorBankMenu.kt | 28 ++-- .../mc/otm/menu/MatterDecomposerMenu.kt | 21 ++- .../mc/otm/menu/MatterPanelMenu.kt | 20 +-- .../mc/otm/menu/MatterRecyclerMenu.kt | 10 +- .../mc/otm/menu/MatterReplicatorMenu.kt | 16 +-- .../mc/otm/menu/MatterScannerMenu.kt | 9 +- .../ru/dbotthepony/mc/otm/menu/MatteryMenu.kt | 126 +++++++++++------- .../mc/otm/menu/MatteryPoweredMenu.kt | 4 +- .../mc/otm/menu/PatternStorageMenu.kt | 24 +--- .../dbotthepony/mc/otm/menu/PlatePressMenu.kt | 7 +- .../dbotthepony/mc/otm/menu/StorageBusMenu.kt | 4 +- .../mc/otm/menu/StorageExporterMenu.kt | 4 +- .../mc/otm/menu/StorageImporterMenu.kt | 4 +- .../mc/otm/menu/StoragePowerSupplierMenu.kt | 4 +- 33 files changed, 250 insertions(+), 304 deletions(-) diff --git a/src/main/java/ru/dbotthepony/mc/otm/client/screen/MatterPanelScreen.java b/src/main/java/ru/dbotthepony/mc/otm/client/screen/MatterPanelScreen.java index d15284693..2ca8e2455 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/client/screen/MatterPanelScreen.java +++ b/src/main/java/ru/dbotthepony/mc/otm/client/screen/MatterPanelScreen.java @@ -77,10 +77,10 @@ public class MatterPanelScreen extends MatteryScreen { scroll_panel.setDock(Dock.RIGHT); scroll_panel.setupRowMultiplier(() -> { if (tasks_tab.isActive()) { - return menu.tasks.size() / GRID_WIDTH; + return menu.getTasks().size() / GRID_WIDTH; } - return menu.patterns.size() / GRID_WIDTH; + return menu.getPatterns().size() / GRID_WIDTH; }); var grid = new GridPanel(this, frame, 0, 0, GRID_WIDTH * AbstractSlotPanel.REGULAR_DIMENSIONS, 0, GRID_WIDTH, GRID_HEIGHT) { @@ -100,25 +100,25 @@ public class MatterPanelScreen extends MatteryScreen { @Nonnull @Override protected ItemStack getItemStack() { - var slot1 = slot + scroll_panel.getScroll(menu.patterns.size() / GRID_WIDTH) * GRID_WIDTH; + var slot1 = slot + scroll_panel.getScroll(menu.getPatterns().size() / GRID_WIDTH) * GRID_WIDTH; - if (slot1 >= menu.patterns.size()) { + if (slot1 >= menu.getPatterns().size()) { return ItemStack.EMPTY; } - return menu.patterns.get(slot1).stack(); + return menu.getPatterns().get(slot1).stack(); } @Nonnull @Override protected List getItemStackTooltip(@Nonnull ItemStack stack) { - var slot1 = slot + scroll_panel.getScroll(menu.patterns.size() / GRID_WIDTH) * GRID_WIDTH; + var slot1 = slot + scroll_panel.getScroll(menu.getPatterns().size() / GRID_WIDTH) * GRID_WIDTH; - if (slot1 >= menu.patterns.size()) { + if (slot1 >= menu.getPatterns().size()) { return List.of(); } - return getPatternTooltip(super.getItemStackTooltip(stack), menu.patterns.get(slot1)); + return getPatternTooltip(super.getItemStackTooltip(stack), menu.getPatterns().get(slot1)); } @Override @@ -128,11 +128,11 @@ public class MatterPanelScreen extends MatteryScreen { @Override protected boolean mouseClickedInner(double mouse_x, double mouse_y, int flag) { - if (slot >= menu.patterns.size()) { + if (slot >= menu.getPatterns().size()) { return true; } - openPattern(menu.patterns.get(slot)); + openPattern(menu.getPatterns().get(slot)); return true; } @@ -142,26 +142,26 @@ public class MatterPanelScreen extends MatteryScreen { @Nonnull @Override protected ItemStack getItemStack() { - var slot1 = slot + scroll_panel.getScroll(menu.tasks.size() / GRID_WIDTH) * GRID_WIDTH; + var slot1 = slot + scroll_panel.getScroll(menu.getTasks().size() / GRID_WIDTH) * GRID_WIDTH; - if (slot1 >= menu.tasks.size()) { + if (slot1 >= menu.getTasks().size()) { return ItemStack.EMPTY; } - var task = menu.tasks.get(slot1); + var task = menu.getTasks().get(slot1); return task.stack(Math.max(task.required(), 1)); } @Nonnull @Override protected List getItemStackTooltip(@Nonnull ItemStack stack) { - var slot1 = slot + scroll_panel.getScroll(menu.tasks.size() / GRID_WIDTH) * GRID_WIDTH; + var slot1 = slot + scroll_panel.getScroll(menu.getTasks().size() / GRID_WIDTH) * GRID_WIDTH; - if (slot1 >= menu.tasks.size()) { + if (slot1 >= menu.getTasks().size()) { return List.of(); } - return getTaskTooltip(super.getItemStackTooltip(stack), menu.tasks.get(slot1)); + return getTaskTooltip(super.getItemStackTooltip(stack), menu.getTasks().get(slot1)); } @Override @@ -171,11 +171,11 @@ public class MatterPanelScreen extends MatteryScreen { @Override protected boolean mouseClickedInner(double mouse_x, double mouse_y, int flag) { - if (slot >= menu.tasks.size()) { + if (slot >= menu.getTasks().size()) { return true; } - openTask(menu.tasks.get(slot)); + openTask(menu.getTasks().get(slot)); return true; } @@ -210,7 +210,7 @@ public class MatterPanelScreen extends MatteryScreen { public void tick() { super.tick(); - if (!menu.tasks.contains(task)) { + if (!menu.getTasks().contains(task)) { remove(); } } @@ -220,10 +220,10 @@ public class MatterPanelScreen extends MatteryScreen { @Nonnull @Override protected ItemStack getItemStack() { - var task1_index = menu.tasks.indexOf(task); + var task1_index = menu.getTasks().indexOf(task); if (task1_index != -1) { - var task1 = menu.tasks.get(task1_index); + var task1 = menu.getTasks().get(task1_index); return task1.stack(Math.max(task1.required(), 1)); } @@ -233,11 +233,11 @@ public class MatterPanelScreen extends MatteryScreen { @Nonnull @Override protected List getItemStackTooltip(@Nonnull ItemStack stack) { - var task1_index = menu.tasks.indexOf(task); + var task1_index = menu.getTasks().indexOf(task); List get_list = super.getItemStackTooltip(stack); if (task1_index != -1) { - getTaskTooltip(get_list, menu.tasks.get(task1_index)); + getTaskTooltip(get_list, menu.getTasks().get(task1_index)); } return get_list; @@ -269,7 +269,7 @@ public class MatterPanelScreen extends MatteryScreen { public void tick() { super.tick(); - if (!menu.patterns.contains(state)) { + if (!menu.getPatterns().contains(state)) { remove(); } } diff --git a/src/main/java/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.java b/src/main/java/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.java index 894af57f0..c8acb860f 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.java +++ b/src/main/java/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.java @@ -115,11 +115,11 @@ public abstract class MatteryScreen extends AbstractConta this.menu = menu; playerInventoryTitle = inventory.getDisplayName(); - if (menu.inventorySlots.size() != 0) { + if (menu.getPlayerInventorySlots().size() != 0) { inventory_frame = new FramePanel(this, null, 0, 0, INVENTORY_FRAME_WIDTH, INVENTORY_FRAME_HEIGHT, inventory.getDisplayName()); panels.add(inventory_frame); - for (var slot : menu.inventorySlots) { + for (var slot : menu.getPlayerInventorySlots()) { new SlotPanel<>( this, inventory_frame, diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/Ext.kt index a20fdabd4..b543630d7 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/Ext.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm +import com.google.common.collect.ImmutableList import com.google.gson.JsonArray import com.google.gson.JsonElement import com.google.gson.JsonObject @@ -201,3 +202,19 @@ fun > T.prev(values: Array): T { return values[next] } + +inline fun ImmutableList(size: Int, initializer: (index: Int) -> T): ImmutableList { + require(size >= 0) { "Invalid list size $size" } + + return when (size) { + 0 -> ImmutableList.of() + 1 -> ImmutableList.of(initializer(0)) + else -> ImmutableList.Builder().let { + for (i in 0 until size) { + it.add(initializer(i)) + } + + it.build() + } + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/BatteryBankScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/BatteryBankScreen.kt index 933d14722..54ca80a92 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/BatteryBankScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/BatteryBankScreen.kt @@ -16,10 +16,10 @@ class BatteryBankScreen(menu: BatteryBankMenu, p_97742_: Inventory, p_97743_: Co PowerGaugePanel(this, frame, menu.powerLevel, LEFT_MARGIN, GAUGE_TOP_WITHOUT_SLOT) for (i in 0 .. 5) - SlotPanel(this, frame, menu.containerSlots[i], 44f + 18 * i, 32f) + SlotPanel(this, frame, menu.storageSlots[i], 44f + 18 * i, 32f) for (i in 6 .. 11) - SlotPanel(this, frame, menu.containerSlots[i], 44f + 18 * (i - 6), 32f + 18f) + SlotPanel(this, frame, menu.storageSlots[i], 44f + 18 * (i - 6), 32f + 18f) return frame } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/CargoCrateScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/CargoCrateScreen.kt index fb13a2196..8aa9490c9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/CargoCrateScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/CargoCrateScreen.kt @@ -12,7 +12,7 @@ class CargoCrateScreen(menu: CargoCrateMenu, inventory: Inventory, title: Compon val frame = FramePanel(this, null, 0f, 0f, INVENTORY_FRAME_WIDTH, 22f + 4f + 6f * 18f, getTitle()) val grid = GridPanel(this, frame, 8f, 18f, 9f * 18f, 6f * 18f, 9, 6) - for (slot in menu.crateSlots) + for (slot in menu.storageSlots) SlotPanel(this, grid, slot) return frame diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/DriveRackScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/DriveRackScreen.kt index 007069519..3607e856c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/DriveRackScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/DriveRackScreen.kt @@ -16,11 +16,11 @@ class DriveRackScreen(menu: DriveRackMenu, inventory: Inventory, title: Componen PowerGaugePanel(this, frame, menu.powerWidget, LEFT_MARGIN, GAUGE_TOP_WITH_SLOT) SlotPanel(this, frame, menu.batterySlot, LEFT_MARGIN, SLOT_TOP_UNDER_GAUGE) - SlotPanel(this, frame, menu.drives[0], 71f, 32f) - SlotPanel(this, frame, menu.drives[1], 71f + 18f, 32f) + SlotPanel(this, frame, menu.storageSlots[0], 71f, 32f) + SlotPanel(this, frame, menu.storageSlots[1], 71f + 18f, 32f) - SlotPanel(this, frame, menu.drives[2], 71f, 32f + 18f) - SlotPanel(this, frame, menu.drives[3], 71f + 18f, 32f + 18f) + SlotPanel(this, frame, menu.storageSlots[2], 71f, 32f + 18f) + SlotPanel(this, frame, menu.storageSlots[3], 71f + 18f, 32f + 18f) return frame } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterBottlerScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterBottlerScreen.kt index 66027cf90..d37af67b3 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterBottlerScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterBottlerScreen.kt @@ -25,11 +25,11 @@ class MatterBottlerScreen(menu: MatterBottlerMenu, inventory: Inventory, title: SlotPanel(this, frame, menu.batterySlot, LEFT_MARGIN, SLOT_TOP_UNDER_GAUGE) for (i in 0 .. 2) { - SlotPanel(this, frame, menu.container[i], 31f + i * 18, PROGRESS_SLOT_TOP) + SlotPanel(this, frame, menu.storageSlots[i], 31f + i * 18, PROGRESS_SLOT_TOP) } for (i in 3 .. 5) { - SlotPanel(this, frame, menu.container[i], 116f + (i - 3) * 18, PROGRESS_SLOT_TOP) + SlotPanel(this, frame, menu.storageSlots[i], 116f + (i - 3) * 18, PROGRESS_SLOT_TOP) } progress = ProgressGaugePanel(this, frame, menu.progressWidget, 90f, PROGRESS_ARROW_TOP) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterCapacitorBankScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterCapacitorBankScreen.kt index df93334cf..a98770359 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterCapacitorBankScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterCapacitorBankScreen.kt @@ -16,10 +16,10 @@ class MatterCapacitorBankScreen(p_97741_: MatterCapacitorBankMenu, p_97742_: Inv MatterGaugePanel(this, frame, menu.matterGauge, LEFT_MARGIN + m.width, GAUGE_TOP_WITHOUT_SLOT) for (i in 0 .. 5) - SlotPanel(this, frame, menu.workSlots[i], 44f + 18 * i, 32f) + SlotPanel(this, frame, menu.storageSlots[i], 44f + 18 * i, 32f) for (i in 6 .. 11) - SlotPanel(this, frame, menu.workSlots[i], 44f + 18 * (i - 6), 32f + 18f) + SlotPanel(this, frame, menu.storageSlots[i], 44f + 18 * (i - 6), 32f + 18f) return frame } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterReplicatorScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterReplicatorScreen.kt index df404a3b6..a2937b928 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterReplicatorScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterReplicatorScreen.kt @@ -22,10 +22,10 @@ class MatterReplicatorScreen(p_97741_: MatterReplicatorMenu, p_97742_: Inventory ProgressGaugePanel(this, frame, menu.progress, 54f, PROGRESS_ARROW_TOP) for (i in 0 until 3) - SlotPanel(this, frame, menu.outputSlots[i], 80f + i * 18, PROGRESS_SLOT_TOP) + SlotPanel(this, frame, menu.storageSlots[i], 80f + i * 18, PROGRESS_SLOT_TOP) - SlotPanel(this, frame, menu.outputSlots[3], 80f, PROGRESS_SLOT_TOP + 22f) - SlotPanel(this, frame, menu.outputSlots[4], 80f + 18f, PROGRESS_SLOT_TOP + 22f) + SlotPanel(this, frame, menu.storageSlots[3], 80f, PROGRESS_SLOT_TOP + 22f) + SlotPanel(this, frame, menu.storageSlots[4], 80f + 18f, PROGRESS_SLOT_TOP + 22f) return frame } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/PatternStorageScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/PatternStorageScreen.kt index 5d7d8d7c3..992001b02 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/PatternStorageScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/PatternStorageScreen.kt @@ -17,10 +17,10 @@ class PatternStorageScreen(p_97741_: PatternStorageMenu, p_97742_: Inventory, p_ PatternGaugePanel(this, frame, menu.storedGrid, LEFT_MARGIN + m.width, GAUGE_TOP_WITHOUT_SLOT) for (i in 0 until 4) - SlotPanel(this, frame, menu.patternSlots[i], 62f + i * 18, 32f) + SlotPanel(this, frame, menu.storageSlots[i], 62f + i * 18, 32f) for (i in 4 until 8) - SlotPanel(this, frame, menu.patternSlots[i], 62f + (i - 4) * 18, 32f + 18f) + SlotPanel(this, frame, menu.storageSlots[i], 62f + (i - 4) * 18, 32f + 18f) return frame } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.kt index 92278826d..7a2068402 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.world.SimpleContainer import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Player +import net.minecraft.world.inventory.Slot import net.minecraft.world.item.ItemStack import ru.dbotthepony.mc.otm.block.entity.AndroidStationBlockEntity import ru.dbotthepony.mc.otm.capability.MatteryCapability @@ -36,16 +37,9 @@ class AndroidStationMenu @JvmOverloads constructor( ) : MatteryPoweredMenu(MMenus.ANDROID_STATION, containerID, inventory, tile) { val androidBattery: MatterySlot = AndroidBatterySlot(AndroidStationContainer(inventory.player), 0) + override val storageSlots = listOf(addSlot(androidBattery)) + init { - addSlot(androidBattery) addInventorySlots() } - - override fun getWorkingSlotStart(): Int { - return androidBattery.index - } - - override fun getWorkingSlotEnd(): Int { - return androidBattery.index + 1 - } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/BatteryBankMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/BatteryBankMenu.kt index dee60ea8e..9cb34abd8 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/BatteryBankMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/BatteryBankMenu.kt @@ -1,10 +1,13 @@ package ru.dbotthepony.mc.otm.menu +import com.google.common.collect.ImmutableList import net.minecraft.world.Container import kotlin.jvm.JvmOverloads import net.minecraft.world.entity.player.Inventory import ru.dbotthepony.mc.otm.block.entity.BatteryBankBlockEntity import net.minecraft.world.SimpleContainer +import net.minecraft.world.inventory.Slot +import ru.dbotthepony.mc.otm.ImmutableList import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.orNull @@ -16,25 +19,18 @@ class BatteryBankMenu @JvmOverloads constructor( tile: BatteryBankBlockEntity? = null, ) : MatteryMenu(MMenus.BATTERY_BANK, p_38852_, inventory, tile) { val powerLevel: LevelGaugeWidget - val containerSlots: Array + override val storageSlots: List init { val container: Container = tile?.container ?: SimpleContainer(BatteryBankBlockEntity.CAPACITY) powerLevel = LevelGaugeWidget(this, tile?.getCapability(MatteryCapability.ENERGY)?.orNull()) - containerSlots = Array(BatteryBankBlockEntity.CAPACITY) { + + storageSlots = ImmutableList(BatteryBankBlockEntity.CAPACITY) { val slot = BatterySlot(container, it) addSlot(slot) - return@Array slot + return@ImmutableList slot } addInventorySlots() } - - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return BatteryBankBlockEntity.CAPACITY - } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/CargoCrateMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/CargoCrateMenu.kt index 4a951716c..01697b220 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/CargoCrateMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/CargoCrateMenu.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.world.SimpleContainer import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Player +import ru.dbotthepony.mc.otm.ImmutableList import ru.dbotthepony.mc.otm.block.entity.CargoCrateBlockEntity import ru.dbotthepony.mc.otm.registry.MMenus @@ -11,15 +12,15 @@ class CargoCrateMenu @JvmOverloads constructor( inventory: Inventory, tile: CargoCrateBlockEntity? = null ) : MatteryMenu(MMenus.CARGO_CRATE, p_38852_, inventory, tile) { - val crateSlots: Array + override val storageSlots: List init { val container = tile?.container ?: SimpleContainer(CargoCrateBlockEntity.CAPACITY) - crateSlots = Array(CargoCrateBlockEntity.CAPACITY) { + storageSlots = ImmutableList(CargoCrateBlockEntity.CAPACITY) { val slot = MatterySlot(container, it) addSlot(slot) - return@Array slot + return@ImmutableList slot } tile?.onPlayerOpen() @@ -30,12 +31,4 @@ class CargoCrateMenu @JvmOverloads constructor( super.removed(p_38940_) (tile as? CargoCrateBlockEntity)?.onPlayerClose() } - - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return CargoCrateBlockEntity.CAPACITY - } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ChemicalGeneratorMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ChemicalGeneratorMenu.kt index 5002dfb09..6c753e604 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ChemicalGeneratorMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ChemicalGeneratorMenu.kt @@ -2,6 +2,7 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.world.SimpleContainer import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot import net.minecraft.world.item.ItemStack import net.minecraftforge.common.ForgeHooks import net.minecraftforge.energy.CapabilityEnergy @@ -38,10 +39,13 @@ class ChemicalGeneratorMenu @JvmOverloads constructor(id: Int, inv: Inventory, t val energy = LevelGaugeWidget(this, tile?.energy) val burnTime = IntDataContainer() + override val storageSlots = listOf( + addSlot(fuelSlot), + addSlot(batterySlot), + addSlot(residueSlot), + ) + init { - addSlot(fuelSlot) - addSlot(batterySlot) - addSlot(residueSlot) addDataContainer(burnTime) addInventorySlots() } @@ -51,12 +55,4 @@ class ChemicalGeneratorMenu @JvmOverloads constructor(id: Int, inv: Inventory, t progress.updateServer() burnTime.value = (tile as ChemicalGeneratorBlockEntity).workingTicks } - - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return 3 - } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/DriveRackMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/DriveRackMenu.kt index 8a616b930..5d2d5451d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/DriveRackMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/DriveRackMenu.kt @@ -2,6 +2,8 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.world.SimpleContainer import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot +import ru.dbotthepony.mc.otm.ImmutableList import ru.dbotthepony.mc.otm.block.entity.storage.DriveRackBlockEntity import ru.dbotthepony.mc.otm.registry.MMenus @@ -10,29 +12,17 @@ class DriveRackMenu @JvmOverloads constructor( inventory: Inventory, tile: DriveRackBlockEntity? = null ) : MatteryPoweredMenu(MMenus.DRIVE_RACK, p_38852_, inventory, tile) { - val drives: Array + override val storageSlots: List init { val container = tile?.drives ?: SimpleContainer(4) - val drives = arrayOfNulls(4) - - for (i in 0 until container.containerSize) { - val slot = DriveSlot(container, i) - drives[i] = slot + storageSlots = ImmutableList(4) { + val slot = DriveSlot(container, it) addSlot(slot) + slot } - this.drives = drives as Array - addInventorySlots() } - - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return 5 - } } \ No newline at end of file diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/DriveViewerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/DriveViewerMenu.kt index 37c2fc5e7..e86f3a491 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/DriveViewerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/DriveViewerMenu.kt @@ -1,8 +1,10 @@ package ru.dbotthepony.mc.otm.menu +import com.google.common.collect.ImmutableList import net.minecraft.world.SimpleContainer import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Player +import net.minecraft.world.inventory.Slot import net.minecraft.world.item.ItemStack import net.minecraftforge.energy.CapabilityEnergy import ru.dbotthepony.mc.otm.block.entity.storage.DriveViewerBlockEntity @@ -58,6 +60,8 @@ class DriveViewerMenu @JvmOverloads constructor( addInventorySlots() } + override val storageSlots: List = ImmutableList.of(driveSlot, batterySlot) + val driveFilter = ItemFilter(PortableCondensationDriveItem.MAX_FILTERS) { self, _, _, _ -> if (tile?.container?.get(0)?.item is PortableCondensationDriveItem) { tile.container[0].getOrCreateTag().put(PortableCondensationDriveItem.FILTER_PATH, self.serializeNBT()) @@ -132,14 +136,6 @@ class DriveViewerMenu @JvmOverloads constructor( } } - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return 2 - } - override fun removed(p_38940_: Player) { super.removed(p_38940_) @@ -149,12 +145,12 @@ class DriveViewerMenu @JvmOverloads constructor( view.removed() } - override fun quickMoveStack(ply: Player, slot_index: Int): ItemStack { - val slot = slots[slot_index] + override fun quickMoveStack(ply: Player, slotIndex: Int): ItemStack { + val slot = slots[slotIndex] val item = slot.item if (item.isEmpty || item.getCapability(MatteryCapability.DRIVE).isPresent || item.getCapability(CapabilityEnergy.ENERGY).isPresent) - return super.quickMoveStack(ply, slot_index) + return super.quickMoveStack(ply, slotIndex) val powered = powered if (lastDrive == null || powered == null) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/EnergyCounterMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/EnergyCounterMenu.kt index d7dd2a55b..8e3394627 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/EnergyCounterMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/EnergyCounterMenu.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.core.Direction import kotlin.jvm.JvmOverloads import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot import net.minecraft.world.level.block.Block import ru.dbotthepony.mc.otm.block.EnergyCounterBlock import ru.dbotthepony.mc.otm.block.entity.EnergyCounterBlockEntity @@ -76,13 +77,8 @@ class EnergyCounterMenu @JvmOverloads constructor( super.broadcastChanges() } - override fun getWorkingSlotStart(): Int { - return 0 - } - override fun getWorkingSlotEnd(): Int { - return 0 - } + override val storageSlots: Collection get() = emptySet() companion object { private val MINUS_ONE = -BigDecimal.ONE diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ItemMonitorMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ItemMonitorMenu.kt index e9e5d24e1..098f42055 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ItemMonitorMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ItemMonitorMenu.kt @@ -10,12 +10,10 @@ import net.minecraft.world.item.ItemStack import net.minecraftforge.network.PacketDistributor import ru.dbotthepony.mc.otm.block.entity.storage.ItemMonitorBlockEntity import ru.dbotthepony.mc.otm.block.entity.storage.ItemMonitorPlayerSettings -import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.get import ru.dbotthepony.mc.otm.menu.data.INetworkedItemViewSupplier import ru.dbotthepony.mc.otm.menu.data.NetworkedItemView import ru.dbotthepony.mc.otm.network.MatteryNetworking -import ru.dbotthepony.mc.otm.orThrow import ru.dbotthepony.mc.otm.registry.MMenus import ru.dbotthepony.mc.otm.storage.* import java.util.* @@ -133,21 +131,15 @@ class ItemMonitorMenu @JvmOverloads constructor( } } - override fun getWorkingSlotStart(): Int { - return 0 - } + override val storageSlots: List = listOf(batterySlot) - override fun getWorkingSlotEnd(): Int { - return 1 - } - - override fun quickMoveStack(ply: Player, slot_index: Int): ItemStack { - if (slot_index in inventorySlotIndexStart..inventorySlotIndexEnd) { + override fun quickMoveStack(ply: Player, slotIndex: Int): ItemStack { + if (slotIndex in inventorySlotIndexStart..inventorySlotIndexEnd) { if (tile == null) { return ItemStack.EMPTY } - val slot = slots[slot_index] + val slot = slots[slotIndex] if (slot.item.isEmpty) { return ItemStack.EMPTY @@ -162,30 +154,30 @@ class ItemMonitorMenu @JvmOverloads constructor( val old = slot.item.copy() slot.item.count = leftover.count return old - } else if (slot_index in craftingSlots[0].index .. craftingSlots[craftingSlots.size - 1].index) { + } else if (slotIndex in craftingSlots[0].index .. craftingSlots[craftingSlots.size - 1].index) { // from crafting grid to inventory - val item = slots[slot_index].item + val item = slots[slotIndex].item if (item.isEmpty) { return ItemStack.EMPTY } - var remainder = moveItemStackToSlots(item, inventorySlotIndexStart, inventorySlotIndexEnd) - slots[slot_index].set(remainder) + var remainder = moveItemStackToSlots(item, playerInventorySlots) + slots[slotIndex].set(remainder) if (remainder.isEmpty) { return item } remainder = view.provider?.insertStack(ItemStackWrapper(remainder), false)?.stack ?: remainder - slots[slot_index].set(remainder) + slots[slotIndex].set(remainder) if (remainder.isEmpty) { return item } return if (remainder.count != item.count) item else ItemStack.EMPTY - } else if (slot_index == craftingResult.index) { + } else if (slotIndex == craftingResult.index) { // quickcraft... god damn it if (!craftingResult.hasItem()) { return ItemStack.EMPTY @@ -252,11 +244,11 @@ class ItemMonitorMenu @JvmOverloads constructor( return item } - remaining = moveItemStackToSlots(remaining, inventorySlotIndexStart, inventorySlotIndexEnd, simulate = true) + remaining = moveItemStackToSlots(remaining, playerInventorySlots, simulate = true) if (remaining.isEmpty) { remaining = tile.poweredView!!.insertStack(wrapper, false).stack - moveItemStackToSlots(remaining, inventorySlotIndexStart, inventorySlotIndexEnd, simulate = false) + moveItemStackToSlots(remaining, playerInventorySlots, simulate = false) craftingResult.remove(item.count) return item } @@ -265,10 +257,10 @@ class ItemMonitorMenu @JvmOverloads constructor( } ItemMonitorPlayerSettings.ResultTarget.MIXED, ItemMonitorPlayerSettings.ResultTarget.ALL_INVENTORY -> { - var remaining = moveItemStackToSlots(item, inventorySlotIndexStart, inventorySlotIndexEnd, simulate = true) + var remaining = moveItemStackToSlots(item, playerInventorySlots, simulate = true) if (remaining.isEmpty) { - moveItemStackToSlots(item, inventorySlotIndexStart, inventorySlotIndexEnd, simulate = false) + moveItemStackToSlots(item, playerInventorySlots, simulate = false) craftingResult.remove(item.count) return item } @@ -277,7 +269,7 @@ class ItemMonitorMenu @JvmOverloads constructor( remaining = tile.poweredView?.insertStack(wrapper, true)?.stack ?: return ItemStack.EMPTY if (remaining.isEmpty) { - moveItemStackToSlots(item, inventorySlotIndexStart, inventorySlotIndexEnd, simulate = false) + moveItemStackToSlots(item, playerInventorySlots, simulate = false) tile.poweredView!!.insertStack(wrapper, false).stack craftingResult.remove(item.count) return item @@ -291,6 +283,6 @@ class ItemMonitorMenu @JvmOverloads constructor( } } - return super.quickMoveStack(ply, slot_index) + return super.quickMoveStack(ply, slotIndex) } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterBottlerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterBottlerMenu.kt index 6b7a987fd..44920deb3 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterBottlerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterBottlerMenu.kt @@ -2,7 +2,9 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.world.SimpleContainer import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot import net.minecraft.world.item.ItemStack +import ru.dbotthepony.mc.otm.ImmutableList import ru.dbotthepony.mc.otm.block.entity.matter.MatterBottlerBlockEntity import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.menu.widget.BooleanPlayerInputWidget @@ -22,7 +24,8 @@ class MatterBottlerMenu @JvmOverloads constructor( val progressWidget: ProgressGaugeWidget val matterWidget: LevelGaugeWidget - val container: Array + + override val storageSlots: List init { val container = tile?.container ?: SimpleContainer(6) @@ -37,7 +40,7 @@ class MatterBottlerMenu @JvmOverloads constructor( workFlow = BooleanPlayerInputWidget(this, tile::workFlow) } - this.container = Array(6) { index -> + storageSlots = ImmutableList(6) { index -> object : MatterySlot(container, index) { override fun mayPlace(p_40231_: ItemStack): Boolean { val cap = p_40231_.getCapability(MatteryCapability.MATTER).orNull() ?: return false @@ -51,7 +54,7 @@ class MatterBottlerMenu @JvmOverloads constructor( } } - this.container.forEach(this::addSlot) + storageSlots.forEach(this::addSlot) addInventorySlots() } @@ -59,12 +62,4 @@ class MatterBottlerMenu @JvmOverloads constructor( super.broadcastChanges() workFlow.value = (tile as MatterBottlerBlockEntity).workFlow } - - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return 7 - } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterCapacitorBankMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterCapacitorBankMenu.kt index bb922b715..b20cd9789 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterCapacitorBankMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterCapacitorBankMenu.kt @@ -2,6 +2,8 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.world.SimpleContainer import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot +import ru.dbotthepony.mc.otm.ImmutableList import ru.dbotthepony.mc.otm.block.entity.matter.MatterCapacitorBankBlockEntity import ru.dbotthepony.mc.otm.core.ImpreciseFraction import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget @@ -16,7 +18,8 @@ class MatterCapacitorBankMenu @JvmOverloads constructor( ) { val matterGauge: LevelGaugeWidget val totalMatterGauge: LevelGaugeWidget - val workSlots: Array + + override val storageSlots: List init { if (tile == null) { @@ -32,26 +35,13 @@ class MatterCapacitorBankMenu @JvmOverloads constructor( } val container = tile?.container ?: SimpleContainer(2 * 6) - val workSlots = arrayOfNulls(2 * 6) - for (row in 0..1) { - for (column in 0..5) { - val slot = MatterContainerInputSlot(container, row * 6 + column) - workSlots[row * 6 + column] = slot - addSlot(slot) - } + storageSlots = ImmutableList(2 * 6) { + val slot = MatterContainerInputSlot(container, it) + addSlot(slot) + slot } - this.workSlots = workSlots as Array - addInventorySlots() } - - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return 2 * 6 - } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterDecomposerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterDecomposerMenu.kt index 8eec21425..2d1d57858 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterDecomposerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterDecomposerMenu.kt @@ -1,11 +1,13 @@ package ru.dbotthepony.mc.otm.menu +import com.google.common.collect.ImmutableList import kotlin.jvm.JvmOverloads import net.minecraft.world.entity.player.Inventory import ru.dbotthepony.mc.otm.block.entity.matter.MatterDecomposerBlockEntity import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget import net.minecraft.world.SimpleContainer +import net.minecraft.world.inventory.Slot import net.minecraft.world.item.ItemStack import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.matter.canDecompose @@ -31,15 +33,10 @@ class MatterDecomposerMenu @JvmOverloads constructor( override fun mayPlace(p_40231_: ItemStack) = canDecompose(p_40231_) } - addSlot(input) - // Выход outputMain = MachineOutputSlot(container, 1) outputStacking = MachineOutputSlot(container, 2) - addSlot(outputMain) - addSlot(outputStacking) - matterWidget = LevelGaugeWidget(this, tile?.getCapability(MatteryCapability.MATTER)?.orNull()) if (tile == null) { @@ -51,11 +48,9 @@ class MatterDecomposerMenu @JvmOverloads constructor( addInventorySlots() } - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return 3 - } -} \ No newline at end of file + override val storageSlots: List = ImmutableList.of( + addSlot(outputMain), + addSlot(outputStacking), + addSlot(input), + ) +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterPanelMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterPanelMenu.kt index ee70bd828..eee28367c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterPanelMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterPanelMenu.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.server.level.ServerPlayer import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Player +import net.minecraft.world.inventory.Slot import net.minecraftforge.network.PacketDistributor import ru.dbotthepony.mc.otm.OverdriveThatMatters import ru.dbotthepony.mc.otm.block.entity.matter.MatterPanelBlockEntity @@ -32,12 +33,8 @@ class MatterPanelMenu @JvmOverloads constructor( } // client code - @JvmField - var patterns = ArrayList() - - @JvmField - var tasks = ArrayList() - + val patterns = ArrayList() + val tasks = ArrayList() var changeset = 0 fun networkPatternsUpdated(patterns: Collection) { @@ -176,11 +173,6 @@ class MatterPanelMenu @JvmOverloads constructor( } } - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return 0 - } -} \ No newline at end of file + override val storageSlots: Collection + get() = emptyList() +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterRecyclerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterRecyclerMenu.kt index 2e6bd82be..fc9a02bb1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterRecyclerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterRecyclerMenu.kt @@ -1,7 +1,9 @@ package ru.dbotthepony.mc.otm.menu +import com.google.common.collect.ImmutableList import net.minecraft.world.SimpleContainer import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot import net.minecraft.world.item.ItemStack import ru.dbotthepony.mc.otm.block.entity.matter.MatterRecyclerBlockEntity import ru.dbotthepony.mc.otm.item.MatterDustItem @@ -37,11 +39,5 @@ class MatterRecyclerMenu @JvmOverloads constructor( addInventorySlots() } - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return 2 - } + override val storageSlots: Collection = ImmutableList.of(input) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterReplicatorMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterReplicatorMenu.kt index 99db2e270..73f509ba6 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterReplicatorMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterReplicatorMenu.kt @@ -6,6 +6,8 @@ import ru.dbotthepony.mc.otm.block.entity.matter.MatterReplicatorBlockEntity import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget import net.minecraft.world.SimpleContainer +import net.minecraft.world.inventory.Slot +import ru.dbotthepony.mc.otm.ImmutableList import ru.dbotthepony.mc.otm.registry.MMenus class MatterReplicatorMenu @JvmOverloads constructor( @@ -17,15 +19,15 @@ class MatterReplicatorMenu @JvmOverloads constructor( ) { val matter: LevelGaugeWidget val progress: ProgressGaugeWidget - val outputSlots: Array + override val storageSlots: List init { val container = tile?.container ?: SimpleContainer(5) - outputSlots = Array(5) { + storageSlots = ImmutableList(5) { val slot = MachineOutputSlot(container, it) addSlot(slot) - return@Array slot + return@ImmutableList slot } if (tile != null) { @@ -38,12 +40,4 @@ class MatterReplicatorMenu @JvmOverloads constructor( addInventorySlots() } - - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return 5 - } } \ No newline at end of file diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterScannerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterScannerMenu.kt index 59e28edc9..fa796cb87 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterScannerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterScannerMenu.kt @@ -6,6 +6,7 @@ import ru.dbotthepony.mc.otm.block.entity.matter.MatterScannerBlockEntity import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget import net.minecraft.world.SimpleContainer +import net.minecraft.world.inventory.Slot import net.minecraft.world.item.ItemStack import ru.dbotthepony.mc.otm.core.ImpreciseFraction import ru.dbotthepony.mc.otm.matter.canDecompose @@ -47,11 +48,5 @@ class MatterScannerMenu @JvmOverloads constructor( addInventorySlots() } - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return 2 - } + override val storageSlots: List = listOf(input) } \ No newline at end of file diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt index 326585820..4bb3202e4 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt @@ -22,22 +22,17 @@ abstract class MatteryMenu @JvmOverloads protected constructor( ) : AbstractContainerMenu(p_38851_, p_38852_) { val ply: Player get() = inventory.player - @JvmField val matteryWidgets = ArrayList() - @JvmField - val inventorySlots = ArrayList() + private val _playerInventorySlots = ArrayList() + val playerInventorySlots: List = Collections.unmodifiableList(_playerInventorySlots) val multiByteContainers = ArrayList() - @JvmField protected val lockedInventorySlots: MutableSet = HashSet() protected open fun isInventorySlotLocked(index: Int): Boolean = lockedInventorySlots.contains(index) - @JvmField - protected var _synchronizer: ContainerSynchronizer? = null - private val _filterSlots = ArrayList() - val filterSlots = Collections.unmodifiableList(_filterSlots) + val filterSlots: List = Collections.unmodifiableList(_filterSlots) fun addFilterSlots(slots: ItemFilter): List { val result = ArrayList(slots.size) @@ -59,11 +54,6 @@ abstract class MatteryMenu @JvmOverloads protected constructor( return result } - override fun setSynchronizer(p_150417_: ContainerSynchronizer) { - _synchronizer = p_150417_ - super.setSynchronizer(p_150417_) - } - fun addWidget(widget: AbstractWidget): Int { val indexOf = matteryWidgets.indexOf(widget) @@ -99,10 +89,7 @@ abstract class MatteryMenu @JvmOverloads protected constructor( return multiByteContainers[index] } - @JvmField protected var inventorySlotIndexStart = 0 - - @JvmField protected var inventorySlotIndexEnd = 0 protected fun addInventorySlots() { @@ -120,7 +107,7 @@ abstract class MatteryMenu @JvmOverloads protected constructor( } } - inventorySlots.add(slot) + _playerInventorySlots.add(slot) addSlot(slot) if (first) { @@ -144,13 +131,13 @@ abstract class MatteryMenu @JvmOverloads protected constructor( } addSlot(last) - inventorySlots.add(last) + _playerInventorySlots.add(last) } inventorySlotIndexEnd = last!!.index } - private val pdistributor = PacketDistributor.PLAYER.with { ply as ServerPlayer } + private val playerPacketDistributor = PacketDistributor.PLAYER.with { ply as ServerPlayer } override fun broadcastChanges() { for (widget in matteryWidgets) { @@ -160,7 +147,7 @@ abstract class MatteryMenu @JvmOverloads protected constructor( for (data in multiByteContainers) { if (data.dirty) { data.dirty = false - MatteryNetworking.CHANNEL.send(pdistributor, data.makePacket()) + MatteryNetworking.CHANNEL.send(playerPacketDistributor, data.makePacket()) } } @@ -183,7 +170,7 @@ abstract class MatteryMenu @JvmOverloads protected constructor( } for (data in multiByteContainers) { - MatteryNetworking.CHANNEL.send(pdistributor, data.makePacket()) + MatteryNetworking.CHANNEL.send(playerPacketDistributor, data.makePacket()) } super.broadcastFullState() @@ -211,40 +198,56 @@ abstract class MatteryMenu @JvmOverloads protected constructor( return player.distanceToSqr(pos.x.toDouble() + 0.5, pos.y.toDouble() + 0.5, pos.z.toDouble() + 0.5) <= 64.0 } - protected abstract fun getWorkingSlotStart(): Int - protected abstract fun getWorkingSlotEnd(): Int + abstract val storageSlots: Collection // This method receive Player interactor and slot_index where Shift + Right click occurred // It shall return item stack that got moved - override fun quickMoveStack(ply: Player, slot_index: Int): ItemStack { - // this.moveItemStackTo(ItemStack, int start_slot_index, int end_slot_index, boolean iteration_order) - // returns boolean, telling whenever ItemStack was modified (moved or shrank) - // false means nothing happened - // Last boolean determine order of slot iteration, where: - // if TRUE, iteration order is end_slot_index -> start_slot_index - // if FALSE iteration order is start_slot_index -> end_slot_index - val start = getWorkingSlotStart() - val end = getWorkingSlotEnd() + override fun quickMoveStack(ply: Player, slotIndex: Int): ItemStack { + if (storageSlots.isEmpty()) { + return ItemStack.EMPTY + } - if (start == end) { + var moveToPlayer: Boolean? = null + + for (slot in storageSlots) { + if (slot.index == slotIndex) { + moveToPlayer = true + break + } + } + + if (moveToPlayer == null) { + for (slot in playerInventorySlots) { + if (slot.index == slotIndex) { + moveToPlayer = false + break + } + } + } + + if (moveToPlayer == null) { return ItemStack.EMPTY } var moved = ItemStack.EMPTY - val initialSlot = slots[slot_index] + val initialSlot = slots[slotIndex] if (initialSlot.hasItem()) { + if (!initialSlot.mayPickup(ply)) { + return moved + } + val initialItem = initialSlot.item moved = initialItem.copy() - if (slot_index < inventorySlotIndexStart) { - // Moving FROM machine TO inventory - if (!moveItemStackTo(initialItem, inventorySlotIndexStart, inventorySlotIndexEnd + 1, false)) { + if (moveToPlayer) { + if (!moveItemStackTo(initialItem, playerInventorySlots)) { + return ItemStack.EMPTY + } + } else { + if (!moveItemStackTo(initialItem, storageSlots)) { return ItemStack.EMPTY } - } else if (!moveItemStackTo(initialItem, start, end, false)) { - // Moving FROM inventory TO machine - return ItemStack.EMPTY } if (initialItem.isEmpty) { @@ -281,20 +284,29 @@ abstract class MatteryMenu @JvmOverloads protected constructor( return true } - fun moveItemStackToSlots(item: ItemStack, initialSlot: Int, finalSlot: Int, inverse: Boolean = false, simulate: Boolean = false): ItemStack { - if (initialSlot > finalSlot) { - return item + fun moveItemStackTo( + item: ItemStack, + slots: Collection + ): Boolean { + val remainder = moveItemStackToSlots(item, slots) + + if (remainder.count == item.count) { + return false } + item.count = remainder.count + return true + } + + fun moveItemStackToSlots(item: ItemStack, slots: Collection, simulate: Boolean = false): ItemStack { val copy = item.copy() // first pass - stack with existing slots if (copy.isStackable) { - for (i in (if (inverse) finalSlot downTo initialSlot else initialSlot .. finalSlot)) { - val slot = slots[i] + for (slot in slots) { val limit = slot.getMaxStackSize(copy) - if (limit > slot.item.count && ItemStack.isSameItemSameTags(slot.item, copy)) { + if (limit > slot.item.count && ItemStack.isSameItemSameTags(slot.item, copy) && slot.mayPlace(item)) { val newCount = (slot.item.count + copy.count).coerceAtMost(limit) val diff = newCount - slot.item.count copy.count -= diff @@ -312,11 +324,10 @@ abstract class MatteryMenu @JvmOverloads protected constructor( } // second pass - drop stack into first free slot - for (i in (if (inverse) finalSlot downTo initialSlot else initialSlot .. finalSlot)) { - val slot = slots[i] + for (slot in slots) { val limit = slot.getMaxStackSize(copy) - if (!slot.hasItem()) { + if (!slot.hasItem() && slot.mayPlace(item)) { val newCount = copy.count.coerceAtMost(limit) if (!simulate) { @@ -335,6 +346,23 @@ abstract class MatteryMenu @JvmOverloads protected constructor( return copy } + fun moveItemStackToSlots(item: ItemStack, initialSlot: Int, finalSlot: Int, inverse: Boolean = false, simulate: Boolean = false): ItemStack { + if (initialSlot > finalSlot) { + return item + } + + require(initialSlot >= 0) { "Invalid initial slot $initialSlot" } + require(finalSlot < slots.size) { "Final slot $finalSlot is bigger than total size of array of ${slots.size}" } + + val slots = ArrayList(finalSlot - initialSlot + 1) + + for (i in (if (inverse) finalSlot downTo initialSlot else initialSlot .. finalSlot)) { + slots.add(slots[i]) + } + + return moveItemStackToSlots(item, slots, simulate) + } + public override fun addDataSlots(p_38885_: ContainerData) { super.addDataSlots(p_38885_) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryPoweredMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryPoweredMenu.kt index 19d73d7e9..2f2f226a6 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryPoweredMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryPoweredMenu.kt @@ -12,8 +12,8 @@ abstract class MatteryPoweredMenu protected constructor( inventory: Inventory, tile: MatteryPoweredBlockEntity? = null ) : MatteryMenu(menuType, containerID, inventory, tile) { - @JvmField val powerWidget: LevelGaugeWidget - @JvmField val batterySlot: BatterySlot + val powerWidget: LevelGaugeWidget + val batterySlot: BatterySlot init { if (tile == null) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/PatternStorageMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/PatternStorageMenu.kt index 9ef7bbfb8..bb64c1874 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/PatternStorageMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/PatternStorageMenu.kt @@ -2,6 +2,8 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.world.SimpleContainer import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot +import ru.dbotthepony.mc.otm.ImmutableList import ru.dbotthepony.mc.otm.block.entity.PatternStorageBlockEntity import ru.dbotthepony.mc.otm.core.ImpreciseFraction import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget @@ -14,10 +16,11 @@ class PatternStorageMenu @JvmOverloads constructor( ) : MatteryMenu( MMenus.PATTERN_STORAGE, p_38852_, inventory, tile ) { - val patternSlots: Array val storedThis: LevelGaugeWidget val storedGrid: LevelGaugeWidget + override val storageSlots: List + init { if (tile == null) { storedThis = LevelGaugeWidget(this) @@ -33,25 +36,12 @@ class PatternStorageMenu @JvmOverloads constructor( val patterns = tile?.patterns ?: SimpleContainer(2 * 4) - val patternSlots = arrayOfNulls(2 * 4) - - for (row in 0..1) { - for (column in 0..3) { - patternSlots[row * 4 + column] = PatternSlot(patterns, row * 4 + column, 48 + column * 20, 27 + row * 24) - addSlot(patternSlots[row * 4 + column]!!) - } + storageSlots = ImmutableList(2 * 4) { + PatternSlot(patterns, it) } - this.patternSlots = patternSlots as Array + storageSlots.forEach(this::addSlot) addInventorySlots() } - - override fun getWorkingSlotStart(): Int { - return 0 - } - - override fun getWorkingSlotEnd(): Int { - return 2 * 4 - } } \ No newline at end of file diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/PlatePressMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/PlatePressMenu.kt index 8033706b7..a4fc2680b 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/PlatePressMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/PlatePressMenu.kt @@ -1,7 +1,9 @@ package ru.dbotthepony.mc.otm.menu +import com.google.common.collect.ImmutableList import net.minecraft.world.SimpleContainer import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot import ru.dbotthepony.mc.otm.block.entity.PlatePressBlockEntity import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget import ru.dbotthepony.mc.otm.registry.MMenus @@ -16,6 +18,8 @@ class PlatePressMenu @JvmOverloads constructor( val inputSlot = MatterySlot(container, PlatePressBlockEntity.SLOT_INPUT) val outputSlot = MachineOutputSlot(container, PlatePressBlockEntity.SLOT_OUTPUT) + override val storageSlots: List = ImmutableList.of(inputSlot, outputSlot) + val progressGauge = if (tile != null) ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess) else ProgressGaugeWidget(this) init { @@ -23,7 +27,4 @@ class PlatePressMenu @JvmOverloads constructor( addSlot(outputSlot) addInventorySlots() } - - override fun getWorkingSlotStart() = 0 - override fun getWorkingSlotEnd() = 2 } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageBusMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageBusMenu.kt index becc821a8..f7dfd875b 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageBusMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageBusMenu.kt @@ -1,6 +1,7 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot import ru.dbotthepony.mc.otm.block.entity.storage.StorageBusBlockEntity import ru.dbotthepony.mc.otm.container.ItemFilterNetworkSlot import ru.dbotthepony.mc.otm.menu.widget.BooleanPlayerInputWidget @@ -28,6 +29,5 @@ class StorageBusMenu @JvmOverloads constructor( addInventorySlots() } - override fun getWorkingSlotStart() = 0 - override fun getWorkingSlotEnd() = 1 + override val storageSlots: Collection = listOf(batterySlot) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageExporterMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageExporterMenu.kt index 31bdcd9e9..777118f53 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageExporterMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageExporterMenu.kt @@ -1,6 +1,7 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot import ru.dbotthepony.mc.otm.block.entity.storage.StorageExporterBlockEntity import ru.dbotthepony.mc.otm.container.ItemFilterNetworkSlot import ru.dbotthepony.mc.otm.menu.widget.BooleanPlayerInputWidget @@ -28,6 +29,5 @@ class StorageExporterMenu @JvmOverloads constructor( addInventorySlots() } - override fun getWorkingSlotStart() = 0 - override fun getWorkingSlotEnd() = 1 + override val storageSlots: List = listOf(batterySlot) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageImporterMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageImporterMenu.kt index eb4645055..554153aef 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageImporterMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StorageImporterMenu.kt @@ -1,6 +1,7 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot import ru.dbotthepony.mc.otm.block.entity.storage.StorageImporterBlockEntity import ru.dbotthepony.mc.otm.container.ItemFilterNetworkSlot import ru.dbotthepony.mc.otm.menu.widget.BooleanPlayerInputWidget @@ -28,6 +29,5 @@ class StorageImporterMenu @JvmOverloads constructor( addInventorySlots() } - override fun getWorkingSlotStart() = 0 - override fun getWorkingSlotEnd() = 1 + override val storageSlots: List = listOf(batterySlot) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StoragePowerSupplierMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StoragePowerSupplierMenu.kt index b4b16c9d3..d4bc75b59 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StoragePowerSupplierMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/StoragePowerSupplierMenu.kt @@ -1,6 +1,7 @@ package ru.dbotthepony.mc.otm.menu import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.inventory.Slot import ru.dbotthepony.mc.otm.block.entity.storage.StoragePowerSupplierBlockEntity import ru.dbotthepony.mc.otm.menu.data.ImpreciseFractionDataContainer import ru.dbotthepony.mc.otm.registry.MMenus @@ -28,6 +29,5 @@ class StoragePowerSupplierMenu @JvmOverloads constructor( super.broadcastChanges() } - override fun getWorkingSlotStart() = 0 - override fun getWorkingSlotEnd() = 1 + override val storageSlots: List = listOf(batterySlot) }