use of net.minecraft.item.BlockItem in project Overloaded by CJ-MC-Mods.
the class PlayerInteractionUtil method placeBlock.
@Nonnull
public static BlockPlaceResult placeBlock(@Nonnull ItemStack searchStack, @Nonnull ServerPlayerEntity player, @Nonnull World worldIn, @Nonnull BlockPos newPosition, @Nonnull Direction facing, @Nonnull IEnergyStorage energy, float hitX, float hitY, float hitZ) {
// Can we place a block at this Pos
BlockItem itemBlock = ((BlockItem) searchStack.getItem());
// if (worldIn.loadedAndEntityCanStandOn(newPosition, player)) {
// return BlockPlaceResult.FAIL_DENY;
// }
BlockSnapshot blockSnapshot = BlockSnapshot.create(worldIn.dimension(), worldIn, newPosition);
BlockState placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().relative(facing.getOpposite()));
BlockEvent.EntityPlaceEvent event = new BlockEvent.EntityPlaceEvent(blockSnapshot, placedAgainst, player);
MinecraftForge.EVENT_BUS.post(event);
if (event.isCanceled()) {
return BlockPlaceResult.FAIL_DENY;
}
long distance = Math.round(Math.sqrt(player.blockPosition().distSqr(newPosition)));
long cost = OverloadedConfig.INSTANCE.multiToolConfig.placeBaseCost + OverloadedConfig.INSTANCE.multiToolConfig.costPerMeterAway * distance;
if (!player.abilities.instabuild && (cost > Integer.MAX_VALUE || cost < 0 || energy.getEnergyStored() < cost))
return BlockPlaceResult.FAIL_ENERGY;
LazyOptional<IItemHandler> opInventory = player.getCapability(ITEM_HANDLER_CAPABILITY, Direction.UP);
if (!opInventory.isPresent()) {
Overloaded.logger.warn("Player has no ItemHandler Capability? NBT: " + player.serializeNBT());
return BlockPlaceResult.FAIL_PREREQUISITE;
}
IItemHandler inventory = opInventory.orElseThrow(() -> new RuntimeException("Impossible Condition"));
int foundStackSlot = findItemStackSlot(searchStack, inventory);
if (foundStackSlot == -1) {
return BlockPlaceResult.FAIL_PREREQUISITE;
}
ItemStack foundStack = inventory.extractItem(foundStackSlot, 1, player.abilities.instabuild);
BlockItemUseContext context = new BlockItemUseContextPublic(worldIn, player, Hand.MAIN_HAND, foundStack, new BlockRayTraceResult(new Vector3d(hitX + newPosition.getX(), hitY + newPosition.getY(), hitZ + newPosition.getZ()), facing, newPosition, false));
ActionResultType result = ForgeHooks.onPlaceItemIntoWorld(context);
switch(result) {
case CONSUME:
case SUCCESS:
SoundType soundtype = worldIn.getBlockState(newPosition).getBlock().getSoundType(worldIn.getBlockState(newPosition), worldIn, newPosition, player);
worldIn.playSound(null, newPosition, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
if (!player.abilities.instabuild) {
energy.extractEnergy((int) cost, false);
}
return BlockPlaceResult.SUCCESS;
case PASS:
case FAIL:
default:
inventory.insertItem(foundStackSlot, foundStack, player.abilities.instabuild);
return BlockPlaceResult.FAIL_DENY;
}
}
use of net.minecraft.item.BlockItem in project Overloaded by CJ-MC-Mods.
the class ItemMultiTool method rightClickWithItem.
public void rightClickWithItem(@Nonnull ServerPlayerEntity player, RightClickBlockMessage message) {
BlockPos pos = message.getPos();
Direction sideHit = message.getHitSide();
float hitX = message.getHitX();
float hitY = message.getHitY();
float hitZ = message.getHitZ();
ServerWorld worldIn = player.getLevel();
ItemStack multiTool = player.getMainHandItem();
if (multiTool.getItem() != this) {
return;
}
ItemStack blockStack = getSelectedBlockItemStack(multiTool);
if (blockStack.isEmpty()) {
player.displayClientMessage(new StringTextComponent("No block type selected to place."), true);
return;
}
if (!(blockStack.getItem() instanceof BlockItem)) {
player.displayClientMessage(new StringTextComponent("No valid block type selected to place."), true);
return;
}
LazyOptional<IEnergyStorage> opEnergy = multiTool.getCapability(ENERGY);
if (!opEnergy.isPresent()) {
Overloaded.logger.warn("MultiTool has no Energy Capability? NBT: " + multiTool.getTag());
return;
}
IEnergyStorage energy = opEnergy.orElseThrow(() -> new RuntimeException("Impossible Condition"));
Vector3i sideVector = sideHit.getNormal();
BlockPos.Mutable newPosition = pos.offset(sideVector).mutable();
switch(placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ)) {
case FAIL_PREREQUISITE:
player.displayClientMessage(new StringTextComponent("Do not have the required items"), true);
return;
case FAIL_DENY:
player.displayClientMessage(new StringTextComponent("Unable to place blocks"), true);
return;
case FAIL_RANGE:
player.displayClientMessage(new StringTextComponent("To far away"), true);
return;
case FAIL_ENERGY:
player.displayClientMessage(new StringTextComponent("Not enough energy"), true);
return;
case SUCCESS:
}
if (player.isShiftKeyDown()) {
BlockPos playerPos = player.blockPosition();
switch(sideHit) {
case UP:
while (newPosition.getY() < playerPos.getY()) {
newPosition.move(sideHit);
if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
break;
}
break;
case DOWN:
while (newPosition.getY() > playerPos.getY()) {
newPosition.move(sideHit);
if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
break;
}
break;
case NORTH:
while (newPosition.getZ() > playerPos.getZ()) {
newPosition.move(sideHit);
if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
break;
}
break;
case SOUTH:
while (newPosition.getZ() < playerPos.getZ()) {
newPosition.move(sideHit);
if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
break;
}
break;
case EAST:
while (newPosition.getX() < playerPos.getX()) {
newPosition.move(sideHit);
if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
break;
}
break;
case WEST:
while (newPosition.getX() > playerPos.getX()) {
newPosition.move(sideHit);
if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
break;
}
break;
}
}
}
use of net.minecraft.item.BlockItem in project NetherEx by LogicTechCorp.
the class NetherExBlocks method registerBlockWithBasicItem.
private static RegistryObject<Block> registerBlockWithBasicItem(String name, Block.Properties properties, ItemGroup group) {
RegistryObject<Block> registryObject = registerBlock(name, () -> new Block(properties));
ITEM_BLOCKS.register(name, () -> new BlockItem(registryObject.get(), new Item.Properties().group(group)));
return registryObject;
}
use of net.minecraft.item.BlockItem in project NetherEx by LogicTechCorp.
the class NetherExBlocks method registerBlockWithBasicItem.
private static <B extends Block> RegistryObject<B> registerBlockWithBasicItem(String name, Supplier<B> supplier, ItemGroup group) {
RegistryObject<B> registryObject = registerBlock(name, supplier);
ITEM_BLOCKS.register(name, () -> new BlockItem(registryObject.get(), new Item.Properties().group(group)));
return registryObject;
}
use of net.minecraft.item.BlockItem in project Geolosys by oitsjustjose.
the class BlockColorInit method registerItemColors.
@SubscribeEvent
public static void registerItemColors(ColorHandlerEvent.Item evt) {
final BlockColors blockColors = evt.getBlockColors();
final ItemColors itemColors = evt.getItemColors();
// Use the Block's colour handler for an ItemBlock
IItemColor itemBlockColourHandler = (stack, tintIndex) -> {
BlockState state = ((BlockItem) stack.getItem()).getBlock().getDefaultState();
return blockColors.getColor(state, null, null, tintIndex);
};
if (itemBlockColourHandler != null) {
itemColors.register(itemBlockColourHandler, ModBlocks.getInstance().peat);
itemColors.register(itemBlockColourHandler, ModBlocks.getInstance().rhododendron);
}
}
Aggregations