-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from Arim-Minecraft/master
Allow Omitting PlayerMoveEvent
- Loading branch information
Showing
6 changed files
with
195 additions
and
43 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
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,77 @@ | ||
package net.jitse.npclib; | ||
|
||
/** | ||
* Mutable preferences for library usage | ||
* | ||
* @author A248 | ||
* | ||
*/ | ||
public class NPCLibOptions { | ||
|
||
MovementHandling moveHandling; | ||
|
||
/** | ||
* Creates the default options | ||
* | ||
*/ | ||
public NPCLibOptions() { | ||
moveHandling = MovementHandling.playerMoveEvent(); | ||
} | ||
|
||
/** | ||
* Specifies the motion handling which will be used for the library. <br> | ||
* Programmers may choose between using the PlayerMoveEvent or | ||
* a periodic task. <br> | ||
* <br> | ||
* Note that NPCLib will always use events such as the PlayerTeleportEvent | ||
* and PlayerChangedWorldEvent in addition to the specified option. | ||
* | ||
* @param moveHandling the movement handling | ||
* @return the same NPCLibOptions | ||
*/ | ||
public NPCLibOptions setMovementHandling(MovementHandling moveHandling) { | ||
this.moveHandling = moveHandling; | ||
return this; | ||
} | ||
|
||
/** | ||
* Options relating to movement handling | ||
* | ||
* @author A248 | ||
* | ||
*/ | ||
public static class MovementHandling { | ||
|
||
final boolean usePme; | ||
final long updateInterval; | ||
|
||
private MovementHandling(boolean usePme, long updateInterval) { | ||
this.usePme = usePme; | ||
this.updateInterval = updateInterval; | ||
} | ||
|
||
/** | ||
* Gets movement handling using the PlayerMoveEvent | ||
* | ||
* @return movement handling | ||
*/ | ||
public static MovementHandling playerMoveEvent() { | ||
return new MovementHandling(false, 0); | ||
} | ||
|
||
/** | ||
* Gets movement handling using a periodic update interval in ticks. | ||
* | ||
* @param updateInterval the update interval in ticks | ||
* @return movement handling | ||
*/ | ||
public static MovementHandling repeatingTask(long updateInterval) { | ||
if (updateInterval <= 0) { | ||
throw new IllegalArgumentException("Negative update interval"); | ||
} | ||
return new MovementHandling(true, updateInterval); | ||
} | ||
|
||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
api/src/main/java/net/jitse/npclib/listeners/HandleMoveBase.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,26 @@ | ||
package net.jitse.npclib.listeners; | ||
|
||
import org.bukkit.entity.Player; | ||
|
||
import net.jitse.npclib.internal.NPCBase; | ||
import net.jitse.npclib.internal.NPCManager; | ||
|
||
public class HandleMoveBase { | ||
|
||
void handleMove(Player player) { | ||
for (NPCBase npc : NPCManager.getAllNPCs()) { | ||
if (!npc.getShown().contains(player.getUniqueId())) { | ||
continue; // NPC was never supposed to be shown to the player. | ||
} | ||
|
||
if (!npc.isShown(player) && npc.inRangeOf(player) && npc.inViewOf(player)) { | ||
// The player is in range and can see the NPC, auto-show it. | ||
npc.show(player, true); | ||
} else if (npc.isShown(player) && !npc.inRangeOf(player)) { | ||
// The player is not in range of the NPC anymore, auto-hide it. | ||
npc.hide(player, true); | ||
} | ||
} | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
api/src/main/java/net/jitse/npclib/listeners/PeriodicMoveListener.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,48 @@ | ||
package net.jitse.npclib.listeners; | ||
|
||
import java.util.HashMap; | ||
import java.util.UUID; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
import org.bukkit.scheduler.BukkitTask; | ||
|
||
import net.jitse.npclib.NPCLib; | ||
|
||
public class PeriodicMoveListener extends HandleMoveBase implements Listener { | ||
|
||
private final NPCLib instance; | ||
private final long updateInterval; | ||
|
||
private final HashMap<UUID, BukkitTask> tasks = new HashMap<>(); | ||
|
||
public PeriodicMoveListener(NPCLib instance, long updateInterval) { | ||
this.instance = instance; | ||
this.updateInterval = updateInterval; | ||
} | ||
|
||
private void startTask(UUID uuid) { | ||
// purposefully using UUIDs and not holding player references | ||
tasks.put(uuid, Bukkit.getScheduler().runTaskTimer(instance.getPlugin(), () -> { | ||
Player player = Bukkit.getPlayer(uuid); | ||
if (player != null) { // safety check | ||
handleMove(player); | ||
} | ||
}, 1L, updateInterval)); | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerJoin(PlayerJoinEvent evt) { | ||
startTask(evt.getPlayer().getUniqueId()); | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerQuit(PlayerQuitEvent evt) { | ||
tasks.remove(evt.getPlayer().getUniqueId()).cancel(); | ||
} | ||
|
||
} |
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
22 changes: 22 additions & 0 deletions
22
api/src/main/java/net/jitse/npclib/listeners/PlayerMoveEventListener.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,22 @@ | ||
package net.jitse.npclib.listeners; | ||
|
||
import org.bukkit.Location; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerMoveEvent; | ||
|
||
public class PlayerMoveEventListener extends HandleMoveBase implements Listener { | ||
|
||
@EventHandler | ||
public void onPlayerMove(PlayerMoveEvent event) { | ||
Location from = event.getFrom(); | ||
Location to = event.getTo(); | ||
// Only check movement when the player moves from one block to another. The event is called often | ||
// as it is also called when the pitch or yaw change. This is worth it from a performance view. | ||
if (to == null || from.getBlockX() != to.getBlockX() | ||
|| from.getBlockY() != to.getBlockY() | ||
|| from.getBlockZ() != to.getBlockZ()) | ||
handleMove(event.getPlayer()); | ||
} | ||
|
||
} |