concurrency

This commit is contained in:
Tofaa 2024-02-22 01:08:31 +04:00
parent 11da55bda5
commit c374b5f5af
5 changed files with 77 additions and 34 deletions

View file

@ -13,6 +13,7 @@
<option value="$PROJECT_DIR$/common" /> <option value="$PROJECT_DIR$/common" />
<option value="$PROJECT_DIR$/platforms" /> <option value="$PROJECT_DIR$/platforms" />
<option value="$PROJECT_DIR$/platforms/spigot" /> <option value="$PROJECT_DIR$/platforms/spigot" />
<option value="$PROJECT_DIR$/test-plugin" />
</set> </set>
</option> </option>
</GradleProjectSettings> </GradleProjectSettings>

View file

@ -4,7 +4,11 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment="" /> <list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java" beforeDir="false" afterPath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperPlayer.java" beforeDir="false" afterPath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperPlayer.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -303,6 +307,7 @@
<workItem from="1708352150286" duration="3592000" /> <workItem from="1708352150286" duration="3592000" />
<workItem from="1708462795417" duration="1244000" /> <workItem from="1708462795417" duration="1244000" />
<workItem from="1708508458793" duration="103000" /> <workItem from="1708508458793" duration="103000" />
<workItem from="1708512937708" duration="3296000" />
</task> </task>
<servers /> <servers />
</component> </component>

View file

