diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-03-18 14:24:35 +0100 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-03-18 14:24:35 +0100 |
| commit | b867fa2efb789818dbc2b41e7483ecd5ba0b6279 (patch) | |
| tree | 24b9f529ab6a0703d7e2530048c7b9a8ed9eccd6 /src/main/java | |
| parent | fd2b7214837996a09ff8a9ab13c7ed806d99b803 (diff) | |
Diffstat (limited to 'src/main/java')
6 files changed, 347 insertions, 345 deletions
diff --git a/src/main/java/world/anhgelus/manhunt/Config.java b/src/main/java/world/anhgelus/manhunt/Config.java index d669de2..f441a58 100644 --- a/src/main/java/world/anhgelus/manhunt/Config.java +++ b/src/main/java/world/anhgelus/manhunt/Config.java @@ -3,6 +3,8 @@ package world.anhgelus.manhunt; import eu.midnightdust.lib.config.MidnightConfig;
public class Config extends MidnightConfig {
- @Entry(category = "timings") public static int secondsBeforeRelease = 30;
- @Entry(category = "timings") public static int updateCompassEach = 15;
+ @Entry(category = "timings")
+ public static int secondsBeforeRelease = 30;
+ @Entry(category = "timings")
+ public static int updateCompassEach = 15;
}
diff --git a/src/main/java/world/anhgelus/manhunt/Manhunt.java b/src/main/java/world/anhgelus/manhunt/Manhunt.java index 66b8fb4..4a3ba4e 100644 --- a/src/main/java/world/anhgelus/manhunt/Manhunt.java +++ b/src/main/java/world/anhgelus/manhunt/Manhunt.java @@ -41,221 +41,221 @@ import java.util.*; import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
public class Manhunt implements ModInitializer {
- public static final String MOD_ID = "manhunt";
- public static final Logger LOGGER = LogManager.getLogger(MOD_ID);
+ public static final String MOD_ID = "manhunt";
+ public static final Logger LOGGER = LogManager.getLogger(MOD_ID);
- private final Set<UUID> hunters = new HashSet<>();
- private final Set<UUID> speedrunners = new HashSet<>();
- private final Map<UUID, UUID> trackedMap = new HashMap<>();
+ private final Set<UUID> hunters = new HashSet<>();
+ private final Set<UUID> speedrunners = new HashSet<>();
+ private final Map<UUID, UUID> trackedMap = new HashMap<>();
- private State state = State.OFF;
+ private State state = State.OFF;
- @Override
- public void onInitialize() {
- LOGGER.info("Initializing Manhunt");
- MidnightConfig.init(MOD_ID, Config.class);
+ @Override
+ public void onInitialize() {
+ LOGGER.info("Initializing Manhunt");
+ MidnightConfig.init(MOD_ID, Config.class);
- final LiteralArgumentBuilder<ServerCommandSource> command = literal("manhunt");
+ final LiteralArgumentBuilder<ServerCommandSource> command = literal("manhunt");
- final LiteralArgumentBuilder<ServerCommandSource> track = literal("track");
- track.then(RequiredArgumentBuilder.<ServerCommandSource, EntitySelector>argument("player", EntityArgumentType.player())
- .executes(context -> {
- final var source = context.getSource();
- final var tracked = (ServerPlayerEntity) EntityArgumentType.getEntity(context, "player");
- assert tracked.getDisplayName() != null;
- final var player = source.getPlayer();
- if (player == null) return 2;
- final var uuid = player.getUuid();
- if (trackedMap.get(uuid) != null && trackedMap.get(uuid) == tracked.getUuid()) {
- source.sendFeedback(() -> Text.literal("Already tracking " + tracked.getDisplayName().getString()), false);
- return Command.SINGLE_SUCCESS;
- }
- trackedMap.put(player.getUuid(), tracked.getUuid());
- updateCompass(player, tracked);
- source.sendFeedback(() -> Text.literal("Tracking " + tracked.getDisplayName().getString()), false);
- return Command.SINGLE_SUCCESS;
- })
- );
- final LiteralArgumentBuilder<ServerCommandSource> team = literal("team");
- final RequiredArgumentBuilder<ServerCommandSource, EntitySelector> teamP = RequiredArgumentBuilder.argument("player", EntityArgumentType.player());
- teamP.then(LiteralArgumentBuilder.<ServerCommandSource>literal("hunter").executes(context -> {
- final var p = (ServerPlayerEntity) EntityArgumentType.getEntity(context, "player");
- speedrunners.remove(p.getUuid());
- hunters.add(p.getUuid());
- assert p.getDisplayName() != null;
- context.getSource().sendFeedback(() -> Text.literal(p.getDisplayName().getString() + " added to hunter"), true);
- return Command.SINGLE_SUCCESS;
- }))
- .then(LiteralArgumentBuilder.<ServerCommandSource>literal("speedrunner").executes(context -> {
- final var p = (ServerPlayerEntity) EntityArgumentType.getEntity(context, "player");
- hunters.remove(p.getUuid());
- speedrunners.add(p.getUuid());
- assert p.getDisplayName() != null;
- context.getSource().sendFeedback(() -> Text.literal(p.getDisplayName().getString() + " added to speedrunner"), true);
- return Command.SINGLE_SUCCESS;
- }));
- team.then(teamP);
- final LiteralArgumentBuilder<ServerCommandSource> start = literal("start");
- start.requires(CommandManager.requirePermissionLevel(CommandManager.GAMEMASTERS_CHECK));
- start.executes(context -> {
- final var source = context.getSource();
- if (state == State.ON) {
- source.sendFeedback(() -> Text.literal("Cannot start a manhunt if one is already started!"), false);
- return Command.SINGLE_SUCCESS;
- }
- final var server = source.getServer();
- final var timer = TimerAccess.getTimerFromOverworld(server);
- final PlayerManager pm = server.getPlayerManager();
- for (final ServerPlayerEntity player : pm.getPlayerList()) {
- player.setHealth(player.getMaxHealth());
- player.setExperienceLevel(0);
- player.getInventory().clear();
- player.getHungerManager().eat(
- new FoodComponent(20, 20.0f, true)
- );
- final var spawn = player.getEntityWorld().getSpawnPoint().getPos();
- player.teleport(spawn.getX(), spawn.getY(), spawn.getZ(), false);
- }
- state = State.ON;
- for (final UUID uuid : hunters) {
- final var hunter = pm.getPlayer(uuid);
- assert hunter != null;
- final var isACompass = new ItemStack(Items.COMPASS);
- hunter.giveItemStack(isACompass);
- hunter.addStatusEffect(new StatusEffectInstance(StatusEffects.MINING_FATIGUE, Config.secondsBeforeRelease * 20, 255, false, false));
- var attr = hunter.getAttributeInstance(EntityAttributes.MOVEMENT_SPEED);
- if (attr != null) {
- final var modifier = new EntityAttributeModifier(
- Identifier.of("manhunt.speed"),
- -1,
- EntityAttributeModifier.Operation.ADD_MULTIPLIED_BASE
- );
- attr.addTemporaryModifier(modifier);
- }
- attr = hunter.getAttributeInstance(EntityAttributes.GRAVITY);
- if (attr != null) {
- final var modifier = new EntityAttributeModifier(
- Identifier.of("manhunt.gravity"),
- 5,
- EntityAttributeModifier.Operation.ADD_VALUE
- );
- attr.addTemporaryModifier(modifier);
- }
- LOGGER.info("Added modifiers to {}", hunter.getDisplayName());
- }
- timer.dds_runTask(new TickTask(() -> {
- LOGGER.info("Removing modifier to hunters");
- for (final UUID uuid : hunters) {
- final var hunter = pm.getPlayer(uuid);
- assert hunter != null;
- var attr = hunter.getAttributeInstance(EntityAttributes.MOVEMENT_SPEED);
- if (attr != null) {
- attr.removeModifier(Identifier.of("manhunt.speed"));
- }
- attr = hunter.getAttributeInstance(EntityAttributes.GRAVITY);
- if (attr != null) {
- attr.removeModifier(Identifier.of("manhunt.gravity"));
- }
- }
- pm.broadcast(Text.of("Hunters released!"), false);
- }, Config.secondsBeforeRelease * 20L));
- setTimer(pm);
- source.sendFeedback(() -> Text.literal("Game started!"), true);
- return Command.SINGLE_SUCCESS;
- });
+ final LiteralArgumentBuilder<ServerCommandSource> track = literal("track");
+ track.then(RequiredArgumentBuilder.<ServerCommandSource, EntitySelector>argument("player", EntityArgumentType.player())
+ .executes(context -> {
+ final var source = context.getSource();
+ final var tracked = (ServerPlayerEntity) EntityArgumentType.getEntity(context, "player");
+ assert tracked.getDisplayName() != null;
+ final var player = source.getPlayer();
+ if (player == null) return 2;
+ final var uuid = player.getUuid();
+ if (trackedMap.get(uuid) != null && trackedMap.get(uuid) == tracked.getUuid()) {
+ source.sendFeedback(() -> Text.literal("Already tracking " + tracked.getDisplayName().getString()), false);
+ return Command.SINGLE_SUCCESS;
+ }
+ trackedMap.put(player.getUuid(), tracked.getUuid());
+ updateCompass(player, tracked);
+ source.sendFeedback(() -> Text.literal("Tracking " + tracked.getDisplayName().getString()), false);
+ return Command.SINGLE_SUCCESS;
+ })
+ );
+ final LiteralArgumentBuilder<ServerCommandSource> team = literal("team");
+ final RequiredArgumentBuilder<ServerCommandSource, EntitySelector> teamP = RequiredArgumentBuilder.argument("player", EntityArgumentType.player());
+ teamP.then(LiteralArgumentBuilder.<ServerCommandSource>literal("hunter").executes(context -> {
+ final var p = (ServerPlayerEntity) EntityArgumentType.getEntity(context, "player");
+ speedrunners.remove(p.getUuid());
+ hunters.add(p.getUuid());
+ assert p.getDisplayName() != null;
+ context.getSource().sendFeedback(() -> Text.literal(p.getDisplayName().getString() + " added to hunter"), true);
+ return Command.SINGLE_SUCCESS;
+ }))
+ .then(LiteralArgumentBuilder.<ServerCommandSource>literal("speedrunner").executes(context -> {
+ final var p = (ServerPlayerEntity) EntityArgumentType.getEntity(context, "player");
+ hunters.remove(p.getUuid());
+ speedrunners.add(p.getUuid());
+ assert p.getDisplayName() != null;
+ context.getSource().sendFeedback(() -> Text.literal(p.getDisplayName().getString() + " added to speedrunner"), true);
+ return Command.SINGLE_SUCCESS;
+ }));
+ team.then(teamP);
+ final LiteralArgumentBuilder<ServerCommandSource> start = literal("start");
+ start.requires(CommandManager.requirePermissionLevel(CommandManager.GAMEMASTERS_CHECK));
+ start.executes(context -> {
+ final var source = context.getSource();
+ if (state == State.ON) {
+ source.sendFeedback(() -> Text.literal("Cannot start a manhunt if one is already started!"), false);
+ return Command.SINGLE_SUCCESS;
+ }
+ final var server = source.getServer();
+ final var timer = TimerAccess.getTimerFromOverworld(server);
+ final PlayerManager pm = server.getPlayerManager();
+ for (final ServerPlayerEntity player : pm.getPlayerList()) {
+ player.setHealth(player.getMaxHealth());
+ player.setExperienceLevel(0);
+ player.getInventory().clear();
+ player.getHungerManager().eat(
+ new FoodComponent(20, 20.0f, true)
+ );
+ final var spawn = player.getEntityWorld().getSpawnPoint().getPos();
+ player.teleport(spawn.getX(), spawn.getY(), spawn.getZ(), false);
+ }
+ state = State.ON;
+ for (final UUID uuid : hunters) {
+ final var hunter = pm.getPlayer(uuid);
+ assert hunter != null;
+ final var isACompass = new ItemStack(Items.COMPASS);
+ hunter.giveItemStack(isACompass);
+ hunter.addStatusEffect(new StatusEffectInstance(StatusEffects.MINING_FATIGUE, Config.secondsBeforeRelease * 20, 255, false, false));
+ var attr = hunter.getAttributeInstance(EntityAttributes.MOVEMENT_SPEED);
+ if (attr != null) {
+ final var modifier = new EntityAttributeModifier(
+ Identifier.of("manhunt.speed"),
+ -1,
+ EntityAttributeModifier.Operation.ADD_MULTIPLIED_BASE
+ );
+ attr.addTemporaryModifier(modifier);
+ }
+ attr = hunter.getAttributeInstance(EntityAttributes.GRAVITY);
+ if (attr != null) {
+ final var modifier = new EntityAttributeModifier(
+ Identifier.of("manhunt.gravity"),
+ 5,
+ EntityAttributeModifier.Operation.ADD_VALUE
+ );
+ attr.addTemporaryModifier(modifier);
+ }
+ LOGGER.info("Added modifiers to {}", hunter.getDisplayName());
+ }
+ timer.dds_runTask(new TickTask(() -> {
+ LOGGER.info("Removing modifier to hunters");
+ for (final UUID uuid : hunters) {
+ final var hunter = pm.getPlayer(uuid);
+ assert hunter != null;
+ var attr = hunter.getAttributeInstance(EntityAttributes.MOVEMENT_SPEED);
+ if (attr != null) {
+ attr.removeModifier(Identifier.of("manhunt.speed"));
+ }
+ attr = hunter.getAttributeInstance(EntityAttributes.GRAVITY);
+ if (attr != null) {
+ attr.removeModifier(Identifier.of("manhunt.gravity"));
+ }
+ }
+ pm.broadcast(Text.of("Hunters released!"), false);
+ }, Config.secondsBeforeRelease * 20L));
+ setTimer(pm);
+ source.sendFeedback(() -> Text.literal("Game started!"), true);
+ return Command.SINGLE_SUCCESS;
+ });
- final LiteralArgumentBuilder<ServerCommandSource> resetTimer = literal("reset-timer");
- resetTimer.requires(CommandManager.requirePermissionLevel(CommandManager.GAMEMASTERS_CHECK));
- resetTimer.executes(context -> {
- state = State.ON;
- setTimer(context.getSource().getServer().getPlayerManager());
- context.getSource().sendFeedback(() -> Text.literal("Timer reset"), true);
- return Command.SINGLE_SUCCESS;
- });
+ final LiteralArgumentBuilder<ServerCommandSource> resetTimer = literal("reset-timer");
+ resetTimer.requires(CommandManager.requirePermissionLevel(CommandManager.GAMEMASTERS_CHECK));
+ resetTimer.executes(context -> {
+ state = State.ON;
+ setTimer(context.getSource().getServer().getPlayerManager());
+ context.getSource().sendFeedback(() -> Text.literal("Timer reset"), true);
+ return Command.SINGLE_SUCCESS;
+ });
- command.then(track);
- command.then(team);
- command.then(start);
- command.then(resetTimer);
+ command.then(track);
+ command.then(team);
+ command.then(start);
+ command.then(resetTimer);
- CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(command));
+ CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(command));
- ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> {
- if (state == State.OFF) return;
- final var uuid = oldPlayer.getUuid();
- if (hunters.contains(uuid)) {
- newPlayer.giveItemStack(new ItemStack(Items.COMPASS));
- return;
- }
- speedrunners.remove(uuid);
- newPlayer.changeGameMode(GameMode.SPECTATOR);
- if (!speedrunners.isEmpty()) return;
- final var server = newPlayer.getEntityWorld().getServer();
- assert server != null;
- for (final ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
- player.changeGameMode(GameMode.SPECTATOR);
- hunters.remove(player.getUuid());
- speedrunners.remove(player.getUuid());
- }
- state = State.OFF;
- TimerAccess.getTimerFromOverworld(server).dds_cancel();
- });
+ ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> {
+ if (state == State.OFF) return;
+ final var uuid = oldPlayer.getUuid();
+ if (hunters.contains(uuid)) {
+ newPlayer.giveItemStack(new ItemStack(Items.COMPASS));
+ return;
+ }
+ speedrunners.remove(uuid);
+ newPlayer.changeGameMode(GameMode.SPECTATOR);
+ if (!speedrunners.isEmpty()) return;
+ final var server = newPlayer.getEntityWorld().getServer();
+ assert server != null;
+ for (final ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
+ player.changeGameMode(GameMode.SPECTATOR);
+ hunters.remove(player.getUuid());
+ speedrunners.remove(player.getUuid());
+ }
+ state = State.OFF;
+ TimerAccess.getTimerFromOverworld(server).dds_cancel();
+ });
- ServerEntityEvents.ENTITY_LOAD.register((entity, world) -> {
- if (!(entity instanceof PiglinBruteEntity)) return;
- EntityType.PIGLIN.spawn(world, entity.getBlockPos(), SpawnReason.MOB_SUMMONED);
- entity.discard();
- });
- }
+ ServerEntityEvents.ENTITY_LOAD.register((entity, world) -> {
+ if (!(entity instanceof PiglinBruteEntity)) return;
+ EntityType.PIGLIN.spawn(world, entity.getBlockPos(), SpawnReason.MOB_SUMMONED);
+ entity.discard();
+ });
+ }
- private void setTimer(PlayerManager pm) {
- TimerAccess.getTimerFromOverworld(pm.getServer()).dds_runTask(new TickTask(() -> {
- for (final UUID uuid : hunters) {
- final ServerPlayerEntity hunter = pm.getPlayer(uuid);
- if (hunter == null) continue;
- final ServerPlayerEntity tracked = pm.getPlayer(trackedMap.get(uuid));
- if (tracked == null) continue;
- updateCompass(hunter, tracked);
- }
- }, Config.secondsBeforeRelease * 20L, Config.updateCompassEach * 20L));
- }
+ private void setTimer(PlayerManager pm) {
+ TimerAccess.getTimerFromOverworld(pm.getServer()).dds_runTask(new TickTask(() -> {
+ for (final UUID uuid : hunters) {
+ final ServerPlayerEntity hunter = pm.getPlayer(uuid);
+ if (hunter == null) continue;
+ final ServerPlayerEntity tracked = pm.getPlayer(trackedMap.get(uuid));
+ if (tracked == null) continue;
+ updateCompass(hunter, tracked);
+ }
+ }, Config.secondsBeforeRelease * 20L, Config.updateCompassEach * 20L));
+ }
- private void updateCompass(ServerPlayerEntity player, ServerPlayerEntity tracked) {
- final var trackerCpnt = new LodestoneTrackerComponent(Optional.of(GlobalPos.create(tracked.getEntityWorld().getRegistryKey(), tracked.getBlockPos())), true);
- LOGGER.info(tracked.getEntityWorld().getRegistryKey().toString());
- LOGGER.info(tracked.getBlockPos().toString());
- LOGGER.info(trackerCpnt.toString());
- ItemStack is = null;
- int slot = PlayerInventory.NOT_FOUND;
- final var inv = player.getInventory();
- if (inv.getSelectedStack().isOf(Items.COMPASS)) {
- is = inv.getSelectedStack();
- slot = inv.getSlotWithStack(is);
- } else if (inv.getStack(PlayerInventory.OFF_HAND_SLOT).isOf(Items.COMPASS)) {
- is = inv.getStack(PlayerInventory.OFF_HAND_SLOT);
- slot = PlayerInventory.OFF_HAND_SLOT;
- } else {
- for (int i = 0; i < PlayerInventory.MAIN_SIZE && is == null; i++) {
- final var stack = inv.getStack(i);
- if (stack.isOf(Items.COMPASS)) {
- is = stack;
- slot = i;
- }
- }
- }
- if (is == null) {
- LOGGER.warn("Compass item is null");
- is = new ItemStack(Items.COMPASS);
- }
- is.set(DataComponentTypes.LODESTONE_TRACKER, trackerCpnt);
- if (slot == PlayerInventory.NOT_FOUND) {
- player.giveItemStack(is);
- return;
- }
- inv.setStack(slot, is);
- }
+ private void updateCompass(ServerPlayerEntity player, ServerPlayerEntity tracked) {
+ final var trackerCpnt = new LodestoneTrackerComponent(Optional.of(GlobalPos.create(tracked.getEntityWorld().getRegistryKey(), tracked.getBlockPos())), true);
+ LOGGER.info(tracked.getEntityWorld().getRegistryKey().toString());
+ LOGGER.info(tracked.getBlockPos().toString());
+ LOGGER.info(trackerCpnt.toString());
+ ItemStack is = null;
+ int slot = PlayerInventory.NOT_FOUND;
+ final var inv = player.getInventory();
+ if (inv.getSelectedStack().isOf(Items.COMPASS)) {
+ is = inv.getSelectedStack();
+ slot = inv.getSlotWithStack(is);
+ } else if (inv.getStack(PlayerInventory.OFF_HAND_SLOT).isOf(Items.COMPASS)) {
+ is = inv.getStack(PlayerInventory.OFF_HAND_SLOT);
+ slot = PlayerInventory.OFF_HAND_SLOT;
+ } else {
+ for (int i = 0; i < PlayerInventory.MAIN_SIZE && is == null; i++) {
+ final var stack = inv.getStack(i);
+ if (stack.isOf(Items.COMPASS)) {
+ is = stack;
+ slot = i;
+ }
+ }
+ }
+ if (is == null) {
+ LOGGER.warn("Compass item is null");
+ is = new ItemStack(Items.COMPASS);
+ }
+ is.set(DataComponentTypes.LODESTONE_TRACKER, trackerCpnt);
+ if (slot == PlayerInventory.NOT_FOUND) {
+ player.giveItemStack(is);
+ return;
+ }
+ inv.setStack(slot, is);
+ }
- private enum State {
- ON, OFF
- }
+ private enum State {
+ ON, OFF
+ }
}
\ No newline at end of file diff --git a/src/main/java/world/anhgelus/manhunt/mixins/Tracker.java b/src/main/java/world/anhgelus/manhunt/mixins/Tracker.java index eaaa336..0155652 100644 --- a/src/main/java/world/anhgelus/manhunt/mixins/Tracker.java +++ b/src/main/java/world/anhgelus/manhunt/mixins/Tracker.java @@ -9,8 +9,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(LodestoneTrackerComponent.class)
public class Tracker {
- @Inject(method = "forWorld", at = @At("HEAD"), cancellable = true)
- public void forWorld(ServerWorld world, CallbackInfoReturnable<LodestoneTrackerComponent> cir) {
- cir.setReturnValue((LodestoneTrackerComponent) (Object) this);
- }
+ @Inject(method = "forWorld", at = @At("HEAD"), cancellable = true)
+ public void forWorld(ServerWorld world, CallbackInfoReturnable<LodestoneTrackerComponent> cir) {
+ cir.setReturnValue((LodestoneTrackerComponent) (Object) this);
+ }
}
diff --git a/src/main/java/world/anhgelus/manhunt/mixins/WorldTimerAccess.java b/src/main/java/world/anhgelus/manhunt/mixins/WorldTimerAccess.java index b61a204..4d390e1 100644 --- a/src/main/java/world/anhgelus/manhunt/mixins/WorldTimerAccess.java +++ b/src/main/java/world/anhgelus/manhunt/mixins/WorldTimerAccess.java @@ -14,32 +14,32 @@ import java.util.function.BooleanSupplier; @Mixin(ServerWorld.class) public class WorldTimerAccess implements TimerAccess { - @Unique - private final List<TimerAccess.TickTask> tasks = new ArrayList<>(); - - @Unique - private final List<TimerAccess.TickTask> tasksToAdd = new ArrayList<>(); - - @Inject(method = "tick", at = @At("TAIL")) - private void onTick(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { - tasks.stream().filter(TickTask::isRunning).forEach(TickTask::tick); - tasks.addAll(tasksToAdd); - tasksToAdd.clear(); - } - - @Override - public void dds_runTask(TimerAccess.TickTask task) { - tasksToAdd.add(task); - } - - @Override - public void dds_cancel() { - tasks.stream().filter(TickTask::isRunning).forEach(TickTask::cancel); - tasks.clear(); - } - - @Override - public List<TickTask> dds_getTasks() { - return tasks.stream().filter(TickTask::isRunning).toList(); - } + @Unique + private final List<TimerAccess.TickTask> tasks = new ArrayList<>(); + + @Unique + private final List<TimerAccess.TickTask> tasksToAdd = new ArrayList<>(); + + @Inject(method = "tick", at = @At("TAIL")) + private void onTick(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { + tasks.stream().filter(TickTask::isRunning).forEach(TickTask::tick); + tasks.addAll(tasksToAdd); + tasksToAdd.clear(); + } + + @Override + public void dds_runTask(TimerAccess.TickTask task) { + tasksToAdd.add(task); + } + + @Override + public void dds_cancel() { + tasks.stream().filter(TickTask::isRunning).forEach(TickTask::cancel); + tasks.clear(); + } + + @Override + public List<TickTask> dds_getTasks() { + return tasks.stream().filter(TickTask::isRunning).toList(); + } }
\ No newline at end of file diff --git a/src/main/java/world/anhgelus/manhunt/timer/TickTask.java b/src/main/java/world/anhgelus/manhunt/timer/TickTask.java index 5449166..4d9a1a5 100644 --- a/src/main/java/world/anhgelus/manhunt/timer/TickTask.java +++ b/src/main/java/world/anhgelus/manhunt/timer/TickTask.java @@ -4,69 +4,69 @@ package world.anhgelus.manhunt.timer; * Represents a complete task called each tick */ public class TickTask implements TimerAccess.TickTask { - public final long ticksDelay; - public final long ticksRepeat; - public final boolean repeating; - public final TimerAccess.Task task; - private boolean cancelled = false; - private long currentTicking; + public final long ticksDelay; + public final long ticksRepeat; + public final boolean repeating; + public final TimerAccess.Task task; + private boolean cancelled = false; + private long currentTicking; - /** - * Create a new repeating TickTask - * - * @param task Task to run after the delay or the repeat time - * @param ticksDelay Delay before the first task's run - * @param ticksRepeat Repeat each tick (if the repeat is 0, it will repeat each tick, if it is below 0, it will not repeat) - * @throws IllegalArgumentException if ticksDelay is below 0 - */ - public TickTask(TimerAccess.Task task, long ticksDelay, long ticksRepeat) { - if (ticksDelay < 0) throw new IllegalArgumentException("Ticks delay must be non-negative"); - this.ticksDelay = ticksDelay; - this.ticksRepeat = ticksRepeat; - this.task = task; - repeating = ticksRepeat >= 0; - currentTicking = ticksDelay; - } + /** + * Create a new repeating TickTask + * + * @param task Task to run after the delay or the repeat time + * @param ticksDelay Delay before the first task's run + * @param ticksRepeat Repeat each tick (if the repeat is 0, it will repeat each tick, if it is below 0, it will not repeat) + * @throws IllegalArgumentException if ticksDelay is below 0 + */ + public TickTask(TimerAccess.Task task, long ticksDelay, long ticksRepeat) { + if (ticksDelay < 0) throw new IllegalArgumentException("Ticks delay must be non-negative"); + this.ticksDelay = ticksDelay; + this.ticksRepeat = ticksRepeat; + this.task = task; + repeating = ticksRepeat >= 0; + currentTicking = ticksDelay; + } - /** - * Create a new delayed TickTask - * - * @param task Task to run after the delay or the repeat time - * @param ticksDelay Delay before the first task's run - * @throws IllegalArgumentException if ticksDelay or if ticksRepeat is below 0 - */ - public TickTask(TimerAccess.Task task, long ticksDelay) { - if (ticksDelay < 0) throw new IllegalArgumentException("Ticks delay must be non-negative"); - this.ticksDelay = ticksDelay; - this.ticksRepeat = -1; - this.task = task; - repeating = false; - currentTicking = ticksDelay; - } + /** + * Create a new delayed TickTask + * + * @param task Task to run after the delay or the repeat time + * @param ticksDelay Delay before the first task's run + * @throws IllegalArgumentException if ticksDelay or if ticksRepeat is below 0 + */ + public TickTask(TimerAccess.Task task, long ticksDelay) { + if (ticksDelay < 0) throw new IllegalArgumentException("Ticks delay must be non-negative"); + this.ticksDelay = ticksDelay; + this.ticksRepeat = -1; + this.task = task; + repeating = false; + currentTicking = ticksDelay; + } - public void tick() { - if (--currentTicking > 0) return; - task.run(); - if (repeating) { - currentTicking = ticksRepeat; - } else { - cancel(); - } - } + public void tick() { + if (--currentTicking > 0) return; + task.run(); + if (repeating) { + currentTicking = ticksRepeat; + } else { + cancel(); + } + } - public long cancel() { - if (cancelled) throw new IllegalStateException("Task already cancelled"); - cancelled = true; - return currentTicking; - } + public long cancel() { + if (cancelled) throw new IllegalStateException("Task already cancelled"); + cancelled = true; + return currentTicking; + } - public boolean isRunning() { - return !cancelled; - } + public boolean isRunning() { + return !cancelled; + } - @Override - public long getTickingBeforeRun() { - if (cancelled) return -1; - return currentTicking; - } + @Override + public long getTickingBeforeRun() { + if (cancelled) return -1; + return currentTicking; + } }
\ No newline at end of file diff --git a/src/main/java/world/anhgelus/manhunt/timer/TimerAccess.java b/src/main/java/world/anhgelus/manhunt/timer/TimerAccess.java index f9ed08e..3307167 100644 --- a/src/main/java/world/anhgelus/manhunt/timer/TimerAccess.java +++ b/src/main/java/world/anhgelus/manhunt/timer/TimerAccess.java @@ -5,60 +5,60 @@ import net.minecraft.server.MinecraftServer; import java.util.List; public interface TimerAccess { - /** - * Get the timer linked to the overworld - * - * @param server Current server - * @return TimerAccess linked to the overworld - */ - static TimerAccess getTimerFromOverworld(MinecraftServer server) { - final var timer = (TimerAccess) server.getOverworld(); - if (timer == null) - throw new NullPointerException("Impossible to get TimerAccess from the overworld (it is null)"); - return timer; - } + /** + * Get the timer linked to the overworld + * + * @param server Current server + * @return TimerAccess linked to the overworld + */ + static TimerAccess getTimerFromOverworld(MinecraftServer server) { + final var timer = (TimerAccess) server.getOverworld(); + if (timer == null) + throw new NullPointerException("Impossible to get TimerAccess from the overworld (it is null)"); + return timer; + } - /** - * Run a task (called each tick ticked) - * - * @param task Task to run - */ - void dds_runTask(TimerAccess.TickTask task); + /** + * Run a task (called each tick ticked) + * + * @param task Task to run + */ + void dds_runTask(TimerAccess.TickTask task); - void dds_cancel(); + void dds_cancel(); - /** - * @return All non-cancelled tasks - */ - List<TickTask> dds_getTasks(); + /** + * @return All non-cancelled tasks + */ + List<TickTask> dds_getTasks(); - interface TickTask { - /** - * Tick the task - */ - void tick(); + interface TickTask { + /** + * Tick the task + */ + void tick(); - /** - * Cancel the task - * - * @return the remaining ticks before the run of the Task - * @throws IllegalStateException if the task is already cancelled - */ - long cancel(); + /** + * Cancel the task + * + * @return the remaining ticks before the run of the Task + * @throws IllegalStateException if the task is already cancelled + */ + long cancel(); - boolean isRunning(); + boolean isRunning(); - /** - * @return the number of ticks before run of the task (if the task is cancelled, returns -1) - */ - long getTickingBeforeRun(); - } + /** + * @return the number of ticks before run of the task (if the task is cancelled, returns -1) + */ + long getTickingBeforeRun(); + } - /** - * Represents a task to run after ticking - */ - @FunctionalInterface - interface Task { - void run(); - } + /** + * Represents a task to run after ticking + */ + @FunctionalInterface + interface Task { + void run(); + } }
\ No newline at end of file |
