Skip to content

Commit

Permalink
Pick block support (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mari023 authored Aug 6, 2024
1 parent c40e631 commit 1f7a46f
Show file tree
Hide file tree
Showing 20 changed files with 340 additions and 63 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- move Menus into ae2 namespace
- move Menus into ae2 namespace
- allow Pick Block to draw items from the terminal
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class AE2wtlibComponents {
public static final DataComponentType<IncludeExclude> INSERT_MODE = register("insert_mode", INCLUDE_EXCLUDE_CODECS);
public static final DataComponentType<Boolean> RESTOCK = register("restock", builder -> builder
.persistent(Codec.BOOL).networkSynchronized(ByteBufCodecs.BOOL));
public static final DataComponentType<Boolean> PICK_BLOCK = register("pick_block", builder -> builder
.persistent(Codec.BOOL).networkSynchronized(ByteBufCodecs.BOOL));

public static final DataComponentType<CompoundTag> PATTERN_ENCODING_LOGIC = register("pattern_encoding_logic",
COMPOUND_TAG_CODECS);
Expand Down
13 changes: 7 additions & 6 deletions api/src/main/java/de/mari_023/ae2wtlib/api/TextConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ private TextConstants() {}
Component.translatable("gui.ae2wtlib.magnetcard.desc.inv").setStyle(STYLE_GREEN));
public static final MutableComponent HOTKEY_MAGNETCARD_ME = Component.translatable("gui.ae2wtlib.magnetcard.hotkey",
Component.translatable("gui.ae2wtlib.magnetcard.desc.me").setStyle(STYLE_GREEN));
public static final MutableComponent PICKUP_ME_NO_MAGNET = Component.translatable("gui.ae2wtlib.magnetcard.hotkey",
Component.translatable("gui.ae2wtlib.magnetcard.desc.me_no_magnet").setStyle(STYLE_GREEN));
public static final MutableComponent HOTKEY_MAGNETCARD_OFF = Component.translatable(
"gui.ae2wtlib.magnetcard.hotkey",
Component.translatable("gui.ae2wtlib.magnetcard.desc.off").setStyle(STYLE_RED));
public static final MutableComponent MAGNETCARD_OFF = Component.translatable("gui.ae2wtlib.magnetcard").append("\n")
.append(Component.translatable("gui.ae2wtlib.magnetcard.desc.off"));
public static final MutableComponent MAGNETCARD_INVENTORY = Component.translatable("gui.ae2wtlib.magnetcard")
.append("\n").append(Component.translatable("gui.ae2wtlib.magnetcard.desc.inv"));
public static final MutableComponent MAGNETCARD_ME = Component.translatable("gui.ae2wtlib.magnetcard").append("\n")
.append(Component.translatable("gui.ae2wtlib.magnetcard.desc.me"));

public static final MutableComponent UNIVERSAL = Component
.translatable("item.ae2wtlib.wireless_universal_terminal.desc").withStyle(STYLE_GRAY);
Expand Down Expand Up @@ -95,4 +91,9 @@ public static Component getInsertMode(IncludeExclude includeExclude) {
public static final Component NO_QNB_UPGRADE = Component.translatable("chat.ae2wtlib.NoQuantumBridgeCard");
public static final Component NO_QNB = Component.translatable("chat.ae2wtlib.NoQuantumBridge");
public static final Component DIFFERENT_NETWORKS = Component.translatable("chat.ae2wtlib.NetworkMismatch");

public static final Component PICK_BLOCK = Component.translatable("gui.ae2wtlib.pick_block.text");
public static final Component RESTOCK = Component.translatable("gui.ae2wtlib.restock.text");
public static final Component MAGNET = Component.translatable("gui.ae2wtlib.magnet.text");
public static final Component PICKUP_TO_ME = Component.translatable("gui.ae2wtlib.pickup_to_me.text");
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ public WirelessTerminalMenuHost<?> getMenuHost(Player player, ItemMenuHostLocato
return WTDefinition.of(locator.locateItem(player)).wTMenuHostFactory().create(this, player, locator,
(p, subMenu) -> tryOpen(player, locator, true));
}

public boolean isNotReplaceableByPickAction(ItemStack stack, Player player, int inventorySlot) {
return true;
}
}
53 changes: 51 additions & 2 deletions src/main/java/de/mari_023/ae2wtlib/AE2wtlibEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.function.Consumer;

import net.minecraft.network.protocol.game.ClientboundSetCarriedItemPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stats;
import net.minecraft.world.entity.item.ItemEntity;
Expand All @@ -16,11 +17,11 @@
import appeng.me.helpers.PlayerSource;

import de.mari_023.ae2wtlib.api.AE2wtlibComponents;
import de.mari_023.ae2wtlib.networking.PickBlockPacket;
import de.mari_023.ae2wtlib.networking.UpdateRestockPacket;
import de.mari_023.ae2wtlib.wct.CraftingTerminalHandler;
import de.mari_023.ae2wtlib.wct.magnet_card.MagnetHandler;
import de.mari_023.ae2wtlib.wct.magnet_card.MagnetHost;
import de.mari_023.ae2wtlib.wct.magnet_card.MagnetMode;

public class AE2wtlibEvents {
/**
Expand Down Expand Up @@ -91,7 +92,7 @@ public static void insertStackInME(ItemEntity entity, Player player) {
CraftingTerminalHandler cTHandler = CraftingTerminalHandler.getCraftingTerminalHandler(player);
ItemStack terminal = cTHandler.getCraftingTerminal();

if (!(MagnetHandler.getMagnetMode(terminal) == MagnetMode.PICKUP_ME))
if (!(MagnetHandler.getMagnetMode(terminal).pickupToME()))
return;
if (!cTHandler.inRange())
return;
Expand Down Expand Up @@ -120,4 +121,52 @@ public static void insertStackInME(ItemEntity entity, Player player) {

stack.setCount(leftover);
}

public static void pickBlock(ItemStack stack) {
PacketDistributor.sendToServer(new PickBlockPacket(stack));
}

public static void pickBlock(ServerPlayer player, ItemStack stack) {
var cTHandler = CraftingTerminalHandler.getCraftingTerminalHandler(player);
ItemStack terminal = cTHandler.getCraftingTerminal();
if (!terminal.getOrDefault(AE2wtlibComponents.PICK_BLOCK, false))
return;

if (cTHandler.getTargetGrid() == null)
return;
if (cTHandler.getTargetGrid().getStorageService() == null)
return;
var networkInventory = cTHandler.getTargetGrid().getStorageService().getInventory();
var playerSource = new PlayerSource(player, null);

var inventory = player.getInventory();
int targetSlot = inventory.getSuitableHotbarSlot();
var toReplace = inventory.getItem(targetSlot);

var insert = networkInventory.insert(AEItemKey.of(toReplace), toReplace.getCount(), Actionable.SIMULATE,
playerSource);
if (insert < toReplace.getCount())
return;
var extracted = networkInventory.extract(AEItemKey.of(stack), 32, Actionable.SIMULATE, playerSource);
if (extracted == 0)
return;

insert = networkInventory.insert(AEItemKey.of(toReplace), toReplace.getCount(), Actionable.MODULATE,
playerSource);
if (insert < toReplace.getCount()) {
toReplace.setCount(toReplace.getCount() - (int) insert);
inventory.setItem(targetSlot, toReplace);
return;
}

extracted = networkInventory.extract(AEItemKey.of(stack), 32, Actionable.MODULATE, playerSource);
if (extracted == 0) {
inventory.setItem(targetSlot, ItemStack.EMPTY);
return;
}
stack.setCount((int) extracted);
inventory.setItem(targetSlot, stack);
inventory.selected = targetSlot;
player.connection.send(new ClientboundSetCarriedItemPacket(player.getInventory().selected));
}
}
8 changes: 3 additions & 5 deletions src/main/java/de/mari_023/ae2wtlib/AE2wtlibForge.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@

import de.mari_023.ae2wtlib.api.AE2wtlibAPI;
import de.mari_023.ae2wtlib.api.terminal.ItemWT;
import de.mari_023.ae2wtlib.networking.AE2wtlibPacket;
import de.mari_023.ae2wtlib.networking.CycleTerminalPacket;
import de.mari_023.ae2wtlib.networking.RestockAmountPacket;
import de.mari_023.ae2wtlib.networking.UpdateRestockPacket;
import de.mari_023.ae2wtlib.networking.UpdateWUTPackage;
import de.mari_023.ae2wtlib.networking.*;

@Mod(AE2wtlibAPI.MOD_NAME)
@EventBusSubscriber
Expand All @@ -56,6 +52,8 @@ public AE2wtlibForge(IEventBus modEventBus, ModContainer modContainer) {
modEventBus.addListener((RegisterPayloadHandlersEvent event) -> {
PayloadRegistrar registrar = event.registrar(AE2wtlibAPI.MOD_NAME);
registerPacket(registrar, CycleTerminalPacket.ID, CycleTerminalPacket.STREAM_CODEC);
registerPacket(registrar, PickBlockPacket.ID, PickBlockPacket.STREAM_CODEC);
registerPacket(registrar, TerminalSettingsPacket.ID, TerminalSettingsPacket.STREAM_CODEC);
registerPacket(registrar, UpdateWUTPackage.ID, UpdateWUTPackage.STREAM_CODEC);
registerPacket(registrar, UpdateRestockPacket.ID, UpdateRestockPacket.STREAM_CODEC);
registerPacket(registrar, RestockAmountPacket.ID, RestockAmountPacket.STREAM_CODEC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public boolean run(Player player) {
yield MagnetMode.PICKUP_ME;
}
case PICKUP_ME -> {
player.displayClientMessage(TextConstants.PICKUP_ME_NO_MAGNET, true);
yield MagnetMode.PICKUP_ME_NO_MAGNET;
}
case PICKUP_ME_NO_MAGNET -> {
player.displayClientMessage(TextConstants.HOTKEY_MAGNETCARD_OFF, true);
yield MagnetMode.OFF;
}
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/de/mari_023/ae2wtlib/mixin/MinecraftMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.mari_023.ae2wtlib.mixin;

import com.llamalad7.mixinextras.sugar.Local;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.item.ItemStack;

import de.mari_023.ae2wtlib.AE2wtlibEvents;

@Mixin(Minecraft.class)
public abstract class MinecraftMixin {
@Shadow
public LocalPlayer player;

@Inject(method = "pickBlock", at = {
@At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/world/entity/player/Inventory;findSlotMatchingItem(Lnet/minecraft/world/item/ItemStack;)I") })
public void pickBlock(CallbackInfo ci, @Local ItemStack itemstack, @Local int i) {
if (player.getAbilities().instabuild)
return;
if (player.isSpectator())
return;
if (i != -1)
return;
AE2wtlibEvents.pickBlock(itemstack);
}
}
29 changes: 29 additions & 0 deletions src/main/java/de/mari_023/ae2wtlib/networking/PickBlockPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.mari_023.ae2wtlib.networking;

import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

import de.mari_023.ae2wtlib.AE2wtlibEvents;
import de.mari_023.ae2wtlib.api.AE2wtlibAPI;

public record PickBlockPacket(ItemStack itemStack) implements AE2wtlibPacket {
public static final Type<PickBlockPacket> ID = new Type<>(AE2wtlibAPI.id("pick_block"));
public static final StreamCodec<RegistryFriendlyByteBuf, PickBlockPacket> STREAM_CODEC = ItemStack.STREAM_CODEC
.map(PickBlockPacket::new, PickBlockPacket::itemStack);

@Override
public void processPacketData(Player player) {
if (!(player instanceof ServerPlayer serverPlayer))
return;
AE2wtlibEvents.pickBlock(serverPlayer, itemStack);
}

@Override
public Type<? extends CustomPacketPayload> type() {
return ID;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.mari_023.ae2wtlib.networking;

import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.world.entity.player.Player;

import appeng.menu.locator.ItemMenuHostLocator;

import de.mari_023.ae2wtlib.AE2wtlibAdditionalComponents;
import de.mari_023.ae2wtlib.api.AE2wtlibAPI;
import de.mari_023.ae2wtlib.api.AE2wtlibComponents;
import de.mari_023.ae2wtlib.wct.magnet_card.MagnetMode;

public record TerminalSettingsPacket(ItemMenuHostLocator terminal, boolean pickBlock, boolean restock, boolean magnet,
boolean pickupToME) implements AE2wtlibPacket {

public static final Type<TerminalSettingsPacket> ID = new Type<>(AE2wtlibAPI.id("terminal_settings"));
public static final StreamCodec<RegistryFriendlyByteBuf, TerminalSettingsPacket> STREAM_CODEC = StreamCodec
.composite(
AE2wtlibComponents.MENU_HOST_LOCATOR_STREAM_CODEC, TerminalSettingsPacket::terminal,
ByteBufCodecs.BOOL, TerminalSettingsPacket::pickBlock,
ByteBufCodecs.BOOL, TerminalSettingsPacket::restock,
ByteBufCodecs.BOOL, TerminalSettingsPacket::magnet,
ByteBufCodecs.BOOL, TerminalSettingsPacket::pickupToME,
TerminalSettingsPacket::new);
@Override
public void processPacketData(Player player) {
var stack = terminal.locateItem(player);
stack.set(AE2wtlibComponents.PICK_BLOCK, pickBlock);
stack.set(AE2wtlibComponents.RESTOCK, restock);
var magnetSettings = stack.getOrDefault(AE2wtlibAdditionalComponents.MAGNET_SETTINGS, MagnetMode.OFF);
magnetSettings = magnetSettings.set(magnet, pickupToME);
stack.set(AE2wtlibAdditionalComponents.MAGNET_SETTINGS, magnetSettings);
}

@Override
public Type<? extends CustomPacketPayload> type() {
return ID;
}
}
49 changes: 8 additions & 41 deletions src/main/java/de/mari_023/ae2wtlib/wct/WCTScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
import de.mari_023.ae2wtlib.api.gui.ScrollingUpgradesPanel;
import de.mari_023.ae2wtlib.api.terminal.IUniversalTerminalCapable;
import de.mari_023.ae2wtlib.api.terminal.WTMenuHost;
import de.mari_023.ae2wtlib.wct.magnet_card.MagnetMode;

public class WCTScreen extends CraftingTermScreen<WCTMenu> implements IUniversalTerminalCapable {
private final IconButton magnetCardToggleButton;
private final IconButton magnetCardMenuButton;
private final ScrollingUpgradesPanel upgradesPanel;

Expand All @@ -29,8 +27,9 @@ public WCTScreen(WCTMenu container, Inventory playerInventory, Component title,
if (getMenu().isWUT())
addToLeftToolbar(cycleTerminalButton());

magnetCardToggleButton = new IconButton(btn -> setMagnetMode(), Icon.MAGNET);
widgets.add("magnetCardToggleButton", magnetCardToggleButton);
IconButton wirelessTerminalSettingsButton = new IconButton(
btn -> switchToScreen(new WirelessTerminalSettingsScreen(this)), Icon.MAGNET);
widgets.add("wirelessTerminalSettingsButton", wirelessTerminalSettingsButton);

magnetCardMenuButton = new IconButton(btn -> getMenu().openMagnetMenu(), Icon.MAGNET_FILTER);
widgets.add("magnetCardMenuButton", magnetCardMenuButton);
Expand All @@ -50,44 +49,12 @@ public void init() {
upgradesPanel.setMaxRows(Math.max(2, getVisibleRows()));
}

private void setMagnetMode() {
if (isHandlingRightClick()) {
switch (getMenu().getMagnetMode()) {
case OFF -> getMenu().setMagnetMode(MagnetMode.PICKUP_ME);
case PICKUP_INVENTORY -> getMenu().setMagnetMode(MagnetMode.OFF);
case PICKUP_ME -> getMenu().setMagnetMode(MagnetMode.PICKUP_INVENTORY);
}
return;
}
switch (getMenu().getMagnetMode()) {
case OFF -> getMenu().setMagnetMode(MagnetMode.PICKUP_INVENTORY);
case PICKUP_INVENTORY -> getMenu().setMagnetMode(MagnetMode.PICKUP_ME);
case PICKUP_ME -> getMenu().setMagnetMode(MagnetMode.OFF);
}
}

private void setMagnetModeText() {
switch (getMenu().getMagnetMode()) {
case INVALID, NO_CARD -> {
magnetCardToggleButton.setVisibility(false);
magnetCardMenuButton.setVisibility(false);
}
case OFF -> {
magnetCardToggleButton.setVisibility(true);
magnetCardMenuButton.setVisibility(true);
magnetCardToggleButton.setMessage(TextConstants.MAGNETCARD_OFF);
}
case PICKUP_INVENTORY -> {
magnetCardToggleButton.setVisibility(true);
magnetCardMenuButton.setVisibility(true);
magnetCardToggleButton.setMessage(TextConstants.MAGNETCARD_INVENTORY);
}
case PICKUP_ME -> {
magnetCardToggleButton.setVisibility(true);
magnetCardMenuButton.setVisibility(true);
magnetCardToggleButton.setMessage(TextConstants.MAGNETCARD_ME);
}
}
magnetCardMenuButton.setVisibility(
switch (getMenu().getMagnetMode()) {
case INVALID, NO_CARD -> false;
case OFF, PICKUP_ME, PICKUP_INVENTORY, PICKUP_ME_NO_MAGNET -> true;
});
}

protected void updateBeforeRender() {
Expand Down
Loading

0 comments on commit 1f7a46f

Please sign in to comment.