Search in sources :

Example 1 with ClientLevel

use of net.minecraft.client.multiplayer.ClientLevel 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 2 with ClientLevel

use of net.minecraft.client.multiplayer.ClientLevel in project SpongeCommon by SpongePowered.

the class SpongePacketHandler method init.

public static void init(final SpongeChannelManager registry) {
    SpongePacketHandler.channel = registry.createChannel(ResourceKey.sponge("default"), PacketChannel.class);
    SpongePacketHandler.channel.registerTransactional(RequestBlockTrackerDataPacket.class, TrackerDataResponsePacket.class, 0).setRequestHandler(EngineConnectionTypes.SERVER_PLAYER, (requestPacket, connection, response) -> {
        final ServerPlayer player = connection.player();
        if (!player.hasPermission("sponge.debug.block-tracking")) {
            return;
        }
        final net.minecraft.server.level.ServerPlayer sender = (net.minecraft.server.level.ServerPlayer) player;
        final BlockPos pos = new BlockPos(requestPacket.x, requestPacket.y, requestPacket.z);
        if (!sender.level.hasChunkAt(pos)) {
            return;
        }
        final LevelChunkBridge levelChunkBridge = (LevelChunkBridge) sender.level.getChunkAt(pos);
        final Optional<UUID> owner = levelChunkBridge.bridge$getBlockCreatorUUID(pos);
        final Optional<UUID> notifier = levelChunkBridge.bridge$getBlockNotifierUUID(pos);
        response.success(SpongePacketHandler.createTrackerDataResponse(owner, notifier));
    });
    SpongePacketHandler.channel.registerTransactional(RequestEntityTrackerDataPacket.class, TrackerDataResponsePacket.class, 1).setRequestHandler(EngineConnectionTypes.SERVER_PLAYER, (requestPacket, connection, response) -> {
        final ServerPlayer player = connection.player();
        if (!player.hasPermission("sponge.debug.entity-tracking")) {
            return;
        }
        final net.minecraft.server.level.ServerPlayer sender = (net.minecraft.server.level.ServerPlayer) player;
        final Entity entity = sender.level.getEntity(requestPacket.entityId);
        if (!(entity instanceof CreatorTrackedBridge)) {
            return;
        }
        final CreatorTrackedBridge creatorTrackedBridge = (CreatorTrackedBridge) entity;
        final Optional<UUID> owner = creatorTrackedBridge.tracker$getCreatorUUID();
        final Optional<UUID> notifier = creatorTrackedBridge.tracker$getNotifierUUID();
        response.success(SpongePacketHandler.createTrackerDataResponse(owner, notifier));
    });
    SpongePacketHandler.channel.register(ChangeViewerEnvironmentPacket.class, 3).addHandler(ClientSideConnection.class, (packet, connection) -> {
        final ClientLevel world = Minecraft.getInstance().level;
        if (world == null) {
            return;
        }
        final DimensionType dimensionType = SpongeCommon.server().registryAccess().dimensionTypes().get(packet.dimensionLogic);
        ((LevelBridge) world).bridge$adjustDimensionLogic(dimensionType);
    });
}
Also used : Entity(net.minecraft.world.entity.Entity) DimensionType(net.minecraft.world.level.dimension.DimensionType) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) PacketChannel(org.spongepowered.api.network.channel.packet.PacketChannel) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) UUID(java.util.UUID)

Aggregations

UUID (java.util.UUID)2 ClientLevel (net.minecraft.client.multiplayer.ClientLevel)2 Entity (net.minecraft.world.entity.Entity)2 Multimap (com.google.common.collect.Multimap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 BlockPos (net.minecraft.core.BlockPos)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 EquipmentSlot (net.minecraft.world.entity.EquipmentSlot)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1 Attribute (net.minecraft.world.entity.ai.attributes.Attribute)1 AttributeInstance (net.minecraft.world.entity.ai.attributes.AttributeInstance)1 AttributeModifier (net.minecraft.world.entity.ai.attributes.AttributeModifier)1 Operation (net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation)1 CreativeModeTab (net.minecraft.world.item.CreativeModeTab)1 Item (net.minecraft.world.item.Item)1 Properties (net.minecraft.world.item.Item.Properties)1