Files
src
pom.xml
Code
7 608
src/main/java/com/kodari/endsword/EndSwordPlugin.java
package com.kodari.endsword;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public final class EndSwordPlugin extends JavaPlugin {
private NamespacedKey recipeKey;
private EndSwordItem endSwordItem;
@Override
public void onEnable() {
saveDefaultConfig();
recipeKey = new NamespacedKey(this, "end_sword");
endSwordItem = new EndSwordItem(this);
getServer().getPluginManager().registerEvents(new EndSwordListener(this, endSwordItem), this);
EndSwordCommand command = new EndSwordCommand(this, endSwordItem);
getCommand("endsword").setExecutor(command);
getCommand("endsword").setTabCompleter(command);
registerCraftingRecipe();
}
public void reloadSettings() {
reloadConfig();
registerCraftingRecipe();
}
private void registerCraftingRecipe() {
Bukkit.removeRecipe(recipeKey);
List<String> rows = getConfig().getStringList("recipe.rows");
if (rows.isEmpty() || rows.size() > 3) {
getLogger().warning("The End Sword recipe must have between one and three rows.");
return;
}
int rowWidth = rows.get(0).length();
if (rowWidth < 1 || rowWidth > 3 || rows.stream().anyMatch(row -> row.length() != rowWidth)) {
getLogger().warning("The End Sword recipe rows must have the same width, between one and three characters.");
return;
}
ConfigurationSection ingredients = getConfig().getConfigurationSection("recipe.ingredients");
if (ingredients == null) {
getLogger().warning("The End Sword recipe has no ingredient mappings.");
return;
}
Set<Character> requiredSymbols = new HashSet<>();
for (String row : rows) {
for (char symbol : row.toCharArray()) {
if (symbol != ' ') {
requiredSymbols.add(symbol);
}
}
}
ShapedRecipe recipe = new ShapedRecipe(recipeKey, endSwordItem.create());
recipe.shape(rows.toArray(new String[0]));
Set<Character> mappedSymbols = new HashSet<>();
for (String symbolName : ingredients.getKeys(false)) {
if (symbolName.length() != 1) {
continue;
}
char symbol = symbolName.charAt(0);
String materialName = ingredients.getString(symbolName);
if (materialName == null) {
continue;
}
Material material = XMaterial.matchXMaterial(materialName)
.map(XMaterial::parseMaterial)
.orElse(null);
if (material != null && requiredSymbols.contains(symbol)) {
recipe.setIngredient(symbol, material);
mappedSymbols.add(symbol);
}
}
if (!mappedSymbols.containsAll(requiredSymbols)) {
getLogger().warning("The End Sword recipe is missing a valid material for one or more symbols.");
return;
}
Bukkit.addRecipe(recipe);
}
public EndSwordItem getEndSwordItem() {
return endSwordItem;
}
}