Java Code Examples for net.minecraftforge.items.ItemStackHandler#setStackInSlot()

The following examples show how to use net.minecraftforge.items.ItemStackHandler#setStackInSlot() . 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: BackpackDataItems.java    From WearableBackpacks with MIT License 6 votes vote down vote up
public static void generateLoot(ItemStackHandler items, String tableStr, long seed,
                                World world, EntityPlayer player) {
	Random rnd = new Random(seed);
	double maxFullness = (0.6 + rnd.nextDouble() * 0.2);
	int maxOccupiedSlots = (int)Math.ceil(items.getSlots() * maxFullness);
	
	LootTableManager manager = world.getLootTableManager();
	LootTable table = manager.getLootTableFromLocation(new ResourceLocation(tableStr));
	LootContext context = new LootContext(((player != null) ? player.getLuck() : 0),
	                                      (WorldServer)world, manager, player, null, null);
	List<ItemStack> loot = table.generateLootForPools(rnd, context);
	Collections.shuffle(loot);
	
	List<Integer> randomizedSlots = new ArrayList<Integer>(items.getSlots());
	for (int i = 0; i < items.getSlots(); i++) randomizedSlots.add(i);
	Collections.shuffle(randomizedSlots);
	for (int i = 0; (i < maxOccupiedSlots) && (i < loot.size()); i++) {
		ItemStack stack = loot.get(i);
		int slot = randomizedSlots.get(i);
		items.setStackInSlot(slot, stack);
	}
}
 
Example 2
Source File: ItemHelperTests.java    From customstuff4 with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void test_removeInputsFromInventory_stacks()
{
    ItemStackHandler inv = new ItemStackHandler(2);
    inv.setStackInSlot(0, new ItemStack(Items.APPLE));
    inv.setStackInSlot(1, new ItemStack(Items.STICK, 3));

    RecipeInputImpl input1 = RecipeInputImpl.create(new ItemStack(Items.STICK, 2));
    RecipeInputImpl input2 = RecipeInputImpl.create(new ItemStack(Items.APPLE, 1));
    ItemHelper.removeInputsFromInventory(Lists.newArrayList(input1, input2), inv, 0, 2);

    assertTrue(inv.getStackInSlot(0).isEmpty());
    assertEquals(1, inv.getStackInSlot(1).getCount());
}
 
Example 3
Source File: ItemHelperTests.java    From customstuff4 with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void test_removeInputsFromInventory_ore()
{
    ItemStackHandler inv = new ItemStackHandler(2);
    inv.setStackInSlot(0, new ItemStack(Items.APPLE));
    inv.setStackInSlot(1, new ItemStack(Items.STICK, 3));

    RecipeInputImpl input1 = new RecipeInputImpl("stickWood", 2);
    RecipeInputImpl input2 = RecipeInputImpl.create(new ItemStack(Items.APPLE, 1));
    ItemHelper.removeInputsFromInventory(Lists.newArrayList(input1, input2), inv, 0, 2);

    assertTrue(inv.getStackInSlot(0).isEmpty());
    assertEquals(1, inv.getStackInSlot(1).getCount());
}
 
