Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add player spectates entity and stops events, fixes #2068
- Loading branch information
1 parent
77c9783
commit 630aee4
Showing
4 changed files
with
174 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...rc/main/java/com/denizenscript/denizen/paper/events/PlayerSpectatesEntityScriptEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.denizenscript.denizen.paper.events; | ||
|
||
import com.denizenscript.denizen.events.BukkitScriptEvent; | ||
import com.denizenscript.denizen.objects.EntityTag; | ||
import com.denizenscript.denizen.objects.PlayerTag; | ||
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData; | ||
import com.denizenscript.denizencore.objects.ObjectTag; | ||
import com.denizenscript.denizencore.scripts.ScriptEntryData; | ||
import com.destroystokyo.paper.event.player.PlayerStartSpectatingEntityEvent; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
|
||
public class PlayerSpectatesEntityScriptEvent extends BukkitScriptEvent implements Listener { | ||
|
||
// <--[event] | ||
// @Events | ||
// player spectates <entity> | ||
// | ||
// @Regex ^on player spectates [^]s+$ | ||
// | ||
// @Switch in <area> | ||
// | ||
// @Plugin Paper | ||
// | ||
// @Cancellable true | ||
// | ||
// @Triggers when a player starts spectating an entity. | ||
// | ||
// @Context | ||
// <context.entity> returns the entity that is being spectated. | ||
// <context.old_entity> returns the entity that was previously being spectated (or the player themself if they weren't spectating anything). | ||
// | ||
// @Player Always. | ||
// | ||
// --> | ||
|
||
public PlayerSpectatesEntityScriptEvent() { | ||
instance = this; | ||
} | ||
|
||
public static PlayerSpectatesEntityScriptEvent instance; | ||
public PlayerStartSpectatingEntityEvent event; | ||
|
||
@Override | ||
public boolean couldMatch(ScriptPath path) { | ||
return path.eventLower.startsWith("player spectates "); | ||
} | ||
|
||
@Override | ||
public boolean matches(ScriptPath path) { | ||
if (!tryEntity(new EntityTag(event.getNewSpectatorTarget()), path.eventArgLowerAt(2))) { | ||
return false; | ||
} | ||
if (!runInCheck(path, event.getPlayer().getLocation())) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "PlayerSpectates"; | ||
} | ||
|
||
@Override | ||
public ScriptEntryData getScriptEntryData() { | ||
return new BukkitScriptEntryData(new PlayerTag(event.getPlayer()), null); | ||
} | ||
|
||
@Override | ||
public ObjectTag getContext(String name) { | ||
if (name.equals("entity")) { | ||
return new EntityTag(event.getNewSpectatorTarget()); | ||
} | ||
else if (name.equals("old_entity")) { | ||
return new EntityTag(event.getCurrentSpectatorTarget()); | ||
} | ||
return super.getContext(name); | ||
} | ||
|
||
@EventHandler | ||
public void playerSpectateEvent(PlayerStartSpectatingEntityEvent event) { | ||
this.event = event; | ||
fire(event); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...rc/main/java/com/denizenscript/denizen/paper/events/PlayerStopsSpectatingScriptEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.denizenscript.denizen.paper.events; | ||
|
||
import com.denizenscript.denizen.events.BukkitScriptEvent; | ||
import com.denizenscript.denizen.objects.EntityTag; | ||
import com.denizenscript.denizen.objects.PlayerTag; | ||
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData; | ||
import com.denizenscript.denizencore.objects.ObjectTag; | ||
import com.denizenscript.denizencore.scripts.ScriptEntryData; | ||
import com.destroystokyo.paper.event.player.PlayerStopSpectatingEntityEvent; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
|
||
public class PlayerStopsSpectatingScriptEvent extends BukkitScriptEvent implements Listener { | ||
|
||
// <--[event] | ||
// @Events | ||
// player stops spectating (<entity>) | ||
// | ||
// @Regex ^on player stops spectating( [\^s]+)?$ | ||
// | ||
// @Switch in <area> | ||
// | ||
// @Plugin Paper | ||
// | ||
// @Cancellable true | ||
// | ||
// @Triggers when a player stops spectating an entity. | ||
// | ||
// @Context | ||
// <context.entity> returns the entity that was being spectated. | ||
// | ||
// @Player Always. | ||
// | ||
// --> | ||
|
||
public PlayerStopsSpectatingScriptEvent() { | ||
instance = this; | ||
} | ||
|
||
public static PlayerStopsSpectatingScriptEvent instance; | ||
public PlayerStopSpectatingEntityEvent event; | ||
|
||
@Override | ||
public boolean couldMatch(ScriptPath path) { | ||
return path.eventLower.startsWith("player stops spectating"); | ||
} | ||
|
||
@Override | ||
public boolean matches(ScriptPath path) { | ||
if (path.eventArgLowerAt(3).length() > 0 && !tryEntity(new EntityTag(event.getSpectatorTarget()), path.eventArgLowerAt(3))) { | ||
return false; | ||
} | ||
if (!runInCheck(path, event.getPlayer().getLocation())) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "PlayerStopsSpectating"; | ||
} | ||
|
||
@Override | ||
public ScriptEntryData getScriptEntryData() { | ||
return new BukkitScriptEntryData(new PlayerTag(event.getPlayer()), null); | ||
} | ||
|
||
@Override | ||
public ObjectTag getContext(String name) { | ||
if (name.equals("entity")) { | ||
return new EntityTag(event.getSpectatorTarget()); | ||
} | ||
return super.getContext(name); | ||
} | ||
|
||
@EventHandler | ||
public void playerSpectateEvent(PlayerStopSpectatingEntityEvent event) { | ||
this.event = event; | ||
fire(event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters