Skip to content

Commit

Permalink
Anvil inventory support...
Browse files Browse the repository at this point in the history
  • Loading branch information
bensku committed Mar 30, 2016
1 parent 6a3e78f commit be3bb5b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -573,14 +573,7 @@ public Block get(final PlayerInteractEvent e) {
@Override
@Nullable
public ItemStack get(final PlayerInteractEvent e) {
if (offHandSupport) {
EquipmentSlot hand = e.getHand();
if (hand == EquipmentSlot.HAND) return e.getPlayer().getInventory().getItemInMainHand();
else if (hand == EquipmentSlot.OFF_HAND) return e.getPlayer().getInventory().getItemInOffHand();
else return null;
} else {
return e.getPlayer().getItemInHand();
}
return e.getItem();
}
}, 0);
// PlayerShearEntityEvent
Expand Down
41 changes: 35 additions & 6 deletions src/main/java/ch/njol/skript/effects/EffOpenInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

package ch.njol.skript.effects;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -44,29 +46,47 @@
"Please note that currently 'show' and 'open' have the same effect, but 'show' will eventually show an unmodifiable view of the inventory in the future."})
@Examples({"show the victim's inventory to the player",
"open the player's inventory for the player"})
@Since("2.0, 2.1.1 (closing)")
@Since("2.0, 2.1.1 (closing), 2.2-Fixes-V10 (anvil)")
public class EffOpenInventory extends Effect {

private final static int WORKBENCH = 0, CHEST = 1, ANVIL = 2;

static {
Skript.registerEffect(EffOpenInventory.class,
"(0¦open|1¦show) ((crafting [table]|workbench) (view|window|inventory|)|%-inventory%) (to|for) %players%",
"(0¦open|1¦show) ((20¦(crafting [table]|workbench)|40¦chest|60¦anvil) (view|window|inventory|)|%-inventory%) (to|for) %players%",
"close [the] inventory [view] (to|of|for) %players%", "close %players%'[s] inventory [view]");
}

@Nullable
private Expression<Inventory> invi;

boolean open;
private int invType;

@SuppressWarnings("null")
private Expression<Player> players;

@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
int openFlag = 0;
if (parseResult.mark >= 60) {
openFlag = parseResult.mark ^ 60;
invType = ANVIL;
} else if (parseResult.mark >= 40) {
openFlag = parseResult.mark ^ 40;
invType = CHEST;
} else if (parseResult.mark >= 20) {
invType = WORKBENCH;
openFlag = parseResult.mark ^ 20;
} else {
openFlag = parseResult.mark;
}

open = matchedPattern == 0;
invi = open ? (Expression<Inventory>) exprs[0] : null;
players = (Expression<Player>) exprs[exprs.length - 1];
if (parseResult.mark == 1 && invi != null) {
if (openFlag == 1 && invi != null) {
Skript.warning("Using 'show' inventory instead of 'open' is not recommended as it will eventually show an unmodifiable view of the inventory in the future.");
}
return true;
Expand All @@ -83,9 +103,18 @@ protected void execute(final Event e) {
}
} else {
for (final Player p : players.getArray(e)) {
if (open)
p.openWorkbench(null, true);
else
if (open) {
switch (invType) {
case WORKBENCH:
p.openWorkbench(null, true);
break;
case CHEST:
p.openInventory(Bukkit.createInventory(p, InventoryType.CHEST));
break;
case ANVIL:
p.openInventory(Bukkit.createInventory(p, InventoryType.ANVIL));
}
} else
p.closeInventory();
}
}
Expand Down

0 comments on commit be3bb5b

Please sign in to comment.