Search in sources :

Example 46 with Level

use of net.minecraft.world.level.Level in project MinecraftForge by MinecraftForge.

the class ItemHandlerHelper method giveItemToPlayer.

/**
 * Inserts the given itemstack into the players inventory.
 * If the inventory can't hold it, the item will be dropped in the world at the players position.
 *
 * @param player The player to give the item to
 * @param stack  The itemstack to insert
 */
public static void giveItemToPlayer(Player player, @Nonnull ItemStack stack, int preferredSlot) {
    if (stack.isEmpty())
        return;
    IItemHandler inventory = new PlayerMainInvWrapper(player.getInventory());
    Level world = player.level;
    // try adding it into the inventory
    ItemStack remainder = stack;
    // insert into preferred slot first
    if (preferredSlot >= 0 && preferredSlot < inventory.getSlots()) {
        remainder = inventory.insertItem(preferredSlot, stack, false);
    }
    // then into the inventory in general
    if (!remainder.isEmpty()) {
        remainder = insertItemStacked(inventory, remainder, false);
    }
    // play sound if something got picked up
    if (remainder.isEmpty() || remainder.getCount() != stack.getCount()) {
        world.playSound(null, player.getX(), player.getY() + 0.5, player.getZ(), SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 0.2F, ((world.random.nextFloat() - world.random.nextFloat()) * 0.7F + 1.0F) * 2.0F);
    }
    // drop remaining itemstack into the world
    if (!remainder.isEmpty() && !world.isClientSide) {
        ItemEntity entityitem = new ItemEntity(world, player.getX(), player.getY() + 0.5, player.getZ(), remainder);
        entityitem.setPickUpDelay(40);
        entityitem.setDeltaMovement(entityitem.getDeltaMovement().multiply(0, 1, 0));
        world.addFreshEntity(entityitem);
    }
}
Also used : ItemEntity(net.minecraft.world.entity.item.ItemEntity) PlayerMainInvWrapper(net.minecraftforge.items.wrapper.PlayerMainInvWrapper) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack)

Example 47 with Level

use of net.minecraft.world.level.Level in project MinecraftForge by MinecraftForge.

the class GravityAttributeTest method worldTick.

