Search in sources :

Example 56 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project Tropicraft by Tropicraft.

the class TropicraftTrees method create.

private static AbstractTreeGrower create(FeatureProvider featureProvider) {
    return new AbstractTreeGrower() {

        @Nullable
        @Override
        protected ConfiguredFeature<TreeConfiguration, ?> getConfiguredFeature(Random random, boolean beehive) {
            return null;
        }

        @Override
        public boolean growTree(ServerLevel world, ChunkGenerator generator, BlockPos pos, BlockState sapling, Random random) {
            ConfiguredFeature<?, ?> feature = featureProvider.getFeature(world.getServer(), random, this.hasFlowers(world, pos));
            if (feature == null) {
                return false;
            }
            world.setBlock(pos, Blocks.AIR.defaultBlockState(), Block.UPDATE_INVISIBLE);
            if (feature.place(world, generator, random, pos)) {
                return true;
            } else {
                world.setBlock(pos, sapling, Block.UPDATE_INVISIBLE);
                return false;
            }
        }

        private boolean hasFlowers(LevelAccessor world, BlockPos origin) {
            BlockPos min = origin.offset(-2, -1, -2);
            BlockPos max = origin.offset(2, 1, 2);
            for (BlockPos pos : BlockPos.MutableBlockPos.betweenClosed(min, max)) {
                if (world.getBlockState(pos).is(BlockTags.FLOWERS)) {
                    return true;
                }
            }
            return false;
        }
    };
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) AbstractTreeGrower(net.minecraft.world.level.block.grower.AbstractTreeGrower) LevelAccessor(net.minecraft.world.level.LevelAccessor) BlockState(net.minecraft.world.level.block.state.BlockState) Random(java.util.Random) BlockPos(net.minecraft.core.BlockPos) ChunkGenerator(net.minecraft.world.level.chunk.ChunkGenerator) TreeConfiguration(net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration)

Example 57 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project Tropicraft by Tropicraft.

the class TropicraftDimension method teleportPlayerNoPortal.

/**
 * Finds the top Y position relative to the dimension the player is teleporting to and places
 * the entity at that position. Avoids portal generation by using player.teleport() instead of
 * player.changeDimension()
 *
 * @param player The player that will be teleported
 * @param destination The target dimension to teleport to
 */
public static void teleportPlayerNoPortal(ServerPlayer player, ResourceKey<Level> destination) {
    ServerLevel world = player.server.getLevel(destination);
    if (world == null) {
        LOGGER.error("Cannot teleport player to dimension {} as it does not exist!", destination.location());
        return;
    }
    if (!ForgeHooks.onTravelToDimension(player, destination))
        return;
    int x = Mth.floor(player.getX());
    int z = Mth.floor(player.getZ());
    LevelChunk chunk = world.getChunk(x >> 4, z >> 4);
    int topY = chunk.getHeight(Heightmap.Types.WORLD_SURFACE, x & 15, z & 15);
    player.teleportTo(world, x + 0.5, topY + 1.0, z + 0.5, player.getYRot(), player.getXRot());
    BasicEventHooks.firePlayerChangedDimensionEvent(player, destination, destination);
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) LevelChunk(net.minecraft.world.level.chunk.LevelChunk)

Example 58 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project Tropicraft by Tropicraft.

the class EntityAIPartyTime method tick.

