use of org.bukkit.inventory.Inventory in project BKCommonLib by bergerhealer.
the class CommonEntity method replaceEntity.
@SuppressWarnings("unchecked")
private void replaceEntity(final EntityHandle newInstance) {
final EntityHandle oldInstance = this.handle;
if (oldInstance.getRaw() == newInstance.getRaw()) {
throw new RuntimeException("Can not replace an entity with itself!");
}
// Reset entity state
oldInstance.setDead(true);
newInstance.setDead(false);
oldInstance.setValid(false);
newInstance.setValid(true);
// *** Bukkit Entity ***
CraftEntityHandle craftEntity = CraftEntityHandle.createHandle(this.entity);
craftEntity.setHandle(newInstance);
if (entity instanceof InventoryHolder) {
Inventory inv = ((InventoryHolder) entity).getInventory();
if (CraftInventoryHandle.T.isAssignableFrom(inv)) {
CraftInventoryHandle cInv = CraftInventoryHandle.createHandle(inv);
if (IInventoryHandle.T.isAssignableFrom(newInstance.getRaw())) {
IInventoryHandle iinvHandle = IInventoryHandle.createHandle(newInstance.getRaw());
cInv.setHandleField(iinvHandle);
}
}
}
// *** Give the old entity a new Bukkit Entity ***
oldInstance.setBukkitEntityField(CraftEntityHandle.createCraftEntity(Bukkit.getServer(), oldInstance));
this.handle = newInstance;
// *** Replace entity in passenger and vehicle fields ***
EntityHandle vehicle = newInstance.getVehicle();
if (vehicle != null) {
replaceInList(vehicle.getPassengers(), newInstance);
}
if (newInstance.getPassengers() != null) {
for (EntityHandle passenger : newInstance.getPassengers()) {
if (oldInstance.getRaw() == passenger.getVehicle().getRaw()) {
passenger.setVehicle(newInstance);
}
}
}
// *** DataWatcher field of the old Entity ***
Object dataWatcher = EntityHandle.T.datawatcherField.raw.get(newInstance.getRaw());
if (dataWatcher != null) {
DataWatcherHandle.T.owner.set(dataWatcher, newInstance);
}
// *** Perform further replacement all over the place in the server ***
replaceEntityInServer(oldInstance, newInstance);
// *** Repeat the replacement in the server the next tick to make sure nothing lingers ***
CommonUtil.nextTick(new Runnable() {
@Override
public void run() {
replaceEntityInServer(oldInstance, newInstance);
}
});
// *** Make sure a controller is set when hooked ***
if (this.isHooked()) {
DefaultEntityController controller = new DefaultEntityController();
controller.bind(this);
}
}
use of org.bukkit.inventory.Inventory in project BKCommonLib by bergerhealer.
the class CraftRecipe method craft.
/**
* Performs this recipe multiple times in the inventory specified
*
* @param inventory to craft in
* @param limit the amount of times it can craft
* @return the amount of times it crafted
*/
public int craft(Inventory inventory, int limit) {
// Before cloning everything, check whether we can craft at all
if (!this.containsInput(inventory)) {
return 0;
}
// Create a (temporary) clone of the inventory to work with
final ItemStack[] items = inventory.getContents();
final int size = items.length;
final Inventory inventoryClone = new InventoryBaseImpl(items, true);
int amount, i;
// Below is the craftloop label, which is used to break out of crafting
craftloop: for (amount = 0; amount < limit; amount++) {
// input item check
if (!this.containsInput(inventoryClone)) {
break;
}
// remove ingredients from inventory
for (ItemStack item : this.input) {
ItemUtil.removeItems(inventoryClone, item);
}
// add resulting items to inventory
for (ItemStack item : this.output) {
ItemStack cloned = ItemUtil.cloneItem(item);
ItemUtil.transfer(cloned, inventoryClone, Integer.MAX_VALUE);
// Could not add result (full), unsuccessful
if (!LogicUtil.nullOrEmpty(cloned)) {
break craftloop;
}
}
// Be sure NOT to produce new ItemStack instances!
for (i = 0; i < size; i++) {
ItemStack newItem = inventoryClone.getItem(i);
if (LogicUtil.nullOrEmpty(newItem)) {
items[i] = null;
} else if (items[i] == null) {
items[i] = newItem.clone();
} else {
// Transfer info and amount
ItemUtil.transferInfo(newItem, items[i]);
items[i].setAmount(newItem.getAmount());
}
}
}
// Update input inventory with the new items
inventory.setContents(items);
return amount;
}
use of org.bukkit.inventory.Inventory in project Essentials by drtshock.
the class InventoryWorkaround method addOversizedItems.
// Returns what it couldn't store
// Set oversizedStack to below normal stack size to disable oversized stacks
public static Map<Integer, ItemStack> addOversizedItems(final Inventory inventory, final int oversizedStacks, final ItemStack... items) {
if (isCombinedInventory(inventory)) {
Inventory fakeInventory = makeTruncatedPlayerInventory((PlayerInventory) inventory);
Map<Integer, ItemStack> overflow = addOversizedItems(fakeInventory, oversizedStacks, items);
for (int i = 0; i < fakeInventory.getContents().length; i++) {
inventory.setItem(i, fakeInventory.getContents()[i]);
}
return overflow;
}
final Map<Integer, ItemStack> leftover = new HashMap<>();
/*
* TODO: some optimization - Create a 'firstPartial' with a 'fromIndex' - Record the lastPartial per Material -
* Cache firstEmpty result
*/
// combine items
final ItemStack[] combined = new ItemStack[items.length];
for (ItemStack item : items) {
if (item == null || item.getAmount() < 1) {
continue;
}
for (int j = 0; j < combined.length; j++) {
if (combined[j] == null) {
combined[j] = item.clone();
break;
}
if (combined[j].isSimilar(item)) {
combined[j].setAmount(combined[j].getAmount() + item.getAmount());
break;
}
}
}
for (int i = 0; i < combined.length; i++) {
final ItemStack item = combined[i];
if (item == null || item.getType() == Material.AIR) {
continue;
}
while (true) {
// Do we already have a stack of it?
final int maxAmount = oversizedStacks > item.getType().getMaxStackSize() ? oversizedStacks : item.getType().getMaxStackSize();
final int firstPartial = firstPartial(inventory, item, maxAmount);
// Drat! no partial stack
if (firstPartial == -1) {
// Find a free spot!
final int firstFree = inventory.firstEmpty();
if (firstFree == -1) {
// No space at all!
leftover.put(i, item);
break;
} else {
// More than a single stack!
if (item.getAmount() > maxAmount) {
final ItemStack stack = item.clone();
stack.setAmount(maxAmount);
inventory.setItem(firstFree, stack);
item.setAmount(item.getAmount() - maxAmount);
} else {
// Just store it
inventory.setItem(firstFree, item);
break;
}
}
} else {
// So, apparently it might only partially fit, well lets do just that
final ItemStack partialItem = inventory.getItem(firstPartial);
final int amount = item.getAmount();
final int partialAmount = partialItem.getAmount();
// Check if it fully fits
if (amount + partialAmount <= maxAmount) {
partialItem.setAmount(amount + partialAmount);
break;
}
// It fits partially
partialItem.setAmount(maxAmount);
item.setAmount(amount + partialAmount - maxAmount);
}
}
}
return leftover;
}
use of org.bukkit.inventory.Inventory in project Essentials by drtshock.
the class EssentialsPlayerListener method onInventoryClickEvent.
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onInventoryClickEvent(final InventoryClickEvent event) {
Player refreshPlayer = null;
final Inventory top = event.getView().getTopInventory();
final InventoryType type = top.getType();
if (type == InventoryType.PLAYER) {
final User user = ess.getUser((Player) event.getWhoClicked());
final InventoryHolder invHolder = top.getHolder();
if (invHolder != null && invHolder instanceof HumanEntity) {
final User invOwner = ess.getUser((Player) invHolder);
if (user.isInvSee() && (!user.isAuthorized("essentials.invsee.modify") || invOwner.isAuthorized("essentials.invsee.preventmodify") || !invOwner.getBase().isOnline())) {
event.setCancelled(true);
refreshPlayer = user.getBase();
}
}
} else if (type == InventoryType.ENDER_CHEST) {
final User user = ess.getUser((Player) event.getWhoClicked());
if (user.isEnderSee() && (!user.isAuthorized("essentials.enderchest.modify"))) {
event.setCancelled(true);
refreshPlayer = user.getBase();
}
} else if (type == InventoryType.WORKBENCH) {
User user = ess.getUser((Player) event.getWhoClicked());
if (user.isRecipeSee()) {
event.setCancelled(true);
refreshPlayer = user.getBase();
}
} else if (type == InventoryType.CHEST && top.getSize() == 9) {
final User user = ess.getUser((Player) event.getWhoClicked());
final InventoryHolder invHolder = top.getHolder();
if (invHolder != null && invHolder instanceof HumanEntity && user.isInvSee()) {
event.setCancelled(true);
refreshPlayer = user.getBase();
}
}
if (refreshPlayer != null) {
final Player player = refreshPlayer;
ess.scheduleSyncDelayedTask(new Runnable() {
@Override
public void run() {
player.updateInventory();
}
}, 1);
}
}
use of org.bukkit.inventory.Inventory in project HawkEye by oliverwoodings.
the class ContainerEntry method rollback.
@Override
public boolean rollback(Block block) {
if (!(block instanceof InventoryHolder))
return false;
Inventory inv = ((InventoryHolder) block.getState()).getInventory();
List<HashMap<String, Integer>> ops = InventoryUtil.interpretDifferenceString(data);
//Handle the additions
if (ops.size() > 0) {
for (ItemStack stack : InventoryUtil.uncompressInventory(ops.get(0))) inv.removeItem(stack);
}
//Handle subtractions
if (ops.size() > 1) {
for (ItemStack stack : InventoryUtil.uncompressInventory(ops.get(1))) inv.addItem(stack);
}
return true;
}
Aggregations