Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modifier key for placing cable parts on the opposite side #7864

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add modifier key for placing cable parts on the opposite side
  • Loading branch information
62832 committed Jun 16, 2024
commit 98ef7b9c4fac5028e7522fc26ed1c60bad0dff5a
1 change: 1 addition & 0 deletions src/generated/resources/assets/ae2/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@
"key.ae2.category": "Applied Energistics 2",
"key.ae2.guide": "Open Guide for Items",
"key.ae2.mouse_wheel_item_modifier": "Modifier for Mouse-Wheel Items",
"key.ae2.part_placement_opposite": "Place Parts on Opposite Side",
"key.ae2.portable_fluid_cell": "Open Portable Fluid Cell",
"key.ae2.portable_item_cell": "Open Portable Item Cell",
"key.ae2.wireless_terminal": "Open Wireless Terminal",
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/appeng/core/AppEngBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import appeng.api.stacks.AEKeyType;
import appeng.api.stacks.AEKeyTypes;
import appeng.api.stacks.AEKeyTypesInternal;
import appeng.core.definitions.AEAttachmentTypes;
import appeng.core.definitions.AEBlocks;
import appeng.core.definitions.AEItems;
import appeng.core.definitions.AEParts;
Expand Down Expand Up @@ -123,6 +124,7 @@ public AppEngBase(IEventBus modEventBus) {

AEComponents.DR.register(modEventBus);
InitStructures.register(modEventBus);
AEAttachmentTypes.register(modEventBus);
modEventBus.addListener(this::registerRegistries);
modEventBus.addListener(MainCreativeTab::initExternal);
modEventBus.addListener(InitNetwork::init);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/appeng/core/AppEngClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@
import appeng.client.render.effects.ParticleTypes;
import appeng.client.render.effects.VibrantFX;
import appeng.client.render.overlay.OverlayManager;
import appeng.core.definitions.AEAttachmentTypes;
import appeng.core.definitions.AEBlocks;
import appeng.core.network.ServerboundPacket;
import appeng.core.network.serverbound.MouseWheelPacket;
import appeng.core.network.serverbound.PartPlacementOppositePacket;
import appeng.helpers.IMouseWheelItem;
import appeng.hooks.BlockAttackHook;
import appeng.hooks.RenderBlockOutlineHook;
Expand Down Expand Up @@ -132,6 +134,10 @@ public class AppEngClient extends AppEngBase {
"key.ae2.mouse_wheel_item_modifier", KeyConflictContext.IN_GAME, InputConstants.Type.KEYSYM,
InputConstants.KEY_LSHIFT, "key.ae2.category");

private static final KeyMapping PART_PLACEMENT_OPPOSITE = new KeyMapping(
"key.ae2.part_placement_opposite", KeyConflictContext.IN_GAME, InputConstants.Type.KEYSYM,
InputConstants.KEY_LCONTROL, "key.ae2.category");

private final Guide guide;

public AppEngClient(IEventBus modEventBus) {
Expand Down Expand Up @@ -231,6 +237,7 @@ private void registerHotkeys(RegisterKeyMappingsEvent e) {
e.register(OpenGuideHotkey.getHotkey());
}
e.register(MOUSE_WHEEL_ITEM_MODIFIER);
e.register(PART_PLACEMENT_OPPOSITE);
Hotkeys.finalizeRegistration(e::register);
}

Expand Down Expand Up @@ -271,6 +278,7 @@ private void clientSetup(FMLClientSetupEvent event) {
});

NeoForge.EVENT_BUS.addListener(this::wheelEvent);
NeoForge.EVENT_BUS.addListener(this::ctrlEvent);
NeoForge.EVENT_BUS.register(OverlayManager.getInstance());
}

Expand Down Expand Up @@ -330,6 +338,18 @@ private void wheelEvent(final InputEvent.MouseScrollingEvent me) {
}
}

private void ctrlEvent(InputEvent.Key event) {
if (event.getKey() == PART_PLACEMENT_OPPOSITE.getKey().getValue()) {
var player = Minecraft.getInstance().player;

if (player != null) {
var isDown = event.getAction() == InputConstants.PRESS;
player.setData(AEAttachmentTypes.HOLDING_CTRL, isDown);
PacketDistributor.sendToServer(new PartPlacementOppositePacket(isDown));
}
}
}

