Files
src
build.gradle
settings.gradle
Code
14 1,402
src/main/java/com/kodari/customweapons/CustomWeaponsPlugin.java
package com.kodari.customweapons;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.EnumMap;
import java.util.Map;
public class CustomWeaponsPlugin extends JavaPlugin {
private final Map<WeaponType, WeaponDefinition> definitions = new EnumMap<>(WeaponType.class);
private WeaponItemFactory itemFactory;
private CooldownManager cooldownManager;
private PlayerStateManager playerStateManager;
@Override
public void onEnable() {
saveDefaultConfig();
loadDefinitions();
itemFactory = new WeaponItemFactory(this, definitions);
cooldownManager = new CooldownManager();
playerStateManager = new PlayerStateManager(this);
registerRecipes();
getServer().getPluginManager().registerEvents(
new WeaponListener(this, itemFactory, cooldownManager, playerStateManager),
this
);
if (getCommand("resetcraft") != null) {
getCommand("resetcraft").setExecutor(new ResetCraftCommand(playerStateManager));
}
if (getCommand("resetcraft") != null) {
getCommand("resetcraft").setExecutor(new ResetCraftCommand(playerStateManager));
}
if (getCommand("resetcraft") != null) {
getCommand("resetcraft").setExecutor(new ResetCraftCommand(playerStateManager));
}
if (getCommand("weapons") != null) {
WeaponsCommand weaponsCommand = new WeaponsCommand(this, definitions, itemFactory);
getCommand("weapons").setExecutor(weaponsCommand);
getCommand("weapons").setTabCompleter(weaponsCommand);
getServer().getPluginManager().registerEvents(weaponsCommand, this);
}
}
@Override
public void onDisable() {
if (playerStateManager != null) {
playerStateManager.save();
}
}
private void loadDefinitions() {
for (WeaponType type : WeaponType.values()) {
String path = "weapons." + type.id();
definitions.put(type, new WeaponDefinition(
type,
getConfig().getString(path + ".display-name", "<white>" + type.id()),
getConfig().getInt(path + ".model-data", 1000),
getConfig().getStringList(path + ".lore")
));
}
}
private void registerRecipes() {
for (WeaponType type : WeaponType.values()) {
String path = "weapons." + type.id() + ".recipe";
ConfigurationSection section = getConfig().getConfigurationSection(path);
if (section == null) {
continue;
}
ItemStack result = itemFactory.create(type);
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "craft_" + type.id()), result);
recipe.shape(section.getStringList("shape").toArray(new String[0]));
ConfigurationSection ingredients = section.getConfigurationSection("ingredients");
if (ingredients == null) {
continue;
}
for (String key : ingredients.getKeys(false)) {
Material material = Material.matchMaterial(ingredients.getString(key, ""));
if (material == null || key.length() != 1) {
throw new IllegalArgumentException("Invalid ingredient for " + type.id() + ": " + key);
}
recipe.setIngredient(key.charAt(0), new RecipeChoice.MaterialChoice(material));
}
getServer().addRecipe(recipe);
}
}
}