Search in sources :

Example 36 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class CommandsMixin method impl$preventPutIntoMapIfNodeIsComplex.

@SuppressWarnings("unchecked")
@Redirect(method = "fillUsableCommands", at = @At(value = "INVOKE", target = "Ljava/util/Map;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", remap = false))
private <K, V> V impl$preventPutIntoMapIfNodeIsComplex(final Map<K, V> map, final K key, final V value, final CommandNode<CommandSourceStack> rootCommandSource, final CommandNode<SharedSuggestionProvider> rootSuggestion, final CommandSourceStack source, final Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> commandNodeToSuggestionNode) {
    if (!map.containsKey(key)) {
        // done here because this check is applicable
        final ServerPlayer e = (ServerPlayer) source.getEntity();
        final Map<CommandNode<CommandSourceStack>, List<CommandNode<SharedSuggestionProvider>>> playerNodes = this.impl$playerNodeCache.get(e);
        if (!playerNodes.containsKey(key)) {
            final List<CommandNode<SharedSuggestionProvider>> children = new ArrayList<>();
            children.add((CommandNode<SharedSuggestionProvider>) value);
            playerNodes.put((CommandNode<CommandSourceStack>) key, children);
        }
        // we need to swap it out.
        if (value instanceof ArgumentCommandNode && CommandUtil.checkForCustomSuggestions(rootSuggestion)) {
            rootSuggestion.addChild(this.impl$cloneArgumentCommandNodeWithoutSuggestions((ArgumentCommandNode<SharedSuggestionProvider, ?>) value));
        } else {
            rootSuggestion.addChild((CommandNode<SharedSuggestionProvider>) value);
        }
        return map.put(key, value);
    }
    // it's ignored anyway.
    return null;
}
Also used : ArgumentCommandNode(com.mojang.brigadier.tree.ArgumentCommandNode) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) ArgumentCommandNode(com.mojang.brigadier.tree.ArgumentCommandNode) RootCommandNode(com.mojang.brigadier.tree.RootCommandNode) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) CommandNode(com.mojang.brigadier.tree.CommandNode) ServerPlayer(net.minecraft.server.level.ServerPlayer) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SharedSuggestionProvider(net.minecraft.commands.SharedSuggestionProvider) CommandSourceStack(net.minecraft.commands.CommandSourceStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 37 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer 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)

Example 38 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project MC-Prefab by Brian-Wuest.

the class StructureEventHandler method onClone.

/**
 * This occurs when a player dies and is used to make sure that a player does not get a duplicate starting house.
 *
 * @param event The player clone event.
 */
@SubscribeEvent
public static void onClone(PlayerEvent.Clone event) {
    if (event.getPlayer() instanceof ServerPlayer) {
        // Don't add the tag unless the house item was added. This way it can be added if the feature is turned on.
        // When the player is cloned, make sure to copy the tag. If this is not done the item can be given to the
        // player again if they die before the log out and log back in.
        CompoundTag originalTag = event.getOriginal().getPersistentData();
        // Use the server configuration to determine if the house should be added for this player.
        String startingItem = CommonProxy.proxyConfiguration.serverConfiguration.startingItem;
        if (startingItem != null && !startingItem.equalsIgnoreCase("Nothing")) {
            if (originalTag.contains(EntityPlayerConfiguration.PLAYER_ENTITY_TAG)) {
                CompoundTag newPlayerTag = event.getPlayer().getPersistentData();
                newPlayerTag.put(EntityPlayerConfiguration.PLAYER_ENTITY_TAG, originalTag.get(EntityPlayerConfiguration.PLAYER_ENTITY_TAG));
                // Send the persist tag to the client.
                Prefab.network.sendTo(new PlayerEntityTagMessage(originalTag.getCompound(EntityPlayerConfiguration.PLAYER_ENTITY_TAG)), ((ServerPlayer) event.getPlayer()).connection.connection, NetworkDirection.PLAY_TO_CLIENT);
            }
        }
    }
}
Also used : PlayerEntityTagMessage(com.wuest.prefab.proxy.messages.PlayerEntityTagMessage) ServerPlayer(net.minecraft.server.level.ServerPlayer) CompoundTag(net.minecraft.nbt.CompoundTag) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 39 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project MC-Prefab by Brian-Wuest.

the class StructureModerateHouse method AfterBuilding.

/**
 * This method is used after the main building is build for any additional structures or modifications.
 *
 * @param configuration The structure configuration.
 * @param world         The current world.
 * @param originalPos   The original position clicked on.
 * @param player        The player which initiated the construction.
 */
