public int getNumBytes() {
return 34;
}
public void run(int playerId) {
SpoutPlayer p = SpoutManager.getPlayerFromId(playerId);
InGameHUD mainScreen = p.getMainScreen();
PopupScreen popup = mainScreen.getActivePopup();
Screen current = p.getCurrentScreen();
Screen in = null;
if (mainScreen != null && screen.equals(mainScreen.getId())) {
in = mainScreen;
}
if (popup != null && screen.equals(popup.getId())) {
in = popup;
}
if (current != null && screen.equals(current.getId())) {
in = current;
}
if (in == null) {
return;
}
if (!in.containsWidget(slot)) {
return;
}
// Slot handling code goes here.
Slot slot = (Slot) in.getWidget(this.slot);
try {
ItemStack stackOnCursor = p.getItemOnCursor();
if (stackOnCursor == null) {
stackOnCursor = new ItemStack(0);
}
ItemStack stackInSlot = slot.getItem();
if ((stackOnCursor == null || stackOnCursor.getTypeId() == 0) && stackInSlot.getTypeId() == 0) {
return; // Nothing to do
}
if (stackOnCursor.getTypeId() == 0 && stackInSlot.getTypeId() != 0 && button == 1) { // Split item
int amountSlot = stackInSlot.getAmount() / 2;
int amountCursor = stackInSlot.getAmount() - amountSlot;
if (stackInSlot.getAmount() == 1) {
amountSlot = 0;
amountCursor = 1;
}
stackOnCursor = stackInSlot.clone();
stackOnCursor.setAmount(amountCursor);
stackInSlot.setAmount(amountSlot);
if (amountSlot == 0) {
stackInSlot = new ItemStack(0);
}
SlotEvent s = new SlotTakeEvent(p, slot, stackInSlot, !slot.onItemTake(stackOnCursor));
Bukkit.getPluginManager().callEvent(s);
if (!s.isCancelled()) {
slot.setItem(stackInSlot);
} else {
slot.setDirty(true); // We need to tell the client that the operation was denied.
return;
}
} else if (stackOnCursor != null && (stackInSlot.getTypeId() == 0 || (stackInSlot.getTypeId() == stackOnCursor.getTypeId() && stackInSlot.getDurability() == stackOnCursor.getDurability()))) { // Put item
ItemStack toPut = stackOnCursor.clone();
int putAmount = toPut.getAmount();
if (button == 1) {
putAmount = 1;
}
int amount = stackInSlot.getTypeId() == 0 ? 0 : stackInSlot.getAmount();
amount += putAmount;
int maxStackSize = toPut.getMaxStackSize();
if (maxStackSize == -1) {
maxStackSize = 64;
}
if (amount > maxStackSize) {
putAmount -= amount - maxStackSize;
amount = maxStackSize;
}
if (putAmount <= 0) {
return;
}
toPut.setAmount(putAmount);
SlotEvent s = new SlotPutEvent(p, slot, stackInSlot, !slot.onItemPut(toPut));
Bukkit.getPluginManager().callEvent(s);
if (!s.isCancelled()) {
stackOnCursor.setAmount(stackOnCursor.getAmount() - putAmount);
if (stackOnCursor.getAmount() == 0) {
stackOnCursor = new ItemStack(0);
}
ItemStack put = toPut.clone();
put.setAmount(amount);
slot.setItem(put);
} else {
slot.setDirty(true); // We need to tell the client that the operation was denied.
}
} else if (stackOnCursor == null || stackOnCursor.getTypeId() == 0) { //Take item or shift click
if (holdingShift) {
slot.onItemShiftClicked();
SlotEvent s = new SlotShiftClickEvent(p, slot);
Bukkit.getPluginManager().callEvent(s);
} else { // Take item
SlotEvent s = new SlotTakeEvent(p, slot, stackInSlot, !slot.onItemTake(stackInSlot));
Bukkit.getPluginManager().callEvent(s);
if (!s.isCancelled()) {
stackOnCursor = stackInSlot;
slot.setItem(new ItemStack(0));
} else {
slot.setDirty(true); // We need to tell the client that the operation was denied.
}
}
} else if (stackOnCursor.getTypeId() != stackInSlot.getTypeId() || stackOnCursor.getDurability() != stackInSlot.getDurability()) { // Exchange slot stack and cursor stack
SlotEvent s = new SlotExchangeEvent(p, slot, stackInSlot, stackOnCursor.clone(), !slot.onItemExchange(stackInSlot, stackOnCursor.clone()));
Bukkit.getPluginManager().callEvent(s);
if (!s.isCancelled()) {
slot.setItem(stackOnCursor.clone());
stackOnCursor = stackInSlot;
} else {
slot.setDirty(true); // We need to tell the client that the operation was denied.
}
}
if (stackOnCursor == null || stackOnCursor.getTypeId() == 0) {
p.setItemOnCursor(null);
} else {
p.setItemOnCursor(stackOnCursor);
}
} catch (Exception e) {
e.printStackTrace();
}
}