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