Skip to content

Commit

Permalink
Fix pump not filling itself and overhaul Tweakeruler controls
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaTheMartian committed Dec 27, 2024
1 parent 6d92fb8 commit 5d4af71
Show file tree
Hide file tree
Showing 24 changed files with 754 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/main/java/martian/minefactorial/Minefactorial.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Minefactorial(IEventBus modBus, Dist dist) {
// Add event listeners
modBus.addListener(MFTabs::addItems);
modBus.register(MinefactorialListeners.ModBusEvents.class);
// NeoForge.EVENT_BUS.register(MinefactorialListeners.GameBusEvents.class); // There are no events on this *yet*
NeoForge.EVENT_BUS.register(MinefactorialListeners.GameBusEvents.class);
if (dist.isClient()) {
modBus.register(MinefactorialClient.ModBusEvents.class);
NeoForge.EVENT_BUS.register(MinefactorialClient.GameBusEvents.class);
Expand Down
76 changes: 72 additions & 4 deletions src/main/java/martian/minefactorial/MinefactorialListeners.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
package martian.minefactorial;

import martian.minefactorial.client.overlay.scrollmenu.OverlayScrollMenu;
import martian.minefactorial.content.block.logistics.BlockFluidExtractorBE;
import martian.minefactorial.content.block.machinery.husbandry.BlockSlaughterhouseBE;
import martian.minefactorial.content.block.power.BlockSteamBoilerBE;
import martian.minefactorial.content.block.storage.BlockCapacitorBE;
import martian.minefactorial.content.item.tweakeruler.ItemTweakeruler;
import martian.minefactorial.content.net.PacketServerboundSetSmasherFortuneLevel;
import martian.minefactorial.content.net.PacketServerboundSetTweakerulerMode;
import martian.minefactorial.content.net.PacketServerboundTweakerulerRedo;
import martian.minefactorial.content.net.PacketServerboundTweakerulerUndo;
import martian.minefactorial.content.registry.MFBlockEntityTypes;
import martian.minefactorial.content.registry.MFDataComponents;
import martian.minefactorial.content.registry.MFItems;
import martian.minefactorial.foundation.block.AbstractEnergyBE;
import martian.minefactorial.foundation.block.IInventoryBE;
import martian.minefactorial.foundation.block.ISingleTankBE;
import martian.minefactorial.managers.TweakerulerHistoryManager;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.ICapabilityProvider;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
import net.neoforged.neoforge.common.util.TriState;
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.items.wrapper.SidedInvWrapper;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.registration.PayloadRegistrar;
import org.jetbrains.annotations.Nullable;

import javax.annotation.Nullable;
import java.util.Optional;

final class MinefactorialListeners {
private static @Nullable RegisterCapabilitiesEvent registerCapabilitiesEvent;

private MinefactorialListeners() { }
private MinefactorialListeners() {
}

@EventBusSubscriber(modid = Minefactorial.MODID, bus = EventBusSubscriber.Bus.MOD)
static final class ModBusEvents {
Expand Down Expand Up @@ -118,11 +133,64 @@ static void onRegisterPayloadHandlers(final RegisterPayloadHandlersEvent event)
final PayloadRegistrar registrar = event.registrar("1");

registrar.playToServer(PacketServerboundSetSmasherFortuneLevel.TYPE, PacketServerboundSetSmasherFortuneLevel.STREAM_CODEC, PacketServerboundSetSmasherFortuneLevel::handle);
registrar.playToServer(PacketServerboundTweakerulerUndo.TYPE, PacketServerboundTweakerulerUndo.STREAM_CODEC, PacketServerboundTweakerulerUndo::handle);
registrar.playToServer(PacketServerboundTweakerulerRedo.TYPE, PacketServerboundTweakerulerRedo.STREAM_CODEC, PacketServerboundTweakerulerRedo::handle);
registrar.playToServer(PacketServerboundSetTweakerulerMode.TYPE, PacketServerboundSetTweakerulerMode.STREAM_CODEC, PacketServerboundSetTweakerulerMode::handle);
}
}

// @EventBusSubscriber(modid = Minefactorial.MODID, bus = EventBusSubscriber.Bus.GAME)
// static final class GameBusEvents { }
@EventBusSubscriber(modid = Minefactorial.MODID, bus = EventBusSubscriber.Bus.GAME)
static final class GameBusEvents {
@SubscribeEvent
static void onPlayerLogout(final PlayerEvent.PlayerLoggedOutEvent event) {
if (event.getEntity() instanceof ServerPlayer serverPlayer) {
TweakerulerHistoryManager.removeHistoryFor(serverPlayer);
}
}

@SubscribeEvent
static void onPlayerLeftClickEmpty(final PlayerInteractEvent.LeftClickEmpty event) {
// Cancel a Ruler or Tweakeruler selection
if (event.getItemStack().is(MFItems.RULER) || event.getItemStack().is(MFItems.TWEAKERULER)) {
@Nullable Optional<BlockPos> posComponent = event.getItemStack().get(MFDataComponents.POS);
if (posComponent != null && posComponent.isPresent()) {
event.getItemStack().set(MFDataComponents.POS, Optional.empty());
}
// If the position was not set, check if the player was trying to close a Tweakeruler menu
else if (
event.getLevel().isClientSide &&
OverlayScrollMenu.isMenuOpen() &&
event.getItemStack().is(MFItems.TWEAKERULER) &&
OverlayScrollMenu.getTopMenu() instanceof ItemTweakeruler.Menu
) {
OverlayScrollMenu.popMenu();
}
}
}

@SubscribeEvent
static void onPlayerLeftClickBlock(final PlayerInteractEvent.LeftClickBlock event) {
// Cancel a Ruler or Tweakeruler selection
if (event.getItemStack().is(MFItems.RULER) || event.getItemStack().is(MFItems.TWEAKERULER)) {
@Nullable Optional<BlockPos> posComponent = event.getItemStack().get(MFDataComponents.POS);
if (posComponent != null && posComponent.isPresent()) {
event.getItemStack().set(MFDataComponents.POS, Optional.empty());
}
// If the position was not set, check if the player was trying to close a Tweakeruler menu
else if (
event.getLevel().isClientSide &&
OverlayScrollMenu.isMenuOpen() &&
event.getItemStack().is(MFItems.TWEAKERULER) &&
OverlayScrollMenu.getTopMenu() instanceof ItemTweakeruler.Menu
) {
OverlayScrollMenu.popMenu();
}

// Prevent rulers and tweakerulers from hitting blocks
event.setCanceled(true);
}
}
}

// Helpers
private static <T extends AbstractEnergyBE> void registerEnergyCapability(BlockEntityType<T> type) {
Expand Down
42 changes: 41 additions & 1 deletion src/main/java/martian/minefactorial/client/MFKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import net.neoforged.neoforge.client.settings.KeyConflictContext;
import net.neoforged.neoforge.client.settings.KeyModifier;
import net.neoforged.neoforge.common.util.Lazy;
import org.lwjgl.glfw.GLFW;

Expand All @@ -14,6 +15,45 @@ private MFKeys() { }
KeyConflictContext.GUI,
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_LEFT_SHIFT,
"key.categories.misc"
"key.categories.minefactorial"
));

public static final Lazy<KeyMapping> SCROLL_MENU_UP = Lazy.of(() -> new KeyMapping(
"key.minefactorial.scroll_menu_up",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_PAGE_UP,
"key.categories.minefactorial"
));

public static final Lazy<KeyMapping> SCROLL_MENU_DOWN = Lazy.of(() -> new KeyMapping(
"key.minefactorial.scroll_menu_down",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_PAGE_DOWN,
"key.categories.minefactorial"
));

public static final Lazy<KeyMapping> SCROLL_MENU_SELECT = Lazy.of(() -> new KeyMapping(
"key.minefactorial.scroll_menu_select",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_ENTER,
"key.categories.minefactorial"
));

public static final Lazy<KeyMapping> TWEAKERULER_UNDO = Lazy.of(() -> new KeyMapping(
"key.minefactorial.tweakeruler_undo",
KeyConflictContext.IN_GAME,
KeyModifier.CONTROL,
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_Z,
"key.categories.minefactorial"
));

public static final Lazy<KeyMapping> TWEAKERULER_REDO = Lazy.of(() -> new KeyMapping(
"key.minefactorial.tweakeruler_redo",
KeyConflictContext.IN_GAME,
KeyModifier.CONTROL,
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_Y,
"key.categories.minefactorial"
));
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.blaze3d.vertex.PoseStack;
import martian.minefactorial.Minefactorial;
import martian.minefactorial.client.overlay.scrollmenu.OverlayScrollMenu;
import martian.minefactorial.client.overlay.scrollmenu.ScrollMenu;
import martian.minefactorial.client.screen.*;
import martian.minefactorial.content.MFTags;
import martian.minefactorial.content.net.PacketServerboundTweakerulerRedo;
import martian.minefactorial.content.net.PacketServerboundTweakerulerUndo;
import martian.minefactorial.content.registry.MFDataComponents;
import martian.minefactorial.content.registry.MFFluidTypes;
import martian.minefactorial.content.registry.MFItems;
Expand All @@ -29,16 +33,16 @@
import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
import net.neoforged.neoforge.client.event.RenderLevelStageEvent;
import net.neoforged.neoforge.client.event.ScreenEvent;
import net.neoforged.neoforge.client.event.*;
import net.neoforged.neoforge.client.extensions.common.RegisterClientExtensionsEvent;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

import static martian.minefactorial.Minefactorial.id;

@OnlyIn(Dist.CLIENT)
public final class MinefactorialClient {
private MinefactorialClient() { }
Expand Down Expand Up @@ -83,6 +87,16 @@ static void registerClientExtensions(final RegisterClientExtensionsEvent event)
@SubscribeEvent
static void registerKeys(final RegisterKeyMappingsEvent event) {
event.register(MFKeys.SHOW_EXTENDED_TOOLTIP.get());
event.register(MFKeys.SCROLL_MENU_UP.get());
event.register(MFKeys.SCROLL_MENU_DOWN.get());
event.register(MFKeys.SCROLL_MENU_SELECT.get());
event.register(MFKeys.TWEAKERULER_UNDO.get());
event.register(MFKeys.TWEAKERULER_REDO.get());
}

@SubscribeEvent
static void registerGuiLayers(final RegisterGuiLayersEvent event) {
event.registerBelowAll(id("scroll_menu"), new OverlayScrollMenu());
}
}

Expand Down Expand Up @@ -156,6 +170,15 @@ static void onRenderWorld(final RenderLevelStageEvent event) {
}
}

