use of net.minecraft.block.BlockState in project Overloaded by CJ-MC-Mods.
the class ItemMultiTool method leftClickOnBlockServer.
public static void leftClickOnBlockServer(@Nonnull ServerPlayerEntity player, LeftClickBlockMessage message) {
BlockPos pos = message.getPos();
ServerWorld world = player.getLevel();
ItemStack itemStack = player.getItemInHand(Hand.MAIN_HAND);
if (itemStack.getItem() != ModItems.multiTool || world.isEmptyBlock(pos)) {
return;
}
player.startUsingItem(Hand.MAIN_HAND);
if (player.isShiftKeyDown()) {
CompoundNBT tag = itemStack.getTag();
if (tag == null) {
tag = new CompoundNBT();
}
BlockState state = world.getBlockState(pos);
Item item = Item.byBlock(state.getBlock());
ItemStack stackToPlace = new ItemStack(() -> item, 1);
CompoundNBT blockTag = new CompoundNBT();
stackToPlace.save(blockTag);
tag.put("Item", blockTag);
itemStack.setTag(tag);
ITextComponent component = stackToPlace.getDisplayName();
player.displayClientMessage(new StringTextComponent("Bound tool to ").append(component), true);
} else {
LazyOptional<IEnergyStorage> opEnergy = itemStack.getCapability(ENERGY);
if (!opEnergy.isPresent()) {
Overloaded.logger.warn("MultiTool has no Energy Capability? NBT: " + itemStack.getTag());
return;
}
// Used to catch item spawn to teleport
RegistryKey<World> worldId = world.dimension();
CommonSideEvents.enabled = true;
CommonSideEvents.world = worldId;
CommonSideEvents.pos = pos;
CommonSideEvents.uuid = player.getUUID();
IEnergyStorage energy = opEnergy.orElseThrow(() -> new RuntimeException("Impossible Error"));
int efficiency = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.BLOCK_EFFICIENCY, itemStack);
int unbreaking = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.UNBREAKING, itemStack);
switch(breakAndUseEnergy(world, pos, energy, player, efficiency, unbreaking)) {
case FAIL_REMOVE:
player.displayClientMessage(new StringTextComponent("Unable to break block, reason unknown"), true);
break;
case FAIL_ENERGY:
player.displayClientMessage(new StringTextComponent("Unable to break block, not enough energy"), true);
break;
case FAIL_UNBREAKABLE:
player.displayClientMessage(new StringTextComponent("Block is unbreakable"), true);
break;
case FAIL_RANGE:
player.displayClientMessage(new StringTextComponent("Block is out of range."), true);
break;
case SUCCESS:
break;
}
CommonSideEvents.enabled = false;
}
}
use of net.minecraft.block.BlockState in project Overloaded by CJ-MC-Mods.
the class RenderMultiToolAssist method renderWorldLastEvent.
@SubscribeEvent
public static void renderWorldLastEvent(RenderWorldLastEvent event) {
float partialTick = Minecraft.getInstance().getFrameTime();
PlayerEntity player = Minecraft.getInstance().player;
if (player.getMainHandItem().getItem() != ModItems.multiTool)
return;
RayTraceResult resultPick = player.pick(OverloadedConfig.INSTANCE.multiToolConfig.reach, partialTick, false);
if (resultPick.getType() == RayTraceResult.Type.MISS || !(resultPick instanceof BlockRayTraceResult)) {
return;
}
BlockRayTraceResult result = ((BlockRayTraceResult) resultPick);
ItemStack stack = ModItems.multiTool.getSelectedBlockItemStack(player.getMainHandItem());
BlockState state;
if (stack.getItem() instanceof BlockItem) {
state = ((BlockItem) stack.getItem()).getBlock().defaultBlockState();
state = state.getStateAtViewpoint(player.getCommandSenderWorld(), result.getBlockPos(), player.getEyePosition(partialTick));
state = state.getBlock().getStateForPlacement(new BlockItemUseContextPublic(player.getCommandSenderWorld(), player, Hand.MAIN_HAND, stack, result));
} else {
state = Blocks.COBBLESTONE.defaultBlockState();
}
switch(getAssistMode()) {
case PLACE_PREVIEW:
if (!stack.isEmpty() && state != null)
renderBlockPreview(event, result, state);
break;
case REMOVE_PREVIEW:
renderRemovePreview(event, result);
break;
case BOTH_PREVIEW:
renderRemovePreview(event, result);
if (!stack.isEmpty() && state != null)
renderBlockPreview(event, result, state);
}
}
use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.
the class DebugModeIGrowable method debugActionBlockClicked.
@Override
public void debugActionBlockClicked(ItemStack stack, ItemUseContext context) {
if (context.getWorld().isRemote) {
return;
}
BlockPos pos = context.getPos();
PlayerEntity player = context.getPlayer();
World world = context.getWorld();
// Start with the position of the block that was clicked on. '7' is gray. Btw, the chat font is not fixed width. :(
StringBuilder outputRaw = new StringBuilder(String.format("`7%1$4d,%2$4d,%3$4d`r ", pos.getX(), pos.getY(), pos.getZ()));
// Check if the clicked on block has the IGrowable interface.
final IGrowable crop = WorldHelper.getBlock(world, pos, IGrowable.class).orElse(null);
if (crop == null) {
// If it does not, add a nicely formatted report, then skip onward.
outputRaw.append(chatNotIG);
} else if (world instanceof ServerWorld) {
// Otherwise run the tests and record the results.
BlockState state = world.getBlockState(pos);
// canGrow
outputRaw.append(crop.canGrow(world, pos, state, false) ? chatTrue : chatFalse);
// canUseBonemeal
outputRaw.append(crop.canUseBonemeal(world, world.rand, pos, state) ? chatTrue : chatFalse);
// grow
crop.grow((ServerWorld) world, world.rand, pos, state);
// It's also helpful to also make clear what block was being tested.
// '3' is dark aqua.
outputRaw.append("`3");
outputRaw.append(crop.toString().replaceFirst("Block", ""));
outputRaw.append("`r");
}
// Ellipsis are added as a clue that there's more text.
// '8' is dark gray.
outputRaw.append(" `8[...]`r");
// Create a hover box with explanatory information.
StringTextComponent hoverComponent = new StringTextComponent(MessageUtil.colorize(chatInfo));
HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverComponent);
// Turn the output String into a chat message.
StringTextComponent outputComponent = new StringTextComponent(MessageUtil.colorize(outputRaw.toString()));
// Add the hover box to the chat message.
outputComponent.getStyle().setHoverEvent(hoverEvent);
// Now send the completed chat message.
player.sendMessage(outputComponent, Util.DUMMY_UUID);
}
use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.
the class CropHelper method checkGrowthSpace.
public static boolean checkGrowthSpace(World world, BlockPos pos, IAgriGrowable plant, IAgriGrowthStage stage) {
if (world == null) {
return false;
}
double height = plant.getPlantHeight(stage);
while (height > 16) {
int offset = ((int) height) / 16;
BlockPos up = pos.up(offset);
BlockState state = world.getBlockState(up);
if (!state.getBlock().isAir(state, world, up)) {
return false;
}
height -= 16;
}
return true;
}
use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.
the class ItemTrowel method tryNewPlant.
protected ActionResultType tryNewPlant(World world, BlockPos pos, ItemStack stack, @Nullable PlayerEntity player) {
if (AgriCraft.instance.getConfig().allowPlantingOutsideCropSticks()) {
BlockState newState = AgriCraft.instance.getModBlockRegistry().crop_plant.getStateForPlacement(world, pos);
if (newState != null && world.setBlockState(pos, newState, 11)) {
boolean success = AgriApi.getCrop(world, pos).map(crop -> {
if (MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Trowel.Pre(crop, stack, player))) {
return false;
}
return this.getGenome(stack).map(genome -> this.getGrowthStage(stack).map(stage -> {
boolean result = crop.plantGenome(genome, player) && this.setGrowthStage(crop, stage);
if (result) {
this.removePlant(stack);
MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Trowel.Post(crop, stack, player));
}
return result;
}).orElse(false)).orElse(false);
}).orElse(false);
if (success) {
return ActionResultType.SUCCESS;
} else {
world.setBlockState(pos, Blocks.AIR.getDefaultState());
}
}
}
return ActionResultType.FAIL;
}
Aggregations