-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to 2.1.0; Add player join and leave compat for customentities
- Loading branch information
1 parent
b88a859
commit 4ad4c7f
Showing
24 changed files
with
221 additions
and
19 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
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
85 changes: 85 additions & 0 deletions
85
src/main/java/net/sourcewriters/minecraft/vcompat/listener/PlayerListener.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,85 @@ | ||
package net.sourcewriters.minecraft.vcompat.listener; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
import net.sourcewriters.minecraft.vcompat.listener.handler.IPlayerHandler; | ||
import net.sourcewriters.minecraft.vcompat.reflection.PlayerProvider; | ||
import net.sourcewriters.minecraft.vcompat.reflection.VersionControl; | ||
import net.sourcewriters.minecraft.vcompat.reflection.entity.NmsPlayer; | ||
|
||
public final class PlayerListener implements Listener { | ||
|
||
public static final PlayerListener INSTANCE = new PlayerListener(); | ||
|
||
public static void register(Plugin plugin) { | ||
Bukkit.getPluginManager().registerEvents(INSTANCE, plugin); | ||
} | ||
|
||
public static void registerHandler(IPlayerHandler handler) { | ||
INSTANCE.register(handler); | ||
} | ||
|
||
public static void unregisterHandler(IPlayerHandler handler) { | ||
INSTANCE.unregister(handler); | ||
} | ||
|
||
private final PlayerProvider<?> provider = VersionControl.get().getPlayerProvider(); | ||
|
||
private final List<IPlayerHandler> handlers = Collections.synchronizedList(new ArrayList<>()); | ||
private final Set<UUID> set = Collections.synchronizedSet(new HashSet<>()); | ||
|
||
public void register(IPlayerHandler handler) { | ||
if (handlers.contains(handler)) { | ||
return; | ||
} | ||
handlers.add(handler); | ||
} | ||
|
||
public void unregister(IPlayerHandler handler) { | ||
if (!handlers.contains(handler)) { | ||
return; | ||
} | ||
handlers.remove(handler); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
public void onJoin(PlayerJoinEvent event) { | ||
Player player = event.getPlayer(); | ||
if (set.contains(player.getUniqueId())) { | ||
return; | ||
} | ||
set.add(player.getUniqueId()); | ||
NmsPlayer nmsPlayer = provider.getPlayer(player); | ||
for (IPlayerHandler handler : handlers) { | ||
handler.onJoin(nmsPlayer); | ||
} | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
public void onQuit(PlayerQuitEvent event) { | ||
Player player = event.getPlayer(); | ||
if (!set.contains(player.getUniqueId())) { | ||
return; | ||
} | ||
set.remove(player.getUniqueId()); | ||
NmsPlayer nmsPlayer = provider.getPlayer(player); | ||
for (IPlayerHandler handler : handlers) { | ||
handler.onLeave(nmsPlayer); | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/net/sourcewriters/minecraft/vcompat/listener/handler/IPlayerHandler.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,11 @@ | ||
package net.sourcewriters.minecraft.vcompat.listener.handler; | ||
|
||
import net.sourcewriters.minecraft.vcompat.reflection.entity.NmsPlayer; | ||
|
||
public interface IPlayerHandler { | ||
|
||
default void onJoin(NmsPlayer player) {} | ||
|
||
default void onLeave(NmsPlayer player) {} | ||
|
||
} |
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
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
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
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
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
Oops, something went wrong.