159 lines
5.8 KiB
Java
159 lines
5.8 KiB
Java
package games.dmg.spigottyrant;
|
|
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.UUID;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.NamespacedKey;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
|
import org.bukkit.persistence.PersistentDataType;
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
public final class BukkitAbilityItemService implements AbilityItemService {
|
|
private final NamespacedKey abilityKey;
|
|
private final NamespacedKey ownerKey;
|
|
private final PluginSettings settings;
|
|
|
|
public BukkitAbilityItemService(Plugin plugin, PluginSettings settings) {
|
|
abilityKey = new NamespacedKey(plugin, "ability");
|
|
ownerKey = new NamespacedKey(plugin, "owner");
|
|
this.settings = settings;
|
|
}
|
|
|
|
@Override
|
|
public void recover(Player player, PlayerState state) {
|
|
for (Ability ability : state.readyAbilityItems()) {
|
|
if (!isItemAbilityForClass(ability, state.tyrantClass())
|
|
|| contains(player, state.playerId(), ability)) {
|
|
continue;
|
|
}
|
|
ItemStack item = create(state.playerId(), ability);
|
|
Map<Integer, ItemStack> leftovers = player.getInventory().addItem(item);
|
|
if (!leftovers.isEmpty()) {
|
|
player.sendMessage(ChatColor.RED + settings.inventoryFullMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean isBoundAbilityItem(ItemStack item) {
|
|
return ability(item).isPresent() && owner(item).isPresent();
|
|
}
|
|
|
|
@Override
|
|
public Optional<Ability> ability(ItemStack item) {
|
|
if (item == null || !item.hasItemMeta()) {
|
|
return Optional.empty();
|
|
}
|
|
String value = item.getItemMeta().getPersistentDataContainer()
|
|
.get(abilityKey, PersistentDataType.STRING);
|
|
if (value == null) {
|
|
return Optional.empty();
|
|
}
|
|
try {
|
|
return Optional.of(Ability.valueOf(value));
|
|
} catch (IllegalArgumentException exception) {
|
|
return Optional.empty();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Optional<UUID> owner(ItemStack item) {
|
|
if (item == null || !item.hasItemMeta()) {
|
|
return Optional.empty();
|
|
}
|
|
String value = item.getItemMeta().getPersistentDataContainer()
|
|
.get(ownerKey, PersistentDataType.STRING);
|
|
if (value == null) {
|
|
return Optional.empty();
|
|
}
|
|
try {
|
|
return Optional.of(UUID.fromString(value));
|
|
} catch (IllegalArgumentException exception) {
|
|
return Optional.empty();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void removeInvalid(Player player, PlayerState state) {
|
|
ItemStack[] contents = player.getInventory().getContents();
|
|
java.util.Set<Ability> retained = java.util.EnumSet.noneOf(Ability.class);
|
|
for (int index = 0; index < contents.length; index++) {
|
|
ItemStack item = contents[index];
|
|
if (!isBoundAbilityItem(item)) {
|
|
continue;
|
|
}
|
|
Optional<Ability> itemAbility = ability(item);
|
|
boolean valid = owner(item).filter(state.playerId()::equals).isPresent()
|
|
&& itemAbility.filter(state.readyAbilityItems()::contains).isPresent()
|
|
&& itemAbility.filter(retained::add).isPresent();
|
|
if (!valid) {
|
|
player.getInventory().setItem(index, null);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void removeAll(Player player) {
|
|
ItemStack[] contents = player.getInventory().getContents();
|
|
for (int index = 0; index < contents.length; index++) {
|
|
if (isBoundAbilityItem(contents[index])) {
|
|
player.getInventory().setItem(index, null);
|
|
}
|
|
}
|
|
}
|
|
|
|
private ItemStack create(UUID ownerId, Ability ability) {
|
|
PluginSettings.AbilityItemSettings configured = settingsFor(ability);
|
|
Material material = Material.matchMaterial(configured.material());
|
|
if (material == null) {
|
|
throw new IllegalStateException("Configured item material is unavailable");
|
|
}
|
|
ItemStack item = new ItemStack(material);
|
|
ItemMeta meta = item.getItemMeta();
|
|
meta.setDisplayName(ChatColor.GOLD + configured.name());
|
|
meta.getPersistentDataContainer().set(
|
|
abilityKey, PersistentDataType.STRING, ability.name()
|
|
);
|
|
meta.getPersistentDataContainer().set(
|
|
ownerKey, PersistentDataType.STRING, ownerId.toString()
|
|
);
|
|
item.setItemMeta(meta);
|
|
return item;
|
|
}
|
|
|
|
private boolean contains(Player player, UUID ownerId, Ability ability) {
|
|
for (ItemStack item : player.getInventory().getContents()) {
|
|
if (owner(item).filter(ownerId::equals).isPresent()
|
|
&& ability(item).filter(ability::equals).isPresent()) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private PluginSettings.AbilityItemSettings settingsFor(Ability ability) {
|
|
return switch (ability) {
|
|
case ASSASSIN_INVISIBILITY -> settings.assassinItem();
|
|
case FIXER_BOOST -> settings.fixerItem();
|
|
case TAMER_CAPTURE -> settings.tamerItem();
|
|
default -> throw new IllegalArgumentException(
|
|
ability.name().toLowerCase(Locale.ROOT) + " has no class item"
|
|
);
|
|
};
|
|
}
|
|
|
|
private static boolean isItemAbilityForClass(Ability ability, TyrantClass tyrantClass) {
|
|
return switch (tyrantClass) {
|
|
case ASSASSIN -> ability == Ability.ASSASSIN_INVISIBILITY;
|
|
case FIXER -> ability == Ability.FIXER_BOOST;
|
|
case TAMER -> ability == Ability.TAMER_CAPTURE;
|
|
case NONE -> false;
|
|
};
|
|
}
|
|
}
|