Search in sources :

Example 1 with LanternGameMode

use of org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode in project LanternServer by LanternPowered.

the class AbstractUser method registerKeys.

@Override
public void registerKeys() {
    super.registerKeys();
    final ValueCollection c = getValueCollection();
    c.register(LanternKeys.ACCESSORIES, new ArrayList<>());
    c.register(LanternKeys.MAX_EXHAUSTION, 40.0, 0.0, Double.MAX_VALUE);
    c.register(Keys.EXHAUSTION, DEFAULT_EXHAUSTION, 0.0, LanternKeys.MAX_EXHAUSTION);
    c.register(LanternKeys.MAX_FOOD_LEVEL, 20, 0, Integer.MAX_VALUE);
    c.register(Keys.FOOD_LEVEL, 20, 0, LanternKeys.MAX_FOOD_LEVEL);
    c.registerWithSuppliedMax(Keys.SATURATION, DEFAULT_SATURATION, 0.0, container -> container.get(Keys.FOOD_LEVEL).orElse(20).doubleValue());
    c.register(Keys.LAST_DATE_PLAYED, null);
    c.register(Keys.FIRST_DATE_PLAYED, null);
    c.registerNonRemovable(Keys.WALKING_SPEED, 0.1);
    c.registerNonRemovable(LanternKeys.FIELD_OF_VIEW_MODIFIER, 1.0);
    c.registerNonRemovable(Keys.IS_FLYING, false);
    c.registerNonRemovable(Keys.IS_SNEAKING, false);
    c.registerNonRemovable(Keys.IS_SPRINTING, false);
    c.registerNonRemovable(Keys.FLYING_SPEED, 0.1);
    c.registerNonRemovable(Keys.CAN_FLY, false);
    c.registerNonRemovable(Keys.RESPAWN_LOCATIONS, new HashMap<>());
    c.registerNonRemovable(Keys.GAME_MODE, GameModes.NOT_SET).addListener((oldElement, newElement) -> {
        ((LanternGameMode) newElement).getAbilityApplier().accept(this);
        // This MUST be updated, unless you want strange behavior on the client,
        // the client has 3 different concepts of 'isCreative', and each combination
        // gives a different outcome...
        // For example:
        // - Disable noClip and glow in spectator, but you can place blocks
        // - NoClip in creative, but you cannot change your hotbar, or drop items
        // Not really worth the trouble right now
        // TODO: Differentiate the 'global tab list entry' and the entry to update
        // TODO: these kind of settings to avoid possible 'strange' behavior.
        GlobalTabList.getInstance().get(getProfile()).ifPresent(e -> e.setGameMode(newElement));
    });
    c.registerNonRemovable(Keys.DOMINANT_HAND, HandPreferences.RIGHT);
    c.registerNonRemovable(LanternKeys.IS_ELYTRA_FLYING, false);
    c.registerNonRemovable(LanternKeys.ELYTRA_GLIDE_SPEED, 0.1);
    c.registerNonRemovable(LanternKeys.ELYTRA_SPEED_BOOST, false);
    c.registerNonRemovable(LanternKeys.SUPER_STEVE, false);
    c.registerNonRemovable(LanternKeys.CAN_WALL_JUMP, false);
    c.registerNonRemovable(LanternKeys.CAN_DUAL_WIELD, false);
    c.registerNonRemovable(LanternKeys.SCORE, 0);
    c.registerNonRemovable(LanternKeys.ACTIVE_HAND, Optional.empty());
    c.registerNonRemovable(LanternKeys.RECIPE_BOOK_FILTER_ACTIVE, false);
    c.registerNonRemovable(LanternKeys.RECIPE_BOOK_GUI_OPEN, false);
    c.registerProcessor(Keys.STATISTICS).add(builder -> builder.offerHandler((key, valueContainer, map) -> {
        this.statisticMap.setStatisticValues(map);
        return DataTransactionResult.successNoData();
    }).retrieveHandler((key, valueContainer) -> Optional.of(this.statisticMap.getStatisticValues())).failAlwaysRemoveHandler());
    c.registerNonRemovable(LanternKeys.OPEN_ADVANCEMENT_TREE, Optional.empty());
}
Also used : LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) HandPreferences(org.spongepowered.api.data.type.HandPreferences) Vector3d(com.flowpowered.math.vector.Vector3d) HashMap(java.util.HashMap) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) ValueCollection(org.lanternpowered.server.data.ValueCollection) ArrayList(java.util.ArrayList) ChestInventory(org.lanternpowered.server.inventory.vanilla.block.ChestInventory) SubjectCollection(org.spongepowered.api.service.permission.SubjectCollection) Text(org.spongepowered.api.text.Text) NetworkSession(org.lanternpowered.server.network.NetworkSession) VanillaInventoryArchetypes(org.lanternpowered.server.inventory.vanilla.VanillaInventoryArchetypes) LanternWorld(org.lanternpowered.server.world.LanternWorld) UserStore(org.lanternpowered.server.data.io.store.entity.UserStore) GameProfile(org.spongepowered.api.profile.GameProfile) GlobalTabList(org.lanternpowered.server.entity.living.player.tab.GlobalTabList) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) Nullable(javax.annotation.Nullable) SubjectData(org.spongepowered.api.service.permission.SubjectData) User(org.spongepowered.api.entity.living.player.User) Context(org.spongepowered.api.service.context.Context) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) UUID(java.util.UUID) StatisticMap(org.lanternpowered.server.statistic.StatisticMap) AbstractUserInventory(org.lanternpowered.server.inventory.vanilla.AbstractUserInventory) LanternWorldProperties(org.lanternpowered.server.world.LanternWorldProperties) Tristate(org.spongepowered.api.util.Tristate) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) List(java.util.List) Lantern(org.lanternpowered.server.game.Lantern) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Optional(java.util.Optional) LanternHumanoid(org.lanternpowered.server.entity.LanternHumanoid) ValueCollection(org.lanternpowered.server.data.ValueCollection)