@SubscribeEvent
public void worldTick(TickEvent.WorldTickEvent event) {
    if (!event.world.isClientSide) {
        if (ticks++ > 60) {
            ticks = 0;
            Level w = event.world;
            List<LivingEntity> list;
            if (w.isClientSide) {
                ClientLevel cw = (ClientLevel) w;
                list = new ArrayList<>(100);
                for (Entity e : cw.entitiesForRendering()) {
                    if (e.isAlive() && e instanceof LivingEntity)
                        list.add((LivingEntity) e);
                }
            } else {
                ServerLevel sw = (ServerLevel) w;
                Stream<LivingEntity> s = StreamSupport.stream(sw.getEntities().getAll().spliterator(), false).filter(Entity::isAlive).filter(e -> e instanceof LivingEntity).map(e -> (LivingEntity) e);
                list = s.collect(Collectors.toList());
            }
            for (LivingEntity liv : list) {
                AttributeInstance grav = liv.getAttribute(ForgeMod.ENTITY_GRAVITY.get());
                boolean inPlains = liv.level.getBiome(liv.blockPosition()).getBiomeCategory() == BiomeCategory.PLAINS;
                if (inPlains && !grav.hasModifier(REDUCED_GRAVITY)) {
                    logger.info("Granted low gravity to Entity: {}", liv);
                    grav.addTransientModifier(REDUCED_GRAVITY);
                } else if (!inPlains && grav.hasModifier(REDUCED_GRAVITY)) {
                    logger.info("Removed low gravity from Entity: {}", liv);
                    grav.removeModifier(REDUCED_GRAVITY);
                }
            }
        }
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) LivingEntity(net.minecraft.world.entity.LivingEntity) CreativeModeTab(net.minecraft.world.item.CreativeModeTab) Item(net.minecraft.world.item.Item) ForgeMod(net.minecraftforge.common.ForgeMod) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) Multimap(com.google.common.collect.Multimap) ServerLevel(net.minecraft.server.level.ServerLevel) ArrayList(java.util.ArrayList) FMLJavaModLoadingContext(net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance) AttributeModifier(net.minecraft.world.entity.ai.attributes.AttributeModifier) RegistryEvent(net.minecraftforge.event.RegistryEvent) StreamSupport(java.util.stream.StreamSupport) Mod(net.minecraftforge.fml.common.Mod) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) Properties(net.minecraft.world.item.Item.Properties) BiomeCategory(net.minecraft.world.level.biome.Biome.BiomeCategory) UUID(java.util.UUID) TickEvent(net.minecraftforge.event.TickEvent) Collectors(java.util.stream.Collectors) Attribute(net.minecraft.world.entity.ai.attributes.Attribute) List(java.util.List) MinecraftForge(net.minecraftforge.common.MinecraftForge) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) Entity(net.minecraft.world.entity.Entity) Operation(net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Level(net.minecraft.world.level.Level) LogManager(org.apache.logging.log4j.LogManager) Rarity(net.minecraft.world.item.Rarity) LivingEntity(net.minecraft.world.entity.LivingEntity) Entity(net.minecraft.world.entity.Entity) ServerLevel(net.minecraft.server.level.ServerLevel) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 48 with Level

use of net.minecraft.world.level.Level in project Denizen-For-Bukkit by DenizenScript.

the class ItemHelperImpl method renderFullMap.

/**
 * Copied from MapItem.update, redesigned slightly to render totally rather than just relative to a player.
 * Some variables manually renamed for readability.
 * Also contains reflection fixes for Spigot's FluidState bug.
 */
public static void renderFullMap(MapItemSavedData worldmap, int xMin, int zMin, int xMax, int zMax) {
    Level world = ((CraftWorld) worldmap.mapView.getWorld()).getHandle();
    int scale = 1 << worldmap.scale;
    int mapX = worldmap.x;
    int mapZ = worldmap.z;
    for (int x = xMin; x < xMax; x++) {
        double d0 = 0.0D;
        for (int z = zMin; z < zMax; z++) {
            int k2 = (mapX / scale + x - 64) * scale;
            int l2 = (mapZ / scale + z - 64) * scale;
            Multiset<MaterialColor> multiset = LinkedHashMultiset.create();
            LevelChunk chunk = world.getChunkAt(new BlockPos(k2, 0, l2));
            if (!chunk.isEmpty()) {
                ChunkPos chunkcoordintpair = chunk.getPos();
                int i3 = k2 & 15;
                int j3 = l2 & 15;
                int k3 = 0;
                double d1 = 0.0D;
                if (world.dimensionType().hasCeiling()) {
                    int l3 = k2 + l2 * 231871;
                    l3 = l3 * l3 * 31287121 + l3 * 11;
                    if ((l3 >> 20 & 1) == 0) {
                        multiset.add(Blocks.DIRT.defaultBlockState().getMapColor(world, BlockPos.ZERO), 10);
                    } else {
                        multiset.add(Blocks.STONE.defaultBlockState().getMapColor(world, BlockPos.ZERO), 100);
                    }
                    d1 = 100.0D;
                } else {
                    BlockPos.MutableBlockPos blockposition_mutableblockposition = new BlockPos.MutableBlockPos();
                    BlockPos.MutableBlockPos blockposition_mutableblockposition1 = new BlockPos.MutableBlockPos();
                    for (int i4 = 0; i4 < scale; ++i4) {
                        for (int j4 = 0; j4 < scale; ++j4) {
                            int k4 = chunk.getHeight(Heightmap.Types.WORLD_SURFACE, i4 + i3, j4 + j3) + 1;
                            BlockState iblockdata;
                            if (k4 <= world.getMinBuildHeight() + 1) {
                                iblockdata = Blocks.BEDROCK.defaultBlockState();
                            } else {
                                do {
                                    --k4;
                                    blockposition_mutableblockposition.set(chunkcoordintpair.getMinBlockX() + i4 + i3, k4, chunkcoordintpair.getMinBlockZ() + j4 + j3);
                                    iblockdata = chunk.getBlockState(blockposition_mutableblockposition);
                                } while (iblockdata.getMapColor(world, blockposition_mutableblockposition) == MaterialColor.NONE && k4 > world.getMinBuildHeight());
                                if (k4 > world.getMinBuildHeight() && !blockStateFluidIsEmpty(iblockdata)) {
                                    int l4 = k4 - 1;
                                    blockposition_mutableblockposition1.set(blockposition_mutableblockposition);
                                    BlockState iblockdata1;
                                    do {
                                        blockposition_mutableblockposition1.t(l4--);
                                        iblockdata1 = chunk.getBlockState(blockposition_mutableblockposition1);
                                        k3++;
                                    } while (l4 > world.getMinBuildHeight() && !blockStateFluidIsEmpty(iblockdata1));
                                    iblockdata = getCorrectStateForFluidBlock(world, iblockdata, blockposition_mutableblockposition);
                                }
                            }
                            worldmap.checkBanners(world, chunkcoordintpair.getMinBlockX() + i4 + i3, chunkcoordintpair.getMinBlockZ() + j4 + j3);
                            d1 += (double) k4 / (double) (scale * scale);
                            multiset.add(iblockdata.getMapColor(world, blockposition_mutableblockposition));
                        }
                    }
                }
                k3 /= scale * scale;
                double d2 = (d1 - d0) * 4.0D / (double) (scale + 4) + ((double) (x + z & 1) - 0.5D) * 0.4D;
                byte b0 = 1;
                if (d2 > 0.6D) {
                    b0 = 2;
                }
                if (d2 < -0.6D) {
                    b0 = 0;
                }
                MaterialColor materialmapcolor = Iterables.getFirst(Multisets.copyHighestCountFirst(multiset), MaterialColor.NONE);
                if (materialmapcolor == MaterialColor.WATER) {
                    d2 = (double) k3 * 0.1D + (double) (x + z & 1) * 0.2D;
                    b0 = 1;
                    if (d2 < 0.5D) {
                        b0 = 2;
                    }
                    if (d2 > 0.9D) {
                        b0 = 0;
                    }
                }
                d0 = d1;
                worldmap.updateColor(x, z, (byte) (materialmapcolor.id * 4 + b0));
            }
        }
    }
}
Also used : MaterialColor(net.minecraft.world.level.material.MaterialColor) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) BlockState(net.minecraft.world.level.block.state.BlockState) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ChunkPos(net.minecraft.world.level.ChunkPos) CraftWorld(org.bukkit.craftbukkit.v1_17_R1.CraftWorld)

