From c16d56821e75980271a9501df5c66a4aea00c68a Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Sat, 28 Jun 2025 01:24:14 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4m2hb=E7=9A=84=E5=8F=AF?= =?UTF-8?q?=E7=94=A8perk=E5=88=97=E8=A1=A8=EF=BC=8C=E6=9A=82=E6=97=B6?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E6=8E=89=E6=97=A0=E4=BA=BA=E6=9C=BA=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/DroneEventHandler.java | 106 +++---- .../atsuishio/superbwarfare/item/Monitor.java | 27 +- .../superbwarfare/tools/DronesTool.java | 259 +++++++++--------- .../data/superbwarfare/guns/m_2_hb.json | 1 - 4 files changed, 182 insertions(+), 211 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/event/DroneEventHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/DroneEventHandler.java index 1896c8b78..694bb4e9f 100644 --- a/src/main/java/com/atsuishio/superbwarfare/event/DroneEventHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/event/DroneEventHandler.java @@ -1,70 +1,54 @@ 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(); - } - } - } - } +// @SubscribeEvent +// public static void onClientTick(TickEvent.ClientTickEvent event) { +// if (Minecraft.getInstance().screen != null) return; +// if (event.phase != TickEvent.Phase.END) 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 f4d83906c..8644cef3d 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/Monitor.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/Monitor.java @@ -2,7 +2,6 @@ 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; @@ -30,18 +29,16 @@ 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 static final String DRONE_UUID = "DroneUUID"; public Monitor() { super(new Properties().stacksTo(1)); @@ -199,15 +196,15 @@ 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; - } +// @Nullable +// public static UUID getDroneUUID(Player player) { +// if (player == null) return null; +// if (player.getMainHandItem().is(ModItems.MONITOR.get())) { +// CompoundTag tag = player.getMainHandItem().getOrCreateTag(); +// 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 82261f733..2881578c4 100644 --- a/src/main/java/com/atsuishio/superbwarfare/tools/DronesTool.java +++ b/src/main/java/com/atsuishio/superbwarfare/tools/DronesTool.java @@ -1,142 +1,133 @@ 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; - -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; - /** * Code based on @Mafuyu404's DiligentStalker */ public class DronesTool { - private final UUID playerUUID; - private final int droneId; - public final Level level; - public static final ConcurrentHashMap INSTANCE_MAP = new ConcurrentHashMap<>(); - - private static final ConcurrentHashMap INSTANCE_CACHE = new ConcurrentHashMap<>(); - private static final ConcurrentHashMap DRONES_CACHE = new ConcurrentHashMap<>(); - private static final ConcurrentHashMap DRONE_TO_PLAYER = new ConcurrentHashMap<>(); - - // 缓存失效时间 - private static final long CACHE_EXPIRE_TIME = 5000; - private long lastAccessTime; - - public DronesTool(UUID playerUUID, int droneId, Level level) { - this.playerUUID = playerUUID; - this.droneId = droneId; - this.level = level; - this.lastAccessTime = System.currentTimeMillis(); - } - - public Player getPlayer() { - this.lastAccessTime = System.currentTimeMillis(); - return this.level.getPlayerByUUID(this.playerUUID); - } - - public Entity getDrone() { - this.lastAccessTime = System.currentTimeMillis(); - return this.level.getEntity(this.droneId); - } - - 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()); - - DronesTool instance = new DronesTool(player.getUUID(), stalker.getId(), player.level()); - INSTANCE_CACHE.put(player.getUUID(), instance); - DRONES_CACHE.put(stalker.getId(), instance); - - return instance; - } - - public static DronesTool getInstanceOf(Entity entity) { - if (entity == null) return null; - - UUID uuid = entity.getUUID(); - int id = entity.getId(); - - DronesTool cache = INSTANCE_CACHE.get(uuid); - if (cache != null && !isCacheExpired(cache)) { - return cache; - } - - cache = DRONES_CACHE.get(id); - if (cache != null && !isCacheExpired(cache)) { - return cache; - } - - boolean isPlayer = INSTANCE_MAP.containsKey(uuid); - boolean isDrone = DRONE_TO_PLAYER.containsKey(id); - - DronesTool instance = null; - if (isPlayer) { - var stalkerId = INSTANCE_MAP.get(uuid); - if (stalkerId != null) { - instance = new DronesTool(uuid, stalkerId, entity.level()); - INSTANCE_CACHE.put(uuid, instance); - } - } else if (isDrone) { - UUID playerUUID = DRONE_TO_PLAYER.get(id); - if (playerUUID != null) { - instance = new DronesTool(playerUUID, id, entity.level()); - DRONES_CACHE.put(id, instance); - } - } - - return instance; - } - - public void disconnect() { - if (level.isClientSide) { - // TODO 向客户端发送断开连接的网络包 - } - INSTANCE_MAP.remove(this.playerUUID); - DRONE_TO_PLAYER.remove(this.droneId); - INSTANCE_CACHE.remove(this.playerUUID); - DRONES_CACHE.remove(this.droneId); - } - - public static boolean hasInstanceOf(Entity entity) { - if (entity == null) return false; - - UUID uuid = entity.getUUID(); - if (INSTANCE_CACHE.containsKey(uuid) && !isCacheExpired(INSTANCE_CACHE.get(uuid))) { - return true; - } - - int id = entity.getId(); - if (DRONES_CACHE.containsKey(id) && !isCacheExpired(DRONES_CACHE.get(id))) { - return true; - } - - return (INSTANCE_MAP.containsKey(uuid) || INSTANCE_MAP.containsValue(id)); - } - - private static boolean isCacheExpired(DronesTool instance) { - return System.currentTimeMillis() - instance.lastAccessTime > CACHE_EXPIRE_TIME; - } - - public static void cleanupExpiredCache() { - long currentTime = System.currentTimeMillis(); - INSTANCE_CACHE.entrySet().removeIf(entry -> - currentTime - entry.getValue().lastAccessTime > CACHE_EXPIRE_TIME); - DRONES_CACHE.entrySet().removeIf(entry -> - currentTime - entry.getValue().lastAccessTime > CACHE_EXPIRE_TIME); - } +// private final UUID playerUUID; +// private final int droneId; +// public final Level level; +// public static final ConcurrentHashMap INSTANCE_MAP = new ConcurrentHashMap<>(); +// +// private static final ConcurrentHashMap INSTANCE_CACHE = new ConcurrentHashMap<>(); +// private static final ConcurrentHashMap DRONES_CACHE = new ConcurrentHashMap<>(); +// private static final ConcurrentHashMap DRONE_TO_PLAYER = new ConcurrentHashMap<>(); +// +// // 缓存失效时间 +// private static final long CACHE_EXPIRE_TIME = 5000; +// private long lastAccessTime; +// +// public DronesTool(UUID playerUUID, int droneId, Level level) { +// this.playerUUID = playerUUID; +// this.droneId = droneId; +// this.level = level; +// this.lastAccessTime = System.currentTimeMillis(); +// } +// +// public Player getPlayer() { +// this.lastAccessTime = System.currentTimeMillis(); +// return this.level.getPlayerByUUID(this.playerUUID); +// } +// +// public Entity getDrone() { +// this.lastAccessTime = System.currentTimeMillis(); +// return this.level.getEntity(this.droneId); +// } +// +// 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()); +// +// DronesTool instance = new DronesTool(player.getUUID(), stalker.getId(), player.level()); +// INSTANCE_CACHE.put(player.getUUID(), instance); +// DRONES_CACHE.put(stalker.getId(), instance); +// +// return instance; +// } +// +// public static DronesTool getInstanceOf(Entity entity) { +// if (entity == null) return null; +// +// UUID uuid = entity.getUUID(); +// int id = entity.getId(); +// +// DronesTool cache = INSTANCE_CACHE.get(uuid); +// if (cache != null && !isCacheExpired(cache)) { +// return cache; +// } +// +// cache = DRONES_CACHE.get(id); +// if (cache != null && !isCacheExpired(cache)) { +// return cache; +// } +// +// boolean isPlayer = INSTANCE_MAP.containsKey(uuid); +// boolean isDrone = DRONE_TO_PLAYER.containsKey(id); +// +// DronesTool instance = null; +// if (isPlayer) { +// var stalkerId = INSTANCE_MAP.get(uuid); +// if (stalkerId != null) { +// instance = new DronesTool(uuid, stalkerId, entity.level()); +// INSTANCE_CACHE.put(uuid, instance); +// } +// } else if (isDrone) { +// UUID playerUUID = DRONE_TO_PLAYER.get(id); +// if (playerUUID != null) { +// instance = new DronesTool(playerUUID, id, entity.level()); +// DRONES_CACHE.put(id, instance); +// } +// } +// +// return instance; +// } +// +// public void disconnect() { +// if (level.isClientSide) { +// // TODO 向客户端发送断开连接的网络包 +// } +// INSTANCE_MAP.remove(this.playerUUID); +// DRONE_TO_PLAYER.remove(this.droneId); +// INSTANCE_CACHE.remove(this.playerUUID); +// DRONES_CACHE.remove(this.droneId); +// } +// +// public static boolean hasInstanceOf(Entity entity) { +// if (entity == null) return false; +// +// UUID uuid = entity.getUUID(); +// if (INSTANCE_CACHE.containsKey(uuid) && !isCacheExpired(INSTANCE_CACHE.get(uuid))) { +// return true; +// } +// +// int id = entity.getId(); +// if (DRONES_CACHE.containsKey(id) && !isCacheExpired(DRONES_CACHE.get(id))) { +// return true; +// } +// +// return (INSTANCE_MAP.containsKey(uuid) || INSTANCE_MAP.containsValue(id)); +// } +// +// private static boolean isCacheExpired(DronesTool instance) { +// return System.currentTimeMillis() - instance.lastAccessTime > CACHE_EXPIRE_TIME; +// } +// +// public static void cleanupExpiredCache() { +// long currentTime = System.currentTimeMillis(); +// INSTANCE_CACHE.entrySet().removeIf(entry -> +// currentTime - entry.getValue().lastAccessTime > CACHE_EXPIRE_TIME); +// DRONES_CACHE.entrySet().removeIf(entry -> +// currentTime - entry.getValue().lastAccessTime > CACHE_EXPIRE_TIME); +// } } diff --git a/src/main/resources/data/superbwarfare/guns/m_2_hb.json b/src/main/resources/data/superbwarfare/guns/m_2_hb.json index cdc9a39c2..f33a4c55c 100644 --- a/src/main/resources/data/superbwarfare/guns/m_2_hb.json +++ b/src/main/resources/data/superbwarfare/guns/m_2_hb.json @@ -26,7 +26,6 @@ "AvailablePerks": [ "@Ammo", "superbwarfare:fourth_times_charm", - "superbwarfare:turbo_charger", "superbwarfare:subsistence", "superbwarfare:powerful_attraction", "superbwarfare:intelligent_chip",