Search in sources :

Example 6 with ServerWorld

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

the class JsonPlantCallBackFungus method onRightClickPre.

@Override
public Optional<ActionResultType> onRightClickPre(@Nonnull IAgriCrop crop, @Nonnull ItemStack stack, @Nullable Entity entity) {
    // Fetch world
    World world = crop.world();
    // Perform checks
    if (stack.isEmpty()) {
        return Optional.empty();
    }
    if (stack.getItem() != Items.BONE_MEAL) {
        return Optional.empty();
    }
    if (world == null) {
        return Optional.empty();
    }
    if (!crop.isMature()) {
        return Optional.empty();
    }
    if (!this.fungus.canGrow(world, crop.getPosition(), crop.getBlockState(), world.isRemote())) {
        return Optional.empty();
    }
    if (!this.fungus.canUseBonemeal(world, world.getRandom(), crop.getPosition(), crop.getBlockState())) {
        return Optional.empty();
    }
    // grow
    if (world instanceof ServerWorld) {
        fungus.grow((ServerWorld) world, world.getRandom(), crop.getPosition(), crop.getBlockState());
        world.playEvent(2005, crop.getPosition(), 0);
    }
    return Optional.of(ActionResultType.SUCCESS);
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World)

Example 7 with ServerWorld

use of net.minecraft.world.server.ServerWorld in project BluePower by Qmunity.

the class TileBPMultipart method removeState.

public void removeState(BlockState state) {
    // Drop Items
    if (level instanceof ServerWorld) {
        NonNullList<ItemStack> drops = NonNullList.create();
        drops.addAll(Block.getDrops(state, (ServerWorld) level, worldPosition, this));
        InventoryHelper.dropContents(level, worldPosition, drops);
    }
    // Remove Tile Entity
    if (stateMap.get(state) != null) {
        stateMap.get(state).setRemoved();
    }
    // Remove State
    this.stateMap.remove(state);
    markDirtyClient();
    if (stateMap.size() == 1) {
        // Convert back to Standalone Block
        TileEntity te = (TileEntity) stateMap.values().toArray()[0];
        if (level != null) {
            CompoundNBT nbt = te != null ? te.save(new CompoundNBT()) : null;
            level.setBlockAndUpdate(worldPosition, ((BlockState) stateMap.keySet().toArray()[0]));
            TileEntity tile = level.getBlockEntity(worldPosition);
            if (tile != null && nbt != null)
                tile.load(getBlockState(), nbt);
        }
    } else if (stateMap.size() == 0) {
        // Remove if this is empty
        if (level != null) {
            level.removeBlock(worldPosition, false);
        }
    }
    if (level != null)
        level.getBlockState(worldPosition).neighborChanged(level, worldPosition, getBlockState().getBlock(), worldPosition, false);
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) TileEntity(net.minecraft.tileentity.TileEntity) ITickableTileEntity(net.minecraft.tileentity.ITickableTileEntity) IForgeBlockState(net.minecraftforge.common.extensions.IForgeBlockState) BlockState(net.minecraft.block.BlockState) CompoundNBT(net.minecraft.nbt.CompoundNBT) ItemStack(net.minecraft.item.ItemStack)

Example 8 with ServerWorld

use of net.minecraft.world.server.ServerWorld in project BluePower by Qmunity.

the class BlockBasalt method getDrops.

@Deprecated
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
    ResourceLocation resourcelocation = this.getLootTable();
    if (resourcelocation == LootTables.EMPTY) {
        return Collections.emptyList();
    } else {
        LootContext lootcontext = builder.withParameter(LootParameters.BLOCK_STATE, state).create(LootParameterSets.BLOCK);
        ServerWorld serverworld = lootcontext.getLevel();
        LootTable loottable = serverworld.getServer().getLootTables().get(resourcelocation);
        return loottable.getRandomItems(lootcontext);
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) ResourceLocation(net.minecraft.util.ResourceLocation)

Example 9 with ServerWorld

use of net.minecraft.world.server.ServerWorld 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;
        }
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) Vector3i(net.minecraft.util.math.vector.Vector3i) BlockPos(net.minecraft.util.math.BlockPos) StringTextComponent(net.minecraft.util.text.StringTextComponent) ItemStack(net.minecraft.item.ItemStack) BlockItem(net.minecraft.item.BlockItem)

Example 10 with ServerWorld

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

the class BlockUpdateHandler method onBlockUpdate.

@SubscribeEvent
@SuppressWarnings("unused")
public void onBlockUpdate(BlockEvent.NeighborNotifyEvent event) {
    if (event.getWorld() instanceof ServerWorld) {
        ServerWorld world = (ServerWorld) event.getWorld();
        RegistryKey<World> dimension = world.getDimensionKey();
        this.listeners.computeIfPresent(dimension, (dim, chunkMap) -> {
            chunkMap.computeIfPresent(new ChunkPos(event.getPos()), (chunkPos, posMap) -> {
                posMap.computeIfPresent(event.getPos(), (pos, set) -> {
                    set.forEach(listener -> listener.onBlockUpdate(world, pos));
                    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) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

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