package net.fretux.knockedback; import net.minecraft.network.FriendlyByteBuf; import net.minecraftforge.network.NetworkEvent; import javax.annotation.Nullable; import java.util.UUID; import java.util.function.Supplier; public class ExecutionProgressPacket { public enum Action { NONE, EXECUTION, HELP_UP } private final int timeLeft; private final int totalTicks; private final Action action; private final String targetName; @Nullable private final UUID executorId; public ExecutionProgressPacket(int timeLeft, @Nullable UUID executorId) { this(timeLeft, PlayerExecutionHandler.getExecutionTime(), timeLeft > 0 ? Action.EXECUTION : Action.NONE, "", executorId); } public ExecutionProgressPacket(int timeLeft, int totalTicks, Action action, @Nullable UUID executorId) { this(timeLeft, totalTicks, action, "", executorId); } public ExecutionProgressPacket(int timeLeft, int totalTicks, Action action, String targetName, @Nullable UUID executorId) { this.timeLeft = timeLeft; this.totalTicks = totalTicks; this.action = timeLeft > 0 ? action : Action.NONE; this.targetName = timeLeft > 0 ? targetName : ""; this.executorId = executorId; } public static void encode(ExecutionProgressPacket msg, FriendlyByteBuf buf) { buf.writeInt(msg.timeLeft); buf.writeInt(msg.totalTicks); buf.writeEnum(msg.action); buf.writeUtf(msg.targetName); buf.writeBoolean(msg.executorId != null); if (msg.executorId != null) { buf.writeUUID(msg.executorId); } } public static ExecutionProgressPacket decode(FriendlyByteBuf buf) { int timeLeft = buf.readInt(); int totalTicks = buf.readInt(); Action action = buf.readEnum(Action.class); String targetName = buf.readUtf(); UUID executorId = buf.readBoolean() ? buf.readUUID() : null; return new ExecutionProgressPacket(timeLeft, totalTicks, action, targetName, executorId); } public static void handle(ExecutionProgressPacket msg, Supplier ctx) { ctx.get().enqueueWork(() -> { net.fretux.knockedback.client.ClientExecutionState.setProgress(msg.timeLeft, msg.totalTicks, msg.action, msg.targetName, msg.executorId); }); ctx.get().setPacketHandled(true); } }