From 5ee5e5e4d3f4251cada4aa7ddfdc6f5683e0dea1 Mon Sep 17 00:00:00 2001 From: Shynixn Date: Sat, 13 Jul 2024 17:29:51 +0200 Subject: [PATCH] #588 Set pet to auto hide while the owner is flying. --- docs/wiki/docs/commands.md | 2 +- docs/wiki/docs/placeholders.md | 36 +++++++++---------- .../petblocks/enumeration/PetVisibility.kt | 3 +- .../petblocks/enumeration/PlaceHolder.kt | 3 +- .../shynixn/petblocks/impl/PetEntityImpl.kt | 13 ------- .../github/shynixn/petblocks/impl/PetImpl.kt | 2 +- .../PetBlocksCommandExecutor.kt | 2 +- .../petblocks/impl/physic/PlayerComponent.kt | 18 ++++++++-- .../provider/PetBlocksPlaceHolderProvider.kt | 2 ++ src/main/resources/lang/en_us.properties | 2 +- src/main/resources/pets/pet_classic.yml | 16 +++++++++ .../resources/pets/pet_flying_dolphin.yml | 16 +++++++++ src/main/resources/pets/pet_mining.yml | 16 +++++++++ 13 files changed, 91 insertions(+), 40 deletions(-) diff --git a/docs/wiki/docs/commands.md b/docs/wiki/docs/commands.md index 8c126bc73..7e203136d 100644 --- a/docs/wiki/docs/commands.md +++ b/docs/wiki/docs/commands.md @@ -281,7 +281,7 @@ Allows values should be set in the ``config.yml``. Changes who can see the pet. * Name: Identifier of a pet -* Types: Possibles values: ``ALL``, ``OWNER`` +* Types: Possibles values: ``ALL``, ``OWNER``, ``NOBODY`` * Player: Optional player_name/player_UUID parameter targeting a player from the console or command block. ### /petblocks loop diff --git a/docs/wiki/docs/placeholders.md b/docs/wiki/docs/placeholders.md index b00e1dc07..8f23ffaf4 100644 --- a/docs/wiki/docs/placeholders.md +++ b/docs/wiki/docs/placeholders.md @@ -7,24 +7,24 @@ The following placeholders are available in PetBlocks and can also be used via P This results into placeholders such as e.g. ``%petblocks_pet_displayName_1%`` or ``%petblocks_pet_displayName_selected%``. This is only relevant in external plugins. For actions in PetBlocks, you can directly use the placeholders below. -| Player PlaceHolders | Description | -|---------------------------------|-------------------------------------------------| -| %petblocks_owner_name% | Player name of the owner of a pet. | -| %petblocks_owner_displayName% | Player displayName of the owner of a pet. | -| %petblocks_owner_locationWorld% | Name of the world the owner of a pet is inside. | -| %petblocks_owner_locationX% | X coordinate of the owner of a pet. | -| %petblocks_owner_locationY% | Y coordinate of the owner of a pet. | -| %petblocks_owner_locationZ% | Z coordinate of the owner of a pet. | -| %petblocks_owner_locationYaw% | Yaw rotation of the owner of a pet. | -| %petblocks_owner_locationPitch% | Yaw rotation of the owner of a pet. | -| %petblocks_owner_itemMainHand_type% | Name of the item type in the owner's main hand | +| Player PlaceHolders | Description | +|--------------------------------|------------------------------------------------------| +| %petblocks_owner_name% | Player name of the owner of a pet. | +| %petblocks_owner_displayName% | Player displayName of the owner of a pet. | +| %petblocks_owner_locationWorld% | Name of the world the owner of a pet is inside. | +| %petblocks_owner_locationX% | X coordinate of the owner of a pet. | +| %petblocks_owner_locationY% | Y coordinate of the owner of a pet. | +| %petblocks_owner_locationZ% | Z coordinate of the owner of a pet. | +| %petblocks_owner_locationYaw% | Yaw rotation of the owner of a pet. | +| %petblocks_owner_locationPitch% | Yaw rotation of the owner of a pet. | +| %petblocks_owner_itemMainHand_type% | Name of the item type in the owner's main hand | +| %petblocks_owner_isFlying% | True if the owner is currently flying, false if not. | | Pet PlaceHolders | Description | |---------------------------------|----------------------------------------------------------------------------------------------------| | %petblocks_pet_name% | Id of a pet | | %petblocks_pet_displayName% | Displayname of a pet | | %petblocks_pet_distanceToOwner% | Distance from the pet to the owner. If the owner is in another world, this value becomes very high. | -| %petblocks_pet_exists% | True if the pet exists, false if not. | | %petblocks_pet_isSpawned% | True if the pet is spawned, false if not. | | %petblocks_pet_template% | Name of the template a pet uses. | | %petblocks_pet_visibility% | Visibility Type a pet uses. | @@ -49,13 +49,13 @@ The following placeholders are available in PetBlocks and can also be used via P | %petblocks_eventPlayer_name% | Player name of the event trigger player. | | %petblocks_eventPlayer_displayName% | Player displayName of the event trigger player. | | %petblocks_eventPlayer_locationWorld% | Name of the world event trigger player of a pet is inside. | -| %petblocks_eventPlayer_locationX% | X coordinate of the event trigger player. | -| %petblocks_eventPlayer_locationY% | Y coordinate of the event trigger player. | -| %petblocks_eventPlayer_locationZ% | Z coordinate of the event trigger player. | -| %petblocks_eventPlayer_locationYaw% | Yaw rotation of the event trigger player. | -| %petblocks_eventPlayer_locationPitch% | Yaw rotation of the event trigger player. | +| %petblocks_eventPlayer_locationX% | X coordinate of the event trigger player. | +| %petblocks_eventPlayer_locationY% | Y coordinate of the event trigger player. | +| %petblocks_eventPlayer_locationZ% | Z coordinate of the event trigger player. | +| %petblocks_eventPlayer_locationYaw% | Yaw rotation of the event trigger player. | +| %petblocks_eventPlayer_locationPitch% | Yaw rotation of the event trigger player. | | %petblocks_eventPlayer_itemMainHand_type% | Name of the item type in the players's main hand | - +| %petblocks_eventPlayer_isFlying% | True if the player is currently flying, false if not. | | GUI PlaceHolders | Description | |-------------------------------------------|------------------------------------------------------------| diff --git a/src/main/java/com/github/shynixn/petblocks/enumeration/PetVisibility.kt b/src/main/java/com/github/shynixn/petblocks/enumeration/PetVisibility.kt index 4341f725d..cdb251cf3 100644 --- a/src/main/java/com/github/shynixn/petblocks/enumeration/PetVisibility.kt +++ b/src/main/java/com/github/shynixn/petblocks/enumeration/PetVisibility.kt @@ -2,5 +2,6 @@ package com.github.shynixn.petblocks.enumeration enum class PetVisibility { ALL, - OWNER + OWNER, + NOBODY } diff --git a/src/main/java/com/github/shynixn/petblocks/enumeration/PlaceHolder.kt b/src/main/java/com/github/shynixn/petblocks/enumeration/PlaceHolder.kt index aca0d14ce..330b94632 100644 --- a/src/main/java/com/github/shynixn/petblocks/enumeration/PlaceHolder.kt +++ b/src/main/java/com/github/shynixn/petblocks/enumeration/PlaceHolder.kt @@ -11,6 +11,7 @@ enum class PlaceHolder(val fullPlaceHolder: String) { PLAYER_OWNER_LOCATION_YAW("%petblocks_owner_locationYaw%"), PLAYER_OWNER_LOCATION_PITCH("%petblocks_owner_locationPitch%"), PLAYER_OWNER_ITEMMAINHAND_TYPE("%petblocks_owner_itemMainHand_type%"), + PLAYER_OWNER_ISFLYING("%petblocks_owner_isFlying%"), // Event Player EVENT_PLAYER_OWNER_NAME("%petblocks_eventPlayer_name%"), @@ -22,12 +23,12 @@ enum class PlaceHolder(val fullPlaceHolder: String) { EVENT_PLAYER_OWNER_LOCATION_YAW("%petblocks_eventPlayer_locationYaw%"), EVENT_PLAYER_OWNER_LOCATION_PITCH("%petblocks_eventPlayer_locationPitch%"), EVENT_PLAYER_OWNER_ITEMMAINHAND_TYPE("%petblocks_eventPlayer_itemMainHand_type%"), + EVENT_PLAYER_OWNER_ISFLYING("%petblocks_eventPlayer_isFlying%"), // Pet PlaceHolders PET_NAME("%petblocks_pet_name%"), PET_DISPLAYNAME("%petblocks_pet_displayName%"), PET_DISTANCETOOWNER("%petblocks_pet_distanceToOwner%"), - PET_EXISTS("%petblocks_pet_exists%"), PET_ISSPAWNED("%petblocks_pet_isSpawned%"), PET_TEMPLATE("%petblocks_pet_template%"), PET_VISIBILITY("%petblocks_pet_visibility%"), diff --git a/src/main/java/com/github/shynixn/petblocks/impl/PetEntityImpl.kt b/src/main/java/com/github/shynixn/petblocks/impl/PetEntityImpl.kt index 0ce71fec9..ba936ceb5 100644 --- a/src/main/java/com/github/shynixn/petblocks/impl/PetEntityImpl.kt +++ b/src/main/java/com/github/shynixn/petblocks/impl/PetEntityImpl.kt @@ -379,19 +379,6 @@ class PetEntityImpl( return rayTraceResult.hitBlock } - /** - * If owner parameter is not null, only the owner receives packets. - */ - fun updateVisibility(visibility: PetVisibility, owner: Player, location: Location) { - for (player in playerComponent.visiblePlayers) { - if (visibility == PetVisibility.OWNER && player != owner) { - playerComponent.onRemoveMinecraft.forEach { e -> e.invoke(player, location) } - } else if (visibility == PetVisibility.ALL && player != owner) { - playerComponent.onSpawnMinecraft.forEach { e -> e.invoke(player, location) } - } - } - } - /** * Updates the displayName in the world. */ diff --git a/src/main/java/com/github/shynixn/petblocks/impl/PetImpl.kt b/src/main/java/com/github/shynixn/petblocks/impl/PetImpl.kt index cd25960e2..d3c33c48d 100644 --- a/src/main/java/com/github/shynixn/petblocks/impl/PetImpl.kt +++ b/src/main/java/com/github/shynixn/petblocks/impl/PetImpl.kt @@ -176,7 +176,7 @@ class PetImpl( petMeta.visibility = value if (previousVisibility != value && petEntity != null) { - petEntity!!.updateVisibility(visibility, player, location) + petEntity!!.playerComponent.forceMinecraftTick() } } diff --git a/src/main/java/com/github/shynixn/petblocks/impl/commandexecutor/PetBlocksCommandExecutor.kt b/src/main/java/com/github/shynixn/petblocks/impl/commandexecutor/PetBlocksCommandExecutor.kt index ab1ee537f..94aa8ed42 100644 --- a/src/main/java/com/github/shynixn/petblocks/impl/commandexecutor/PetBlocksCommandExecutor.kt +++ b/src/main/java/com/github/shynixn/petblocks/impl/commandexecutor/PetBlocksCommandExecutor.kt @@ -915,7 +915,7 @@ class PetBlocksCommandExecutor @Inject constructor( sender: CommandSender, pet: Pet, visibility: PetVisibility ) { pet.visibility = visibility - sender.sendPluginMessage(String.format(PetBlocksLanguage.visibilityChangedMessage, visibility.name)) + sender.sendPluginMessage(String.format(PetBlocksLanguage.visibilityChangedMessage, pet.name, visibility)) } private fun setPetLoop(sender: CommandSender, pet: Pet, loop: String) { diff --git a/src/main/java/com/github/shynixn/petblocks/impl/physic/PlayerComponent.kt b/src/main/java/com/github/shynixn/petblocks/impl/physic/PlayerComponent.kt index 2a81df080..0841156c4 100644 --- a/src/main/java/com/github/shynixn/petblocks/impl/physic/PlayerComponent.kt +++ b/src/main/java/com/github/shynixn/petblocks/impl/physic/PlayerComponent.kt @@ -20,7 +20,7 @@ class PlayerComponent( * Render distance blocks. */ renderDistanceBlocks: Int = 70, - private val pet : Pet + private val pet: Pet ) : PhysicComponent { var lastTimeRenderUpdate = 0L @@ -38,6 +38,14 @@ class PlayerComponent( */ val onRemoveMinecraft: MutableList<(Player, Location) -> Unit> = arrayListOf() + /** + * Forces to perform rendering logic immediately. + */ + fun forceMinecraftTick() { + lastTimeRenderUpdate = 0L + tickMinecraft() + } + override fun tickMinecraft() { val currentTime = Date().time @@ -47,11 +55,15 @@ class PlayerComponent( val players = HashSet() val location = physicsComponent.position.toLocation() for (player in location.world!!.players) { + if (pet.visibility == PetVisibility.NOBODY) { + continue + } + if (player.location.distanceSquared(location) <= renderVisibilityDistance) { if (!visiblePlayers.contains(player)) { - if( pet.visibility == PetVisibility.ALL){ + if (pet.visibility == PetVisibility.ALL) { onSpawnMinecraft.forEach { e -> e.invoke(player, location) } - }else if(pet.visibility == PetVisibility.OWNER && player == pet.player){ + } else if (pet.visibility == PetVisibility.OWNER && player == pet.player) { onSpawnMinecraft.forEach { e -> e.invoke(player, location) } } diff --git a/src/main/java/com/github/shynixn/petblocks/impl/provider/PetBlocksPlaceHolderProvider.kt b/src/main/java/com/github/shynixn/petblocks/impl/provider/PetBlocksPlaceHolderProvider.kt index b12f9ee0c..cbd8b4968 100644 --- a/src/main/java/com/github/shynixn/petblocks/impl/provider/PetBlocksPlaceHolderProvider.kt +++ b/src/main/java/com/github/shynixn/petblocks/impl/provider/PetBlocksPlaceHolderProvider.kt @@ -68,6 +68,7 @@ class PetBlocksPlaceHolderProvider( ) } registerPlayerPlaceHolder(PlaceHolder.PLAYER_OWNER_ITEMMAINHAND_TYPE) { player -> "minecraft:" + player.inventory.itemInMainHand.type.name.lowercase() } + registerPlayerPlaceHolder(PlaceHolder.PLAYER_OWNER_ISFLYING) { player -> player.isFlying.toString() } // Event Player registerPlayerPlaceHolder(PlaceHolder.EVENT_PLAYER_OWNER_NAME) { player: Player -> player.name } @@ -109,6 +110,7 @@ class PetBlocksPlaceHolderProvider( ) } registerPlayerPlaceHolder(PlaceHolder.EVENT_PLAYER_OWNER_ITEMMAINHAND_TYPE) { player -> "minecraft:" + player.inventory.itemInMainHand.type.name.lowercase() } + registerPlayerPlaceHolder(PlaceHolder.EVENT_PLAYER_OWNER_ISFLYING) { player -> player.isFlying.toString() } // Pet registerPetPlaceHolder(PlaceHolder.PET_NAME) { pet -> pet.name } diff --git a/src/main/resources/lang/en_us.properties b/src/main/resources/lang/en_us.properties index b70778bdb..747c2a8e0 100644 --- a/src/main/resources/lang/en_us.properties +++ b/src/main/resources/lang/en_us.properties @@ -9,7 +9,7 @@ petCalledMessage=[&9PetBlocks&f] Pet with name %1$1s has been called. petSpawnedMessage=[&9PetBlocks&f] Pet with name %1$1s has been spawned. petDespawnedMessage=[&9PetBlocks&f] Pet with name %1$1s has been removed. visibilityTypeNotFoundMessage=[&9PetBlocks&f] &cOnly the visibility types %1$1s are supported. -visibilityChangedMessage=[&9PetBlocks&f] The visibility of pet %1$1s has been changed. +visibilityChangedMessage=[&9PetBlocks&f] The visibility of pet %1$1s has been changed to %2$1s. petSkinTypeChangedMessage=[&9PetBlocks&f] The skinType of pet %1$1s has been changed. petSkinNbtChanged=[&9PetBlocks&f] The skin of pet %1$1s has been changed. petSkinTypeNotFound=[&9PetBlocks&f] &cMaterial %1$1s not found. diff --git a/src/main/resources/pets/pet_classic.yml b/src/main/resources/pets/pet_classic.yml index 56fe9df9e..376566445 100644 --- a/src/main/resources/pets/pet_classic.yml +++ b/src/main/resources/pets/pet_classic.yml @@ -153,6 +153,22 @@ loops: level: "SERVER" run: - "/petblocks moveToOwner %petblocks_pet_name% 0.2 %petblocks_owner_name%" + - name: "Hide the pet while player is flying" + condition: + type: JAVASCRIPT + js: "%petblocks_owner_isFlying% && '%petblocks_pet_visibility%' != 'NOBODY'" + type: "COMMAND" + level: "SERVER" + run: + - "/petblocks visibility %petblocks_pet_name% NOBODY %petblocks_owner_name%" + - name: "Show the pet while player is not flying" + condition: + type: JAVASCRIPT + js: "!%petblocks_owner_isFlying% && '%petblocks_pet_visibility%' == 'NOBODY'" + type: "COMMAND" + level: "SERVER" + run: + - "/petblocks visibility %petblocks_pet_name% ALL %petblocks_owner_name%" - name: "Switch to idle if the pet is beside the player" condition: type: NUMBER_LESS_THAN diff --git a/src/main/resources/pets/pet_flying_dolphin.yml b/src/main/resources/pets/pet_flying_dolphin.yml index b51873d7e..83f450109 100644 --- a/src/main/resources/pets/pet_flying_dolphin.yml +++ b/src/main/resources/pets/pet_flying_dolphin.yml @@ -153,6 +153,22 @@ loops: level: "SERVER" run: - "/petblocks moveToOwner %petblocks_pet_name% 0.2 %petblocks_owner_name%" + - name: "Hide the pet while player is flying" + condition: + type: JAVASCRIPT + js: "%petblocks_owner_isFlying% && '%petblocks_pet_visibility%' != 'NOBODY'" + type: "COMMAND" + level: "SERVER" + run: + - "/petblocks visibility %petblocks_pet_name% NOBODY %petblocks_owner_name%" + - name: "Show the pet while player is not flying" + condition: + type: JAVASCRIPT + js: "!%petblocks_owner_isFlying% && '%petblocks_pet_visibility%' == 'NOBODY'" + type: "COMMAND" + level: "SERVER" + run: + - "/petblocks visibility %petblocks_pet_name% ALL %petblocks_owner_name%" - name: "Switch to idle if the pet is beside the player" condition: type: NUMBER_LESS_THAN diff --git a/src/main/resources/pets/pet_mining.yml b/src/main/resources/pets/pet_mining.yml index 30253e987..bbe9b3c8f 100644 --- a/src/main/resources/pets/pet_mining.yml +++ b/src/main/resources/pets/pet_mining.yml @@ -165,6 +165,22 @@ loops: level: "SERVER" run: - "/petblocks moveToOwner %petblocks_pet_name% 0.2 %petblocks_owner_name%" + - name: "Hide the pet while player is flying" + condition: + type: JAVASCRIPT + js: "%petblocks_owner_isFlying% && '%petblocks_pet_visibility%' != 'NOBODY'" + type: "COMMAND" + level: "SERVER" + run: + - "/petblocks visibility %petblocks_pet_name% NOBODY %petblocks_owner_name%" + - name: "Show the pet while player is not flying" + condition: + type: JAVASCRIPT + js: "!%petblocks_owner_isFlying% && '%petblocks_pet_visibility%' == 'NOBODY'" + type: "COMMAND" + level: "SERVER" + run: + - "/petblocks visibility %petblocks_pet_name% ALL %petblocks_owner_name%" - name: "Switch to idle if the pet is beside the player" condition: type: NUMBER_LESS_THAN