Refactor April Fools' prank logic: replace single explosion sound with randomized sounds, add logging, and adjust sound radius.

This commit is contained in:
akastijn 2026-03-31 23:25:35 +02:00
parent 592f58a89b
commit 6c66ac7cc8
2 changed files with 32 additions and 22 deletions

View File

@ -22,7 +22,7 @@ public class AprilFools extends SubCommand {
commandSender.sendRichMessage(Messages.GENERIC.PLAYER_ONLY); commandSender.sendRichMessage(Messages.GENERIC.PLAYER_ONLY);
return true; return true;
} }
boolean ok = prank.playExplosionAround(player); boolean ok = prank.playSoundAroundPlayer(player);
if (ok) { if (ok) {
commandSender.sendRichMessage("<green>April Fools test triggered. Listen closely..."); commandSender.sendRichMessage("<green>April Fools test triggered. Listen closely...");
} else { } else {

View File

@ -1,6 +1,7 @@
package com.alttd.playerutils.util; package com.alttd.playerutils.util;
import com.alttd.playerutils.PlayerUtils; import com.alttd.playerutils.PlayerUtils;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Sound; import org.bukkit.Sound;
@ -14,11 +15,12 @@ import java.util.List;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
/** /**
* Encapsulates the April Fools' prank logic. * Encapsulates the April Fools' prank logic. - schedule(): registers the timed prank that only runs on April 1st and
* - schedule(): registers the timed prank that only runs on April 1st and only during overworld night. * only during overworld night. - playExplosionAround(Player): immediately plays the explosion sound around the target
* - playExplosionAround(Player): immediately plays the explosion sound around the target for testing (no date/time checks), * for testing (no date/time checks), but still requires the target to be in the overworld named "world" to match
* but still requires the target to be in the overworld named "world" to match intended environment. * intended environment.
*/ */
@Slf4j
public class AprilFoolsPrank { public class AprilFoolsPrank {
private final PlayerUtils plugin; private final PlayerUtils plugin;
@ -38,17 +40,15 @@ public class AprilFoolsPrank {
return; // only active on April 1st return; // only active on April 1st
} }
// World world = Bukkit.getWorld("world"); World world = Bukkit.getWorld("world");
World world = Bukkit.getWorld("lobby"); if (world == null) {
if (world == null) return; // overworld not present return; // overworld not present
long time = world.getTime() % 24000L;
if (time < 13000L || time > 23000L) {
return; // only at night
} }
List<Player> players = world.getPlayers(); List<Player> players = world.getPlayers();
if (players.isEmpty()) return; if (players.isEmpty()) {
return;
}
Player target = players.get(ThreadLocalRandom.current().nextInt(players.size())); Player target = players.get(ThreadLocalRandom.current().nextInt(players.size()));
playOnce(world, target); playOnce(world, target);
@ -56,29 +56,39 @@ public class AprilFoolsPrank {
} }
/** /**
* Trigger the prank once around the given player for testing. Returns true if executed. * Trigger the prank once around the given player for testing. Returns true if executed. This method ignores the
* This method ignores the date and time checks so it can be tested easily, but it still * date and time checks so it can be tested easily, but it still requires the player to be in the overworld named
* requires the player to be in the overworld named "world". * "world".
*/ */
public boolean playExplosionAround(Player target) { public boolean playSoundAroundPlayer(Player target) {
if (target == null) return false; if (target == null) {
return false;
}
World world = target.getWorld(); World world = target.getWorld();
// if (!"world".equalsIgnoreCase(world.getName())) { if (!"world".equalsIgnoreCase(world.getName())) {
if (!"lobby".equalsIgnoreCase(world.getName())) {
return false; // only intended for overworld return false; // only intended for overworld
} }
playOnce(world, target); playOnce(world, target);
return true; return true;
} }
public List<Sound> getSounds() {
return List.of(Sound.ENTITY_GENERIC_EXPLODE, Sound.BLOCK_BELL_USE, Sound.ENTITY_SPIDER_AMBIENT,
Sound.ENTITY_TNT_PRIMED, Sound.ENTITY_CAT_BEG_FOR_FOOD, Sound.ENTITY_SHULKER_AMBIENT,
Sound.BLOCK_WOODEN_DOOR_CLOSE);
}
private void playOnce(World world, Player target) { private void playOnce(World world, Player target) {
Location base = target.getLocation(); Location base = target.getLocation();
double radius = 30.0; double radius = 25.0;
double r = ThreadLocalRandom.current().nextDouble(radius); double r = ThreadLocalRandom.current().nextDouble(radius);
double theta = ThreadLocalRandom.current().nextDouble(Math.PI * 2); double theta = ThreadLocalRandom.current().nextDouble(Math.PI * 2);
double dx = r * Math.cos(theta); double dx = r * Math.cos(theta);
double dz = r * Math.sin(theta); double dz = r * Math.sin(theta);
Location soundLoc = new Location(world, base.getX() + dx, base.getY(), base.getZ() + dz); Location soundLoc = new Location(world, base.getX() + dx, base.getY(), base.getZ() + dz);
world.playSound(soundLoc, Sound.ENTITY_GENERIC_EXPLODE, 0.8f, 1.0f); List<Sound> sounds = getSounds();
Sound chosen = sounds.get(ThreadLocalRandom.current().nextInt(sounds.size()));
log.info("Playing sound {} at {} (for {})", chosen, soundLoc, target.getName());
world.playSound(soundLoc, chosen, 1f, 1.0f);
} }
} }