Skip to content

Commit

Permalink
Update to 2.1.0; Add player join and leave compat for customentities
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Apr 26, 2021
1 parent b88a859 commit 4ad4c7f
Show file tree
Hide file tree
Showing 24 changed files with 221 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.sourcewriters.minecraft</groupId>
<artifactId>vcompat</artifactId>
<version>2.0.1</version>
<version>2.1.0</version>
<name>vCompat</name>
<distributionManagement>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ public void teleport(Location location) {
* Entity handle
*/

@Override
public Hologram updateVisibility() {
int amount;
synchronized (entities) {
amount = entities.size();
}
for (int index = 0; index < amount; index++) {
NmsArmorStand entity;
synchronized (entities) {
entity = entities.get(index);
}
entity.updateVisibility();
}
return this;
}

public Hologram show(Player... players) {
int amount;
synchronized (entities) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.UUID;

import org.bukkit.Location;
import org.bukkit.entity.Player;

public abstract class CustomEntity {

Expand Down Expand Up @@ -67,5 +68,13 @@ public Location getLocation() {
public abstract void setInvulnerable(boolean invulnerable);

public abstract void teleport(Location location);

public abstract CustomEntity updateVisibility();

public abstract CustomEntity show(Player... players);

public abstract CustomEntity hide(Player... players);

public abstract boolean isShown(Player player);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@
import java.util.Optional;
import java.util.UUID;

public class EntityManager {
import org.bukkit.entity.Player;

import net.sourcewriters.minecraft.vcompat.listener.PlayerListener;
import net.sourcewriters.minecraft.vcompat.listener.handler.IPlayerHandler;
import net.sourcewriters.minecraft.vcompat.reflection.entity.NmsPlayer;

public class EntityManager implements IPlayerHandler {

private final ArrayList<CustomEntity> entities = new ArrayList<>();

private boolean registered = false;

public EntityManager() {
start();
}

public CustomEntity create(EntityType type) {
return create(REGISTRY.getBuilderOrNull(type));
}
Expand Down Expand Up @@ -68,4 +80,36 @@ protected UUID generateId() {
return current;
}

/*
* Listening
*/

public void start() {
if (registered) {
return;
}
registered = true;
PlayerListener.registerHandler(this);
}

public void stop() {
if (!registered) {
return;
}
registered = false;
PlayerListener.unregisterHandler(this);
}

@Override
public void onJoin(NmsPlayer player) {
Player bukkitPlayer = player.getBukkitPlayer();
CustomEntity[] entities = getAll();
for (CustomEntity entity : entities) {
if (!entity.isShown(bukkitPlayer)) {
continue;
}
entity.hide(bukkitPlayer).show(bukkitPlayer);
}
}

}
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);
}
}

}
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) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import net.sourcewriters.minecraft.vcompat.reflection.data.persistence.PersistentContainer;
import net.sourcewriters.minecraft.vcompat.reflection.entity.NmsPlayer;

public abstract class PlayerProvider<V extends VersionControl> extends VersionHandler<V> {
Expand All @@ -30,6 +31,10 @@ public NmsPlayer getPlayer(Player player) {
if (players.containsKey(player.getUniqueId())) {
NmsPlayer nmsPlayer = players.get(player.getUniqueId());
if (nmsPlayer.getBukkitPlayer() != player) {
Object container = nmsPlayer.getDataAdapter().getHandle();
if (container instanceof PersistentContainer) {
((PersistentContainer<?>) container).delete();
}
players.put(player.getUniqueId(), nmsPlayer = createPlayer(player));
}
return nmsPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ protected void read(NbtCompound compound) {
super.fromNbt(compound);
}

protected void shutdown() {
public void shutdown() {
observer.shutdown();
}

protected void delete() {
public void delete() {
lock.writeLock().lock();
observer.save();
super.getRoot().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public interface NmsEntity {
void setLocation(Location location);

Location getLocation();

void updateVisibility();

boolean isShown(Player player);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public NmsBoundingBox getBoundingBox() {
@Override
public void setCustomName(String name) {
handle.setCustomName(name);
updateVisibility();
}

@Override
Expand Down Expand Up @@ -131,7 +132,8 @@ public Location getLocation() {
return new Location(handle.getWorld().getWorld(), vector.x, vector.y, vector.z);
}

private void updateVisibility() {
@Override
public void updateVisibility() {
if (visible.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public NmsBoundingBox getBoundingBox() {
@Override
public void setCustomName(String name) {
handle.setCustomName(name);
updateVisibility();
}

@Override
Expand Down Expand Up @@ -131,7 +132,8 @@ public Location getLocation() {
return new Location(handle.getWorld().getWorld(), vector.x, vector.y, vector.z);
}

private void updateVisibility() {
@Override
public void updateVisibility() {
if (visible.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public NmsBoundingBox getBoundingBox() {
@Override
public void setCustomName(String name) {
handle.setCustomName(name);
updateVisibility();
}

@Override
Expand Down Expand Up @@ -131,7 +132,8 @@ public Location getLocation() {
return new Location(handle.getWorld().getWorld(), vector.x, vector.y, vector.z);
}

private void updateVisibility() {
@Override
public void updateVisibility() {
if (visible.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public NmsBoundingBox getBoundingBox() {
@Override
public void setCustomName(String name) {
handle.setCustomName(CraftChatMessage.fromStringOrNull(name));
updateVisibility();
}

@Override
Expand Down Expand Up @@ -132,7 +133,8 @@ public Location getLocation() {
return new Location(handle.getWorld().getWorld(), vector.x, vector.y, vector.z);
}

private void updateVisibility() {
@Override
public void updateVisibility() {
if (visible.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public NmsBoundingBox getBoundingBox() {
@Override
public void setCustomName(String name) {
handle.setCustomName(CraftChatMessage.fromStringOrNull(name));
updateVisibility();
}

@Override
Expand Down Expand Up @@ -132,7 +133,8 @@ public Location getLocation() {
return new Location(handle.getWorld().getWorld(), vector.x, vector.y, vector.z);
}

private void updateVisibility() {
@Override
public void updateVisibility() {
if (visible.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public NmsBoundingBox getBoundingBox() {
@Override
public void setCustomName(String name) {
handle.setCustomName(CraftChatMessage.fromStringOrNull(name));
updateVisibility();
}

@Override
Expand Down Expand Up @@ -131,7 +132,8 @@ public Location getLocation() {
return new Location(handle.getWorld().getWorld(), vector.x, vector.y, vector.z);
}

private void updateVisibility() {
@Override
public void updateVisibility() {
if (visible.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public NmsBoundingBox getBoundingBox() {
@Override
public void setCustomName(String name) {
handle.setCustomName(CraftChatMessage.fromStringOrNull(name));
updateVisibility();
}

@Override
Expand Down Expand Up @@ -131,7 +132,8 @@ public Location getLocation() {
return new Location(handle.getWorld().getWorld(), vector.x, vector.y, vector.z);
}

private void updateVisibility() {
@Override
public void updateVisibility() {
if (visible.isEmpty()) {
return;
}
Expand Down
Loading

0 comments on commit 4ad4c7f

Please sign in to comment.