use of org.spongepowered.api.item.inventory.ItemStackSnapshot in project Skree by Skelril.
the class CursedMineListener method onItemDrop.
@Listener
public void onItemDrop(DropItemEvent.Destruct event, @Named(NamedCause.SOURCE) BlockSpawnCause spawnCause, @Named(NamedCause.NOTIFIER) Player player) {
if (!Probability.getChance(4)) {
return;
}
BlockSnapshot blockSnapshot = spawnCause.getBlockSnapshot();
Optional<Location<World>> optLocation = blockSnapshot.getLocation();
if (!optLocation.isPresent()) {
return;
}
Location<World> loc = optLocation.get();
Optional<CursedMineInstance> optInst = manager.getApplicableZone(loc);
if (!optInst.isPresent()) {
return;
}
CursedMineInstance inst = optInst.get();
if (!inst.hasrecordForPlayerAt(player, loc)) {
return;
}
List<ItemStackSnapshot> itemStacks = new ArrayList<>();
Iterator<Entity> entityIterator = event.getEntities().iterator();
while (entityIterator.hasNext()) {
Entity entity = entityIterator.next();
if (entity instanceof Item) {
ItemStackSnapshot snapshot = ((Item) entity).item().get();
itemStacks.add(snapshot);
entityIterator.remove();
}
}
int times = 1;
Optional<ModifierService> optService = Sponge.getServiceManager().provide(ModifierService.class);
if (optService.isPresent()) {
ModifierService service = optService.get();
if (service.isActive(Modifiers.DOUBLE_CURSED_ORES)) {
times *= 2;
}
}
for (ItemStackSnapshot stackSnapshot : itemStacks) {
int quantity = Math.min(stackSnapshot.getCount() * Probability.getRangedRandom(4, 8), stackSnapshot.getType().getMaxStackQuantity());
for (int i = 0; i < times; ++i) {
ItemStack stack = stackSnapshot.createStack();
stack.setQuantity(quantity);
player.getInventory().offer(stack);
}
}
}
use of org.spongepowered.api.item.inventory.ItemStackSnapshot in project Skree by Skelril.
the class MarketImplUtil method giveItems.
public static Clause<Boolean, List<Clause<ItemStack, Integer>>> giveItems(Player player, Collection<ItemStack> stacks, Cause cause) {
List<Clause<ItemStack, Integer>> transactions = new ArrayList<>(stacks.size());
List<ItemStackSnapshot> itemBuffer = new ArrayList<>();
itemBuffer.addAll(stacks.stream().map(ItemStack::createSnapshot).collect(Collectors.toList()));
PlayerInventory playerInventory = player.getInventory().query(PlayerInventory.class);
List<Inventory> inventories = new ArrayList<>();
inventories.add(playerInventory.getHotbar());
inventories.add(playerInventory.getMain());
// Loop through replacing empty space with the requested items
for (Inventory inventory : inventories) {
List<ItemStackSnapshot> newBuffer = new ArrayList<>();
for (ItemStackSnapshot snapshot : itemBuffer) {
ItemStack stack = snapshot.createStack();
InventoryTransactionResult result = inventory.offer(stack);
newBuffer.addAll(result.getRejectedItems());
transactions.add(new Clause<>(stack, stack.getQuantity()));
}
itemBuffer = newBuffer;
}
// Drop remaining items
new ItemDropper(player.getLocation()).dropStackSnapshots(itemBuffer, SpawnTypes.PLUGIN);
return new Clause<>(true, transactions);
}
use of org.spongepowered.api.item.inventory.ItemStackSnapshot in project TotalEconomy by Erigitic.
the class TEJobManager method onPlayerFish.
/**
* Used for the catch option in jobs. Will check if the job has the catch node and if it does it will check if the
* item that was caught is present in the config of the player's job. If it is, it will grab the job exp reward as
* well as the pay.
*
* @param event FishingEvent.Stop
*/
@Listener
public void onPlayerFish(FishingEvent.Stop event) {
if (event.getCause().first(Player.class).isPresent()) {
// no transaction, so execution can stop
if (event.getItemStackTransaction().size() == 0) {
return;
}
Transaction<ItemStackSnapshot> itemTransaction = event.getItemStackTransaction().get(0);
ItemStack itemStack = itemTransaction.getFinal().createStack();
Player player = event.getCause().first(Player.class).get();
UUID playerUUID = player.getUniqueId();
String playerJob = getPlayerJob(player);
Optional<TEJob> optPlayerJob = getJob(playerJob, true);
if (optPlayerJob.isPresent()) {
if (itemStack.get(FishData.class).isPresent()) {
FishData fishData = itemStack.get(FishData.class).get();
String fishName = fishData.type().get().getName();
Optional<TEActionReward> reward = Optional.empty();
List<String> sets = optPlayerJob.get().getSets();
for (String s : sets) {
Optional<TEJobSet> optSet = getJobSet(s);
if (!optSet.isPresent()) {
logger.warn("[TE] Job " + getPlayerJob(player) + " has nonexistent set \"" + s + "\"");
continue;
}
reward = optSet.get().getRewardFor("catch", fishName);
}
if (reward.isPresent()) {
int expAmount = reward.get().getExpReward();
BigDecimal payAmount = reward.get().getMoneyReward();
boolean notify = accountConfig.getNode(playerUUID.toString(), "jobnotifications").getBoolean();
TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
if (notify) {
notifyPlayer(player, payAmount);
}
addExp(player, expAmount);
playerAccount.deposit(totalEconomy.getDefaultCurrency(), payAmount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
checkForLevel(player);
}
}
}
}
}
use of org.spongepowered.api.item.inventory.ItemStackSnapshot in project Skree by Skelril.
the class WildernessWorldWrapper method onItemDrop.
@Listener
public void onItemDrop(DropItemEvent.Destruct event, @Named(NamedCause.SOURCE) BlockSpawnCause spawnCause) {
BlockSnapshot blockSnapshot = spawnCause.getBlockSnapshot();
Optional<Location<World>> optLocation = blockSnapshot.getLocation();
if (!optLocation.isPresent()) {
return;
}
Location<World> loc = optLocation.get();
if (!markedOrePoints.remove(loc)) {
return;
}
Optional<Integer> optLevel = getLevel(loc);
if (!optLevel.isPresent()) {
return;
}
List<ItemStackSnapshot> itemStacks = new ArrayList<>();
event.getEntities().forEach((entity -> {
if (entity instanceof Item) {
ItemStackSnapshot snapshot = ((Item) entity).item().get();
itemStacks.add(getPoolItemDrop(snapshot));
}
}));
addPool(loc, () -> itemStacks);
}
use of org.spongepowered.api.item.inventory.ItemStackSnapshot in project Skree by Skelril.
the class CursedMineInstance method sweepFloor.
public void sweepFloor() {
for (Item item : getContained(Item.class)) {
if (!contains(item))
continue;
ItemType id = item.getItemType();
for (ItemType aItem : items) {
if (aItem == id) {
ItemStackSnapshot snapshot = item.get(Keys.REPRESENTED_ITEM).get();
int newAmt = (int) (snapshot.getCount() * .8);
if (newAmt < 1) {
item.remove();
} else {
ItemStack newStack = snapshot.createStack();
newStack.setQuantity(newAmt);
item.offer(Keys.REPRESENTED_ITEM, newStack.createSnapshot());
}
break;
}
}
}
}
Aggregations