Search in sources :

Example 1 with ClientGamePacketListener

use of net.minecraft.network.protocol.game.ClientGamePacketListener in project MC-Prefab by Brian-Wuest.

the class BuildingMethods method FillChest.

/**
 * Fills a chest with basic tools and items.
 *
 * @param world        - The world where the chest resides.
 * @param itemPosition - The block position of the chest.
 */
public static void FillChest(Level world, BlockPos itemPosition) {
    // Add each stone tool to the chest and leather armor.
    BlockEntity tileEntity = world.getBlockEntity(itemPosition);
    if (tileEntity instanceof RandomizableContainerBlockEntity) {
        RandomizableContainerBlockEntity chestTile = (RandomizableContainerBlockEntity) tileEntity;
        int itemSlot = 0;
        // Add the tools.
        if (CommonProxy.proxyConfiguration.serverConfiguration.addAxe) {
            chestTile.setItem(itemSlot++, new ItemStack(Items.STONE_AXE));
        }
        if (CommonProxy.proxyConfiguration.serverConfiguration.addHoe) {
            chestTile.setItem(itemSlot++, new ItemStack(Items.STONE_HOE));
        }
        if (CommonProxy.proxyConfiguration.serverConfiguration.addPickAxe) {
            chestTile.setItem(itemSlot++, new ItemStack(Items.STONE_PICKAXE));
        }
        if (CommonProxy.proxyConfiguration.serverConfiguration.addShovel) {
            chestTile.setItem(itemSlot++, new ItemStack(Items.STONE_SHOVEL));
        }
        if (CommonProxy.proxyConfiguration.serverConfiguration.addSword) {
            Item sword = ModRegistry.SwiftBladeStone.get();
            if (!CommonProxy.proxyConfiguration.serverConfiguration.recipeConfiguration.get(ModConfiguration.SwiftBladeKey)) {
                // Swift blades are disabled; use a regular stone sword instead.
                sword = Items.STONE_SWORD;
            }
            chestTile.setItem(itemSlot++, new ItemStack(sword));
        }
        if (CommonProxy.proxyConfiguration.serverConfiguration.addArmor) {
            // Add the armor.
            chestTile.setItem(itemSlot++, new ItemStack(Items.LEATHER_BOOTS));
            chestTile.setItem(itemSlot++, new ItemStack(Items.LEATHER_CHESTPLATE));
            chestTile.setItem(itemSlot++, new ItemStack(Items.LEATHER_HELMET));
            chestTile.setItem(itemSlot++, new ItemStack(Items.LEATHER_LEGGINGS));
        }
        if (CommonProxy.proxyConfiguration.serverConfiguration.addFood) {
            // Add some bread.
            chestTile.setItem(itemSlot++, new ItemStack(Items.BREAD, 20));
        }
        if (CommonProxy.proxyConfiguration.serverConfiguration.addCrops) {
            // Add potatoes.
            chestTile.setItem(itemSlot++, new ItemStack(Items.POTATO, 3));
            // Add carrots.
            chestTile.setItem(itemSlot++, new ItemStack(Items.CARROT, 3));
            // Add seeds.
            chestTile.setItem(itemSlot++, new ItemStack(Items.WHEAT_SEEDS, 3));
        }
        if (CommonProxy.proxyConfiguration.serverConfiguration.addCobble) {
            // Add Cobblestone.
            chestTile.setItem(itemSlot++, new ItemStack(Item.byBlock(Blocks.COBBLESTONE), 64));
        }
        if (CommonProxy.proxyConfiguration.serverConfiguration.addDirt) {
            // Add Dirt.
            chestTile.setItem(itemSlot++, new ItemStack(Item.byBlock(Blocks.DIRT), 64));
        }
        if (CommonProxy.proxyConfiguration.serverConfiguration.addSaplings) {
            // Add oak saplings.
            chestTile.setItem(itemSlot++, new ItemStack(Item.byBlock(Blocks.OAK_SAPLING), 3));
        }
        if (CommonProxy.proxyConfiguration.serverConfiguration.addTorches) {
            // Add a set of 20 torches.
            chestTile.setItem(itemSlot, new ItemStack(Item.byBlock(Blocks.TORCH), 20));
        }
        chestTile.setChanged();
        Packet<ClientGamePacketListener> packet = chestTile.getUpdatePacket();
        if (packet != null) {
            world.getServer().getPlayerList().broadcastAll(packet);
        }
    }
}
Also used : Item(net.minecraft.world.item.Item) RandomizableContainerBlockEntity(net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity) ItemStack(net.minecraft.world.item.ItemStack) RandomizableContainerBlockEntity(net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) FurnaceBlockEntity(net.minecraft.world.level.block.entity.FurnaceBlockEntity) ClientGamePacketListener(net.minecraft.network.protocol.game.ClientGamePacketListener)

Example 2 with ClientGamePacketListener

use of net.minecraft.network.protocol.game.ClientGamePacketListener in project MC-Prefab by Brian-Wuest.

the class Structure method setBlockEntities.

protected void setBlockEntities() {
    for (BuildTileEntity buildTileEntity : this.tileEntities) {
        try {
            // Beds are processed separately.
            if (buildTileEntity.getEntityName().equals("bed")) {
                continue;
            }
            BlockPos tileEntityPos = buildTileEntity.getStartingPosition().getRelativePosition(this.originalPos, this.getClearSpace().getShape().getDirection(), this.configuration.houseFacing);
            BlockEntity tileEntity = this.world.getBlockEntity(tileEntityPos);
            BlockState tileBlock = this.world.getBlockState(tileEntityPos);
            if (tileEntity != null) {
                this.world.removeBlockEntity(tileEntityPos);
            }
            tileEntity = BlockEntity.loadStatic(tileEntityPos, tileBlock, buildTileEntity.getEntityDataTag());
            if (tileEntity == null) {
                continue;
            }
            this.world.removeBlockEntity(tileEntityPos);
            tileEntity = BlockEntity.loadStatic(tileEntityPos, tileBlock, buildTileEntity.getEntityDataTag());
            this.world.setBlockEntity(tileEntity);
            this.world.getChunkAt(tileEntityPos).setUnsaved(true);
            tileEntity.setChanged();
            Packet<ClientGamePacketListener> packet = tileEntity.getUpdatePacket();
            if (packet != null) {
                this.world.getServer().getPlayerList().broadcastAll(packet);
            }
        } catch (Exception ex) {
            Prefab.LOGGER.error(ex);
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) FurnaceBlockEntity(net.minecraft.world.level.block.entity.FurnaceBlockEntity) ClientGamePacketListener(net.minecraft.network.protocol.game.ClientGamePacketListener)

Aggregations

ClientGamePacketListener (net.minecraft.network.protocol.game.ClientGamePacketListener)2 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)2 ChestBlockEntity (net.minecraft.world.level.block.entity.ChestBlockEntity)2 FurnaceBlockEntity (net.minecraft.world.level.block.entity.FurnaceBlockEntity)2 BlockPos (net.minecraft.core.BlockPos)1 Item (net.minecraft.world.item.Item)1 ItemStack (net.minecraft.world.item.ItemStack)1 RandomizableContainerBlockEntity (net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity)1 BlockState (net.minecraft.world.level.block.state.BlockState)1