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