Search in sources :

Example 11 with ServerWorld

use of net.minecraft.world.server.ServerWorld in project AgriCraft by AgriCraft.

the class BlockUpdateHandler method removeListener.

public void removeListener(World world, BlockPos pos, IListener listener) {
    if (world instanceof ServerWorld) {
        RegistryKey<World> dimension = world.getDimensionKey();
        this.listeners.computeIfPresent(dimension, (dim, chunkMap) -> {
            chunkMap.computeIfPresent(new ChunkPos(pos), (chunkPos, posMap) -> {
                posMap.computeIfPresent(pos, (aPos, set) -> {
                    set.remove(listener);
                    return set;
                });
                return posMap;
            });
            return chunkMap;
        });
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) ChunkPos(net.minecraft.util.math.ChunkPos) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World)

Example 12 with ServerWorld

use of net.minecraft.world.server.ServerWorld in project AgriCraft by AgriCraft.

the class TileEntitySprinkler method irrigateColumn.

/**
 * This method will search through a vertical column of positions, starting from the top. It
 * will stop searching any lower once it hits anything other than air or plants. Any plant found
 * has an independent chance for a growth tick. That percentage is controlled by
 * AgriCraftConfig. Farmland also ends the search, but it first has its moisture set to max (7)
 * if it isn't already. The lowest position is special: a plant this far away is not helped.
 * Only farmland is currently.
 */
