Formatting

This commit is contained in:
akastijn 2026-04-04 01:09:18 +02:00
parent 600f93c253
commit 6ff969f051
15 changed files with 177 additions and 87 deletions

View File

@ -5,10 +5,10 @@ import com.alttd.easter.config.Config;
import com.alttd.easter.config.Messages; import com.alttd.easter.config.Messages;
import com.alttd.easter.data.DataManager; import com.alttd.easter.data.DataManager;
import com.alttd.easter.glow.GlowManager; import com.alttd.easter.glow.GlowManager;
import com.alttd.easter.gui.TurnInGuiManager;
import com.alttd.easter.listeners.DeathListener; import com.alttd.easter.listeners.DeathListener;
import com.alttd.easter.listeners.SpawnListener; import com.alttd.easter.listeners.SpawnListener;
import com.alttd.easter.npc.RabbitNpcManager; import com.alttd.easter.npc.RabbitNpcManager;
import com.alttd.easter.gui.TurnInGuiManager;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -53,7 +53,9 @@ public final class Easter extends JavaPlugin {
public void reloadConfigs() { public void reloadConfigs() {
Config.reload(); Config.reload();
Messages.reload(); Messages.reload();
if (rabbitNpcManager != null) rabbitNpcManager.reload(); if (rabbitNpcManager != null) {
rabbitNpcManager.reload();
}
} }
private void registerSchedulers() { private void registerSchedulers() {

View File

@ -1,7 +1,8 @@
package com.alttd.easter.commands; package com.alttd.easter.commands;
import com.alttd.easter.Easter; import com.alttd.easter.Easter;
import com.alttd.easter.commands.subcommands.*; import com.alttd.easter.commands.subcommands.Reload;
import com.alttd.easter.commands.subcommands.SetPrize;
import com.alttd.easter.config.Messages; import com.alttd.easter.config.Messages;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -18,7 +19,8 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@SuppressWarnings("ClassCanBeRecord") @SuppressWarnings("ClassCanBeRecord")
@Slf4j @Getter @Slf4j
@Getter
public class Command implements CommandExecutor, TabExecutor { public class Command implements CommandExecutor, TabExecutor {
private final List<SubCommand> subCommands; private final List<SubCommand> subCommands;
@ -50,8 +52,9 @@ public class Command implements CommandExecutor, TabExecutor {
} }
SubCommand subCommand = getSubCommand(args[0]); SubCommand subCommand = getSubCommand(args[0]);
if (subCommand == null) if (subCommand == null) {
return false; return false;
}
if (!commandSender.hasPermission(subCommand.getPermission())) { if (!commandSender.hasPermission(subCommand.getPermission())) {
commandSender.sendRichMessage(Messages.GENERIC.NO_PERMISSION, Placeholder.parsed("permission", subCommand.getPermission())); commandSender.sendRichMessage(Messages.GENERIC.NO_PERMISSION, Placeholder.parsed("permission", subCommand.getPermission()));
@ -78,11 +81,12 @@ public class Command implements CommandExecutor, TabExecutor {
); );
} else { } else {
SubCommand subCommand = getSubCommand(args[0]); SubCommand subCommand = getSubCommand(args[0]);
if (subCommand != null && commandSender.hasPermission(subCommand.getPermission())) if (subCommand != null && commandSender.hasPermission(subCommand.getPermission())) {
res.addAll(subCommand.getTabComplete(commandSender, args).stream() res.addAll(subCommand.getTabComplete(commandSender, args).stream()
.filter(str -> str.toLowerCase().startsWith(args[args.length - 1].toLowerCase())) .filter(str -> str.toLowerCase().startsWith(args[args.length - 1].toLowerCase()))
.toList()); .toList());
} }
}
return res; return res;
} }

View File

@ -6,7 +6,8 @@ import java.util.List;
public abstract class SubCommand { public abstract class SubCommand {
public SubCommand() {} public SubCommand() {
}
public abstract boolean onCommand(CommandSender commandSender, String[] args); public abstract boolean onCommand(CommandSender commandSender, String[] args);

View File

@ -19,7 +19,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Slf4j @SuppressWarnings({"unused", "SameParameterValue"}) @Slf4j
@SuppressWarnings({"unused", "SameParameterValue"})
abstract class AbstractConfig { abstract class AbstractConfig {
File file; File file;
YamlConfiguration yaml; YamlConfiguration yaml;
@ -129,8 +130,7 @@ abstract class AbstractConfig {
final ConfigurationSection section = yaml.getConfigurationSection(path); final ConfigurationSection section = yaml.getConfigurationSection(path);
if (section != null) { if (section != null) {
for (String key : section.getKeys(false)) { for (String key : section.getKeys(false)) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked") final T val = (T) section.get(key);
final T val = (T) section.get(key);
if (val != null) { if (val != null) {
builder.put(key, val); builder.put(key, val);
} }

View File

@ -9,7 +9,8 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Slf4j public class Config extends AbstractConfig{ @Slf4j
public class Config extends AbstractConfig {
static Config config; static Config config;
@ -58,7 +59,9 @@ import java.util.List;
public static Location getLocation(org.bukkit.Server server) { public static Location getLocation(org.bukkit.Server server) {
World world = server.getWorld(WORLD); World world = server.getWorld(WORLD);
if (world == null) return null; if (world == null) {
return null;
}
return new Location(world, X, Y, Z, YAW, PITCH); return new Location(world, X, Y, Z, YAW, PITCH);
} }
} }
@ -73,7 +76,9 @@ import java.util.List;
LIST = new ArrayList<>(); LIST = new ArrayList<>();
if (raw != null) { if (raw != null) {
for (Object o : raw) { for (Object o : raw) {
if (o instanceof ItemStack item) LIST.add(item); if (o instanceof ItemStack item) {
LIST.add(item);
}
} }
} }
// ensure path exists // ensure path exists
@ -86,7 +91,9 @@ import java.util.List;
} }
public static ItemStack getRandomPrize() { public static ItemStack getRandomPrize() {
if (LIST.isEmpty()) return null; if (LIST.isEmpty()) {
return null;
}
return LIST.get((int) (Math.random() * LIST.size())).clone(); return LIST.get((int) (Math.random() * LIST.size())).clone();
} }
} }

View File

@ -18,7 +18,10 @@ public class DataManager {
this.file = new File(plugin.getDataFolder(), "data.yml"); this.file = new File(plugin.getDataFolder(), "data.yml");
this.yaml = new YamlConfiguration(); this.yaml = new YamlConfiguration();
if (file.exists()) { if (file.exists()) {
try { yaml.load(file); } catch (Exception ignored) {} try {
yaml.load(file);
} catch (Exception ignored) {
}
} }
load(); load();
} }
@ -29,10 +32,15 @@ public class DataManager {
UUID uuid = UUID.fromString(key); UUID uuid = UUID.fromString(key);
List<String> list = yaml.getStringList(key); List<String> list = yaml.getStringList(key);
Set<EggType> types = list.stream().map(s -> { Set<EggType> types = list.stream().map(s -> {
try { return EggType.valueOf(s); } catch (Exception e) { return null; } try {
return EggType.valueOf(s);
} catch (Exception e) {
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toSet()); }).filter(Objects::nonNull).collect(Collectors.toSet());
playerEggs.put(uuid, types); playerEggs.put(uuid, types);
} catch (IllegalArgumentException ignored) {} } catch (IllegalArgumentException ignored) {
}
} }
} }
@ -41,7 +49,10 @@ public class DataManager {
List<String> list = e.getValue().stream().map(Enum::name).toList(); List<String> list = e.getValue().stream().map(Enum::name).toList();
yaml.set(e.getKey().toString(), list); yaml.set(e.getKey().toString(), list);
} }
try { yaml.save(file); } catch (IOException ignored) {} try {
yaml.save(file);
} catch (IOException ignored) {
}
} }
public Set<EggType> getPlayerEggs(UUID uuid) { public Set<EggType> getPlayerEggs(UUID uuid) {

View File

@ -40,7 +40,9 @@ public enum EggType {
public static EggType fromEntityType(EntityType entityType) { public static EggType fromEntityType(EntityType entityType) {
for (EggType type : values()) { for (EggType type : values()) {
if (type.entityType == entityType) return type; if (type.entityType == entityType) {
return type;
}
} }
return null; return null;
} }

View File

@ -25,28 +25,47 @@ public class GlowManager {
team.setColor(colorFor(type)); team.setColor(colorFor(type));
} }
String entry = entity.getUniqueId().toString(); String entry = entity.getUniqueId().toString();
if (!team.hasEntry(entry)) team.addEntry(entry); if (!team.hasEntry(entry)) {
team.addEntry(entry);
}
} }
private ChatColor colorFor(EggType type) { private ChatColor colorFor(EggType type) {
switch (type) { switch (type) {
case BROWN_HUSK: return ChatColor.DARK_RED; // closest case BROWN_HUSK:
case RED_SPIDER: return ChatColor.RED; return ChatColor.DARK_RED; // closest
case ORANGE_BLAZE: return ChatColor.GOLD; case RED_SPIDER:
case YELLOW_PIGLIN: return ChatColor.YELLOW; return ChatColor.RED;
case LIME_CREEPER: return ChatColor.GREEN; case ORANGE_BLAZE:
case GREEN_ZOMBIE: return ChatColor.DARK_GREEN; return ChatColor.GOLD;
case LIGHT_BLUE_STRAY: return ChatColor.AQUA; case YELLOW_PIGLIN:
case CYAN_GUARDIAN: return ChatColor.DARK_AQUA; return ChatColor.YELLOW;
case BLUE_WARDEN: return ChatColor.BLUE; case LIME_CREEPER:
case PURPLE_ENDER_DRAGON: return ChatColor.DARK_PURPLE; return ChatColor.GREEN;
case MAGENTA_ENDERMAN: return ChatColor.LIGHT_PURPLE; case GREEN_ZOMBIE:
case PINK_HOGLIN: return ChatColor.LIGHT_PURPLE; return ChatColor.DARK_GREEN;
case WHITE_SKELETON: return ChatColor.WHITE; case LIGHT_BLUE_STRAY:
case LIGHT_GRAY_SILVERFISH: return ChatColor.GRAY; return ChatColor.AQUA;
case GRAY_WITHER: return ChatColor.DARK_GRAY; case CYAN_GUARDIAN:
case BLACK_WITHER_SKELETON: return ChatColor.BLACK; return ChatColor.DARK_AQUA;
default: return ChatColor.WHITE; case BLUE_WARDEN:
return ChatColor.BLUE;
case PURPLE_ENDER_DRAGON:
return ChatColor.DARK_PURPLE;
case MAGENTA_ENDERMAN:
return ChatColor.LIGHT_PURPLE;
case PINK_HOGLIN:
return ChatColor.LIGHT_PURPLE;
case WHITE_SKELETON:
return ChatColor.WHITE;
case LIGHT_GRAY_SILVERFISH:
return ChatColor.GRAY;
case GRAY_WITHER:
return ChatColor.DARK_GRAY;
case BLACK_WITHER_SKELETON:
return ChatColor.BLACK;
default:
return ChatColor.WHITE;
} }
} }
} }

