Search in sources :

Example 1 with ServerWorld

use of net.minecraft.world.server.ServerWorld in project Bookshelf by Darkhax-Minecraft.

the class TileEntityBasic method sync.

/**
 * Synchronizes the server state of the tile with all clients tracking it.
 *
 * @param renderUpdate Whether or not a render update should happen as well. Only use this
 *        if you need to change the block model.
 */
public void sync(boolean renderUpdate) {
    if (renderUpdate) {
        this.setChanged();
        final BlockState state = this.getState();
        this.level.sendBlockUpdated(this.worldPosition, state, state, BlockFlags.DEFAULT_AND_RERENDER);
    } else if (this.level instanceof ServerWorld) {
        final IPacket<?> packet = this.getUpdatePacket();
        WorldUtils.sendToTracking((ServerWorld) this.level, this.getChunkPos(), packet, false);
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) IPacket(net.minecraft.network.IPacket) BlockState(net.minecraft.block.BlockState)

Example 2 with ServerWorld

use of net.minecraft.world.server.ServerWorld 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;
    }
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) ITextComponent(net.minecraft.util.text.ITextComponent) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World) ServerWorld(net.minecraft.world.server.ServerWorld) Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) BlockState(net.minecraft.block.BlockState) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) BlockPos(net.minecraft.util.math.BlockPos) StringTextComponent(net.minecraft.util.text.StringTextComponent) ItemStack(net.minecraft.item.ItemStack)

Example 3 with ServerWorld

use of net.minecraft.world.server.ServerWorld 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);
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) HoverEvent(net.minecraft.util.text.event.HoverEvent) BlockState(net.minecraft.block.BlockState) IGrowable(net.minecraft.block.IGrowable) BlockPos(net.minecraft.util.math.BlockPos) StringTextComponent(net.minecraft.util.text.StringTextComponent) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Example 4 with ServerWorld

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

the class BlockUpdateHandler method addListener.

public void addListener(World world, BlockPos pos, IListener listener) {
    if (world instanceof ServerWorld) {
        RegistryKey<World> dimension = world.getDimensionKey();
        this.listeners.computeIfAbsent(dimension, key -> Maps.newHashMap()).computeIfAbsent(new ChunkPos(pos), chunkPos -> Maps.newHashMap()).computeIfAbsent(pos, aPos -> Sets.newIdentityHashSet()).add(listener);
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) ServerWorld(net.minecraft.world.server.ServerWorld) BlockEvent(net.minecraftforge.event.world.BlockEvent) WorldEvent(net.minecraftforge.event.world.WorldEvent) ChunkEvent(net.minecraftforge.event.world.ChunkEvent) World(net.minecraft.world.World) ChunkPos(net.minecraft.util.math.ChunkPos) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) RegistryKey(net.minecraft.util.RegistryKey) Map(java.util.Map) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) ChunkPos(net.minecraft.util.math.ChunkPos) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World)

Example 5 with ServerWorld

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

the class BlockUpdateHandler method onChunkUnloaded.

@SubscribeEvent
@SuppressWarnings("unused")
public void onChunkUnloaded(ChunkEvent.Unload event) {
    if (event.getWorld() instanceof ServerWorld) {
        ServerWorld world = (ServerWorld) event.getWorld();
        RegistryKey<World> dimension = world.getDimensionKey();
        if (listeners.containsKey(dimension)) {
            listeners.computeIfPresent(dimension, (dim, chunkMap) -> {
                if (chunkMap.containsKey(event.getChunk().getPos())) {
                    chunkMap.remove(event.getChunk().getPos()).forEach((pos, set) -> set.forEach(listener -> listener.onChunkUnloaded(world, pos)));
                }
                return chunkMap;
            });
            listeners.get(dimension).remove(event.getChunk().getPos());
        }
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) ServerWorld(net.minecraft.world.server.ServerWorld) BlockEvent(net.minecraftforge.event.world.BlockEvent) WorldEvent(net.minecraftforge.event.world.WorldEvent) ChunkEvent(net.minecraftforge.event.world.ChunkEvent) World(net.minecraft.world.World) ChunkPos(net.minecraft.util.math.ChunkPos) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) RegistryKey(net.minecraft.util.RegistryKey) Map(java.util.Map) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) 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