Skip to content

Instantly share code, notes, and snippets.

@mlow
Last active December 14, 2015 21:59
Show Gist options
  • Save mlow/5155024 to your computer and use it in GitHub Desktop.
Save mlow/5155024 to your computer and use it in GitHub Desktop.
A craft command to get around the craft bench not working in protocol enabled version of spigot
import com.sk89q.commandbook.CommandBook;
import com.sk89q.commandbook.util.PlayerUtil;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.zachsthings.libcomponents.ComponentInformation;
import com.zachsthings.libcomponents.bukkit.BukkitComponent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
@ComponentInformation(friendlyName = "CraftFix",
desc = "Allow players to craft items with a command")
public class CraftFix extends BukkitComponent {
@Override
public void enable() {
registerCommands(Commands.class);
}
public class Commands {
@Command(aliases = {"craft"}, usage = "[item] (times-crafted)",
desc = "Craft items with a command", min = 1)
@CommandPermissions({"bookaddon.craft"})
public void craft(CommandContext args, CommandSender sender)
throws CommandException {
final Inventory inventory = PlayerUtil.checkPlayer(sender).getInventory();
ItemStack item = CommandBook.inst().getCommandItem(args.getString(0));
List<Recipe> recipes = Bukkit.getRecipesFor(item);
int amount = args.argsLength() >= 2 ? args.getInteger(1) : 1;
for (Recipe recipe : recipes) {
Collection<ItemStack> ingredients;
if (recipe instanceof ShapedRecipe) {
ShapedRecipe rec = (ShapedRecipe) recipe;
ingredients = rec.getIngredientMap().values();
} else if (recipe instanceof ShapelessRecipe) {
ShapelessRecipe rec = (ShapelessRecipe) recipe;
ingredients = rec.getIngredientList();
} else {
continue;
}
item = recipe.getResult().clone();
List<ItemStack> aggregate = new ArrayList();
outer:
for (ItemStack is : ingredients) {
if (is == null) {
continue;
}
for (ItemStack req : aggregate) {
if (is.isSimilar(req)) {
req.setAmount(req.getAmount() + 1);
continue outer;
}
}
aggregate.add(is.clone());
}
for (ItemStack ingredient : aggregate) {
if (ingredient.getDurability() == -1) {
ingredient.setDurability((short) 0);
}
ingredient.setAmount(ingredient.getAmount() * amount);
if (!inventory.containsAtLeast(ingredient, ingredient.getAmount())) {
sender.sendMessage(ChatColor.RED + "You don't have the required ingredients.");
return;
}
}
for (ItemStack ingredient : aggregate) {
sender.sendMessage(ingredient.serialize().toString());
inventory.removeItem(ingredient);
}
item.setAmount(item.getAmount() * amount);
inventory.addItem(item);
sender.sendMessage(ChatColor.YELLOW + "Crafted items!");
return;
}
sender.sendMessage(ChatColor.RED + "No recipes found for that item.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment