Skip to content

Commit

Permalink
Recipes in GUI don't show up if they can't actually be crafted.
Browse files Browse the repository at this point in the history
Call CraftItemEvent when an item is crafted.
  • Loading branch information
BenWoodworth committed Mar 30, 2016
1 parent 184b7ec commit ad52113
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 61 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>co.kepler.fastcraftplus</groupId>
<artifactId>fastcraftplus</artifactId>
<version>0.3</version>
<version>0.4</version>

<build>
<plugins>
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/co/kepler/fastcraftplus/FastCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
import co.kepler.fastcraftplus.craftgui.GUIFastCraft;
import co.kepler.fastcraftplus.crafting.CraftingListener;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.logging.Level;

public class FastCraft extends JavaPlugin implements Listener {
public class FastCraft extends JavaPlugin {
private static FastCraft instance;

private Config config;
Expand Down Expand Up @@ -74,8 +73,7 @@ public void onEnable() {

PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(new CraftingListener(), this);

GUIFastCraft.init();
pluginManager.registerEvents(new GUIFastCraft.GUIListener(), this);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/co/kepler/fastcraftplus/config/Recipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void loadRecipes() {

// Remove loaded recipes
for (FCRecipe fcRecipe : recipes) {
for (Iterator<Recipe> iter = Bukkit.recipeIterator(); iter.hasNext();) {
for (Iterator<Recipe> iter = Bukkit.recipeIterator(); iter.hasNext(); ) {
Recipe recipe = iter.next();
if (RecipeUtil.areEqual(recipe, fcRecipe.getRecipe())) {
iter.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public GUIButtonRecipe(GUIFastCraft gui, GUIRecipe recipe) {
super();
this.gui = gui;
this.recipe = recipe;
}

@Override
public ItemStack getItem() {
// Add the ingredients to the lore of the item
ItemStack item = recipe.getResult().clone();
ItemStack item = recipe.getCraftingResult(gui).clone();
ItemMeta meta = item.getItemMeta();
LinkedList<String> lore = new LinkedList<>();
Map<Ingredient, Integer> ingredients = recipe.getIngredients();
Expand All @@ -58,8 +61,8 @@ public GUIButtonRecipe(GUIFastCraft gui, GUIRecipe recipe) {
meta.setLore(lore);
item.setItemMeta(meta);

// Set the item
setItem(item);
// Return the item
return item;
}

/**
Expand All @@ -79,7 +82,7 @@ public void setClickAction(ClickAction clickAction) {
*/
@Override
public boolean isVisible() {
return recipe.canCraft(gui.getPlayer(), false);
return recipe.canCraft(gui);
}

/**
Expand All @@ -101,7 +104,9 @@ public void setVisible(boolean visible) {
@Override
public boolean onClick(Layout layout, InventoryClickEvent invEvent) {
if (ignoreClicks.contains(invEvent.getClick())) return false;
if (!recipe.canCraft(gui.getPlayer(), true)) {

List<ItemStack> results = new ArrayList<>();
if (!recipe.canCraft(gui, true, results)) {
gui.updateLayout();
return false;
}
Expand All @@ -110,14 +115,15 @@ public boolean onClick(Layout layout, InventoryClickEvent invEvent) {
case DROP:
case CONTROL_DROP:
// Drop items on the ground.
for (ItemStack is : recipe.getResults()) {
for (ItemStack is : results) {
invEvent.getView().setItem(InventoryView.OUTSIDE, is);
}
break;
default:
// Add to inventory. Drop rest on ground if not enough space.
Inventory inv = gui.getPlayer().getInventory();
for (ItemStack is : inv.addItem(recipe.getResults()).values()) {
ItemStack[] resultsArr = new ItemStack[results.size()];
for (ItemStack is : inv.addItem(results.toArray(resultsArr)).values()) {
invEvent.getView().setItem(InventoryView.OUTSIDE, is);
}
break;
Expand Down
66 changes: 55 additions & 11 deletions src/main/java/co/kepler/fastcraftplus/craftgui/GUIFastCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import co.kepler.fastcraftplus.FastCraft;
import co.kepler.fastcraftplus.api.gui.*;
import co.kepler.fastcraftplus.crafting.CraftingInvWrapper;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.HumanEntity;
Expand All @@ -16,9 +17,10 @@
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.LeatherArmorMeta;

import java.util.HashMap;
Expand All @@ -31,12 +33,15 @@ public class GUIFastCraft extends GUI {
private static final ChatColor BUTTON_NAME_COLOR = ChatColor.GREEN;
private static final String NOT_YET_IMPLEMENTED = ChatColor.RED + "Not Yet Implemented";

private static Map<Object, GUIFastCraft> guis; // <Location or UUID, GUIFastCraft>
private static Map<Object, GUIFastCraft> guis = new HashMap<>(); // <Location or UUID, GUIFastCraft>

private final LayoutFastCraft craftLayout;
private final Player player;
private final Location location;

private final CraftingInvWrapper craftingInventory;
private final InventoryView craftingInventoryView;

private final GUIButton btnPagePrev;
private final GUIButton btnPageNext;
private final GUIButton btnRefresh;
Expand All @@ -52,16 +57,39 @@ public class GUIFastCraft extends GUI {
* @param player The player who will be shown this GUI.
*/
@SuppressWarnings("all")
public GUIFastCraft(Player player, Location location) {
super(FastCraft.lang().gui.title(), 6); // TODO Localize
public GUIFastCraft(final Player player, Location location) {
super(FastCraft.lang().gui.title(), 6);

this.player = player;
this.location = location;

craftingInventory = new CraftingInvWrapper(player);
craftingInventoryView = new InventoryView() {
@Override
public Inventory getTopInventory() {
return craftingInventory;
}

@Override
public Inventory getBottomInventory() {
return player.getInventory();
}

@Override
public HumanEntity getPlayer() {
return player;
}

@Override
public InventoryType getType() {
return craftingInventory.getType();
}
};

craftLayout = new LayoutFastCraft(this);
setLayout(craftLayout);

// Create Previous Page button // TODO Localize
// Create Previous Page button
btnPagePrev = new GUIButton(new GUIItemBuilder(Material.ARROW)
.setDisplayName(FastCraft.lang().gui.toolbar.pagePrev.title())
.setLore(FastCraft.lang().gui.toolbar.pagePrev.description(
Expand Down Expand Up @@ -185,11 +213,6 @@ public boolean onClick(GUIButton.Click info) {
guis.put(location, this);
}

public static void init() {
Bukkit.getPluginManager().registerEvents(new GUIListener(), FastCraft.getInstance());
guis = new HashMap<>();
}

@Override
public void show(Player... players) {
assert players.length == 1 && players[0].equals(player) :
Expand Down Expand Up @@ -236,6 +259,27 @@ public Player getPlayer() {
return player;
}

/**
* Get this GUI's crafting inventory.
*
* @return Returns this GUI's crafting inventory.
*/
public CraftingInventory getCraftingInventory(Recipe recipe, ItemStack[] matrix, ItemStack result) {
craftingInventory.setRecipe(recipe);
craftingInventory.setMatrix(matrix);
craftingInventory.setResult(result);
return craftingInventory;
}

/**
* Get this GUI's crafting inventory view.
*
* @return Returns this GUI's crafting inventory view.
*/
public InventoryView getCraftingInventoryView() {
return craftingInventoryView;
}

/**
* Show a tab in the GUI.
*
Expand Down Expand Up @@ -274,7 +318,7 @@ private boolean btnCraftingMultiplierClick(GUIButton.Click info) {
}

private boolean btnWorkbenchClick(GUIButton.Click info) {
player.openWorkbench(location, true); // TODO Don't force
player.openWorkbench(location, false);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public LayoutRecipes(GUIFastCraft gui) {
protected void addRecipes(List<GUIRecipe> recipes) {
for (GUIRecipe r : recipes) {
// If the button is already in the gui, or if it can't be crafted, continue.
if (activeRecipes.contains(r) || !r.canCraft(gui.getPlayer(), false)) continue;
if (activeRecipes.contains(r) || !r.canCraft(gui)) continue;

// Create the button, and add it to the GUI.
GUIButtonRecipe button = new GUIButtonRecipe(gui, r);
Expand Down
Loading

0 comments on commit ad52113

Please sign in to comment.