Example 49 with Level

use of net.minecraft.world.level.Level in project Denizen-For-Bukkit by DenizenScript.

the class ItemHelperImpl method renderFullMap.

/**
 * Copied from MapItem.update, redesigned slightly to render totally rather than just relative to a player.
 * Some variables manually renamed for readability.
 * Also contains reflection fixes for Spigot's FluidState bug.
 */
public static void renderFullMap(MapItemSavedData worldmap, int xMin, int zMin, int xMax, int zMax) {
    Level world = ((CraftWorld) worldmap.mapView.getWorld()).getHandle();
    int scale = 1 << worldmap.scale;
    int mapX = worldmap.x;
    int mapZ = worldmap.z;
    for (int x = xMin; x < xMax; x++) {
        double d0 = 0.0D;
        for (int z = zMin; z < zMax; z++) {
            int k2 = (mapX / scale + x - 64) * scale;
            int l2 = (mapZ / scale + z - 64) * scale;
            Multiset<MaterialColor> multiset = LinkedHashMultiset.create();
            LevelChunk chunk = world.getChunkAt(new BlockPos(k2, 0, l2));
            if (!chunk.isEmpty()) {
                ChunkPos chunkcoordintpair = chunk.getPos();
                int i3 = k2 & 15;
                int j3 = l2 & 15;
                int k3 = 0;
                double d1 = 0.0D;
                if (world.dimensionType().hasCeiling()) {
                    int l3 = k2 + l2 * 231871;
                    l3 = l3 * l3 * 31287121 + l3 * 11;
                    if ((l3 >> 20 & 1) == 0) {
                        multiset.add(Blocks.DIRT.defaultBlockState().getMapColor(world, BlockPos.ZERO), 10);
                    } else {
                        multiset.add(Blocks.STONE.defaultBlockState().getMapColor(world, BlockPos.ZERO), 100);
                    }
                    d1 = 100.0D;
                } else {
                    BlockPos.MutableBlockPos blockposition_mutableblockposition = new BlockPos.MutableBlockPos();
                    BlockPos.MutableBlockPos blockposition_mutableblockposition1 = new BlockPos.MutableBlockPos();
                    for (int i4 = 0; i4 < scale; ++i4) {
                        for (int j4 = 0; j4 < scale; ++j4) {
                            int k4 = chunk.getHeight(Heightmap.Types.WORLD_SURFACE, i4 + i3, j4 + j3) + 1;
                            BlockState iblockdata;
                            if (k4 <= world.getMinBuildHeight() + 1) {
                                iblockdata = Blocks.BEDROCK.defaultBlockState();
                            } else {
                                do {
                                    --k4;
                                    blockposition_mutableblockposition.set(chunkcoordintpair.getMinBlockX() + i4 + i3, k4, chunkcoordintpair.getMinBlockZ() + j4 + j3);
                                    iblockdata = chunk.getBlockState(blockposition_mutableblockposition);
                                } while (iblockdata.getMapColor(world, blockposition_mutableblockposition) == MaterialColor.NONE && k4 > world.getMinBuildHeight());
                                if (k4 > world.getMinBuildHeight() && !blockStateFluidIsEmpty(iblockdata)) {
                                    int l4 = k4 - 1;
                                    blockposition_mutableblockposition1.set(blockposition_mutableblockposition);
                                    BlockState iblockdata1;
                                    do {
                                        blockposition_mutableblockposition1.setY(l4--);
                                        iblockdata1 = chunk.getBlockState(blockposition_mutableblockposition1);
                                        k3++;
                                    } while (l4 > world.getMinBuildHeight() && !blockStateFluidIsEmpty(iblockdata1));
                                    iblockdata = getCorrectStateForFluidBlock(world, iblockdata, blockposition_mutableblockposition);
                                }
                            }
                            worldmap.checkBanners(world, chunkcoordintpair.getMinBlockX() + i4 + i3, chunkcoordintpair.getMinBlockZ() + j4 + j3);
                            d1 += (double) k4 / (double) (scale * scale);
                            multiset.add(iblockdata.getMapColor(world, blockposition_mutableblockposition));
                        }
                    }
                }
                k3 /= scale * scale;
                double d2 = (d1 - d0) * 4.0D / (double) (scale + 4) + ((double) (x + z & 1) - 0.5D) * 0.4D;
                byte b0 = 1;
                if (d2 > 0.6D) {
                    b0 = 2;
                }
                if (d2 < -0.6D) {
                    b0 = 0;
                }
                MaterialColor materialmapcolor = Iterables.getFirst(Multisets.copyHighestCountFirst(multiset), MaterialColor.NONE);
                if (materialmapcolor == MaterialColor.WATER) {
                    d2 = (double) k3 * 0.1D + (double) (x + z & 1) * 0.2D;
                    b0 = 1;
                    if (d2 < 0.5D) {
                        b0 = 2;
                    }
                    if (d2 > 0.9D) {
                        b0 = 0;
                    }
                }
                d0 = d1;
                worldmap.updateColor(x, z, (byte) (materialmapcolor.id * 4 + b0));
            }
        }
    }
}
Also used : MaterialColor(net.minecraft.world.level.material.MaterialColor) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) BlockState(net.minecraft.world.level.block.state.BlockState) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ChunkPos(net.minecraft.world.level.ChunkPos) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld)