View File

@ -28,7 +28,9 @@ public class TurnInGuiManager implements Listener {
// crude strip of minimessage tags for inventory title // crude strip of minimessage tags for inventory title
String mm = Messages.RABBIT.GUI_TITLE; String mm = Messages.RABBIT.GUI_TITLE;
String plain = mm.replaceAll("<[^>]+>", ""); String plain = mm.replaceAll("<[^>]+>", "");
if (plain.isBlank()) plain = "Turn in Eggs"; if (plain.isBlank()) {
plain = "Turn in Eggs";
}
return plain; return plain;
} }
@ -40,10 +42,16 @@ public class TurnInGuiManager implements Listener {
@EventHandler @EventHandler
public void onClick(InventoryClickEvent event) { public void onClick(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player player)) return; if (!(event.getWhoClicked() instanceof Player player)) {
return;
}
Inventory top = openInventories.get(player.getUniqueId()); Inventory top = openInventories.get(player.getUniqueId());
if (top == null) return; if (top == null) {
if (!event.getView().getTopInventory().equals(top)) return; return;
}
if (!event.getView().getTopInventory().equals(top)) {
return;
}
// If clicking in player inventory with shift-click, prevent moving non-egg items into GUI // If clicking in player inventory with shift-click, prevent moving non-egg items into GUI
if (event.isShiftClick() && event.getClickedInventory() != null && event.getClickedInventory().equals(player.getInventory())) { if (event.isShiftClick() && event.getClickedInventory() != null && event.getClickedInventory().equals(player.getInventory())) {
@ -68,14 +76,20 @@ public class TurnInGuiManager implements Listener {
public void onClose(InventoryCloseEvent event) { public void onClose(InventoryCloseEvent event) {
Player player = (Player) event.getPlayer(); Player player = (Player) event.getPlayer();
Inventory top = openInventories.remove(player.getUniqueId()); Inventory top = openInventories.remove(player.getUniqueId());
if (top == null) return; if (top == null) {
if (!event.getInventory().equals(top)) return; return;
}
if (!event.getInventory().equals(top)) {
return;
}
// collect egg items and clear them from the GUI so they don't drop // collect egg items and clear them from the GUI so they don't drop
Set<EggType> submitted = new HashSet<>(); Set<EggType> submitted = new HashSet<>();
for (ItemStack item : top.getContents()) { for (ItemStack item : top.getContents()) {
if (ItemUtils.isEggItem(item)) { if (ItemUtils.isEggItem(item)) {
EggType type = ItemUtils.getEggType(item); EggType type = ItemUtils.getEggType(item);
if (type != null) submitted.add(type); if (type != null) {
submitted.add(type);
}
} }
} }
top.clear(); top.clear();
@ -107,7 +121,9 @@ public class TurnInGuiManager implements Listener {
int total = dataManager.getPlayerEggs(player.getUniqueId()).size(); int total = dataManager.getPlayerEggs(player.getUniqueId()).size();
if (total % 4 == 0) { if (total % 4 == 0) {
var prize = com.alttd.easter.config.Config.PRIZES.getRandomPrize(); var prize = com.alttd.easter.config.Config.PRIZES.getRandomPrize();
if (prize != null) player.getInventory().addItem(prize); if (prize != null) {
player.getInventory().addItem(prize);
}
player.sendRichMessage(Messages.RABBIT.REWARD); player.sendRichMessage(Messages.RABBIT.REWARD);
} }
} }

View File

@ -26,13 +26,21 @@ public class SpawnListener implements Listener {
@EventHandler @EventHandler
public void onNaturalSpawn(CreatureSpawnEvent event) { public void onNaturalSpawn(CreatureSpawnEvent event) {
if (event.getSpawnReason() != CreatureSpawnEvent.SpawnReason.NATURAL) return; if (event.getSpawnReason() != CreatureSpawnEvent.SpawnReason.NATURAL) {
if (!(event.getEntity() instanceof Creature creature)) return; return;
}
if (!(event.getEntity() instanceof Creature creature)) {
return;
}
EntityType type = event.getEntityType(); EntityType type = event.getEntityType();
EggType eggType = EggType.fromEntityType(type); EggType eggType = EggType.fromEntityType(type);
if (eggType == null) return; if (eggType == null) {
return;
}
double chance = Config.EGGS.SPAWN_CHANCE; double chance = Config.EGGS.SPAWN_CHANCE;
if (ThreadLocalRandom.current().nextDouble() >= chance) return; if (ThreadLocalRandom.current().nextDouble() >= chance) {
return;
}
ItemStack eggItem = ItemUtils.createEggItem(eggType); ItemStack eggItem = ItemUtils.createEggItem(eggType);
EntityEquipment equipment = creature.getEquipment(); EntityEquipment equipment = creature.getEquipment();

View File

@ -16,7 +16,7 @@ import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.util.*; import java.util.UUID;
@SuppressWarnings("ClassCanBeRecord") @SuppressWarnings("ClassCanBeRecord")
public class RabbitNpcManager implements Listener { public class RabbitNpcManager implements Listener {
@ -46,7 +46,9 @@ public class RabbitNpcManager implements Listener {
} }
} }
var loc = Config.RABBIT.getLocation(plugin.getServer()); var loc = Config.RABBIT.getLocation(plugin.getServer());
if (loc == null) return; if (loc == null) {
return;
}
Rabbit rabbit = (Rabbit) loc.getWorld().spawnEntity(loc, EntityType.RABBIT); Rabbit rabbit = (Rabbit) loc.getWorld().spawnEntity(loc, EntityType.RABBIT);
rabbit.setCustomNameVisible(true); rabbit.setCustomNameVisible(true);
rabbit.customName(net.kyori.adventure.text.Component.text("Easter Bunny")); rabbit.customName(net.kyori.adventure.text.Component.text("Easter Bunny"));
@ -56,7 +58,9 @@ public class RabbitNpcManager implements Listener {
@EventHandler @EventHandler
public void onInteract(PlayerInteractAtEntityEvent event) { public void onInteract(PlayerInteractAtEntityEvent event) {
if (!(event.getRightClicked() instanceof Rabbit) && !(event.getRightClicked() instanceof org.bukkit.entity.LivingEntity le && le.getPersistentDataContainer().has(Keys.RABBIT_NPC, PersistentDataType.BYTE))) return; if (!(event.getRightClicked() instanceof Rabbit) && !(event.getRightClicked() instanceof org.bukkit.entity.LivingEntity le && le.getPersistentDataContainer().has(Keys.RABBIT_NPC, PersistentDataType.BYTE))) {
return;
}
event.setCancelled(true); event.setCancelled(true);
Player player = event.getPlayer(); Player player = event.getPlayer();
// Right click -> open GUI // Right click -> open GUI
@ -65,9 +69,15 @@ public class RabbitNpcManager implements Listener {
@EventHandler @EventHandler
public void onLeftClick(EntityDamageByEntityEvent event) { public void onLeftClick(EntityDamageByEntityEvent event) {
if (!(event.getEntity() instanceof org.bukkit.entity.LivingEntity le)) return; if (!(event.getEntity() instanceof org.bukkit.entity.LivingEntity le)) {
if (!le.getPersistentDataContainer().has(Keys.RABBIT_NPC, PersistentDataType.BYTE)) return; return;
if (!(event.getDamager() instanceof Player player)) return; }
if (!le.getPersistentDataContainer().has(Keys.RABBIT_NPC, PersistentDataType.BYTE)) {
return;
}
if (!(event.getDamager() instanceof Player player)) {
return;
}
event.setCancelled(true); event.setCancelled(true);
player.sendRichMessage(Messages.RABBIT.LEFT_CLICK_INTRO, player.sendRichMessage(Messages.RABBIT.LEFT_CLICK_INTRO,
Placeholder.parsed("player", player.getName()) Placeholder.parsed("player", player.getName())

View File

@ -45,14 +45,20 @@ public final class ItemUtils {
} }
public static boolean isEggItem(ItemStack item) { public static boolean isEggItem(ItemStack item) {
if (item == null) return false; if (item == null) {
return false;
}
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
if (meta == null) return false; if (meta == null) {
return false;
}
return meta.getPersistentDataContainer().has(Keys.EGG_ITEM, PersistentDataType.STRING); return meta.getPersistentDataContainer().has(Keys.EGG_ITEM, PersistentDataType.STRING);
} }
public static EggType getEggType(ItemStack item) { public static EggType getEggType(ItemStack item) {
if (!isEggItem(item)) return null; if (!isEggItem(item)) {
return null;
}
String name = item.getItemMeta().getPersistentDataContainer().get(Keys.EGG_ITEM, PersistentDataType.STRING); String name = item.getItemMeta().getPersistentDataContainer().get(Keys.EGG_ITEM, PersistentDataType.STRING);
try { try {
return name == null ? null : EggType.valueOf(name); return name == null ? null : EggType.valueOf(name);
@ -65,11 +71,14 @@ public final class ItemUtils {
String[] parts = name.toLowerCase().split("_"); String[] parts = name.toLowerCase().split("_");
List<String> out = new ArrayList<>(); List<String> out = new ArrayList<>();
for (String p : parts) { for (String p : parts) {
if (p.isEmpty()) continue; if (p.isEmpty()) {
continue;
}
out.add(Character.toUpperCase(p.charAt(0)) + p.substring(1)); out.add(Character.toUpperCase(p.charAt(0)) + p.substring(1));
} }
return String.join(" ", out); return String.join(" ", out);
} }
private ItemUtils() {} private ItemUtils() {
}
} }

View File

@ -8,5 +8,6 @@ public final class Keys {
public static final NamespacedKey EGG_ENTITY = new NamespacedKey(Easter.getPlugin(Easter.class), "egg-entity"); public static final NamespacedKey EGG_ENTITY = new NamespacedKey(Easter.getPlugin(Easter.class), "egg-entity");
public static final NamespacedKey RABBIT_NPC = new NamespacedKey(Easter.getPlugin(Easter.class), "rabbit-npc"); public static final NamespacedKey RABBIT_NPC = new NamespacedKey(Easter.getPlugin(Easter.class), "rabbit-npc");
private Keys() {} private Keys() {
}
} }