@Override
public void tick() {
    super.tick();
    boolean isClose = false;
    BlockPos blockposGoal = null;
    if (this.entityObj.listPosDrums.size() > assignedDrumIndex) {
        blockposGoal = entityObj.listPosDrums.get(assignedDrumIndex);
    }
    if (entityObj.level.getGameTime() % 200 == 0) {
        if (this.entityObj.listPosDrums.size() > 0) {
            assignedDrumIndex = entityObj.level.random.nextInt(entityObj.listPosDrums.size());
        }
        // if (wasClose) {
        bangDrum = entityObj.level.random.nextBoolean();
    // }
    }
    if (blockposGoal == null) {
        stop();
        return;
    }
    // prevent walking onto source
    double dist = entityObj.position().distanceTo(new Vec3(blockposGoal.getX(), blockposGoal.getY(), blockposGoal.getZ()));
    if (dist < 8D) {
        wasClose = true;
    }
    if (dist < 3D && entityObj.isOnGround()) {
        isClose = true;
        entityObj.getNavigation().stop();
        if (!bangDrum) {
            // entityObj.setSitting(true);
            entityObj.setDancing(true);
            this.entityObj.getJumpControl().jump();
            this.entityObj.setYRot(entityObj.level.random.nextInt(360));
        } else {
            entityObj.setDancing(false);
            if (true || lookUpdateTimer <= 0) {
                entityObj.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
                // keep for testing, was neat sounding
                // entityObj.level.random.nextInt(10) + 1;
                int amp = 1;
                int rate = 4 + (entityObj.getId() % 7);
                int index1 = 0;
                HashMap<Integer, List<Integer>> lookupStateToSequence = new HashMap<>();
                List<Integer> listDelays = Lists.newArrayList(9, 3, 3, 3, 6);
                lookupStateToSequence.put(index1++, listDelays);
                lookupStateToSequence.put(index1++, listDelays);
                lookupStateToSequence.put(index1++, listDelays);
                lookupStateToSequence.put(index1++, Lists.newArrayList(9, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 12));
                int nightStart = 12500;
                int nightEnd = 23500;
                int phases = 4;
                int phaseSplit = (nightEnd - nightStart) / phases;
                int timeOfDay = (int) (entityObj.level.getDayTime() % 24000);
                int nightTime = (timeOfDay - nightStart);
                if (nightTime > phaseSplit * 3) {
                    amp = 1;
                } else if (nightTime > phaseSplit * 2) {
                    amp = 2;
                } else if (nightTime > phaseSplit * 1) {
                    amp = 3;
                } else {
                    amp = 4;
                }
                if (entityObj.hitIndex2 >= lookupStateToSequence.get(entityObj.hitIndex).size()) {
                    entityObj.hitIndex2 = 0;
                    entityObj.hitIndex++;
                }
                if (entityObj.hitIndex >= lookupStateToSequence.size()) {
                    entityObj.hitIndex = 0;
                }
                rate = lookupStateToSequence.get(entityObj.hitIndex).get(entityObj.hitIndex2);
                if (entityObj.hitDelay > 0) {
                    entityObj.hitDelay--;
                }
                boolean perEntDelay = false;
                boolean hit = false;
                if (perEntDelay) {
                    if (entityObj.hitDelay <= 0) {
                        entityObj.hitDelay = (amp * rate);
                        hit = true;
                    }
                } else {
                    hit = entityObj.level.getGameTime() % (amp * rate) == 0;
                }
                if (hit) {
                    // System.out.println("stage: " + entityObj.hitIndex + " - " + entityObj.hitIndex2);
                    entityObj.hitIndex2++;
                    BlockState state = entityObj.level.getBlockState(blockposGoal);
                    /*if (state.getBlock() instanceof BlockBongoDrum) {
                            //((BlockBongoDrum) state.getOwner()).playBongoSound(entityObj.world, null, blockposGoal, state);
                            TropicraftBongos bongo = ((BlockBongoDrum) state.getOwner()).getVariant(state);
                            float pitch = (entityObj.level.random.nextFloat() * 1F) + 0F;
                            entityObj.world.playSound(null, blockposGoal.getX(), blockposGoal.getY() + 0.5D, blockposGoal.getZ(),
                                    bongo.getSoundEvent(), SoundCategory.BLOCKS, 2.5F, pitch);
                            entityObj.swingArm(Hand.MAIN_HAND);
                        } else */
                    if (state.getBlock() instanceof NoteBlock) {
                        if (entityObj.level.random.nextInt(10) == 0) {
                            for (int i = 0; i < 1 + entityObj.level.random.nextInt(4); i++) {
                                // note.changePitch();
                                state.cycle(NoteBlock.NOTE).getValue(NoteBlock.NOTE);
                            }
                        } else {
                            // note.triggerNote(entityObj.world, blockposGoal);
                            state.getBlock().attack(state, entityObj.level, blockposGoal, FakePlayerFactory.get((ServerLevel) entityObj.level, new GameProfile(UUID.fromString(" e517cf6a-ce31-4ac8-b48d-44b4f0f918a7"), "tropicraftKoa")));
                        }
                        entityObj.swing(InteractionHand.MAIN_HAND);
                    }
                }
                entityObj.syncBPM();
            }
            this.entityObj.getLookControl().setLookAt(blockposGoal.getX() + randXPos, blockposGoal.getY() + randYPos + 1D, blockposGoal.getZ() + randZPos, 8F, 8F);
        }
    } else {
        wasClose = false;
        entityObj.setSitting(false);
    }
    if (!isClose) {
        entityObj.setDancing(true);
        if ((this.entityObj.getNavigation().isDone() || walkingTimeout <= 0) && repathPentalty <= 0) {
            int i = blockposGoal.getX();
            int j = blockposGoal.getY();
            int k = blockposGoal.getZ();
            boolean success = false;
            if (this.entityObj.distanceToSqr(Vec3.atCenterOf(blockposGoal)) > 256.0) {
                Vec3 Vector3d = DefaultRandomPos.getPosTowards(this.entityObj, 14, 3, new Vec3((double) i + 0.5D, (double) j, (double) k + 0.5D), (float) Math.PI / 2F);
                if (Vector3d != null) {
                    success = this.entityObj.getNavigation().moveTo(Vector3d.x, Vector3d.y, Vector3d.z, 1.0D);
                } else {
                    success = Util.tryMoveToXYZLongDist(this.entityObj, new BlockPos(i, j, k), 1);
                // System.out.println("success? " + success);
                }
            } else {
                success = this.entityObj.getNavigation().moveTo((double) i + 0.5D, j, (double) k + 0.5D, 1.0D);
            }
            if (!success) {
                repathPentalty = 40;
            } else {
                walkingTimeout = walkingTimeoutMax;
            }
        } else {
            if (walkingTimeout > 0) {
                walkingTimeout--;
            } else {
            }
        }
    }
    if (repathPentalty > 0) {
        repathPentalty--;
    }
    if (lookUpdateTimer > 0) {
        lookUpdateTimer--;
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) HashMap(java.util.HashMap) NoteBlock(net.minecraft.world.level.block.NoteBlock) BlockState(net.minecraft.world.level.block.state.BlockState) GameProfile(com.mojang.authlib.GameProfile) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) List(java.util.List)

