Skip to content

Commit

Permalink
#588 Set pet to auto hide while the owner is flying.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shynixn committed Jul 13, 2024
1 parent 212c942 commit 5ee5e5e
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/wiki/docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 18 additions & 18 deletions docs/wiki/docs/placeholders.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -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 |
|-------------------------------------------|------------------------------------------------------------|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package com.github.shynixn.petblocks.enumeration

enum class PetVisibility {
ALL,
OWNER
OWNER,
NOBODY
}
Original file line number Diff line number Diff line change
Expand Up @@ -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%"),
Expand All @@ -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%"),
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/com/github/shynixn/petblocks/impl/PetEntityImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/shynixn/petblocks/impl/PetImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class PetImpl(
petMeta.visibility = value

if (previousVisibility != value && petEntity != null) {
petEntity!!.updateVisibility(visibility, player, location)
petEntity!!.playerComponent.forceMinecraftTick()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PlayerComponent(
* Render distance blocks.
*/
renderDistanceBlocks: Int = 70,
private val pet : Pet
private val pet: Pet
) : PhysicComponent {
var lastTimeRenderUpdate = 0L

Expand All @@ -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

Expand All @@ -47,11 +55,15 @@ class PlayerComponent(
val players = HashSet<Player>()
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) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lang/en_us.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/pets/pet_classic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/pets/pet_flying_dolphin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/pets/pet_mining.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5ee5e5e

Please sign in to comment.