Added Execution Indicator

This commit is contained in:
IM23a-spirgif
2025-04-24 20:07:08 +02:00
parent 53eb3f3895
commit dd5a7c813b
5 changed files with 92 additions and 26 deletions
@@ -0,0 +1,29 @@
package net.fretux.knockedback;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;
import java.util.function.Supplier;
public class ExecutionProgressPacket {
private final int timeLeft;
public ExecutionProgressPacket(int timeLeft) {
this.timeLeft = timeLeft;
}
public static void encode(ExecutionProgressPacket msg, FriendlyByteBuf buf) {
buf.writeInt(msg.timeLeft);
}
public static ExecutionProgressPacket decode(FriendlyByteBuf buf) {
return new ExecutionProgressPacket(buf.readInt());
}
public static void handle(ExecutionProgressPacket msg, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
net.fretux.knockedback.client.ClientExecutionState.setTimeLeft(msg.timeLeft);
});
ctx.get().setPacketHandled(true);
}
}
@@ -9,23 +9,11 @@ public class NetworkHandler {
public static SimpleChannel CHANNEL;
public static void register() {
CHANNEL = NetworkRegistry.newSimpleChannel(
new ResourceLocation(KnockedBack.MOD_ID, "main"),
() -> PROTOCOL_VERSION,
PROTOCOL_VERSION::equals,
PROTOCOL_VERSION::equals
);
CHANNEL = NetworkRegistry.newSimpleChannel(new ResourceLocation(KnockedBack.MOD_ID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals);
int id = 0;
CHANNEL.registerMessage(id++,
ExecuteKnockedPacket.class,
ExecuteKnockedPacket::encode,
ExecuteKnockedPacket::decode,
ExecuteKnockedPacket::handle);
CHANNEL.registerMessage(id++,
KnockedTimePacket.class,
KnockedTimePacket::encode,
KnockedTimePacket::decode,
KnockedTimePacket::handle);
CHANNEL.registerMessage(id++, ExecuteKnockedPacket.class, ExecuteKnockedPacket::encode, ExecuteKnockedPacket::decode, ExecuteKnockedPacket::handle);
CHANNEL.registerMessage(id++, KnockedTimePacket.class, KnockedTimePacket::encode, KnockedTimePacket::decode, KnockedTimePacket::handle);
CHANNEL.registerMessage(id++, ExecutionProgressPacket.class, ExecutionProgressPacket::encode, ExecutionProgressPacket::decode, ExecutionProgressPacket::handle);
}
}
@@ -12,6 +12,8 @@ import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.network.PacketDistributor;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@@ -21,31 +23,27 @@ import static net.fretux.knockedback.NetworkHandlerHelper.getPlayerByUuid;
@Mod.EventBusSubscriber(modid = "knockedback", bus = Mod.EventBusSubscriber.Bus.FORGE)
public class PlayerExecutionHandler {
public static final double EXECUTION_RANGE = 2.0;
private static final int EXECUTION_DELAY_TICKS = 60;
public static final int EXECUTION_DELAY_TICKS = 60;
public static boolean isBeingPlayerExecuted(UUID knockedId) {
return executionAttempts.containsKey(knockedId);
}
public static boolean isExecuting(UUID playerUuid) {
return executionAttempts.values()
.stream()
.anyMatch(attempt -> attempt.executorUuid.equals(playerUuid));
return executionAttempts.values().stream().anyMatch(attempt -> attempt.executorUuid.equals(playerUuid));
}
@SubscribeEvent
public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
if (event.side.isServer()
&& event.phase == TickEvent.Phase.END
&& event.player instanceof ServerPlayer sp
&& isExecuting(sp.getUUID())) {
if (event.side.isServer() && event.phase == TickEvent.Phase.END && event.player instanceof ServerPlayer sp && isExecuting(sp.getUUID())) {
sp.setDeltaMovement(0, 0, 0);
sp.setSprinting(false);
}
}
@SubscribeEvent
public static void onAttackEntity(AttackEntityEvent event) {
if (event.getEntity() instanceof ServerPlayer sp
&& isExecuting(sp.getUUID())) {
if (event.getEntity() instanceof ServerPlayer sp && isExecuting(sp.getUUID())) {
event.setCanceled(true);
}
}
@@ -142,6 +140,8 @@ public class PlayerExecutionHandler {
attempt.timeLeft--;
NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> executor), new ExecutionProgressPacket(attempt.timeLeft));
if (attempt.timeLeft <= 0) {
executeKnockedPlayer(executor, knocked);
} else {
@@ -0,0 +1,17 @@
package net.fretux.knockedback.client;
public class ClientExecutionState {
private static int timeLeft = 0;
public static void setTimeLeft(int ticks) {
timeLeft = ticks;
}
public static int getTimeLeft() {
return timeLeft;
}
public static boolean isExecuting() {
return timeLeft > 0;
}
}
@@ -0,0 +1,32 @@
package net.fretux.knockedback.client;
import com.mojang.blaze3d.systems.RenderSystem;
import net.fretux.knockedback.KnockedBack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderGuiOverlayEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@Mod.EventBusSubscriber(modid = KnockedBack.MOD_ID, value = Dist.CLIENT)
public class ExecutionProgressOverlay {
private static final int BAR_WIDTH = 100;
private static final int BAR_HEIGHT = 8;
private static final int TOTAL_TICKS = net.fretux.knockedback.PlayerExecutionHandler.EXECUTION_DELAY_TICKS;
@SubscribeEvent
public static void onRender(RenderGuiOverlayEvent.Post event) {
if (!ClientExecutionState.isExecuting()) return;
Minecraft mc = Minecraft.getInstance();
GuiGraphics gui = event.getGuiGraphics();
int sw = mc.getWindow().getGuiScaledWidth();
int sh = mc.getWindow().getGuiScaledHeight();
int x = (sw - BAR_WIDTH) / 2;
int y = (sh - BAR_HEIGHT) / 2;
float progress = 1.0f - (ClientExecutionState.getTimeLeft() / (float) TOTAL_TICKS);
int fillWidth = (int) (BAR_WIDTH * progress);
gui.fill(x, y, x + BAR_WIDTH, y + BAR_HEIGHT, 0x80000000);
gui.fill(x, y, x + fillWidth, y + BAR_HEIGHT, 0x80FFFFFF);
}
}