Initial Commit
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
package net.fretux.knockedback;
|
package net.fretux.knockedback;
|
||||||
|
|
||||||
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.minecraft.world.damagesource.DamageSource;
|
import net.minecraft.world.damagesource.DamageSource;
|
||||||
import net.minecraft.world.entity.LivingEntity;
|
import net.minecraft.world.entity.LivingEntity;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraftforge.event.entity.living.LivingHurtEvent;
|
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.network.PacketDistributor;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -54,18 +56,27 @@ public class KnockedManager {
|
|||||||
Map.Entry<UUID, Integer> entry = it.next();
|
Map.Entry<UUID, Integer> entry = it.next();
|
||||||
UUID entityId = entry.getKey();
|
UUID entityId = entry.getKey();
|
||||||
int timeLeft = entry.getValue();
|
int timeLeft = entry.getValue();
|
||||||
if (grippedEntities.contains(entityId)) {
|
// If the player is not gripped, update the countdown
|
||||||
continue;
|
if (!grippedEntities.contains(entityId)) {
|
||||||
|
timeLeft--;
|
||||||
|
if (timeLeft <= 0) {
|
||||||
|
it.remove();
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
entry.setValue(timeLeft);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
int newTime = timeLeft - 1;
|
// Send the updated time to the knocked player
|
||||||
if (newTime <= 0) {
|
ServerPlayer knockedPlayer = NetworkHandlerHelper.getPlayerByUuid(
|
||||||
it.remove();
|
net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(), entityId);
|
||||||
} else {
|
if (knockedPlayer != null) {
|
||||||
entry.setValue(newTime);
|
NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> knockedPlayer),
|
||||||
|
new net.fretux.knockedback.KnockedTimePacket(timeLeft));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void onLivingHurt(LivingHurtEvent event) {
|
public static void onLivingHurt(LivingHurtEvent event) {
|
||||||
LivingEntity entity = event.getEntity();
|
LivingEntity entity = event.getEntity();
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package net.fretux.knockedback;
|
||||||
|
|
||||||
|
import net.fretux.knockedback.client.ClientKnockedState;
|
||||||
|
import net.minecraft.network.FriendlyByteBuf;
|
||||||
|
import net.minecraftforge.network.NetworkEvent;
|
||||||
|
import net.minecraftforge.network.PacketDistributor;
|
||||||
|
import net.fretux.knockedback.KnockedBack;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class KnockedTimePacket {
|
||||||
|
private final int timeLeft;
|
||||||
|
|
||||||
|
public KnockedTimePacket(int timeLeft) {
|
||||||
|
this.timeLeft = timeLeft;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void encode(KnockedTimePacket packet, FriendlyByteBuf buf) {
|
||||||
|
buf.writeInt(packet.timeLeft);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static KnockedTimePacket decode(FriendlyByteBuf buf) {
|
||||||
|
return new KnockedTimePacket(buf.readInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void handle(KnockedTimePacket packet, Supplier<NetworkEvent.Context> ctx) {
|
||||||
|
ctx.get().enqueueWork(() -> {
|
||||||
|
// Client side: update the client state with the time left.
|
||||||
|
ClientKnockedState.setTimeLeft(packet.timeLeft);
|
||||||
|
});
|
||||||
|
ctx.get().setPacketHandled(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package net.fretux.knockedback.client;
|
||||||
|
|
||||||
|
public class ClientKnockedState {
|
||||||
|
private static int timeLeft = 0;
|
||||||
|
|
||||||
|
public static void setTimeLeft(int ticks) {
|
||||||
|
timeLeft = ticks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getTimeLeft() {
|
||||||
|
return timeLeft;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isKnocked() {
|
||||||
|
return timeLeft > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package net.fretux.knockedback.client;
|
||||||
|
|
||||||
|
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 KnockedTimeOverlay {
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void onRenderOverlay(RenderGuiOverlayEvent.Post event) {
|
||||||
|
if (!ClientKnockedState.isKnocked()) return;
|
||||||
|
|
||||||
|
Minecraft mc = Minecraft.getInstance();
|
||||||
|
GuiGraphics guiGraphics = event.getGuiGraphics();
|
||||||
|
String text = "Time until recovery: " + (ClientKnockedState.getTimeLeft() / 20) + " s";
|
||||||
|
|
||||||
|
int screenWidth = mc.getWindow().getGuiScaledWidth();
|
||||||
|
int x = screenWidth / 2 - mc.font.width(text) / 2;
|
||||||
|
int y = 10; // 10 pixels from the top
|
||||||
|
|
||||||
|
// Draw the text with a drop shadow (true = drop shadow)
|
||||||
|
guiGraphics.drawString(mc.font, text, x, y, 0xFFFFFF, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user