diff --git a/CommonCore b/CommonCore index e52e9c5..d22bb36 160000 --- a/CommonCore +++ b/CommonCore @@ -1 +1 @@ -Subproject commit e52e9c5ccd8ef9b87ce06d4eeeaaa4044afa89b5 +Subproject commit d22bb36fc348fae373713b83235cef67010cf020 diff --git a/SpigotCore_Main/src/de/steamwar/core/events/PlayerJoinedEvent.java b/SpigotCore_Main/src/de/steamwar/core/events/PlayerJoinedEvent.java index aae2000..fe342bd 100644 --- a/SpigotCore_Main/src/de/steamwar/core/events/PlayerJoinedEvent.java +++ b/SpigotCore_Main/src/de/steamwar/core/events/PlayerJoinedEvent.java @@ -35,10 +35,7 @@ public class PlayerJoinedEvent implements Listener{ @EventHandler(priority = EventPriority.LOWEST) private void onJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); - if(!Statement.productionDatabase()) { - SteamwarUser.createOrUpdateUsername(player.getUniqueId(), player.getName()); - } - SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + SteamwarUser user = Statement.productionDatabase() ? SteamwarUser.get(player.getUniqueId()) : SteamwarUser.getOrCreate(player.getUniqueId(), player.getName(), uuid -> {}, (oldName, newName) -> {}); if(user.getUserGroup() != UserGroup.Member) { UserGroup group = user.getUserGroup(); diff --git a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java index 5987112..1ed4c6f 100644 --- a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java +++ b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java @@ -23,6 +23,7 @@ import com.comphenix.tinyprotocol.Reflection; import com.comphenix.tinyprotocol.TinyProtocol; import de.steamwar.core.Core; import de.steamwar.core.FlatteningWrapper; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -39,6 +40,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Consumer; +import java.util.function.Function; public class REntityServer implements Listener { @@ -48,7 +50,17 @@ public class REntityServer implements Listener { private static final Class useEntity = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseEntity"); private static final Reflection.FieldAccessor useEntityTarget = Reflection.getField(useEntity, int.class, 0); - //private static final Reflection.FieldAccessor useEntityAction = Reflection.getField(useEntity, int.class, 1); + private static final Class useEntityEnumAction = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseEntity$EnumEntityUseAction"); + private static final Reflection.FieldAccessor useEntityAction = Reflection.getField(useEntity, useEntityEnumAction, 0); + private static final Function getEntityAction; + static { + if(Core.getVersion() > 15) { + Reflection.MethodInvoker useEntityGetAction = Reflection.getMethod(useEntityEnumAction, "a"); + getEntityAction = value -> ((Enum) useEntityGetAction.invoke(value)).ordinal(); + } else { + getEntityAction = value -> ((Enum) value).ordinal(); + } + } private final ConcurrentHashMap entityMap = new ConcurrentHashMap<>(); private final HashMap> entities = new HashMap<>(); @@ -58,16 +70,13 @@ public class REntityServer implements Listener { private EntityActionListener callback = null; - private BiFunction filter = (player, packet) -> { - if (callback == null) return packet; - int target = useEntityTarget.get(packet); - REntity entity = entityMap.get(target); - if (entity == null) return packet; + private final BiFunction filter = (player, packet) -> { + REntity entity = entityMap.get(useEntityTarget.get(packet)); + if (entity == null) + return packet; - /*EntityAction action = useEntityAction.get(packet) == 1 ? EntityAction.ATTACK : EntityAction.INTERACT; - Bukkit.getScheduler().runTask(Core.getInstance(), () -> { - callback.onAction(player, entity, action); - });*/ + EntityAction action = getEntityAction.apply(useEntityAction.get(packet)) == 1 ? EntityAction.ATTACK : EntityAction.INTERACT; + Bukkit.getScheduler().runTask(Core.getInstance(), () -> callback.onAction(player, entity, action)); return null; }; @@ -192,7 +201,14 @@ public class REntityServer implements Listener { if(location == null) return; - forChunkInView(player, location, (x, z) -> players.get(chunkToId(x, z)).remove(player)); + forChunkInView(player, location, (x, z) -> { + long id = chunkToId(x, z); + Set playersInChunk = players.get(id); + playersInChunk.remove(player); + if(playersInChunk.isEmpty()) + players.remove(id); + }); + viewDistance.remove(player); } private void onMissing(Set of, Set in, Consumer> packetProvider) { @@ -228,7 +244,12 @@ public class REntityServer implements Listener { private void removePlayerFromChunk(Player player, int x, int z) { long id = chunkToId(x, z); - players.get(id).remove(player); + + Set playersInChunk = players.get(id); + playersInChunk.remove(player); + if(playersInChunk.isEmpty()) + players.remove(id); + for(REntity entity : entities.getOrDefault(id, emptyEntities)) { entity.despawn(packet -> TinyProtocol.instance.sendPacket(player, packet)); }