mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-28 19:39:14 +00:00
Added PAPI expansion
This commit is contained in:
@@ -79,7 +79,7 @@ public class HMCCosmetics extends JavaPlugin {
|
||||
this.load();
|
||||
}
|
||||
|
||||
HookManager.getInstance().registerListeners(this);
|
||||
HookManager.getInstance().init();
|
||||
|
||||
this.saveTask = Bukkit.getScheduler().runTaskTimerAsynchronously(
|
||||
this,
|
||||
|
||||
@@ -11,6 +11,9 @@ import java.nio.file.Path;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -20,6 +23,8 @@ import java.util.function.Consumer;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import javax.swing.text.DateFormatter;
|
||||
|
||||
public class DatabaseConverter {
|
||||
|
||||
private static final int CURRENT_VERSION = 2;
|
||||
|
||||
@@ -20,8 +20,10 @@ import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class CosmeticsMenu {
|
||||
|
||||
@@ -33,6 +35,8 @@ public class CosmeticsMenu {
|
||||
|
||||
private final Map<String, CosmeticGui> guiMap = new HashMap<>();
|
||||
|
||||
private final Set<String> registeredPermissions = new HashSet<>();
|
||||
|
||||
public CosmeticsMenu(final HMCCosmetics plugin) {
|
||||
this.plugin = plugin;
|
||||
this.cosmeticManager = this.plugin.getCosmeticManager();
|
||||
@@ -178,8 +182,9 @@ public class CosmeticsMenu {
|
||||
final ArmorItem copy = new ArmorItem(item);
|
||||
copy.setAction(null);
|
||||
this.cosmeticManager.addArmorItem(copy);
|
||||
if (copy.getPermission().isBlank()) continue;
|
||||
Bukkit.getPluginManager().addPermission(new Permission(copy.getPermission()));
|
||||
final String perm = copy.getPermission();
|
||||
if (perm.isBlank() || this.registeredPermissions.contains(perm)) continue;
|
||||
Bukkit.getPluginManager().addPermission(new Permission(perm));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import io.github.fisher2911.hmccosmetics.hook.item.PAPIExpansion;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
@@ -67,6 +69,13 @@ public class HookManager {
|
||||
return this.registeredHooks.contains(hook);
|
||||
}
|
||||
|
||||
public void init() {
|
||||
if (this.isEnabled(PAPIHook.class)) {
|
||||
new PAPIExpansion(this.plugin).register();
|
||||
}
|
||||
this.registerListeners(this.plugin);
|
||||
}
|
||||
|
||||
public void registerListeners(final HMCCosmetics plugin) {
|
||||
for (final Listener listener : this.listeners) {
|
||||
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package io.github.fisher2911.hmccosmetics.hook.item;
|
||||
|
||||
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
|
||||
import io.github.fisher2911.hmccosmetics.api.CosmeticItem;
|
||||
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
|
||||
import io.github.fisher2911.hmccosmetics.message.Placeholder;
|
||||
import io.github.fisher2911.hmccosmetics.message.Translation;
|
||||
import io.github.fisher2911.hmccosmetics.user.User;
|
||||
import io.github.fisher2911.hmccosmetics.user.UserManager;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class PAPIExpansion extends PlaceholderExpansion {
|
||||
|
||||
private final HMCCosmetics plugin;
|
||||
private final UserManager userManager;
|
||||
private static final String IDENTIFIER = "hmccosmetics";
|
||||
private static final String AUTHOR = "MasterOfTheFish";
|
||||
|
||||
public PAPIExpansion(final HMCCosmetics plugin) {
|
||||
this.plugin = plugin;
|
||||
this.userManager = this.plugin.getUserManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getIdentifier() {
|
||||
return IDENTIFIER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getAuthor() {
|
||||
return AUTHOR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getVersion() {
|
||||
return this.plugin.getDescription().getVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String onPlaceholderRequest(final Player player, @NotNull final String params) {
|
||||
final String[] parts = params.split("_");
|
||||
if (parts.length == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Optional<User> optionalUser = this.userManager.get(player.getUniqueId());
|
||||
|
||||
if (optionalUser.isEmpty()) return null;
|
||||
final User user = optionalUser.get();
|
||||
|
||||
// %hmccosmetics_using_id%
|
||||
if (parts[0].equalsIgnoreCase("using")) {
|
||||
if (parts.length < 2) return null;
|
||||
final String id = this.getId(parts, 1);
|
||||
for (final ArmorItem item : user.getPlayerArmor().getArmorItems()) {
|
||||
if (item.getId().equals(id)) return Translation.translate("true");
|
||||
}
|
||||
return Translation.translate("false");
|
||||
}
|
||||
|
||||
// %hmccosmetics_current_type%
|
||||
if (parts[0].equals("current")) {
|
||||
if (parts.length < 2) {
|
||||
final String typeStr = parts[1];
|
||||
try {
|
||||
final ArmorItem.Type type = ArmorItem.Type.valueOf(typeStr);
|
||||
for (final ArmorItem item : user.getPlayerArmor().getArmorItems()) {
|
||||
if (item.getType().equals(type)) return item.getId();
|
||||
}
|
||||
return null;
|
||||
} catch (final IllegalArgumentException exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getId(final String[] parts, final int fromIndex) {
|
||||
final StringBuilder builder = new StringBuilder(parts[fromIndex]);
|
||||
for (int i = fromIndex + 1; i < parts.length; i++) {
|
||||
builder.append(parts[i]);
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user