@SuppressWarnings("deprecation")
protected void irrigateColumn(final int targetX, final int targetZ, final int highestY, final int lowestY) {
    if (this.getWorld() == null || !(this.getWorld() instanceof ServerWorld)) {
        return;
    }
    for (int targetY = highestY; targetY >= lowestY; targetY -= 1) {
        BlockPos target = new BlockPos(targetX, targetY, targetZ);
        BlockState state = this.getWorld().getBlockState(target);
        Block block = state.getBlock();
        // Option A: Skip empty/air blocks.
        if (block.isAir(state, this.getWorld(), target)) {
            continue;
        }
        // Option B: Give plants a chance to grow, and then continue onward to irrigate the farmland too.
        if ((block instanceof IPlantable || block instanceof IGrowable) && targetY != lowestY) {
            if (this.getRandom().nextDouble() < AgriCraft.instance.getConfig().sprinkleGrowthChance()) {
                block.randomTick(state, (ServerWorld) this.getWorld(), target, this.getRandom());
            }
            continue;
        }
        // Option C: Dry farmland gets set as moist.
        if (block instanceof FarmlandBlock) {
            if (state.get(FarmlandBlock.MOISTURE) < 7) {
                this.getWorld().setBlockState(target, state.with(FarmlandBlock.MOISTURE, 7), 2);
            }
            // Explicitly expresses the intent to stop.
            break;
        }
        // Option D: If it's none of the above, it blocks the sprinkler's irrigation. Stop.
        break;
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) BlockState(net.minecraft.block.BlockState) IGrowable(net.minecraft.block.IGrowable) IPlantable(net.minecraftforge.common.IPlantable) Block(net.minecraft.block.Block) FarmlandBlock(net.minecraft.block.FarmlandBlock) BlockPos(net.minecraft.util.math.BlockPos) FarmlandBlock(net.minecraft.block.FarmlandBlock)

Example 13 with ServerWorld

use of net.minecraft.world.server.ServerWorld in project AgriCraft by AgriCraft.

the class BlockCropSticks method onFluidChanged.

@Override
protected boolean onFluidChanged(World world, BlockPos pos, BlockState state, Fluid oldFluid, Fluid newFluid) {
    Optional<IAgriCrop> optCrop = this.getCrop(world, pos);
    boolean noMorePlant = optCrop.map(crop -> {
        if (!crop.hasPlant()) {
            return true;
        }
        IAgriGrowthResponse response = crop.getPlant().getGrowthRequirement(crop.getGrowthStage()).getFluidResponse(newFluid, crop.getStats().getStrength());
        if (response.killInstantly()) {
            response.onPlantKilled(crop);
            crop.removeGenome();
            return true;
        }
        return false;
    }).orElse(true);
    if (this.getVariant().canExistInFluid(newFluid)) {
        // the crop sticks remain, regardless of what happened to the plant
        return false;
    } else {
        if (noMorePlant) {
            // no more crop sticks, no more plant, only fluid
            world.setBlockState(pos, newFluid.getDefaultState().getBlockState());
            if (world instanceof ServerWorld) {
                double x = pos.getX() + 0.5;
                double y = pos.getY() + 0.5;
                double z = pos.getZ() + 0.5;
                for (int i = 0; i < 2; i++) {
                    ((ServerWorld) world).spawnParticle(ParticleTypes.SMOKE, x + 0.25 * world.getRandom().nextDouble(), y, z + 0.25 * world.getRandom().nextDouble(), 1, 0, 1, 0, 0.25);
                }
                world.playSound(null, x, y, z, SoundEvents.BLOCK_LAVA_EXTINGUISH, SoundCategory.BLOCKS, 0.2F + world.getRandom().nextFloat() * 0.2F, 0.9F + world.getRandom().nextFloat() * 0.05F);
            }
        } else {
            // no more crop sticks, but still plant, and fluid
            BlockState newState = AgriCraft.instance.getModBlockRegistry().crop_plant.getDefaultState();
            newState = BlockCropBase.PLANT.mimic(state, newState);
            newState = BlockCropBase.LIGHT.mimic(state, newState);
            newState = InfProperty.Defaults.fluidlogged().mimic(state, newState);
            world.setBlockState(pos, newState);
            // If there was trouble, reset and abort.
            TileEntity tile = world.getTileEntity(pos);
            if (!(tile instanceof TileEntityCropPlant)) {
                world.setBlockState(pos, state);
                return false;
            }
            // Mimic plant and weed
            ((TileEntityCropPlant) tile).mimicFrom(optCrop.get());
        }
        return true;
    }
}
Also used : net.minecraft.util(net.minecraft.util) ServerWorld(net.minecraft.world.server.ServerWorld) TypeHelper(com.agricraft.agricore.util.TypeHelper) Arrays(java.util.Arrays) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) BiFunction(java.util.function.BiFunction) IAgriGrowthResponse(com.infinityraider.agricraft.api.v1.requirement.IAgriGrowthResponse) IAgriGenome(com.infinityraider.agricraft.api.v1.genetics.IAgriGenome) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) IAgriClipperItem(com.infinityraider.agricraft.api.v1.content.items.IAgriClipperItem) ItemStack(net.minecraft.item.ItemStack) LootContext(net.minecraft.loot.LootContext) Lists(com.google.common.collect.Lists) IBlockReader(net.minecraft.world.IBlockReader) net.minecraft.block(net.minecraft.block) Map(java.util.Map) IAgriTrowelItem(com.infinityraider.agricraft.api.v1.content.items.IAgriTrowelItem) ISelectionContext(net.minecraft.util.math.shapes.ISelectionContext) VoxelShape(net.minecraft.util.math.shapes.VoxelShape) InfProperty(com.infinityraider.infinitylib.block.property.InfProperty) AgriCraft(com.infinityraider.agricraft.AgriCraft) MethodsReturnNonnullByDefault(mcp.MethodsReturnNonnullByDefault) AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) PlayerEntity(net.minecraft.entity.player.PlayerEntity) World(net.minecraft.world.World) IBooleanFunction(net.minecraft.util.math.shapes.IBooleanFunction) LootParameters(net.minecraft.loot.LootParameters) BlockPos(net.minecraft.util.math.BlockPos) Maps(com.google.common.collect.Maps) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) ParticleTypes(net.minecraft.particles.ParticleTypes) List(java.util.List) Stream(java.util.stream.Stream) IAgriRakeItem(com.infinityraider.agricraft.api.v1.content.items.IAgriRakeItem) InfPropertyConfiguration(com.infinityraider.infinitylib.block.property.InfPropertyConfiguration) Optional(java.util.Optional) TileEntity(net.minecraft.tileentity.TileEntity) ItemSeedBag(com.infinityraider.agricraft.content.tools.ItemSeedBag) Fluid(net.minecraft.fluid.Fluid) VoxelShapes(net.minecraft.util.math.shapes.VoxelShapes) ServerWorld(net.minecraft.world.server.ServerWorld) TileEntity(net.minecraft.tileentity.TileEntity) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) IAgriGrowthResponse(com.infinityraider.agricraft.api.v1.requirement.IAgriGrowthResponse)

Aggregations

ServerWorld (net.minecraft.world.server.ServerWorld)13 World (net.minecraft.world.World)8 BlockPos (net.minecraft.util.math.BlockPos)7 BlockState (net.minecraft.block.BlockState)5 ItemStack (net.minecraft.item.ItemStack)4 ChunkPos (net.minecraft.util.math.ChunkPos)4 Maps (com.google.common.collect.Maps)3 Map (java.util.Map)3 Sets (com.google.common.collect.Sets)2 Set (java.util.Set)2 IGrowable (net.minecraft.block.IGrowable)2 PlayerEntity (net.minecraft.entity.player.PlayerEntity)2 BlockItem (net.minecraft.item.BlockItem)2 CompoundNBT (net.minecraft.nbt.CompoundNBT)2 TileEntity (net.minecraft.tileentity.TileEntity)2 RegistryKey (net.minecraft.util.RegistryKey)2 StringTextComponent (net.minecraft.util.text.StringTextComponent)2 BlockEvent (net.minecraftforge.event.world.BlockEvent)2 ChunkEvent (net.minecraftforge.event.world.ChunkEvent)2 WorldEvent (net.minecraftforge.event.world.WorldEvent)2