Files
examples
src
build.gradle
settings.gradle
Code
23 1,449
src/main/java/com/customblocks/CustomBlocksPlugin.java
package com.customblocks;
import com.customblocks.block.BlockRegistry;
import com.customblocks.block.CustomBlock;
import com.customblocks.command.BlocksCommand;
import com.customblocks.command.GiveBlockCommand;
import com.customblocks.config.BlockConfigLoader;
import com.customblocks.listener.BlockBreakListener;
import com.customblocks.listener.BlockInteractListener;
import com.customblocks.listener.BlockPlaceListener;
import com.customblocks.listener.ChunkListener;
import com.customblocks.model.BlockDisplayManager;
import com.customblocks.util.BlockItemUtil;
import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Iterator;
/**
* Main plugin class for CustomBlocksAPI.
* Handles initialization, recipe registration, and command registration.
*/
public class CustomBlocksPlugin extends JavaPlugin {
private BlockDisplayManager displayManager;
private BlockConfigLoader configLoader;
@Override
public void onEnable() {
// Initialize utilities
BlockItemUtil.init(this);
// Create data folder
if (!getDataFolder().exists()) {
getDataFolder().mkdirs();
}
// Initialize managers
this.displayManager = new BlockDisplayManager(this);
this.configLoader = new BlockConfigLoader(this);
// Load custom block definitions
configLoader.loadAll();
// Register recipes
registerRecipes();
// Register commands
GiveBlockCommand giveCommand = new GiveBlockCommand();
getCommand("givecustomblock").setExecutor(giveCommand);
getCommand("givecustomblock").setTabCompleter(giveCommand);
BlocksCommand blocksCommand = new BlocksCommand();
getCommand("customblocks").setExecutor(blocksCommand);
getCommand("customblocks").setTabCompleter(blocksCommand);
// Register listeners
Bukkit.getPluginManager().registerEvents(new BlockPlaceListener(displayManager), this);
Bukkit.getPluginManager().registerEvents(new BlockBreakListener(displayManager), this);
Bukkit.getPluginManager().registerEvents(new BlockInteractListener(displayManager), this);
Bukkit.getPluginManager().registerEvents(new ChunkListener(displayManager), this);
getLogger().info("CustomBlocksAPI v" + getDescription().getVersion() + " enabled.");
getLogger().info("Loaded " + BlockRegistry.getInstance().getBlockCount() + " custom blocks.");
}
@Override
public void onDisable() {
// Clean up all ItemDisplay entities
if (displayManager != null) {
displayManager.cleanupAll();
}
// Remove custom recipes
Iterator<Recipe> it = Bukkit.recipeIterator();
while (it.hasNext()) {
Recipe recipe = it.next();
if (recipe instanceof Keyed keyed) {
if (keyed.getKey().getNamespace().equals(getName().toLowerCase())) {
it.remove();
}
}
}
getLogger().info("CustomBlocksAPI disabled.");
}
/**
* Registers crafting recipes for all blocks that have craft.enabled = true.
*/
private void registerRecipes() {
for (CustomBlock block : BlockRegistry.getInstance().getAllBlocks()) {
if (!block.isCraftEnabled()) continue;
if (block.getCraftShape() == null || block.getCraftShape().isEmpty()) continue;
NamespacedKey key = new NamespacedKey(this, "custom_block_" + block.getId());
ShapedRecipe recipe = new ShapedRecipe(key, BlockItemUtil.createItem(block, block.getCraftResultAmount()));
recipe.shape(block.getCraftShape().toArray(new String[0]));
block.getCraftIngredients().forEach((character, materialName) -> {
org.bukkit.Material material = org.bukkit.Material.getMaterial(materialName.toUpperCase());
if (material != null && material.isItem()) {
recipe.setIngredient(character, material);
} else {
getLogger().warning("Invalid ingredient material '" + materialName
+ "' for block " + block.getId());
}
});
Bukkit.addRecipe(recipe);
getLogger().fine("Registered recipe for: " + block.getId());
}
}
/**
* Reloads all block configs and recipes.
* Called from admin command or console.
*/
public void reload() {
// Remove old recipes
Iterator<Recipe> it = Bukkit.recipeIterator();
while (it.hasNext()) {
Recipe recipe = it.next();
if (recipe instanceof Keyed keyed) {
if (keyed.getKey().getNamespace().equals(getName().toLowerCase())) {
it.remove();
}
}
}
// Clean up displays
displayManager.cleanupAll();
// Reload configs
configLoader.loadAll();
// Re-register recipes
registerRecipes();
getLogger().info("Reloaded " + BlockRegistry.getInstance().getBlockCount() + " custom blocks.");
}
}