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);
}
}
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;
}
}
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);
}
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);
}
}
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());
}
}
}
Aggregations