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;
}
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);
}
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;
}
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);
}
}
}
}
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;
}
Aggregations