Example 4
Source File: PearlInfusionRecipe.java    From Wizardry with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void complete(World world, BlockPos pos, ItemStack input, ItemStackHandler inventoryHandler) {
	ArrayList<ItemStack> stacks = new ArrayList<>();

	for (int i = 0; i < inventoryHandler.getSlots(); i++) {
		if (!inventoryHandler.getStackInSlot(i).isEmpty()) {
			stacks.add(inventoryHandler.getStackInSlot(i));
			inventoryHandler.setStackInSlot(i, ItemStack.EMPTY);
		}
	}

	// Process spellData multipliers based on nacre quality
	double pearlMultiplier = 1;
	if (input.getItem() instanceof INacreProduct) {
		float purity = ((INacreProduct) input.getItem()).getQuality(input);
		if (purity >= 1f) pearlMultiplier = ConfigValues.perfectPearlMultiplier * purity;
		else if (purity <= ConfigValues.damagedPearlMultiplier)
			pearlMultiplier = ConfigValues.damagedPearlMultiplier;
		else {
			double base = purity - 1;
			pearlMultiplier = 1 - (base * base * base * base);
		}
	}

	SpellBuilder builder = new SpellBuilder(stacks, pearlMultiplier);

	NBTTagList list = new NBTTagList();
	for (SpellRing spellRing : builder.getSpell()) {
		list.appendTag(spellRing.serializeNBT());
	}

	SpellUtils.infuseSpell(input, list);

	//markDirty();

	PacketHandler.NETWORK.sendToAllAround(new PacketExplode(new Vec3d(pos).add(0.5, 0.5, 0.5), Color.CYAN, Color.BLUE, 2, 2, 500, 300, 20, true),
			new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 256));

	world.playSound(null, pos, ModSounds.BASS_BOOM, SoundCategory.BLOCKS, 1f, (float) RandUtil.nextDouble(1, 1.5));

	List<Entity> entityList = world.getEntitiesWithinAABBExcludingEntity(null, new AxisAlignedBB(pos).grow(32, 32, 32));
	for (Entity entity1 : entityList) {
		double dist = entity1.getDistance(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5);
		final double upperMag = 3;
		final double scale = 0.8;
		double mag = upperMag * (scale * dist / (-scale * dist - 1) + 1);
		Vec3d dir = entity1.getPositionVector().subtract(new Vec3d(pos).add(0.5, 0.5, 0.5)).normalize().scale(mag);

		entity1.motionX = (dir.x);
		entity1.motionY = (dir.y);
		entity1.motionZ = (dir.z);
		entity1.fallDistance = 0;
		entity1.velocityChanged = true;

		if (entity1 instanceof EntityPlayerMP)
			((EntityPlayerMP) entity1).connection.sendPacket(new SPacketEntityVelocity(entity1));
	}
}
 
Example 5
Source File: FairyJarRecipe.java    From Wizardry with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void complete(World world, BlockPos pos, ItemStack input, ItemStackHandler inventoryHandler) {
	TileEntity tileEntity = world.getTileEntity(pos.offset(EnumFacing.UP));
	if (!(tileEntity instanceof TileJar)) return;
	TileJar jar = (TileJar) tileEntity;

	ArrayList<ItemStack> stacks = new ArrayList<>();

	for (int i = 0; i < inventoryHandler.getSlots(); i++) {
		if (!inventoryHandler.getStackInSlot(i).isEmpty()) {
			stacks.add(inventoryHandler.getStackInSlot(i));
			inventoryHandler.setStackInSlot(i, ItemStack.EMPTY);
		}
	}

	SpellBuilder builder = new SpellBuilder(stacks);
	List<SpellRing> spell = builder.getSpell();
	if (spell.isEmpty()) return;

	NBTTagList list = new NBTTagList();
	for (SpellRing spellRing : builder.getSpell()) {
		list.appendTag(spellRing.serializeNBT());
	}

	jar.fairy.infusedSpell.clear();
	jar.fairy.infusedSpell.addAll(builder.getSpell());

	PacketHandler.NETWORK.sendToAllAround(new PacketExplode(new Vec3d(pos).add(0.5, 0.5, 0.5), Color.CYAN, Color.BLUE, 2, 2, 500, 300, 20, true),
			new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 256));

	world.playSound(null, pos, ModSounds.BASS_BOOM, SoundCategory.BLOCKS, 1f, (float) RandUtil.nextDouble(1, 1.5));

	List<Entity> entityList = world.getEntitiesWithinAABBExcludingEntity(null, new AxisAlignedBB(pos).grow(32, 32, 32));
	for (Entity entity1 : entityList) {
		double dist = entity1.getDistance(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5);
		final double upperMag = 3;
		final double scale = 0.8;
		double mag = upperMag * (scale * dist / (-scale * dist - 1) + 1);
		Vec3d dir = entity1.getPositionVector().subtract(new Vec3d(pos).add(0.5, 0.5, 0.5)).normalize().scale(mag);

		entity1.motionX = (dir.x);
		entity1.motionY = (dir.y);
		entity1.motionZ = (dir.z);
		entity1.fallDistance = 0;
		entity1.velocityChanged = true;

		if (entity1 instanceof EntityPlayerMP)
			((EntityPlayerMP) entity1).connection.sendPacket(new SPacketEntityVelocity(entity1));
	}
}