public boolean shouldAddParticles(RandomSource r) {
return switch (Minecraft.getInstance().options.particles().get()) {
case ALL -> true;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/appeng/core/definitions/AEAttachmentTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package appeng.core.definitions;

import java.util.function.Supplier;

import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.registries.DeferredRegister;
import net.neoforged.neoforge.registries.NeoForgeRegistries;

import appeng.core.AppEng;

public final class AEAttachmentTypes {
private static final DeferredRegister<AttachmentType<?>> ATTACHMENT_TYPES = DeferredRegister
.create(NeoForgeRegistries.ATTACHMENT_TYPES, AppEng.MOD_ID);

public static final Supplier<AttachmentType<Boolean>> HOLDING_CTRL = ATTACHMENT_TYPES.register("ctrl",
() -> AttachmentType.builder(() -> false).build());

public static void register(IEventBus modEventBus) {
ATTACHMENT_TYPES.register(modEventBus);
}
}
2 changes: 2 additions & 0 deletions src/main/java/appeng/core/network/InitNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import appeng.core.network.serverbound.MEInteractionPacket;
import appeng.core.network.serverbound.MouseWheelPacket;
import appeng.core.network.serverbound.PartLeftClickPacket;
import appeng.core.network.serverbound.PartPlacementOppositePacket;
import appeng.core.network.serverbound.SelectKeyTypePacket;
import appeng.core.network.serverbound.SwapSlotsPacket;
import appeng.core.network.serverbound.SwitchGuisPacket;
Expand Down Expand Up @@ -78,6 +79,7 @@ public static void init(RegisterPayloadHandlersEvent event) {
serverbound(registrar, SelectKeyTypePacket.TYPE, SelectKeyTypePacket.STREAM_CODEC);
serverbound(registrar, SwapSlotsPacket.TYPE, SwapSlotsPacket.STREAM_CODEC);
serverbound(registrar, SwitchGuisPacket.TYPE, SwitchGuisPacket.STREAM_CODEC);
serverbound(registrar, PartPlacementOppositePacket.TYPE, PartPlacementOppositePacket.STREAM_CODEC);

// Bidirectional
bidirectional(registrar, ConfigValuePacket.TYPE, ConfigValuePacket.STREAM_CODEC);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package appeng.core.network.serverbound;

import org.jetbrains.annotations.NotNull;

import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.server.level.ServerPlayer;

import appeng.core.definitions.AEAttachmentTypes;
import appeng.core.network.CustomAppEngPayload;
import appeng.core.network.ServerboundPacket;

public record PartPlacementOppositePacket(boolean keyDown) implements ServerboundPacket {
public static final StreamCodec<RegistryFriendlyByteBuf, PartPlacementOppositePacket> STREAM_CODEC = StreamCodec
.ofMember(
PartPlacementOppositePacket::write,
PartPlacementOppositePacket::decode);

public static final Type<PartPlacementOppositePacket> TYPE = CustomAppEngPayload.createType("ctrl_down");

@NotNull
@Override
public Type<PartPlacementOppositePacket> type() {
return TYPE;
}

public static PartPlacementOppositePacket decode(RegistryFriendlyByteBuf buf) {
var keyDown = buf.readBoolean();
return new PartPlacementOppositePacket(keyDown);
}

public void write(RegistryFriendlyByteBuf data) {
data.writeBoolean(keyDown);
}

@Override
public void handleOnServer(ServerPlayer player) {
player.setData(AEAttachmentTypes.HOLDING_CTRL, keyDown);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private void generateLocalizations() {
add("key.ae2.wireless_terminal", "Open Wireless Terminal");
add("key.ae2.guide", "Open Guide for Items");
add("key.ae2.mouse_wheel_item_modifier", "Modifier for Mouse-Wheel Items");
add("key.ae2.part_placement_opposite", "Place Parts on Opposite Side");
add("key.toggle_focus.desc", "Toggle search box focus");
add("stat.ae2.items_extracted", "Items extracted from ME Storage");
add("stat.ae2.items_inserted", "Items added to ME Storage");
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/appeng/parts/PartPlacement.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import appeng.api.parts.IPartItem;
import appeng.api.parts.PartHelper;
import appeng.core.AELog;
import appeng.core.definitions.AEAttachmentTypes;
import appeng.parts.networking.CablePart;
import appeng.util.Platform;
import appeng.util.SettingsFrom;

public class PartPlacement {

public static InteractionResult place(UseOnContext context) {

var player = context.getPlayer();
Expand Down Expand Up @@ -136,6 +136,10 @@ public static Placement getPartPlacement(@Nullable Player player,
return replaceCablePlacement;
}

if (player != null) {
side = player.getData(AEAttachmentTypes.HOLDING_CTRL) ? side.getOpposite() : side;
}

if (canPlacePartOnBlock(player, level, partStack, pos, side)) {
return new Placement(pos, side);
}
Expand Down
Loading