Example 50 with Level

use of net.minecraft.world.level.Level in project Tropicraft by Tropicraft.

the class Util method tryMoveToXYZLongDist.

/**
 * From CoroUtilPath
 * If close enough, paths to coords, if too far based on attribute, tries to find best spot towards target to pathfind to
 *
 * @param ent
 * @param x
 * @param y
 * @param z
 * @param moveSpeedAmp
 * @return
 */
public static boolean tryMoveToXYZLongDist(Mob ent, int x, int y, int z, double moveSpeedAmp) {
    Level world = ent.level;
    boolean success = false;
    if (ent.getNavigation().isDone()) {
        // ent.getDistanceToEntity(player);
        double distToPlayer = getDistance(ent, x, y, z);
        double followDist = ent.getAttribute(Attributes.FOLLOW_RANGE).getValue();
        if (distToPlayer <= followDist) {
            // boolean success = ent.getNavigator().tryMoveToEntityLiving(player, moveSpeedAmp);
            success = ent.getNavigation().moveTo(x, y, z, moveSpeedAmp);
        // System.out.println("success? " + success + "- move to player: " + ent + " -> " + player);
        } else {
            /*int x = MathHelper.floor(player.posX);
                int y = MathHelper.floor(player.posY);
                int z = MathHelper.floor(player.posZ);*/
            double d = x + 0.5F - ent.getX();
            double d2 = z + 0.5F - ent.getZ();
            double d1;
            d1 = y + 0.5F - (ent.getY() + (double) ent.getEyeHeight());
            double d3 = Mth.sqrt((float) (d * d + d2 * d2));
            float f2 = (float) ((Math.atan2(d2, d) * 180D) / 3.1415927410125732D) - 90F;
            float f3 = (float) (-((Math.atan2(d1, d3) * 180D) / 3.1415927410125732D));
            // -ent.updateRotation(rotationPitch, f3, 180D);
            float rotationPitch = -f3;
            // updateRotation(rotationYaw, f2, 180D);
            float rotationYaw = f2;
            LivingEntity center = ent;
            Random rand = world.random;
            float randLook = rand.nextInt(90) - 45;
            // int height = 10;
            // rand.nextInt(26)+(queue.get(0).retryState * 6);
            double dist = (followDist * 0.75D) + rand.nextInt((int) followDist / 2);
            int gatherX = (int) Math.floor(center.getX() + ((double) (-Math.sin((rotationYaw + randLook) / 180.0F * 3.1415927F)) * dist));
            // Math.floor(center.posY-0.5 + (double)(-MathHelper.sin(center.rotationPitch / 180.0F * 3.1415927F) * dist) - 0D); //center.posY - 0D;
            int gatherY = (int) center.getY();
            int gatherZ = (int) Math.floor(center.getZ() + ((double) (Math.cos((rotationYaw + randLook) / 180.0F * 3.1415927F)) * dist));
            BlockPos pos = new BlockPos(gatherX, gatherY, gatherZ);
            if (!world.hasChunkAt(pos))
                return false;
            BlockState state = world.getBlockState(pos);
            Block block = state.getBlock();
            int tries = 0;
            if (!world.isEmptyBlock(pos)) {
                while (tries < 30) {
                    if (world.isEmptyBlock(pos) && world.isEmptyBlock(pos.above())) /* || !block.isSideSolid(block.defaultBlockState(), world, new BlockPos(gatherX, gatherY, gatherZ), EnumFacing.UP)*/
                    {
                        break;
                    }
                    // offset++;
                    gatherY += 1;
                    pos = new BlockPos(gatherX, gatherY, gatherZ);
                    state = world.getBlockState(pos);
                    block = state.getBlock();
                    tries++;
                }
            } else {
                // int offset = 0;
                while (tries < 30) {
                    if (!world.isEmptyBlock(pos) && (state.getMaterial().isSolid() || world.getBlockState(pos).getMaterial() == Material.WATER)) {
                        break;
                    }
                    // offset++;
                    gatherY -= 1;
                    pos = new BlockPos(gatherX, gatherY, gatherZ);
                    state = world.getBlockState(pos);
                    block = state.getBlock();
                    tries++;
                }
            }
            if (tries < 30) {
                /*if (world.getBlockState(pos).getMaterial() == Material.WATER) {
                        gatherY--;
                    }*/
                success = ent.getNavigation().moveTo(gatherX, gatherY, gatherZ, moveSpeedAmp);
            // System.out.println("pp success? " + success + "- move to player: " + ent + " -> " + player);
            }
        }
    }
    return success;
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) BlockState(net.minecraft.world.level.block.state.BlockState) Random(java.util.Random) Block(net.minecraft.world.level.block.Block) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos)

Aggregations

Level (net.minecraft.world.level.Level)53 BlockPos (net.minecraft.core.BlockPos)26 ServerLevel (net.minecraft.server.level.ServerLevel)19 ItemStack (net.minecraft.world.item.ItemStack)14 BlockState (net.minecraft.world.level.block.state.BlockState)13 IOException (java.io.IOException)10 Direction (net.minecraft.core.Direction)8 ChunkPos (net.minecraft.world.level.ChunkPos)6 Path (java.nio.file.Path)5 ExecutionException (java.util.concurrent.ExecutionException)5 Nullable (javax.annotation.Nullable)5 Entity (net.minecraft.world.entity.Entity)5 Player (net.minecraft.world.entity.player.Player)5 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)5 LevelStem (net.minecraft.world.level.dimension.LevelStem)5 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)5 Map (java.util.Map)4 Set (java.util.Set)4 CompoundTag (net.minecraft.nbt.CompoundTag)4 ResourceLocation (net.minecraft.resources.ResourceLocation)4