net.minecraft.world.storage.loot.LootContext Java Examples

The following examples show how to use net.minecraft.world.storage.loot.LootContext. 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: ChestGenHooks.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void addItem(ResourceLocation lootTable, ItemStack item, int minAmount, int additionalAmount, int weight) {
    LootEntryItem itemEntry = new LootEntryItem(item.getItem(), weight, 1, new LootFunction[]{
        new LootFunction(NO_CONDITIONS) {
            @Override
            public ItemStack apply(ItemStack stack, Random rand, LootContext context) {
                stack.setItemDamage(item.getItemDamage());
                stack.setTagCompound(item.getTagCompound());
                stack.setCount(minAmount + rand.nextInt(additionalAmount));
                return stack;
            }
        }
    }, NO_CONDITIONS, "#gregtech:loot_" + item.toString());
    if (lootEntryItems.containsKey(lootTable)) {
        lootEntryItems.get(lootTable).add(itemEntry);
    } else {
        lootEntryItems.put(lootTable, Lists.newArrayList(itemEntry));
    }
}
 
Example #2
Source File: AbstractItemLootEntry.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void addLoot(Collection<ItemStack> stacks, Random rand, LootContext context) {
    ItemStack itemStack = createItemStack();
    for (LootFunction lootfunction : this.functions) {
        if (LootConditionManager.testAllConditions(lootfunction.getConditions(), rand, context)) {
            itemStack = lootfunction.apply(itemStack, rand, context);
        }
    }
    if (!itemStack.isEmpty()) {
        if (itemStack.getCount() < itemStack.getItem().getItemStackLimit(itemStack)) {
            stacks.add(itemStack);
        } else {
            int i = itemStack.getCount();

            while (i > 0) {
                ItemStack itemstack1 = itemStack.copy();
                itemstack1.setCount(Math.min(itemStack.getMaxStackSize(), i));
                i -= itemstack1.getCount();
                stacks.add(itemstack1);
            }
        }
    }
}
 
Example #3
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 #4
Source File: MetaTileEntityFisher.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void update() {
    super.update();
    ItemStack baitStack = importItems.getStackInSlot(0);
    if (!getWorld().isRemote && energyContainer.getEnergyStored() >= energyAmountPerFish && getTimer() % fishingTicks == 0L && !baitStack.isEmpty()) {
        WorldServer world = (WorldServer) this.getWorld();
        int waterCount = 0;
        int edgeSize = (int) Math.sqrt(WATER_CHECK_SIZE);
        for (int x = 0; x < edgeSize; x++){
            for (int z = 0; z < edgeSize; z++){
                BlockPos waterCheckPos = getPos().down().add(x - edgeSize / 2, 0, z - edgeSize / 2);
                if (world.getBlockState(waterCheckPos).getBlock() instanceof BlockLiquid &&
                    world.getBlockState(waterCheckPos).getMaterial() == Material.WATER) {
                    waterCount++;
                }
            }
        }
        if (waterCount == WATER_CHECK_SIZE) {
            LootTable table = world.getLootTableManager().getLootTableFromLocation(LootTableList.GAMEPLAY_FISHING);
            NonNullList<ItemStack> itemStacks = NonNullList.create();
            itemStacks.addAll(table.generateLootForPools(world.rand, new LootContext.Builder(world).build()));
            if(addItemsToItemHandler(exportItems, true, itemStacks)) {
                addItemsToItemHandler(exportItems, false, itemStacks);
                energyContainer.removeEnergy(energyAmountPerFish);
                baitStack.shrink(1);
            }
        }
    }
    if(!getWorld().isRemote && getTimer() % 5 == 0) {
        pushItemsIntoNearbyHandlers(getFrontFacing());
    }
}
 
Example #5
Source File: MetaTileEntityLockedSafe.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void generateChestContents() {
    ResourceLocation lootTableLocation = new ResourceLocation(GTValues.MODID, "chests/abandoned_safe_" + unlockComponentTier);
    WorldServer worldServer = (WorldServer) getWorld();
    LootTable lootTable = worldServer.getLootTableManager().getLootTableFromLocation(lootTableLocation);
    LootContext lootContext = new LootContext.Builder(worldServer).build();
    Random random = new Random();
    List<ItemStack> loots = lootTable.generateLootForPools(random, lootContext);
    LootTableHelper.fillInventory(safeLootInventory, random, loots);
}
 
Example #6
Source File: BlockEnderStorage.java    From EnderStorage with MIT License 5 votes vote down vote up
@Override
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
    List<ItemStack> drops = new ArrayList<>();
    TileFrequencyOwner tile = (TileFrequencyOwner) builder.get(LootParameters.BLOCK_ENTITY);
    if (tile != null) {
        drops.add(createItem(tile.getFrequency()));
        if (EnderStorageConfig.anarchyMode && tile.getFrequency().hasOwner()) {
            drops.add(EnderStorageConfig.personalItem.copy());
        }
    }
    return drops;
}
 
Example #7
Source File: LevelCondition.java    From GokiStats with MIT License 4 votes vote down vote up
@Override
public boolean testCondition(@Nonnull Random rand, @Nonnull LootContext context) {
    return false;
}