Example 2 with LanternGameMode

use of org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode in project LanternServer by LanternPowered.

the class PlayerEntityProtocol method spawn.

@Override
protected void spawn(EntityProtocolUpdateContext context) {
    super.spawn(context);
    context.sendToSelf(() -> new MessagePlayOutEntityMetadata(getRootEntityId(), fillParameters(true)));
    final GameMode gameMode = getEntity().get(Keys.GAME_MODE).get();
    context.sendToSelf(() -> new MessagePlayOutSetGameMode((LanternGameMode) gameMode));
    context.sendToSelf(() -> new MessagePlayOutPlayerAbilities(this.entity.get(Keys.IS_FLYING).orElse(false), canFly(), false, gameMode == GameModes.CREATIVE, getFlySpeed(), getFovModifier()));
    final TopHat topHat = getTopHat();
    if (topHat != null && !getEntity().get(Keys.INVISIBLE).get()) {
        sendPassengerStack(context);
        sendHat(context, topHat);
    }
}
Also used : MessagePlayOutSetGameMode(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSetGameMode) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) TopHat(org.lanternpowered.server.extra.accessory.TopHat) MessagePlayOutSetGameMode(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSetGameMode) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) MessagePlayOutEntityMetadata(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityMetadata) MessagePlayOutPlayerAbilities(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutPlayerAbilities)

Example 3 with LanternGameMode

use of org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode in project LanternServer by LanternPowered.

the class CommandGameMode method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    final ImmutableMap.Builder<String, Object> baseBuilder = ImmutableMap.builder();
    final ImmutableMap.Builder<String, Object> aliasesBuilder = ImmutableMap.builder();
    for (GameMode gameMode : Sponge.getRegistry().getAllOf(GameMode.class)) {
        // Ignore the not set game mode
        if (gameMode == GameModes.NOT_SET) {
            continue;
        }
        baseBuilder.put(gameMode.getName(), gameMode);
        aliasesBuilder.put(((LanternGameMode) gameMode).getInternalId() + "", gameMode);
    }
    aliasesBuilder.put("s", GameModes.SURVIVAL);
    aliasesBuilder.put("c", GameModes.CREATIVE);
    aliasesBuilder.put("a", GameModes.ADVENTURE);
    aliasesBuilder.put("sp", GameModes.SPECTATOR);
    specBuilder.arguments(ChoicesElement.of(Text.of("game-mode"), baseBuilder.build(), aliasesBuilder.build(), false, true), GenericArguments.playerOrSource(Text.of("player"))).executor((src, args) -> {
        final GameMode gameMode = args.<GameMode>getOne("game-mode").get();
        final Player player = args.<Player>getOne("player").get();
        player.offer(Keys.GAME_MODE, gameMode);
        final Text gameModeText = Text.of(gameMode.getTranslation());
        if (player == src) {
            src.sendMessage(t("commands.gamemode.success.self", gameModeText));
        } else {
            if (((RuleHolder) player.getWorld()).getOrCreateRule(RuleTypes.SEND_COMMAND_FEEDBACK).getValue()) {
                player.sendMessage(t("gameMode.changed", gameModeText));
            }
            src.sendMessage(t("commands.gamemode.success.other", player.getName(), gameModeText));
        }
        return CommandResult.success();
    });
}
Also used : LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) Player(org.spongepowered.api.entity.living.player.Player) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) Text(org.spongepowered.api.text.Text) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with LanternGameMode

