Search in sources :

Example 6 with ChestTileEntity

use of net.minecraft.tileentity.ChestTileEntity in project Enigmatic-Legacy by Aizistral-Studios.

the class LootGenerator method onItemUse.

public ActionResultType onItemUse(ItemUseContext context) {
    PlayerEntity player = context.getPlayer();
    World world = context.getWorld();
    ItemStack stack = context.getItem();
    if (world.isRemote)
        return ActionResultType.SUCCESS;
    if (world.getBlockState(context.getPos()).hasTileEntity()) {
        if (world.getTileEntity(context.getPos()) instanceof ChestTileEntity) {
            ChestTileEntity chest = (ChestTileEntity) world.getTileEntity(context.getPos());
            if (context.getFace() == Direction.UP) {
                chest.setLootTable(lootList.get(stack.getDamage()), lootRandomizer.nextLong());
                chest.fillWithLoot(player);
            } else if (context.getFace() == Direction.DOWN) {
                HashMap<Item, Integer> lootMap = new HashMap<Item, Integer>();
                for (int counter = 0; counter < 32768; counter++) {
                    chest.setLootTable(lootList.get(stack.getDamage()), lootRandomizer.nextLong());
                    chest.fillWithLoot(player);
                    for (int slot = 0; slot < chest.getSizeInventory(); slot++) {
                        ItemStack generatedStack = chest.getStackInSlot(slot);
                        Item generatedItem = generatedStack.getItem();
                        int amount = generatedStack.getCount();
                        if (!generatedStack.isEmpty()) {
                            if (lootMap.containsKey(generatedItem)) {
                                lootMap.put(generatedItem, lootMap.get(generatedItem) + amount);
                            } else {
                                lootMap.put(generatedItem, amount);
                            }
                        }
                    }
                    chest.clear();
                }
                EnigmaticLegacy.enigmaticLogger.info("Estimated generation complete in 32768 instances, results:");
                for (Item theItem : lootMap.keySet()) {
                    EnigmaticLegacy.enigmaticLogger.info("Item: " + theItem.getDisplayName(new ItemStack(theItem)).getUnformattedComponentText() + ", Amount: " + lootMap.get(theItem));
                }
                player.sendMessage(new TranslationTextComponent("message.enigmaticlegacy.gen_sim_complete").applyTextStyle(TextFormatting.DARK_PURPLE));
            } else {
                chest.clear();
            }
            return ActionResultType.SUCCESS;
        }
    }
    return ActionResultType.PASS;
}
Also used : Item(net.minecraft.item.Item) HashMap(java.util.HashMap) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Example 7 with ChestTileEntity

use of net.minecraft.tileentity.ChestTileEntity in project ChocolateQuestRepoured by TeamChocoQuest.

the class PreparableLootChestInfo method prepare.

@Override
protected GeneratablePosInfo prepare(World world, DungeonPlacement placement, BlockPos pos) {
    BlockState state = Blocks.CHEST.getDefaultState().withProperty(ChestBlock.FACING, this.facing);
    state = state.withMirror(placement.getMirror()).withRotation(placement.getRotation());
    TileEntity tileEntity = state.getBlock().createTileEntity(world, state);
    if (tileEntity instanceof ChestTileEntity) {
        long seed = WorldDungeonGenerator.getSeed(world, pos.getX(), pos.getZ());
        ((ChestTileEntity) tileEntity).setLootTable(this.lootTable, seed);
    }
    return new GeneratableBlockInfo(pos, state, tileEntity);
}
Also used : ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) GeneratableBlockInfo(team.cqr.cqrepoured.world.structure.generation.generation.generatable.GeneratableBlockInfo)

Example 8 with ChestTileEntity

use of net.minecraft.tileentity.ChestTileEntity in project minecolonies by ldtteam.

the class InventoryUtils method hasBuildingEnoughElseCount.

/**
 * Check if a building has more than a count in stack. Return the count it has if it has less.
 *
 * @param provider building to check in.
 * @param stack    the stack to check.
 * @return Amount of occurrences of stacks that match the given predicate.
 */
