Files
src
pom.xml
Code
9 1,165
src/main/java/com/kodari/foundry/FoundryPlugin.java
package com.kodari.foundry;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.data.Levelled;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public final class FoundryPlugin extends JavaPlugin implements Listener {
private CustomItems items;
private ToolService tools;
private NamespacedKey entityKey;
@Override
public void onEnable() {
saveDefaultConfig();
this.entityKey = new NamespacedKey(this, "cropper_villager");
this.items = new CustomItems(this);
this.tools = new ToolService(this, items);
getServer().getPluginManager().registerEvents(new FoundryListener(this), this);
getCommand("foundry").setExecutor(new FoundryCommand(this));
Bukkit.getScheduler().runTaskTimer(this, tools::refreshHeldEffects, 20L, 20L);
Bukkit.getScheduler().runTaskTimer(this, tools::applyLeadPoisoningDamage, 20L, 20L);
}
public CustomItems items() {
return items;
}
public ToolService tools() {
return tools;
}
public NamespacedKey entityKey() {
return entityKey;
}
public org.bukkit.inventory.MerchantRecipe cropperRecipe() {
Material firstMaterial = material(getConfig().getString("cropper-trade.ingredient-one", "WHEAT"));
Material secondMaterial = material(getConfig().getString("cropper-trade.ingredient-two", "CARROT"));
Material resultMaterial = material(getConfig().getString("cropper-trade.result", "EMERALD"));
if (firstMaterial == null || secondMaterial == null || resultMaterial == null) {
throw new IllegalStateException("Cropper trade configuration contains an invalid material.");
}
ItemStack result = new ItemStack(resultMaterial, getConfig().getInt("cropper-trade.result-amount", 1));
org.bukkit.inventory.MerchantRecipe recipe = new org.bukkit.inventory.MerchantRecipe(result, 999999);
recipe.addIngredient(new ItemStack(firstMaterial, getConfig().getInt("cropper-trade.ingredient-one-amount", 64)));
recipe.addIngredient(new ItemStack(secondMaterial, getConfig().getInt("cropper-trade.ingredient-two-amount", 64)));
return recipe;
}
public Material material(String name) {
return XMaterial.matchXMaterial(name.toUpperCase(Locale.ROOT))
.map(XMaterial::parseMaterial)
.orElse(null);
}
public boolean isType(Block block, String materialName) {
Material material = material(materialName);
return material != null && block.getType().equals(material);
}
public boolean isSmeltery(Block core) {
if (!isType(core, "CAULDRON") && !isType(core, "LAVA_CAULDRON")) {
return false;
}
int[][] directions = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
for (int[] front : directions) {
boolean matches = core.getRelative(front[0], 0, front[1]).getType().isAir();
for (int x = -1; x <= 1 && matches; x++) {
for (int z = -1; z <= 1; z++) {
if (x == 0 && z == 0 || x == front[0] && z == front[1]) {
continue;
}
matches = isType(core.getRelative(x, 0, z), "BRICKS");
if (!matches) {
break;
}
}
}
if (matches) {
return true;
}
}
return false;
}
public boolean isForge(Block core) {
if (!isType(core, "ANVIL")) {
return false;
}
int[][] directions = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
for (int[] behind : directions) {
int sideX = -behind[1];
int sideZ = behind[0];
boolean matches = isType(core.getRelative(sideX, 0, sideZ), "DIAMOND_BLOCK")
&& isType(core.getRelative(-sideX, 0, -sideZ), "DIAMOND_BLOCK");
for (int side = -1; side <= 1 && matches; side++) {
Block lower = core.getRelative(behind[0] + sideX * side, 0, behind[1] + sideZ * side);
matches = isType(lower, "IRON_BARS") && isType(lower.getRelative(0, 1, 0), "IRON_BARS");
}
if (matches) {
return true;
}
}
return false;
}
public boolean hasFullLava(Block core) {
if (!isType(core, "LAVA_CAULDRON")) {
return false;
}
return !(core.getBlockData() instanceof Levelled levelled) || levelled.getLevel() == levelled.getMaximumLevel();
}
public void openStation(Player player, Block core, StationType station) {
List<Recipe> recipes = station == StationType.SMELTERY ? smelteryRecipes() : forgeRecipes();
StationMenuHolder holder = new StationMenuHolder(core.getLocation(), station, recipes);
Inventory inventory = Bukkit.createInventory(holder, 54, station == StationType.SMELTERY ? "§6Foundry Smeltery" : "§8Foundry Forge");
holder.setInventory(inventory);
int[] slots = {10, 12, 14, 16, 28, 30};
for (int index = 0; index < recipes.size(); index++) {
Recipe recipe = recipes.get(index);
ItemStack icon = recipe.output().clone();
ItemMeta meta = icon.getItemMeta();
List<String> lore = new ArrayList<>();
lore.add("§7" + recipe.description());
lore.add("§8Ingredients: " + recipe.ingredientsText());
if (station == StationType.SMELTERY) {
lore.add("§cRequires one full lava cauldron");
}
meta.setLore(lore);
icon.setItemMeta(meta);
inventory.setItem(slots[index], icon);
}
player.openInventory(inventory);
}
public void craft(Player player, StationMenuHolder holder, int slot) {
int[] slots = {10, 12, 14, 16, 28, 30};
int recipeIndex = -1;
for (int i = 0; i < slots.length; i++) {
if (slots[i] == slot) {
recipeIndex = i;
break;
}
}
if (recipeIndex < 0 || recipeIndex >= holder.recipes().size()) {
return;
}
Block core = holder.location().getBlock();
if (holder.station() == StationType.SMELTERY && (!isSmeltery(core) || !hasFullLava(core))) {
player.sendMessage("§cThe Smeltery needs its complete structure and a full lava cauldron.");
return;
}
if (holder.station() == StationType.FORGE && !isForge(core)) {
player.sendMessage("§cThe Forge structure is incomplete.");
return;
}
Recipe recipe = holder.recipes().get(recipeIndex);
if (!hasIngredients(player, recipe.ingredients())) {
player.sendMessage("§cYou do not have the required ingredients.");
return;
}
removeIngredients(player, recipe.ingredients());
if (holder.station() == StationType.SMELTERY) {
core.setType(material("CAULDRON"));
}
Map<Integer, ItemStack> overflow = player.getInventory().addItem(recipe.output().clone());
overflow.values().forEach(item -> player.getWorld().dropItemNaturally(player.getLocation(), item));
player.sendMessage("§aCrafted " + recipe.name() + "!");
}
private boolean hasIngredients(Player player, List<Ingredient> ingredients) {
for (Ingredient ingredient : ingredients) {
int found = 0;
for (ItemStack stack : player.getInventory().getStorageContents()) {
if (matchesIngredient(stack, ingredient)) {
found += stack.getAmount();
}
}
if (found < ingredient.amount()) {
return false;
}
}