Files
src
build.gradle
settings.gradle
Code
17 1,105
src/main/java/me/deck/decksmp/Decksmp.java
package me.deck.decksmp;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.plugin.java.JavaPlugin;
public final class Decksmp extends JavaPlugin implements Listener {
public static Decksmp instance;
private static boolean abilitiesEnabled = true;
private final java.util.Set<NamespacedKey> cardRecipeKeys = new java.util.LinkedHashSet<>();
@Override
public void onEnable() {
instance = this;
saveDefaultConfig();
loadCardVariables();
registerCardRecipes();
getServer().getPluginManager().registerEvents(this, this);
getServer().getPluginManager().registerEvents(new CardUseListener(this), this);
getServer().getPluginManager().registerEvents(new CardGUIListener(this), this);
CardInfoGUICommand cardInfo = new CardInfoGUICommand(this);
getServer().getPluginManager().registerEvents(cardInfo, this);
getServer().getPluginManager().registerEvents(new CardEventHandler(this), this);
getServer().getPluginManager().registerEvents(new WithdrawCommand.CardDropOnDeath(this), this);
register("card", new CardCommand());
register("withdraw", new WithdrawCommand());
register("cards", new CardGUICommand());
register("cardinfo", cardInfo);
ChallengesCommand challenges = new ChallengesCommand(this);
register("challenges", challenges);
register("resetchallenges", challenges);
register("toggle", new ToggleCommand());
TrustCommand trust = new TrustCommand(this);
register("trust", trust);
getLogger().info("Deck SMP plugin has started!");
}
private void register(String name, org.bukkit.command.CommandExecutor executor) {
if (getCommand(name) != null) {
getCommand(name).setExecutor(executor);
if (executor instanceof org.bukkit.command.TabCompleter completer) {
getCommand(name).setTabCompleter(completer);
}
}
}
@Override
public void onDisable() {
saveCardVariables();
getLogger().info("Deck SMP plugin has stopped!");
}
public void saveCardVariables() {
Map<String, Object> values = new LinkedHashMap<>();
for (Map.Entry<String, String[]> entry : CardVariables.INSTANCE.getEquippedCards().entrySet()) {
values.put(entry.getKey(), java.util.Arrays.asList(entry.getValue()));
}
getConfig().set("equippedCards", values);
saveConfig();
}
public void loadCardVariables() {
ConfigurationSection section = getConfig().getConfigurationSection("equippedCards");
if (section == null) return;
for (String uuid : section.getKeys(false)) {
java.util.List<String> values = section.getStringList(uuid);
String[] slots = new String[2];
for (int i = 0; i < Math.min(values.size(), slots.length); i++) slots[i] = values.get(i);
CardVariables.INSTANCE.getEquippedCards().put(uuid, slots);
}
}
@EventHandler
public void onItemSpawn(ItemSpawnEvent event) {
Item item = event.getEntity();
if (Cards.INSTANCE.idFor(item.getItemStack()) != null) item.setGlowing(true);
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
unlockCardRecipes(event.getPlayer());
}
private void registerCardRecipes() {
Material base = material("AMETHYST_SHARD");
Material binding = material("GOLD_INGOT");
Material catalyst = material("REDSTONE");
java.util.List<String> uniqueIngredients = java.util.List.of(
"NETHERITE_INGOT", "BREWING_STAND", "IRON_AXE", "PACKED_ICE", "CONDUIT",
"LIGHTNING_ROD", "ENDER_PEARL", "GOLDEN_APPLE", "FEATHER", "SPYGLASS",
"GLOWSTONE_DUST", "PHANTOM_MEMBRANE", "EMERALD", "TOTEM_OF_UNDYING",
"REDSTONE", "COBWEB", "DIAMOND", "PISTON", "CLOCK", "DRAGON_EGG",
"WITHER_SKELETON_SKULL", "SHIELD");
int ingredientIndex = 0;
for (String id : Cards.INSTANCE.allCards().keySet()) {
if (ingredientIndex >= uniqueIngredients.size()) break;
ItemStack card = Cards.INSTANCE.get(id);
if (card == null) continue;
NamespacedKey key = new NamespacedKey(this, "card_" + id);
getServer().removeRecipe(key);
ShapedRecipe recipe = new ShapedRecipe(key, card.clone());
recipe.shape("ABA", "CDC", "ABA");
recipe.setIngredient('A', base);
recipe.setIngredient('B', binding);
recipe.setIngredient('C', catalyst);
recipe.setIngredient('D', material(uniqueIngredients.get(ingredientIndex++)));
getServer().addRecipe(recipe);
cardRecipeKeys.add(key);
}
}
private Material material(String name) {
Material material = Material.matchMaterial(name);
if (material == null) {
throw new IllegalStateException("Missing card recipe material: " + name);
}
return material;
}
private void unlockCardRecipes(Player player) {
for (NamespacedKey key : cardRecipeKeys) {
player.discoverRecipe(key);
}
}
public static Decksmp getInstance() {
return instance;
}
public static boolean isAbilitiesEnabled() {
return abilitiesEnabled;
}
public static void setAbilitiesEnabled(boolean enabled) {
abilitiesEnabled = enabled;
}
}