use of org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode in project LanternServer by LanternPowered.

the class GameModeRegistryModule method registerDefaults.

@Override
public void registerDefaults() {
    register(new LanternGameMode("minecraft", "not_set", "gameMode.notSet", -1, player -> {
    }));
    register(new LanternGameMode("minecraft", "survival", "gameMode.survival", 0, player -> {
        player.offer(Keys.CAN_FLY, false);
        player.offer(Keys.IS_FLYING, false);
        player.offer(Keys.INVULNERABLE, false);
    }));
    register(new LanternGameMode("minecraft", "creative", "gameMode.creative", 1, player -> {
        player.offer(Keys.CAN_FLY, true);
        player.offer(Keys.INVULNERABLE, true);
    }));
    register(new LanternGameMode("minecraft", "adventure", "gameMode.adventure", 2, player -> {
        player.offer(Keys.CAN_FLY, false);
        player.offer(Keys.IS_FLYING, false);
        player.offer(Keys.INVULNERABLE, false);
    }));
    register(new LanternGameMode("minecraft", "spectator", "gameMode.spectator", 3, player -> {
        player.offer(Keys.CAN_FLY, true);
        player.offer(Keys.IS_FLYING, true);
        player.offer(Keys.INVULNERABLE, true);
    }));
}
Also used : LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) AdditionalInternalPluginCatalogRegistryModule(org.lanternpowered.server.game.registry.AdditionalInternalPluginCatalogRegistryModule) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode)

Example 5 with LanternGameMode

use of org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode in project LanternServer by LanternPowered.

the class PlayerEntityProtocol method update.

