use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.
the class GuiPlugin method onSeedAnalyzerRightClick.
public void onSeedAnalyzerRightClick(PlayerInteractEvent.RightClickBlock event) {
BlockPos pos = event.getPos();
BlockState state = event.getWorld().getBlockState(pos);
if (event.getPlayer().isSneaking()) {
return;
}
if (state.getBlock() != AgriCraft.instance.getModBlockRegistry().seed_analyzer.getBlock()) {
return;
}
event.setCancellationResult(ActionResultType.SUCCESS);
event.setCanceled(true);
if (event.getPlayer().world.isRemote) {
return;
}
INamedContainerProvider containerProvider = new INamedContainerProvider() {
@Nonnull
@Override
public ITextComponent getDisplayName() {
return new TranslationTextComponent("screen.agricraft.seed_analyzer");
}
@Override
public Container createMenu(int id, @Nonnull PlayerInventory playerInventory, @Nonnull PlayerEntity player) {
return new SeedAnalyzerContainer(id, event.getWorld(), playerInventory, pos);
}
};
NetworkHooks.openGui((ServerPlayerEntity) event.getPlayer(), containerProvider, pos);
}
use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.
the class ItemDynamicAgriSeed method onItemUse.
@Nonnull
@Override
public ActionResultType onItemUse(@Nonnull ItemUseContext context) {
World world = context.getWorld();
BlockPos pos = context.getPos();
TileEntity tile = world.getTileEntity(pos);
ItemStack stack = context.getItem();
PlayerEntity player = context.getPlayer();
// If crop sticks were clicked, attempt to plant the seed
if (tile instanceof TileEntityCropSticks) {
return this.attemptSeedPlant((TileEntityCropSticks) tile, stack, player);
}
// If a soil was clicked, check the block on top of the soil and handle accordingly
return AgriApi.getSoil(world, pos).map(soil -> {
BlockPos up = pos.up();
TileEntity above = world.getTileEntity(up);
// There are currently crop sticks on the soil, attempt to plant on the crop sticks
if (above instanceof TileEntityCropSticks) {
return this.attemptSeedPlant((TileEntityCropSticks) above, stack, player);
}
// There are currently no crop sticks, check if the place is suitable and plant the plant directly
if (above == null && AgriCraft.instance.getConfig().allowPlantingOutsideCropSticks()) {
BlockState newState = AgriCraft.instance.getModBlockRegistry().crop_plant.getStateForPlacement(world, up);
if (newState != null && world.setBlockState(up, newState, 11)) {
boolean success = AgriApi.getCrop(world, up).map(crop -> this.getGenome(context.getItem()).map(genome -> crop.plantGenome(genome, player)).map(result -> {
if (result) {
// consume item
if (player == null || !player.isCreative()) {
stack.shrink(1);
}
}
return result;
}).orElse(false)).orElse(false);
if (success) {
return ActionResultType.SUCCESS;
} else {
world.setBlockState(up, Blocks.AIR.getDefaultState());
}
}
}
// Neither alternative option was successful, delegate the call to the super method
return super.onItemUse(context);
}).orElse(super.onItemUse(context));
}
use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.
the class TileEntityCropBase method handlePlantUpdate.
protected void handlePlantUpdate() {
if (this.getWorld() != null) {
BlockState oldState = this.getBlockState();
BlockState newState = oldState;
// Update brightness
int brightness = this.getPlant().getBrightness(this);
if (BlockCropBase.LIGHT.fetch(newState) != brightness) {
newState = BlockCropBase.LIGHT.apply(newState, brightness);
}
// Update plant state
boolean plant = this.hasPlant() || this.hasWeeds();
if (BlockCropBase.PLANT.fetch(newState) != plant) {
newState = BlockCropBase.PLANT.apply(newState, plant);
}
// Set block state if necessary
if (newState != oldState) {
this.getWorld().setBlockState(this.getPos(), newState);
}
// Update growth requirement
this.requirement.flush();
this.requirement = RequirementCache.create(this);
// Update model data
this.getModelData().setData(PROPERTY_PLANT, this.getPlant());
this.getModelData().setData(PROPERTY_PLANT_GROWTH, this.getGrowthStage());
this.getModelData().setData(PROPERTY_WEED, this.getWeeds());
this.getModelData().setData(PROPERTY_WEED_GROWTH, this.getWeedGrowthStage());
}
}
use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.
the class TileEntityCropBase method executePlantGrowthTick.
protected void executePlantGrowthTick() {
if (!this.getGrowthStage().isFinal()) {
BlockState state = this.getBlockState();
if (this.calculateGrowthRate() > this.getRandom().nextDouble() && !MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Grow.Plant.Pre(this))) {
// also do the MinecraftForge BlockEvent for crop growth
BlockEvent.CropGrowEvent.Pre blockEvent = new BlockEvent.CropGrowEvent.Pre(this.getWorld(), this.getPos(), state);
MinecraftForge.EVENT_BUS.post(blockEvent);
if (blockEvent.getResult() == Event.Result.ALLOW || blockEvent.getResult() == Event.Result.DEFAULT) {
this.setGrowthStage(this.getGrowthStage().getNextStage(this, this.getRandom()));
this.getPlant().onGrowth(this);
MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Grow.Plant.Post(this));
MinecraftForge.EVENT_BUS.post(new BlockEvent.CropGrowEvent.Post(this.getWorld(), this.getPos(), state, state));
}
}
}
}
use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.
the class BlockGrate method getStateForPlacement.
@Override
@Nullable
public BlockState getStateForPlacement(BlockItemUseContext context) {
BlockState state = this.getDefaultState();
if (state.isValidPosition(context.getWorld(), context.getPos())) {
BlockPos clicked = context.getPos().offset(context.getFace().getOpposite());
BlockState target = context.getWorld().getBlockState(clicked);
// If a grate is clicked on a grate, mimic its placement if a side face is clicked
if (target.getBlock() instanceof BlockGrate) {
if (context.getFace().getAxis() != AXIS.fetch(target)) {
return AXIS.apply(OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), OFFSET.fetch(target)), AXIS.fetch(target));
}
}
// Determine the axis according to the face the player clicked and his look vector
Vector3d hit = context.getHitVec();
double offset;
if (context.getFace().getAxis() == Direction.Axis.Y) {
// The player clicked a horizontal face, determine the axis based on the player's orientation
if (context.getPlacementHorizontalFacing().getAxis() == Direction.Axis.X) {
// player is looking in the X direction
state = AXIS.apply(state, Direction.Axis.X);
offset = hit.getX() - ((int) hit.getX());
} else {
// player is looking in the Z direction
state = AXIS.apply(state, Direction.Axis.Z);
offset = hit.getZ() - ((int) hit.getZ());
}
} else {
// The player clicked a vertical face, the axis will be Y
state = AXIS.apply(state, Direction.Axis.Y);
offset = hit.getY() - ((int) hit.getY());
}
// Finally, determine the offset by how far along the block the player clicked
offset += offset < 0 ? 1 : 0;
if (offset >= 11 * Constants.UNIT) {
return OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), Offset.FAR);
} else if (offset <= 5 * Constants.UNIT) {
return OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), Offset.NEAR);
} else {
return OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), Offset.MID);
}
}
return null;
}
Aggregations