Java Code Examples for com.google.common.collect.Multisets#difference()

The following examples show how to use com.google.common.collect.Multisets#difference() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MotherlodePlugin.java    From plugins with GNU General Public License v3.0 6 votes vote down vote up
@Subscribe
void onItemContainerChanged(ItemContainerChanged event)
{
	final ItemContainer container = event.getItemContainer();

	if (!inMlm || !shouldUpdateOres || inventorySnapshot == null || container != client.getItemContainer(InventoryID.INVENTORY))
	{
		return;
	}

	// Build set of current inventory
	Multiset<Integer> current = HashMultiset.create();
	Arrays.stream(container.getItems())
		.filter(item -> MLM_ORE_TYPES.contains(item.getId()))
		.forEach(item -> current.add(item.getId(), item.getQuantity()));

	// Take the difference
	Multiset<Integer> delta = Multisets.difference(current, inventorySnapshot);

	// Update the session
	delta.forEachEntry(session::updateOreFound);
	inventorySnapshot = null;
	shouldUpdateOres = false;
}
 
Example 2
Source File: QueryAssertions.java    From presto with Apache License 2.0 6 votes vote down vote up
public static void assertEqualsIgnoreOrder(Iterable<?> actual, Iterable<?> expected, String message)
{
    assertNotNull(actual, "actual is null");
    assertNotNull(expected, "expected is null");

    ImmutableMultiset<?> actualSet = ImmutableMultiset.copyOf(actual);
    ImmutableMultiset<?> expectedSet = ImmutableMultiset.copyOf(expected);
    if (!actualSet.equals(expectedSet)) {
        Multiset<?> unexpectedRows = Multisets.difference(actualSet, expectedSet);
        Multiset<?> missingRows = Multisets.difference(expectedSet, actualSet);
        int limit = 100;
        fail(format(
                "%snot equal\n" +
                        "Actual rows (up to %s of %s extra rows shown, %s rows in total):\n    %s\n" +
                        "Expected rows (up to %s of %s missing rows shown, %s rows in total):\n    %s\n",
                message == null ? "" : (message + "\n"),
                limit,
                unexpectedRows.size(),
                actualSet.size(),
                Joiner.on("\n    ").join(Iterables.limit(unexpectedRows, limit)),
                limit,
                missingRows.size(),
                expectedSet.size(),
                Joiner.on("\n    ").join(Iterables.limit(missingRows, limit))));
    }
}
 
Example 3
Source File: MotherlodePlugin.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Subscribe
public void onItemContainerChanged(ItemContainerChanged event)
{
	final ItemContainer container = event.getItemContainer();

	if (!inMlm || !shouldUpdateOres || inventorySnapshot == null || container != client.getItemContainer(InventoryID.INVENTORY))
	{
		return;
	}

	// Build set of current inventory
	Multiset<Integer> current = HashMultiset.create();
	Arrays.stream(container.getItems())
		.filter(item -> MLM_ORE_TYPES.contains(item.getId()))
		.forEach(item -> current.add(item.getId(), item.getQuantity()));

	// Take the difference
	Multiset<Integer> delta = Multisets.difference(current, inventorySnapshot);

	// Update the session
	delta.forEachEntry(session::updateOreFound);
	inventorySnapshot = null;
	shouldUpdateOres = false;
}
 
Example 4
Source File: LootTrackerPlugin.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void processInventoryLoot(String event, LootRecordType lootRecordType, ItemContainer inventoryContainer, Collection<ItemStack> groundItems)
{
	if (inventorySnapshot != null)
	{
		Multiset<Integer> currentInventory = HashMultiset.create();
		Arrays.stream(inventoryContainer.getItems())
			.forEach(item -> currentInventory.add(item.getId(), item.getQuantity()));

		groundItems.stream()
			.forEach(item -> currentInventory.add(item.getId(), item.getQuantity()));

		final Multiset<Integer> diff = Multisets.difference(currentInventory, inventorySnapshot);

		List<ItemStack> items = diff.entrySet().stream()
			.map(e -> new ItemStack(e.getElement(), e.getCount(), client.getLocalPlayer().getLocalLocation()))
			.collect(Collectors.toList());

		addLoot(event, -1, lootRecordType, items);

		inventorySnapshot = null;
	}
}
 