@SubscribeEvent
static void onClientTick(final ClientTickEvent.Pre event) {
if (MFKeys.TWEAKERULER_UNDO.get().consumeClick()) {
PacketDistributor.sendToServer(new PacketServerboundTweakerulerUndo());
} else if (MFKeys.TWEAKERULER_REDO.get().consumeClick()) {
PacketDistributor.sendToServer(new PacketServerboundTweakerulerRedo());
}
}

@SubscribeEvent
static void onScreenKeyPress(final ScreenEvent.KeyPressed.Post event) {
if (MFKeys.SHOW_EXTENDED_TOOLTIP.get().isActiveAndMatches(InputConstants.Type.KEYSYM.getOrCreate(event.getKeyCode()))) {
Expand All @@ -169,5 +192,22 @@ static void onScreenKeyRelease(final ScreenEvent.KeyReleased.Post event) {
MFItem.showExtendedTooltip = false;
}
}

@SubscribeEvent
static void onScroll(final InputEvent.MouseScrollingEvent event) {
if (OverlayScrollMenu.isMenuOpen()) {
ScrollMenu top = OverlayScrollMenu.getTopMenu();

top.selection += event.getScrollDeltaY() > 0 ? -1 : 1;

if (top.selection >= top.entries.size()) {
top.selection = 0;
} else if (top.selection < 0) {
top.selection = top.entries.size() - 1;
}

event.setCanceled(true);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@OnlyIn(Dist.CLIENT)
package martian.minefactorial.client.overlay;

import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package martian.minefactorial.client.overlay.scrollmenu;

import martian.minefactorial.client.MFKeys;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.LayeredDraw;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.NotNull;

import java.util.Stack;

public class OverlayScrollMenu implements LayeredDraw.Layer {
private static final Stack<ScrollMenu> MENUS = new Stack<>();

public static final int LINE_HEIGHT = 20;
public static final float EASING = 0.1f;

@Override
public void render(@NotNull GuiGraphics graphics, @NotNull DeltaTracker delta) {
if (MENUS.isEmpty()) {
return;
}

// Render each menu
for (int i = 0; i < MENUS.size(); i++) {
int finalI = i; // Copy `i`
MENUS.forEach(menu -> menu.render(graphics, finalI * 64));
}

// Get the top menu to control it
ScrollMenu top = MENUS.peek();

if (MFKeys.SCROLL_MENU_DOWN.get().consumeClick()) {
top.selection++;
if (top.selection >= top.entries.size()) {
top.selection = 0;
}
} else if (MFKeys.SCROLL_MENU_UP.get().consumeClick()) {
top.selection--;
if (top.selection < 0) {
top.selection = top.entries.size() - 1;
}
}

if (MFKeys.SCROLL_MENU_SELECT.get().consumeClick()) {
top.entries.get(top.selection).callback().onSelect();
MENUS.pop();
}
}

public static boolean isMenuOpen() {
return !MENUS.isEmpty();
}

public static void clearMenus() {
MENUS.clear();
}

public static void pushMenu(ScrollMenu menu) {
MENUS.push(menu);
}

public static ScrollMenu popMenu() {
return MENUS.pop();
}

public static ScrollMenu getTopMenu() {
return MENUS.peek();
}

public static Stack<ScrollMenu> getMenus() {
return MENUS;
}
}
Loading

0 comments on commit 5d4af71

Please sign in to comment.