public static int hasBuildingEnoughElseCount(@NotNull final IBuilding provider, @NotNull final ItemStorage stack, final int count) {
    int totalCount = 0;
    final World world = provider.getColony().getWorld();
    for (final BlockPos pos : provider.getContainers()) {
        if (WorldUtil.isBlockLoaded(world, pos)) {
            final TileEntity entity = world.getBlockEntity(pos);
            if (entity instanceof TileEntityRack) {
                totalCount += ((TileEntityRack) entity).getCount(stack);
            } else if (entity instanceof ChestTileEntity) {
                totalCount += getItemCountInProvider(entity, itemStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, stack.getItemStack(), !stack.ignoreDamageValue(), !stack.ignoreNBT()));
            }
            if (totalCount > count) {
                return Integer.MAX_VALUE;
            }
        }
    }
    return totalCount;
}
Also used : ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) BlockPos(net.minecraft.util.math.BlockPos) ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) World(net.minecraft.world.World)

Example 9 with ChestTileEntity

use of net.minecraft.tileentity.ChestTileEntity in project minecolonies by ldtteam.

the class BuildingWareHouse method handleBuildingOverChest.

/**
 * Handles the chest placement.
 *
 * @param pos            at pos.
 * @param chest          the entity.
 * @param world          the world.
 * @param tileEntityData the rack te data.
 */
public static void handleBuildingOverChest(@NotNull final BlockPos pos, final ChestTileEntity chest, final World world, @Nullable final CompoundNBT tileEntityData) {
    final List<ItemStack> inventory = new ArrayList<>();
    final int size = chest.getContainerSize();
    for (int slot = 0; slot < size; slot++) {
        final ItemStack stack = chest.getItem(slot);
        if (!ItemStackUtils.isEmpty(stack)) {
            inventory.add(stack.copy());
        }
        chest.removeItemNoUpdate(slot);
    }
    world.setBlock(pos, ModBlocks.blockRack.defaultBlockState(), 0x03);
    if (tileEntityData != null) {
        handleTileEntityPlacement(tileEntityData, world, pos);
    }
    final TileEntity entity = world.getBlockEntity(pos);
    if (entity instanceof TileEntityRack) {
        ((AbstractTileEntityRack) entity).setInWarehouse(true);
        for (final ItemStack stack : inventory) {
            if (!ItemStackUtils.isEmpty(stack)) {
                InventoryUtils.addItemStackToItemHandler(((AbstractTileEntityRack) entity).getInventory(), stack);
            }
        }
    }
}
Also used : ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ItemStack(net.minecraft.item.ItemStack)

Example 10 with ChestTileEntity

use of net.minecraft.tileentity.ChestTileEntity in project minecolonies by Minecolonies.

the class InventoryUtils method hasBuildingEnoughElseCount.

/**
 * Check if a building has more than a count in stack. Return the count it has if it has less.
 *
 * @param provider building to check in.
 * @param stack    the stack to check.
 * @return Amount of occurrences of stacks that match the given predicate.
 */
public static int hasBuildingEnoughElseCount(@NotNull final IBuilding provider, @NotNull final ItemStorage stack, final int count) {
    int totalCount = 0;
    final World world = provider.getColony().getWorld();
    for (final BlockPos pos : provider.getContainers()) {
        if (WorldUtil.isBlockLoaded(world, pos)) {
            final TileEntity entity = world.getBlockEntity(pos);
            if (entity instanceof TileEntityRack) {
                totalCount += ((TileEntityRack) entity).getCount(stack);
            } else if (entity instanceof ChestTileEntity) {
                totalCount += getItemCountInProvider(entity, itemStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, stack.getItemStack(), !stack.ignoreDamageValue(), !stack.ignoreNBT()));
            }
            if (totalCount > count) {
                return Integer.MAX_VALUE;
            }
        }
    }
    return totalCount;
}
Also used : ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) BlockPos(net.minecraft.util.math.BlockPos) ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) World(net.minecraft.world.World)

Aggregations

ChestTileEntity (net.minecraft.tileentity.ChestTileEntity)18 TileEntity (net.minecraft.tileentity.TileEntity)13 BlockPos (net.minecraft.util.math.BlockPos)8 TileEntityRack (com.minecolonies.api.tileentities.TileEntityRack)7 World (net.minecraft.world.World)7 ItemStack (net.minecraft.item.ItemStack)6 CompoundNBT (net.minecraft.nbt.CompoundNBT)3 BlockState (net.minecraft.block.BlockState)2 ChestBlock (net.minecraft.block.ChestBlock)2 ResourceLocation (net.minecraft.util.ResourceLocation)2 IItemHandler (net.minecraftforge.items.IItemHandler)2 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 Set (java.util.Set)1 Block (net.minecraft.block.Block)1 Blocks (net.minecraft.block.Blocks)1 HugeMushroomBlock (net.minecraft.block.HugeMushroomBlock)1 VineBlock (net.minecraft.block.VineBlock)1