Example 5
Source File: LootTrackerPlugin.java    From plugins with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Subscribe
public void onItemContainerChanged(ItemContainerChanged event)
{
	if (client.getLocalPlayer() == null || event.getContainerId() != InventoryID.INVENTORY.getId())
	{
		return;
	}

	if (pvpDeath && RESPAWN_REGIONS.contains(client.getLocalPlayer().getWorldLocation().getRegionID()))
	{
		Multiset snapshot;
		snapshot = inventorySnapshot;
		deathInventorySnapshot();
		if (inventorySnapshot != snapshot)
		{
			inventorySnapshot = snapshot;
			ItemContainer inventory = client.getItemContainer(InventoryID.INVENTORY);
			if (inventorySnapshot != null)
			{
				Multiset<Integer> currentInventory = HashMultiset.create();
				if (inventory != null)
				{
					Arrays.stream(Objects.requireNonNull(client.getItemContainer(InventoryID.INVENTORY)).getItems())
						.forEach(item -> currentInventory.add(item.getId(), item.getQuantity()));
				}

				final Multiset<Integer> diff = Multisets.difference(inventorySnapshot, currentInventory);

				List<ItemStack> itemsLost = diff.entrySet().stream()
					.map(e -> new ItemStack(e.getElement(), (-1 * e.getCount()), client.getLocalPlayer().getLocalLocation()))
					.collect(Collectors.toList());

				final LootTrackerItem[] entries = buildEntries(stack(itemsLost));
				String name = "Death: " + client.getLocalPlayer().getName();
				LootRecord lootRecord = new LootRecord(name, client.getLocalPlayer().getName(), LootRecordType.DEATH,
					toGameItems(itemsLost), Instant.now());
				SwingUtilities.invokeLater(() -> panel.add(name, client.getLocalPlayer().getName(),
					lootRecord.getType(), client.getLocalPlayer().getCombatLevel(), entries));
				if (config.saveLoot())
				{
					synchronized (queuedLoots)
					{
						queuedLoots.add(lootRecord);
					}
				}
				if (config.localPersistence())
				{
					saveLocalLootRecord(lootRecord);
				}

				pvpDeath = false;
				inventorySnapshot = null;
			}
		}
	}

	if (eventType == null)
	{
		return;
	}

	if (CHEST_EVENT_TYPES.containsValue(eventType)
		|| HALLOWED_SEPULCHRE_COFFIN_EVENT.equals(eventType)
		|| SHADE_CHEST_OBJECTS.containsValue(eventType)
		|| HERBIBOAR_EVENT.equals(eventType)
		|| HESPORI_EVENT.equals(eventType)
		|| SEEDPACK_EVENT.equals(eventType)
		|| CASKET_EVENT.equals(eventType)
		|| GAUNTLET_EVENT.equals(eventType)
		|| WINTERTODT_EVENT.equals(eventType)
		|| lootRecordType == LootRecordType.PICKPOCKET)
	{
		WorldPoint playerLocation = client.getLocalPlayer().getWorldLocation();
		Collection<ItemStack> groundItems = lootManager.getItemSpawns(playerLocation);

		processInventoryLoot(eventType, lootRecordType, event.getItemContainer(), groundItems);
		eventType = null;
		lootRecordType = null;
	}
}
 
Example 6
Source File: LootTrackerPlugin.java    From plugins with GNU General Public License v3.0 4 votes vote down vote up
private void processInventoryLoot(String event, LootRecordType lootRecordType, ItemContainer inventoryContainer, Collection<ItemStack> groundItems)
{
	if (client.getLocalPlayer() == null)
	{
		return;
	}

	if (inventorySnapshot != null)
	{
		Multiset<Integer> currentInventory = HashMultiset.create();
		Arrays.stream(inventoryContainer.getItems())
			.forEach(item -> currentInventory.add(item.getId(), item.getQuantity()));

		groundItems
			.forEach(item -> currentInventory.add(item.getId(), item.getQuantity()));

		final Multiset<Integer> diff = Multisets.difference(currentInventory, inventorySnapshot);

		List<ItemStack> items = diff.entrySet().stream()
			.map(e -> new ItemStack(e.getElement(), e.getCount(), client.getLocalPlayer().getLocalLocation()))
			.collect(Collectors.toList());

		if (gotPet)
		{
			ItemStack pet = null;
			switch (event)
			{
				case HERBIBOAR_EVENT:
					pet = handlePet("Herbiboar");
					break;
				case WINTERTODT_EVENT:
					pet = handlePet("Wintertodt");
					break;
				case GAUNTLET_EVENT:
					pet = handlePet("Gauntlet");
					break;
			}

			if (pet == null)
			{
				log.warn("Error finding pet for npc name: Herbiboar");
			}
			else
			{
				items.add(pet);
			}
		}

		final LootTrackerItem[] entries = buildEntries(stack(items));

		LootRecord lootRecord = new LootRecord(event, client.getLocalPlayer().getName(),
			LootRecordType.EVENT, toGameItems(items), Instant.now());

		SwingUtilities.invokeLater(() -> panel.add(event, client.getLocalPlayer().getName(), lootRecord.getType(), -1, entries));
		if (config.saveLoot())
		{
			synchronized (queuedLoots)
			{
				queuedLoots.add(lootRecord);
			}
		}

		if (config.localPersistence())
		{
			saveLocalLootRecord(lootRecord);
		}

		eventBus.post(LootReceived.class, new LootReceived(event, -1, LootRecordType.EVENT, items));
		inventorySnapshot = null;
	}
}
 
Example 7
Source File: SPARQLInferenceTest.java    From neo4j-sparql-extension with GNU General Public License v3.0 4 votes vote down vote up
public Multiset<BindingSet> getDiff() {
	return Multisets.difference(expect, actual);
}