From cf9917465a4930c9b1a8889ad44c0cde113a17d4 Mon Sep 17 00:00:00 2001
From: 17146 <1714673995@qq.com>
Date: Sat, 28 Jun 2025 00:16:14 +0800
Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E4=BF=AE=E6=94=B9=E6=97=A0?=
=?UTF-8?q?=E4=BA=BA=E6=9C=BA=E7=9B=B8=E5=85=B3=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../event/DroneEventHandler.java | 70 +++++++++++++++++++
.../atsuishio/superbwarfare/item/Monitor.java | 34 +++++++--
.../superbwarfare/tools/DronesTool.java | 5 ++
3 files changed, 104 insertions(+), 5 deletions(-)
create mode 100644 src/main/java/com/atsuishio/superbwarfare/event/DroneEventHandler.java
diff --git a/src/main/java/com/atsuishio/superbwarfare/event/DroneEventHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/DroneEventHandler.java
new file mode 100644
index 000000000..1896c8b78
--- /dev/null
+++ b/src/main/java/com/atsuishio/superbwarfare/event/DroneEventHandler.java
@@ -0,0 +1,70 @@
+package com.atsuishio.superbwarfare.event;
+
+import com.atsuishio.superbwarfare.init.ModItems;
+import com.atsuishio.superbwarfare.init.ModKeyMappings;
+import com.atsuishio.superbwarfare.item.Monitor;
+import com.atsuishio.superbwarfare.tools.DronesTool;
+import com.mojang.blaze3d.platform.InputConstants;
+import net.minecraft.client.Minecraft;
+import net.minecraft.client.multiplayer.ClientLevel;
+import net.minecraft.world.entity.Entity;
+import net.minecraft.world.entity.player.Player;
+import net.neoforged.bus.api.SubscribeEvent;
+import net.neoforged.neoforge.client.event.ClientTickEvent;
+import net.neoforged.neoforge.client.event.InputEvent;
+import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
+
+import java.util.UUID;
+
+/**
+ * Code based on @Mafuyu404's DiligentStalker
+ */
+//@net.minecraftforge.fml.common.Mod.EventBusSubscriber(modid = Mod.MODID, value = Dist.CLIENT)
+public class DroneEventHandler {
+
+ @SubscribeEvent
+ public static void onClientTick(ClientTickEvent.Post event) {
+ if (Minecraft.getInstance().screen != null) return;
+
+ var player = Minecraft.getInstance().player;
+ UUID entityUUID = Monitor.getDroneUUID(player);
+ if (entityUUID != null) {
+ ClientLevel level = player.clientLevel;
+ level.entitiesForRendering().forEach(entity -> {
+ if (entity.getUUID().equals(entityUUID)) {
+ DronesTool.connect(player, entity);
+ }
+ });
+ }
+ if (!DronesTool.hasInstanceOf(player)) return;
+ DronesTool instance = DronesTool.getInstanceOf(player);
+ Entity stalker = instance.getDrone();
+ // TODO 调整角度?
+ }
+
+ @SubscribeEvent
+ public static void onInteract(PlayerInteractEvent.EntityInteract event) {
+ if (event.getSide().isClient()) {
+ var player = event.getEntity();
+ if (player.isShiftKeyDown()) return;
+ if (event.getItemStack().is(ModItems.MONITOR.get())) {
+ DronesTool.connect(player, event.getTarget());
+ event.setCanceled(true);
+ }
+ }
+ }
+
+ @SubscribeEvent
+ public static void onInputKey(InputEvent.Key event) {
+ if (Minecraft.getInstance().screen != null) return;
+ Player player = Minecraft.getInstance().player;
+ if (!DronesTool.hasInstanceOf(player)) return;
+ if (event.getAction() == InputConstants.PRESS) {
+ if (event.getKey() == ModKeyMappings.DISMOUNT.getKey().getValue()) {
+ if (DronesTool.hasInstanceOf(player)) {
+ DronesTool.getInstanceOf(player).disconnect();
+ }
+ }
+ }
+ }
+}
diff --git a/src/main/java/com/atsuishio/superbwarfare/item/Monitor.java b/src/main/java/com/atsuishio/superbwarfare/item/Monitor.java
index be9547f3e..f4d83906c 100644
--- a/src/main/java/com/atsuishio/superbwarfare/item/Monitor.java
+++ b/src/main/java/com/atsuishio/superbwarfare/item/Monitor.java
@@ -2,6 +2,7 @@ package com.atsuishio.superbwarfare.item;
import com.atsuishio.superbwarfare.entity.vehicle.DroneEntity;
import com.atsuishio.superbwarfare.event.ClientEventHandler;
+import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.network.message.receive.ResetCameraTypeMessage;
import com.atsuishio.superbwarfare.tools.EntityFindUtil;
import com.atsuishio.superbwarfare.tools.FormatTool;
@@ -29,15 +30,18 @@ import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
import java.util.List;
+import java.util.UUID;
public class Monitor extends Item {
public static final String LINKED = "Linked";
public static final String LINKED_DRONE = "LinkedDrone";
+ public static final String DRONE_UUID = "DroneUUID";
public Monitor() {
super(new Properties().stacksTo(1));
@@ -69,24 +73,24 @@ public class Monitor extends Item {
@Override
@ParametersAreNonnullByDefault
- public @NotNull InteractionResultHolder use(Level world, Player player, InteractionHand hand) {
+ public @NotNull InteractionResultHolder use(Level level, Player player, InteractionHand hand) {
ItemStack stack = player.getMainHandItem();
var tag = NBTTool.getTag(stack);
if (!tag.getBoolean(LINKED)) {
- return super.use(world, player, hand);
+ return super.use(level, player, hand);
}
if (tag.getBoolean("Using")) {
tag.putBoolean("Using", false);
- if (world.isClientSide) {
+ if (level.isClientSide) {
if (ClientEventHandler.lastCameraType != null) {
Minecraft.getInstance().options.setCameraType(ClientEventHandler.lastCameraType);
}
}
} else {
tag.putBoolean("Using", true);
- if (world.isClientSide) {
+ if (level.isClientSide) {
ClientEventHandler.lastCameraType = Minecraft.getInstance().options.getCameraType();
Minecraft.getInstance().options.setCameraType(CameraType.THIRD_PERSON_BACK);
}
@@ -96,7 +100,15 @@ public class Monitor extends Item {
DroneEntity drone = EntityFindUtil.findDrone(player.level(), tag.getString(LINKED_DRONE));
this.resetDroneData(drone);
- return super.use(world, player, hand);
+ return super.use(level, player, hand);
+// ItemStack stack = player.getItemInHand(hand);
+// if (!player.isShiftKeyDown()) {
+// CompoundTag tag = stack.getOrCreateTag();
+// if (DronesTool.hasInstanceOf(player)) return InteractionResultHolder.fail(stack);
+// if (!tag.contains(DRONE_UUID)) return InteractionResultHolder.fail(stack);
+// player.startUsingItem(hand);
+// }
+// return InteractionResultHolder.fail(stack);
}
@Override
@@ -186,4 +198,16 @@ public class Monitor extends Item {
}
}
}
+
+ @Nullable
+ public static UUID getDroneUUID(Player player) {
+ if (player == null) return null;
+ if (player.getMainHandItem().is(ModItems.MONITOR.get())) {
+ var tag = NBTTool.getTag(player.getMainHandItem());
+ if (tag.contains(DRONE_UUID)) {
+ return tag.getUUID(DRONE_UUID);
+ }
+ }
+ return null;
+ }
}
diff --git a/src/main/java/com/atsuishio/superbwarfare/tools/DronesTool.java b/src/main/java/com/atsuishio/superbwarfare/tools/DronesTool.java
index a97fadd10..82261f733 100644
--- a/src/main/java/com/atsuishio/superbwarfare/tools/DronesTool.java
+++ b/src/main/java/com/atsuishio/superbwarfare/tools/DronesTool.java
@@ -1,5 +1,7 @@
package com.atsuishio.superbwarfare.tools;
+import net.minecraft.ChatFormatting;
+import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
@@ -44,12 +46,15 @@ public class DronesTool {
public static DronesTool connect(Player player, Entity stalker) {
if (player == null || stalker == null) return null;
+ // 这边可能需要修改一下逻辑,不知道原代码部分是否允许1玩家对n无人机
if (hasInstanceOf(player) || hasInstanceOf(stalker)) return null;
if (player.level().isClientSide) {
// TODO 向客户端发送连接的网络包
}
+ player.displayClientMessage(Component.translatable("tips.superbwarfare.monitor.linked").withStyle(ChatFormatting.GREEN), true);
+
INSTANCE_MAP.put(player.getUUID(), stalker.getId());
DRONE_TO_PLAYER.put(stalker.getId(), player.getUUID());