@Override
public void AfterBuilding(StructureConfiguration configuration, ServerLevel world, BlockPos originalPos, Player player) {
    ModerateHouseConfiguration houseConfig = (ModerateHouseConfiguration) configuration;
    EntityPlayerConfiguration playerConfig = EntityPlayerConfiguration.loadFromEntityData(player);
    BuildingMethods.FillFurnaces(world, this.furnacePosition);
    if (this.chestPosition != null && !playerConfig.builtStarterHouse && houseConfig.addChestContents) {
        // Fill the chest if the player hasn't generated the starting house yet.
        BuildingMethods.FillChest(world, this.chestPosition);
    }
    int minimumHeightForMineshaft = world.getMinBuildHeight() + 21;
    if (this.trapDoorPosition != null && this.trapDoorPosition.getY() > minimumHeightForMineshaft && houseConfig.addMineshaft) {
        // Build the mineshaft.
        BuildingMethods.PlaceMineShaft(world, this.trapDoorPosition.below(), houseConfig.houseFacing, false);
    }
    // Make sure to set this value so the player cannot fill the chest a second time.
    playerConfig.builtStarterHouse = true;
    playerConfig.saveToPlayer(player);
    // Make sure to send a message to the client to sync up the server player information and the client player
    // information.
    Prefab.network.sendTo(new PlayerEntityTagMessage(playerConfig.getModIsPlayerNewTag(player)), ((ServerPlayer) player).connection.connection, NetworkDirection.PLAY_TO_CLIENT);
}
Also used : ModerateHouseConfiguration(com.wuest.prefab.structures.config.ModerateHouseConfiguration) PlayerEntityTagMessage(com.wuest.prefab.proxy.messages.PlayerEntityTagMessage) EntityPlayerConfiguration(com.wuest.prefab.config.EntityPlayerConfiguration) ServerPlayer(net.minecraft.server.level.ServerPlayer)

Example 40 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project MC-Prefab by Brian-Wuest.

the class StructureEventHandler method PlayerLoggedIn.

/**
 * This event is used to determine if the player should be given the starting house item when they log in.
 *
 * @param event The event object.
 */
@SubscribeEvent
public static void PlayerLoggedIn(PlayerLoggedInEvent event) {
    if (!event.getPlayer().level.isClientSide() && event.getPlayer() instanceof ServerPlayer) {
        ServerPlayer player = (ServerPlayer) event.getPlayer();
        EntityPlayerConfiguration playerConfig = EntityPlayerConfiguration.loadFromEntityData(player);
        String startingItem = CommonProxy.proxyConfiguration.serverConfiguration.startingItem;
        if (!playerConfig.givenHouseBuilder && startingItem != null) {
            ItemStack stack = ItemStack.EMPTY;
            switch(startingItem.toLowerCase()) {
                case "starting house":
                    {
                        stack = new ItemStack(ModRegistry.StartHouse.get());
                        break;
                    }
                case "moderate house":
                    {
                        stack = new ItemStack(ModRegistry.ModerateHouse.get());
                        break;
                    }
            }
            if (!stack.isEmpty()) {
                System.out.println(player.getDisplayName().getString() + " joined the game for the first time. Giving them starting item.");
                player.getInventory().add(stack);
                player.containerMenu.broadcastChanges();
                // Make sure to set the tag for this player so they don't get the item again.
                playerConfig.givenHouseBuilder = true;
                playerConfig.saveToPlayer(player);
            }
        }
        // Send the tag to the client.
        Prefab.network.sendTo(new PlayerEntityTagMessage(playerConfig.getModIsPlayerNewTag(player)), ((ServerPlayer) event.getPlayer()).connection.connection, NetworkDirection.PLAY_TO_CLIENT);
    }
}
Also used : PlayerEntityTagMessage(com.wuest.prefab.proxy.messages.PlayerEntityTagMessage) EntityPlayerConfiguration(com.wuest.prefab.config.EntityPlayerConfiguration) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemStack(net.minecraft.world.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

ServerPlayer (net.minecraft.server.level.ServerPlayer)71 TamableAnimal (net.minecraft.world.entity.TamableAnimal)12 Behavior (de.Keyle.MyPet.api.skill.skills.Behavior)11 ServerLevel (net.minecraft.server.level.ServerLevel)11 ArmorStand (net.minecraft.world.entity.decoration.ArmorStand)11 ItemStack (net.minecraft.world.item.ItemStack)10 CraftPlayer (org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer)10 Player (org.bukkit.entity.Player)10 MyPet (de.Keyle.MyPet.api.entity.MyPet)8 EntityMyPet (de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet)8 CraftPlayer (org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer)8 Player (net.minecraft.world.entity.player.Player)7 EntityMyPet (de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet)6 ArrayList (java.util.ArrayList)6 ClientboundRemoveEntitiesPacket (net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket)6 ChunkMap (net.minecraft.server.level.ChunkMap)6 ServerGamePacketListenerImpl (net.minecraft.server.network.ServerGamePacketListenerImpl)6 LivingEntity (net.minecraft.world.entity.LivingEntity)6 ItemEntity (net.minecraft.world.entity.item.ItemEntity)6 PlayerEntityTagMessage (com.wuest.prefab.proxy.messages.PlayerEntityTagMessage)5