Skip to content

Commit

Permalink
Added support for intercepting items sent as equipment.
Browse files Browse the repository at this point in the history
I didn't anticipate this feature in 2.0.0, so I had to make my own 
equipment inventory to fit into the InventoryView that I normally
pass along to the API clients.
  • Loading branch information
aadnk committed Oct 19, 2013
1 parent 7a66bf3 commit e4d03e8
Show file tree
Hide file tree
Showing 6 changed files with 627 additions and 13 deletions.
32 changes: 25 additions & 7 deletions src/main/java/org/shininet/bukkit/itemrenamer/RenameProcessor.java
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.shininet.bukkit.itemrenamer.api.ItemsListener;
import org.shininet.bukkit.itemrenamer.api.RenamerSnapshot;
Expand Down Expand Up @@ -103,8 +104,22 @@ private void packLore(NiceItemMeta itemMeta, RenameRule rule) {
* @return The processed item stack.
*/
public ItemStack process(Player player, ItemStack input, int offset) {
return process(player, player.getOpenInventory(), input, offset);
}

/**
* Apply a player's associated rename rules to a given stack.
* <p>
* The rename rules are referenced by the world the player is in or by the player itself.
* @param player - the player.
* @param InventoyView - the current inventory view.
* @param input - the item to rename.
* @param slotIndex - index of the item in the inventory view we want to rename.
* @return The processed item stack.
*/
public ItemStack process(Player player, InventoryView view, ItemStack input, int offset) {
ItemStack[] temporary = new ItemStack[] { input };
return process(player, getPack(player), temporary, offset)[0];
return process(player, view, getPack(player), temporary, offset)[0];
}

/**
Expand All @@ -117,19 +132,20 @@ public ItemStack[] process(Player player, ItemStack[] input) {
String pack = getPack(player);

if (input != null) {
return process(player, pack, input, 0);
return process(player, player.getOpenInventory(), pack, input, 0);
}
return null;
}

/**
* Apply rename rules to a given item stack.
* @param pack - the current rename package.
* @param view - the current inventory view.
* @param input - the item to process.
* @param offset - the current offset.
* @return The processed item.
*/
private ItemStack[] process(Player player, String pack, ItemStack[] input, int offset) {
private ItemStack[] process(Player player, InventoryView view, String pack, ItemStack[] input, int offset) {
RenameRule[] rules = new RenameRule[input.length];

// Retrieve the rename rule for each item stack
Expand All @@ -138,7 +154,7 @@ private ItemStack[] process(Player player, String pack, ItemStack[] input, int o
}

// Just return it - for chaining
return processRules(player, input, rules, offset);
return processRules(player, view, input, rules, offset);
}

/**
Expand Down Expand Up @@ -216,12 +232,14 @@ public ItemStack processRule(ItemStack stack, RenameRule rule) {

/**
* Rename or append lore to the given item stack.
* @param player - the current player.
* @param input - the item stack.
* @param rule - the rename rule to apply.
* @param rules - the rename rules to apply.
* @param offset - offset to the items we are renaming in the inventory view.
* @return The renamed item stacks.
*/
private ItemStack[] processRules(Player player, ItemStack[] inputs, final RenameRule[] rules, final int offset) {
RenamerSnapshot snapshot = new RenamerSnapshot(inputs, player.getOpenInventory(), offset);
private ItemStack[] processRules(Player player, InventoryView view, ItemStack[] inputs, final RenameRule[] rules, final int offset) {
RenamerSnapshot snapshot = new RenamerSnapshot(inputs, view, offset);

// Save the original NBT tag
NbtCompound[] original = new NbtCompound[inputs.length];
Expand Down
Expand Up @@ -108,6 +108,8 @@ public void apply(Function<ItemStack, ItemStack> action) {

/**
* Retrieve the index of the first slot in the inventory view that matches this snapshot.
* <p>
* This is negative if we're dealing with the cursor item.
* @return The snapshot offset.
*/
public int getInventoryOffset() {
Expand Down

0 comments on commit e4d03e8

Please sign in to comment.