Small Changes
This commit is contained in:
@@ -3,6 +3,7 @@ package net.fretux.knockedback;
|
|||||||
import net.minecraft.network.FriendlyByteBuf;
|
import net.minecraft.network.FriendlyByteBuf;
|
||||||
import net.minecraft.server.level.ServerPlayer;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.minecraft.world.entity.LivingEntity;
|
import net.minecraft.world.entity.LivingEntity;
|
||||||
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraftforge.network.NetworkEvent;
|
import net.minecraftforge.network.NetworkEvent;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
@@ -23,7 +24,9 @@ public class ExecuteKnockedPacket {
|
|||||||
if (executor == null) return;
|
if (executor == null) return;
|
||||||
LivingEntity target = findNearestKnockedEntity(executor);
|
LivingEntity target = findNearestKnockedEntity(executor);
|
||||||
if (target != null) {
|
if (target != null) {
|
||||||
PlayerExecutionHandler.executeKnockedPlayer(executor, target);
|
if (target instanceof Player targetPlayer) {
|
||||||
|
PlayerExecutionHandler.startExecution(executor, targetPlayer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ctx.get().setPacketHandled(true);
|
ctx.get().setPacketHandled(true);
|
||||||
|
|||||||
@@ -1,23 +1,83 @@
|
|||||||
package net.fretux.knockedback;
|
package net.fretux.knockedback;
|
||||||
|
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.server.level.ServerPlayer;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.minecraft.world.entity.LivingEntity;
|
import net.minecraft.world.entity.LivingEntity;
|
||||||
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraftforge.common.MinecraftForge;
|
import net.minecraftforge.common.MinecraftForge;
|
||||||
|
import net.minecraftforge.event.TickEvent;
|
||||||
|
import net.minecraftforge.event.entity.living.LivingHurtEvent;
|
||||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@Mod.EventBusSubscriber(modid = "knockedback", bus = Mod.EventBusSubscriber.Bus.MOD)
|
import static net.fretux.knockedback.NetworkHandlerHelper.getPlayerByUuid;
|
||||||
|
|
||||||
|
@Mod.EventBusSubscriber(modid = "knockedback", bus = Mod.EventBusSubscriber.Bus.FORGE)
|
||||||
public class PlayerExecutionHandler {
|
public class PlayerExecutionHandler {
|
||||||
public static final double EXECUTION_RANGE = 2.0;
|
public static final double EXECUTION_RANGE = 2.0;
|
||||||
|
private static final int EXECUTION_DELAY_TICKS = 60;
|
||||||
|
|
||||||
|
private static final Map<UUID, PlayerExecutionAttempt> executionAttempts = new HashMap<>();
|
||||||
|
|
||||||
|
private static class PlayerExecutionAttempt {
|
||||||
|
private final UUID executorUuid;
|
||||||
|
private int timeLeft;
|
||||||
|
|
||||||
|
public PlayerExecutionAttempt(UUID executorUuid) {
|
||||||
|
this.executorUuid = executorUuid;
|
||||||
|
this.timeLeft = EXECUTION_DELAY_TICKS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void register() {
|
public static void register() {
|
||||||
MinecraftForge.EVENT_BUS.register(new PlayerExecutionHandler());
|
MinecraftForge.EVENT_BUS.register(new PlayerExecutionHandler());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void startExecution(ServerPlayer executor, Player target) {
|
||||||
|
UUID knockedId = target.getUUID();
|
||||||
|
if (!KnockedManager.isKnocked(target)) return;
|
||||||
|
if (executionAttempts.containsKey(knockedId)) return;
|
||||||
|
|
||||||
|
executionAttempts.put(knockedId, new PlayerExecutionAttempt(executor.getUUID()));
|
||||||
|
executor.sendSystemMessage(Component.literal("Execution started..."));
|
||||||
|
target.sendSystemMessage(Component.literal("You're being executed!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void cancelExecution(UUID knockedId) {
|
||||||
|
PlayerExecutionAttempt attempt = executionAttempts.remove(knockedId);
|
||||||
|
if (attempt != null) {
|
||||||
|
ServerPlayer knocked = getPlayerByUuid(knockedId);
|
||||||
|
if (knocked != null) {
|
||||||
|
knocked.sendSystemMessage(Component.literal("Execution interrupted!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
ServerPlayer executor = getPlayerByUuid(attempt.executorUuid);
|
||||||
|
if (executor != null) {
|
||||||
|
executor.sendSystemMessage(Component.literal("Your execution was interrupted!"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void onCommonSetup(FMLCommonSetupEvent event) {
|
public static void onLivingHurt(LivingHurtEvent event) {
|
||||||
register();
|
if (!(event.getEntity() instanceof ServerPlayer hurtPlayer)) return;
|
||||||
|
|
||||||
|
UUID hurtId = hurtPlayer.getUUID();
|
||||||
|
boolean wasExecutor = executionAttempts.values().removeIf(attempt -> attempt.executorUuid.equals(hurtId));
|
||||||
|
if (wasExecutor) {
|
||||||
|
hurtPlayer.sendSystemMessage(Component.literal("Execution canceled because you were hit!"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ServerPlayer getPlayerByUuid(UUID uuid) {
|
||||||
|
MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer();
|
||||||
|
return server.getPlayerList().getPlayer(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void executeKnockedPlayer(ServerPlayer executor, LivingEntity knockedPlayer) {
|
public static void executeKnockedPlayer(ServerPlayer executor, LivingEntity knockedPlayer) {
|
||||||
@@ -28,4 +88,40 @@ public class PlayerExecutionHandler {
|
|||||||
serverPlayer.sendSystemMessage(Component.literal("You were executed by " + executor.getName().getString() + "!"));
|
serverPlayer.sendSystemMessage(Component.literal("You were executed by " + executor.getName().getString() + "!"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void onServerTick(TickEvent.ServerTickEvent event) {
|
||||||
|
if (event.phase != TickEvent.Phase.END || !event.side.isServer()) return;
|
||||||
|
|
||||||
|
Map<UUID, PlayerExecutionAttempt> updated = new HashMap<>();
|
||||||
|
|
||||||
|
for (Map.Entry<UUID, PlayerExecutionAttempt> entry : executionAttempts.entrySet()) {
|
||||||
|
UUID knockedId = entry.getKey();
|
||||||
|
PlayerExecutionAttempt attempt = entry.getValue();
|
||||||
|
|
||||||
|
ServerPlayer knocked = getPlayerByUuid(knockedId);
|
||||||
|
ServerPlayer executor = getPlayerByUuid(attempt.executorUuid);
|
||||||
|
|
||||||
|
if (knocked == null || executor == null || !KnockedManager.isKnocked(knocked)) {
|
||||||
|
continue; // Invalid players or no longer knocked
|
||||||
|
}
|
||||||
|
|
||||||
|
// If out of range, cancel the execution
|
||||||
|
if (executor.distanceTo(knocked) > EXECUTION_RANGE) {
|
||||||
|
cancelExecution(knockedId);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
attempt.timeLeft--;
|
||||||
|
|
||||||
|
if (attempt.timeLeft <= 0) {
|
||||||
|
executeKnockedPlayer(executor, knocked);
|
||||||
|
} else {
|
||||||
|
updated.put(knockedId, attempt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
executionAttempts.clear();
|
||||||
|
executionAttempts.putAll(updated);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user