@ -15,6 +15,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class WrapperEntity implements Tickable, TrackedEntity { public class WrapperEntity implements Tickable, TrackedEntity {
@ -38,10 +39,11 @@ public class WrapperEntity implements Tickable, TrackedEntity {
this.entityType = entityType; this.entityType = entityType;
this.entityMeta = entityMeta; this.entityMeta = entityMeta;
this.ticking = true; this.ticking = true;
this.viewers = new HashSet<>(); this.viewers = ConcurrentHashMap.newKeySet();
this.passengers = new HashSet<>(); this.passengers = ConcurrentHashMap.newKeySet();
} }
public boolean spawn(Location location) { public boolean spawn(Location location) {
if (spawned) return false; if (spawned) return false;
this.location = location; this.location = location;
@ -124,6 +126,13 @@ public class WrapperEntity implements Tickable, TrackedEntity {
if (!viewers.add(uuid)) { if (!viewers.add(uuid)) {
return; return;
} }
if (location == null) {
if (EntityLib.getApi().getSettings().isDebugMode()) {
EntityLib.getPlatform().getLogger().warning("Location is null for entity " + entityId + ". Cannot spawn.");
}
return;
}
if (spawned) {
WrapperPlayServerSpawnEntity packet = new WrapperPlayServerSpawnEntity( WrapperPlayServerSpawnEntity packet = new WrapperPlayServerSpawnEntity(
entityId, entityId,
Optional.of(this.uuid), Optional.of(this.uuid),
@ -138,6 +147,7 @@ public class WrapperEntity implements Tickable, TrackedEntity {
sendPacket(uuid, packet); sendPacket(uuid, packet);
sendPacket(uuid, entityMeta.createPacket()); sendPacket(uuid, entityMeta.createPacket());
} }
}
public void addViewer(User user) { public void addViewer(User user) {
addViewer(user.getUUID()); addViewer(user.getUUID());

View file

@ -1,15 +1,16 @@
package me.tofaa.entitylib.wrapper; package me.tofaa.entitylib.wrapper;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.player.*; import com.github.retrooper.packetevents.protocol.player.*;
import com.github.retrooper.packetevents.protocol.world.Location;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfo; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfo;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoRemove; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoRemove;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoUpdate; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoUpdate;
import me.tofaa.entitylib.EntityLib; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnPlayer;
import me.tofaa.entitylib.meta.EntityMeta; import me.tofaa.entitylib.meta.EntityMeta;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class WrapperPlayer extends WrapperLivingEntity { public class WrapperPlayer extends WrapperLivingEntity {
@ -17,14 +18,13 @@ public class WrapperPlayer extends WrapperLivingEntity {
private final UserProfile profile; private final UserProfile profile;
private GameMode gameMode = GameMode.CREATIVE; private GameMode gameMode = GameMode.CREATIVE;
private Component displayName; private Component displayName;
private boolean tablist = true;
public WrapperPlayer(UserProfile profile, int entityId) { public WrapperPlayer(UserProfile profile, int entityId) {
super(entityId, profile.getUUID(), EntityTypes.PLAYER, EntityMeta.createMeta(entityId, EntityTypes.PLAYER)); super(entityId, profile.getUUID(), EntityTypes.PLAYER, EntityMeta.createMeta(entityId, EntityTypes.PLAYER));
this.profile = profile; this.profile = profile;
} }
public void setGameMode(GameMode gameMode) { public void setGameMode(GameMode gameMode) {
this.gameMode = gameMode; this.gameMode = gameMode;
sendPacketsToViewers(new WrapperPlayServerPlayerInfo( sendPacketsToViewers(new WrapperPlayServerPlayerInfo(
@ -57,14 +57,34 @@ public class WrapperPlayer extends WrapperLivingEntity {
@Override @Override
public void addViewer(User user) { public void addViewer(User user) {
//user.sendPacket(createAddPacket());
sendJoiningPackets();
super.addViewer(user); super.addViewer(user);
user.sendPacket(createAddPacket());
} }
@Override @Override
public void removeViewer(User user) { public void removeViewer(User user) {
super.removeViewer(user);
user.sendPacket(createRemovePacket()); user.sendPacket(createRemovePacket());
super.removeViewer(user);
}
@Override
public boolean spawn(Location location) {
this.setLocation(location);
WrapperPlayServerSpawnPlayer packet = new WrapperPlayServerSpawnPlayer(getEntityId(), getUuid(), location, getEntityMeta());
sendPacketsToViewers(packet);
return true;
//return super.spawn(location);
}
private void sendJoiningPackets() {
List<WrapperPlayServerPlayerInfo.PlayerData> data = new ArrayList<>();
data.add(new WrapperPlayServerPlayerInfo.PlayerData(displayName, profile, gameMode, null, -1));
WrapperPlayServerPlayerInfo p1 = new WrapperPlayServerPlayerInfo(
WrapperPlayServerPlayerInfo.Action.ADD_PLAYER,
data
);
sendPacketsToViewers(p1);
} }
private WrapperPlayServerPlayerInfoUpdate createAddPacket() { private WrapperPlayServerPlayerInfoUpdate createAddPacket() {
@ -72,7 +92,7 @@ public class WrapperPlayer extends WrapperLivingEntity {
WrapperPlayServerPlayerInfoUpdate.Action.ADD_PLAYER, WrapperPlayServerPlayerInfoUpdate.Action.ADD_PLAYER,
new WrapperPlayServerPlayerInfoUpdate.PlayerInfo( new WrapperPlayServerPlayerInfoUpdate.PlayerInfo(
profile, profile,
true, -1, gameMode, null, null true, -1, gameMode, displayName, null
) )
); );
} }
@ -81,4 +101,6 @@ public class WrapperPlayer extends WrapperLivingEntity {
return new WrapperPlayServerPlayerInfoRemove(getUuid()); return new WrapperPlayServerPlayerInfoRemove(getUuid());
} }
} }

View file

@ -2,6 +2,7 @@ package me.tofaa.testentitylib;
import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.player.UserProfile;
import io.github.retrooper.packetevents.util.SpigotConversionUtil; import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import me.tofaa.entitylib.APIConfig; import me.tofaa.entitylib.APIConfig;
import me.tofaa.entitylib.EntityLib; import me.tofaa.entitylib.EntityLib;
@ -10,6 +11,7 @@ import me.tofaa.entitylib.spigot.SpigotEntityLibAPI;
import me.tofaa.entitylib.spigot.SpigotEntityLibPlatform; import me.tofaa.entitylib.spigot.SpigotEntityLibPlatform;
import me.tofaa.entitylib.wrapper.WrapperEntity; import me.tofaa.entitylib.wrapper.WrapperEntity;
import me.tofaa.entitylib.event.types.*; import me.tofaa.entitylib.event.types.*;
import me.tofaa.entitylib.wrapper.WrapperPlayer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -17,11 +19,13 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.UUID;
public class TestEntityLibPlugin extends JavaPlugin implements CommandExecutor { public class TestEntityLibPlugin extends JavaPlugin implements CommandExecutor {
private SpigotEntityLibAPI api; private SpigotEntityLibAPI api;
private WrapperEntity e; private WrapperPlayer e;
@Override @Override
public void onEnable() { public void onEnable() {
@ -50,17 +54,18 @@ public class TestEntityLibPlugin extends JavaPlugin implements CommandExecutor {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player)) return false; if (!(sender instanceof Player)) return false;
Player player = (Player) sender; Player player = (Player) sender;
if (e == null) { if (e != null) {
e = api.spawnEntity(EntityTypes.CHICKEN, SpigotConversionUtil.fromBukkitLocation(player.getLocation())); e.remove();
e.addViewer(player.getUniqueId()); player.sendMessage("Removed");
player.sendMessage("Spawned"); e = null;
return true;
} }
ChickenMeta meta = (ChickenMeta) e.getEntityMeta(); UUID uuid = UUID.randomUUID();
meta.setBaby(!meta.isBaby()); UserProfile profile = new UserProfile(uuid, "RandomGoon");
meta.setHasGlowingEffect(!meta.hasGlowingEffect()); e = new WrapperPlayer(profile, EntityLib.getPlatform().getEntityIdProvider().provide(uuid, EntityTypes.PLAYER));
meta.setHasNoGravity(!meta.hasNoGravity()); e.addViewer(player.getUniqueId());
api.spawnEntity(e, SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
player.sendMessage("Updated"); player.sendMessage("Spawned");
return true; return true;
} }