@Override
protected void update(EntityProtocolUpdateContext context) {
    final GameMode gameMode = this.entity.get(Keys.GAME_MODE).get();
    final boolean canFly = canFly();
    final float flySpeed = getFlySpeed();
    final float fieldOfView = getFovModifier();
    if (gameMode != this.lastGameMode) {
        context.sendToSelf(() -> new MessagePlayOutSetGameMode((LanternGameMode) gameMode));
        context.sendToSelf(() -> new MessagePlayOutPlayerAbilities(this.entity.get(Keys.IS_FLYING).orElse(false), canFly, false, gameMode == GameModes.CREATIVE, flySpeed, fieldOfView));
        this.lastGameMode = gameMode;
        this.lastCanFly = canFly;
        this.lastFlySpeed = flySpeed;
        this.lastFieldOfView = fieldOfView;
    } else if (canFly != this.lastCanFly || flySpeed != this.lastFlySpeed || fieldOfView != this.lastFieldOfView) {
        context.sendToSelf(() -> new MessagePlayOutPlayerAbilities(this.entity.get(Keys.IS_FLYING).orElse(false), canFly, false, gameMode == GameModes.CREATIVE, flySpeed, fieldOfView));
        this.lastCanFly = canFly;
        this.lastFlySpeed = flySpeed;
        this.lastFieldOfView = fieldOfView;
    }
    final float health = this.entity.get(Keys.HEALTH).get().floatValue();
    final int foodLevel = this.entity.get(Keys.FOOD_LEVEL).get();
    final float saturation = this.entity.get(Keys.SATURATION).get().floatValue();
    if (health != this.lastHealth || foodLevel != this.lastFoodLevel || saturation == 0.0f != this.lastHungry) {
        context.sendToSelf(() -> new MessagePlayOutPlayerHealthUpdate(health, foodLevel, saturation));
        this.lastHealth = health;
        this.lastFoodLevel = foodLevel;
        this.lastHungry = saturation == 0.0f;
    }
    super.update(context);
    final TopHat topHat = getTopHat();
    if (topHat != this.lastTopHat) {
        if (this.lastTopHat == null) {
            sendPassengerStack(context);
            sendHat(context, topHat);
        } else if (topHat == null) {
            removePassengerStack(context);
        } else {
            sendHat(context, topHat);
        }
        this.lastTopHat = topHat;
    }
    if (this.lastYaw0 != this.lastYaw || this.lastPitch0 != this.lastPitch || this.lastFlags0 != this.lastFlags) {
        for (final int id : this.passengerStack) {
            context.sendToSelf(() -> new MessagePlayOutEntityLook(id, this.lastYaw, this.lastPitch, this.entity.isOnGround()));
        }
        if (this.lastTopHat != null) {
            context.sendToSelf(() -> new MessagePlayOutEntityHeadLook(this.passengerStack[10], this.lastYaw));
            context.sendToSelf(() -> new MessagePlayOutEntityHeadLook(this.passengerStack[11], this.lastYaw));
            context.sendToSelf(() -> new MessagePlayOutEntityHeadLook(this.passengerStack[12], this.lastYaw));
            // context.sendToSelf(() -> new MessagePlayOutEntityHeadLook(this.passengerStack[14], this.lastYaw));
            if (this.lastFlags0 != this.lastFlags) {
                final boolean glow = (this.lastFlags & 0x40) != 0;
                final ParameterList parameterList = new ByteBufParameterList(ByteBufferAllocator.unpooled());
                parameterList.add(EntityParameters.Base.FLAGS, (byte) (0x20 | (glow ? 0x40 : 0x00)));
                context.sendToAll(() -> new MessagePlayOutEntityMetadata(this.passengerStack[10], parameterList));
                context.sendToAll(() -> new MessagePlayOutEntityMetadata(this.passengerStack[11], parameterList));
            }
        }
        this.lastYaw0 = this.lastYaw;
        this.lastPitch0 = this.lastPitch;
        this.lastFlags0 = this.lastFlags;
    }
    // Some 1.11.2 magic, ultra secret stuff...
    final boolean elytraFlying = this.entity.get(LanternKeys.IS_ELYTRA_FLYING).orElse(false);
    final boolean elytraSpeedBoost = this.entity.get(LanternKeys.ELYTRA_SPEED_BOOST).orElse(false);
    if (this.lastElytraFlying != elytraFlying || this.lastElytraSpeedBoost != elytraSpeedBoost) {
        if (this.lastElytraFlying && this.lastElytraSpeedBoost) {
            context.sendToAll(() -> new MessagePlayOutDestroyEntities(this.elytraRocketId));
        } else if (elytraFlying && elytraSpeedBoost) {
            // Create the fireworks data item
            final LanternItemStack itemStack = new LanternItemStack(ItemTypes.FIREWORKS);
            // Write the item to a parameter list
            final ByteBufParameterList parameterList = new ByteBufParameterList(ByteBufferAllocator.unpooled());
            parameterList.add(EntityParameters.Fireworks.ITEM, itemStack);
            parameterList.add(EntityParameters.Fireworks.ELYTRA_BOOST_PLAYER, getRootEntityId());
            context.sendToAll(() -> new MessagePlayOutSpawnObject(this.elytraRocketId, UUID.randomUUID(), 76, 0, this.entity.getPosition(), 0, 0, Vector3d.ZERO));
            context.sendToAll(() -> new MessagePlayOutEntityMetadata(this.elytraRocketId, parameterList));
        }
        this.lastElytraSpeedBoost = elytraSpeedBoost;
        this.lastElytraFlying = elytraFlying;
    }
}
Also used : TopHat(org.lanternpowered.server.extra.accessory.TopHat) ByteBufParameterList(org.lanternpowered.server.network.entity.parameter.ByteBufParameterList) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) MessagePlayOutSpawnObject(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSpawnObject) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) MessagePlayOutSetGameMode(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSetGameMode) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) MessagePlayOutPlayerHealthUpdate(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutPlayerHealthUpdate) MessagePlayOutEntityLook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityLook) MessagePlayOutSetGameMode(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSetGameMode) MessagePlayOutDestroyEntities(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutDestroyEntities) ParameterList(org.lanternpowered.server.network.entity.parameter.ParameterList) ByteBufParameterList(org.lanternpowered.server.network.entity.parameter.ByteBufParameterList) MessagePlayOutEntityMetadata(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityMetadata) MessagePlayOutPlayerAbilities(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutPlayerAbilities) MessagePlayOutEntityHeadLook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityHeadLook)

Aggregations

LanternGameMode (org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode)5 GameMode (org.spongepowered.api.entity.living.player.gamemode.GameMode)4 LanternKeys (org.lanternpowered.server.data.key.LanternKeys)2 TopHat (org.lanternpowered.server.extra.accessory.TopHat)2 MessagePlayOutEntityMetadata (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityMetadata)2 MessagePlayOutPlayerAbilities (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutPlayerAbilities)2 MessagePlayOutSetGameMode (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSetGameMode)2 Keys (org.spongepowered.api.data.key.Keys)2 GameModes (org.spongepowered.api.entity.living.player.gamemode.GameModes)2 Text (org.spongepowered.api.text.Text)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Nullable (javax.annotation.Nullable)1