Example 59 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project Tropicraft by Tropicraft.

the class TropicalFertilizerItem method useOn.

@Override
public InteractionResult useOn(UseOnContext context) {
    BlockState state = context.getLevel().getBlockState(context.getClickedPos());
    if (state.getBlock() == Blocks.GRASS_BLOCK) {
        if (!context.getLevel().isClientSide) {
            // Logic from GrassBlock#grow, with probability for grass significantly reduced
            BlockPos blockpos = context.getClickedPos().above();
            BlockState blockstate = Blocks.GRASS.defaultBlockState();
            Level world = context.getLevel();
            Random rand = world.getRandom();
            for (int i = 0; i < 128; ++i) {
                BlockPos blockpos1 = blockpos;
                int j = 0;
                while (true) {
                    if (j >= i / 16) {
                        BlockState blockstate2 = world.getBlockState(blockpos1);
                        if (blockstate2.getBlock() == blockstate.getBlock() && rand.nextInt(10) == 0) {
                            if (world instanceof ServerLevel) {
                                ((BonemealableBlock) blockstate.getBlock()).performBonemeal((ServerLevel) world, rand, blockpos1, blockstate2);
                            }
                        }
                        if (!blockstate2.isAir()) {
                            break;
                        }
                        BlockState blockstate1;
                        if (rand.nextInt(8) > 0) {
                            // Modification here, == changed to > to invert chances
                            List<ConfiguredFeature<?, ?>> list = world.getBiome(blockpos1).getGenerationSettings().getFlowerFeatures();
                            if (list.isEmpty()) {
                                break;
                            }
                            // TODO this is so ugly and hacky, pls
                            ConfiguredFeature<?, ?> pFlowerFeature = list.get(0);
                            AbstractFlowerFeature<FeatureConfiguration> abstractflowerfeature = (AbstractFlowerFeature) pFlowerFeature.feature;
                            blockstate1 = abstractflowerfeature.getRandomFlower(rand, blockpos1, pFlowerFeature.config());
                        } else {
                            blockstate1 = blockstate;
                        }
                        if (blockstate1.canSurvive(world, blockpos1)) {
                            world.setBlockAndUpdate(blockpos1, blockstate1);
                        }
                        break;
                    }
                    blockpos1 = blockpos1.offset(rand.nextInt(3) - 1, (rand.nextInt(3) - 1) * rand.nextInt(3) / 2, rand.nextInt(3) - 1);
                    if (world.getBlockState(blockpos1.below()).getBlock() != Blocks.GRASS_BLOCK || world.getBlockState(blockpos1).isCollisionShapeFullBlock(world, blockpos1)) {
                        break;
                    }
                    ++j;
                }
            }
        }
        return InteractionResult.SUCCESS;
    }
    return super.useOn(context);
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) ConfiguredFeature(net.minecraft.world.level.levelgen.feature.ConfiguredFeature) BonemealableBlock(net.minecraft.world.level.block.BonemealableBlock) FeatureConfiguration(net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration) BlockState(net.minecraft.world.level.block.state.BlockState) Random(java.util.Random) BlockPos(net.minecraft.core.BlockPos) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) AbstractFlowerFeature(net.minecraft.world.level.levelgen.feature.AbstractFlowerFeature)

Example 60 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project Tropicraft by Tropicraft.

the class ScubaData method onPlayerTick.

