Refactor class creators with annotations and utility methods.

Added @Contract, @NotNull, and @Unmodifiable annotations for better code safety and clarity. Refactored utility methods in `TankCreator` and `FighterCreator` to enhance readability and reuse, including extracting item creation logic for tools and display items.
This commit is contained in:
Teriuihi 2025-02-08 15:02:42 +01:00
parent a4f96297c2
commit a175d1829b
2 changed files with 49 additions and 11 deletions

View File

@ -6,10 +6,14 @@ import com.alttd.ctf.team.TeamColor;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
import java.util.List;
@ -18,28 +22,44 @@ public class FighterCreator {
private static final MiniMessage miniMessage = MiniMessage.miniMessage();
public static GameClass createFighter(TeamColor teamColor) {
@Contract("_ -> new")
public static @NotNull GameClass createFighter(@NotNull TeamColor teamColor) {
return new Fighter(getArmor(), getTools(teamColor), getDisplayItem(teamColor), 15, 3, 5);
}
private static List<Material> getArmor() {
@Contract(value = " -> new", pure = true)
private static @NotNull @Unmodifiable List<Material> getArmor() {
return (List.of(Material.AIR, Material.AIR, Material.LEATHER_CHESTPLATE, Material.AIR));
}
private static List<ItemStack> getTools(TeamColor teamColor) {
@Contract("_ -> new")
private static @NotNull @Unmodifiable List<ItemStack> getTools(@NotNull TeamColor teamColor) {
return (List.of(getShovel(teamColor), getPotion(teamColor)));
}
private static @NotNull ItemStack getPotion(@NotNull TeamColor teamColor) {
ItemStack healthPot = new ItemStack(Material.SPLASH_POTION);
if (!(healthPot.getItemMeta() instanceof PotionMeta potionMeta)) {
throw new IllegalStateException("No potion meta from a splash potion");
}
potionMeta.setBasePotionType(PotionType.STRONG_HEALING);
potionMeta.itemName(MiniMessage.miniMessage().deserialize(
potionMeta.itemName(miniMessage.deserialize(
String.format("<color:%s>Emergency AOE med kit</color>", teamColor.hex())));
healthPot.setItemMeta(potionMeta);
healthPot.setAmount(3);
return (List.of(healthPot));
return healthPot;
}
private static ItemStack getDisplayItem(TeamColor teamColor) {
private static @NotNull ItemStack getShovel(@NotNull TeamColor teamColor) {
ItemStack shovel = new ItemStack(Material.WOODEN_SHOVEL);
ItemMeta meta = shovel.getItemMeta();
meta.itemName(miniMessage.deserialize(String.format("<color:%s>Snow shovel</color>", teamColor.hex())));
meta.addEnchant(Enchantment.EFFICIENCY, 1, false);
shovel.setItemMeta(meta);
return shovel;
}
private static @NotNull ItemStack getDisplayItem(@NotNull TeamColor teamColor) {
ItemStack itemStack = new ItemStack(Material.IRON_SWORD);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.displayName(miniMessage.deserialize(String.format("<color:%s>Fighter</color>", teamColor.hex())));

View File

@ -9,6 +9,9 @@ import org.bukkit.Material;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
import java.util.List;
@ -17,25 +20,40 @@ public class TankCreator {
private static final MiniMessage miniMessage = MiniMessage.miniMessage();
public static GameClass createTank(TeamColor teamColor) {//TODO add ability to become temp invulnerable (with some particle effects mayb?)
@Contract("_ -> new")
public static @NotNull GameClass createTank(@NotNull TeamColor teamColor) {//TODO add ability to become temp invulnerable (with some particle effects mayb?)
return new Fighter(getArmor(), getTools(teamColor), getDisplayItem(teamColor), 30, 7, 4);
}
private static List<Material> getArmor() {
@Contract(value = " -> new", pure = true)
private static @Unmodifiable List<Material> getArmor() {
return (List.of(Material.CHAINMAIL_BOOTS, Material.CHAINMAIL_LEGGINGS, Material.LEATHER_CHESTPLATE, Material.CHAINMAIL_HELMET));
}
private static List<ItemStack> getTools(TeamColor teamColor) {
@Contract("_ -> new")
private static @NotNull @Unmodifiable List<ItemStack> getTools(TeamColor teamColor) {
return (List.of(getShovel(teamColor), getShield(teamColor)));
}
private static @NotNull ItemStack getShield(@NotNull TeamColor teamColor) {
ItemStack shield = new ItemStack(Material.SHIELD);
ItemMeta itemMeta = shield.getItemMeta();
itemMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
itemMeta.itemName(MiniMessage.miniMessage().deserialize(
String.format("<color:%s>Shield</color>", teamColor.hex())));
shield.setItemMeta(itemMeta);
return (List.of(shield));
return shield;
}
private static ItemStack getDisplayItem(TeamColor teamColor) {
private static @NotNull ItemStack getShovel(@NotNull TeamColor teamColor) {
ItemStack shovel = new ItemStack(Material.WOODEN_SHOVEL);
ItemMeta meta = shovel.getItemMeta();
meta.itemName(miniMessage.deserialize(String.format("<color:%s>Snow shovel</color>", teamColor.hex())));
shovel.setItemMeta(meta);
return shovel;
}
private static @NotNull ItemStack getDisplayItem(@NotNull TeamColor teamColor) {
ItemStack itemStack = new ItemStack(Material.SHIELD);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.displayName(miniMessage.deserialize(String.format("<color:%s>Tank</color>", teamColor.hex())));