@SubscribeEvent
public static void onPlayerTick(PlayerTickEvent event) {
    Level world = event.player.level;
    if (event.phase == Phase.END) {
        // TODO support more than chest slot?
        ItemStack chestStack = event.player.getItemBySlot(EquipmentSlot.CHEST);
        Item chestItem = chestStack.getItem();
        if (chestItem instanceof ScubaArmorItem) {
            LazyOptional<ScubaData> data = event.player.getCapability(CAPABILITY);
            if (!world.isClientSide) {
                underwaterPlayers.add((ServerPlayer) event.player);
            }
            if (isUnderWater(event.player)) {
                data.ifPresent(d -> {
                    d.tick(event.player);
                    if (!world.isClientSide) {
                        d.updateClient((ServerPlayer) event.player, false);
                    }
                });
                ((ScubaArmorItem) chestItem).tickAir(event.player, EquipmentSlot.CHEST, chestStack);
                if (!world.isClientSide && world.getGameTime() % 60 == 0) {
                    // TODO this effect could be better, custom packet?
                    Vec3 eyePos = event.player.getEyePosition(0);
                    Vec3 motion = event.player.getDeltaMovement();
                    Vec3 particlePos = eyePos.add(motion.reverse());
                    ((ServerLevel) world).sendParticles(ParticleTypes.BUBBLE, particlePos.x(), particlePos.y(), particlePos.z(), 4 + world.random.nextInt(3), 0.25, 0.25, 0.25, motion.length());
                }
            } else if (!world.isClientSide && underwaterPlayers.remove(event.player)) {
                // Update client state as they leave the water
                data.ifPresent(d -> d.updateClient((ServerPlayer) event.player, false));
            }
        }
    }
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Direction(net.minecraft.core.Direction) Item(net.minecraft.world.item.Item) CapabilityManager(net.minecraftforge.common.capabilities.CapabilityManager) EventBusSubscriber(net.minecraftforge.fml.common.Mod.EventBusSubscriber) PacketDistributor(net.minecraftforge.fmllegacy.network.PacketDistributor) TropicraftDimension(net.tropicraft.core.common.dimension.TropicraftDimension) FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) ServerLevel(net.minecraft.server.level.ServerLevel) LazyOptional(net.minecraftforge.common.util.LazyOptional) ServerPlayer(net.minecraft.server.level.ServerPlayer) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) Nonnull(javax.annotation.Nonnull) Phase(net.minecraftforge.event.TickEvent.Phase) MessageUpdateScubaData(net.tropicraft.core.common.network.message.MessageUpdateScubaData) Nullable(javax.annotation.Nullable) WeakHashMap(java.util.WeakHashMap) PlayerEvent(net.minecraftforge.event.entity.player.PlayerEvent) PlayerTickEvent(net.minecraftforge.event.TickEvent.PlayerTickEvent) PlayerChangedDimensionEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerChangedDimensionEvent) FluidTags(net.minecraft.tags.FluidTags) Set(java.util.Set) AttachCapabilitiesEvent(net.minecraftforge.event.AttachCapabilitiesEvent) Capability(net.minecraftforge.common.capabilities.Capability) Player(net.minecraft.world.entity.player.Player) PlayerRespawnEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerRespawnEvent) TropicraftPackets(net.tropicraft.core.common.network.TropicraftPackets) ICapabilitySerializable(net.minecraftforge.common.capabilities.ICapabilitySerializable) CompoundTag(net.minecraft.nbt.CompoundTag) Entity(net.minecraft.world.entity.Entity) BlockPos(net.minecraft.core.BlockPos) Vec3(net.minecraft.world.phys.Vec3) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Constants(net.tropicraft.Constants) PlayerLoggedInEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerLoggedInEvent) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) CapabilityToken(net.minecraftforge.common.capabilities.CapabilityToken) ParticleTypes(net.minecraft.core.particles.ParticleTypes) Bus(net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus) Collections(java.util.Collections) INBTSerializable(net.minecraftforge.common.util.INBTSerializable) Item(net.minecraft.world.item.Item) MessageUpdateScubaData(net.tropicraft.core.common.network.message.MessageUpdateScubaData) ServerLevel(net.minecraft.server.level.ServerLevel) Vec3(net.minecraft.world.phys.Vec3) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

ServerLevel (net.minecraft.server.level.ServerLevel)115 BlockPos (net.minecraft.core.BlockPos)30 Nullable (org.checkerframework.checker.nullness.qual.Nullable)21 Level (net.minecraft.world.level.Level)20 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)16 Entity (net.minecraft.world.entity.Entity)14 ServerPlayer (net.minecraft.server.level.ServerPlayer)11 ItemStack (net.minecraft.world.item.ItemStack)10 SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)10 IOException (java.io.IOException)9 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)9 CraftWorld (org.bukkit.craftbukkit.v1_18_R1.CraftWorld)9 ArrayList (java.util.ArrayList)8 BlockState (net.minecraft.world.level.block.state.BlockState)8 List (java.util.List)7 Optional (java.util.Optional)7 UUID (java.util.UUID)7 Vec3 (net.minecraft.world.phys.Vec3)7 CraftWorld (org.bukkit.craftbukkit.v1_17_R1.CraftWorld)7 Cause (org.spongepowered.api.event.Cause)7