From e2ca79b069a1759e8a2b93e7f58d09ab75eec154 Mon Sep 17 00:00:00 2001
From: Tofaa <82680183+Tofaa2@users.noreply.github.com>
Date: Tue, 6 Feb 2024 13:20:15 +0400
Subject: [PATCH] add player implementation
---
.idea/workspace.xml | 9 +-
.../entitylib/wrapper/WrapperEntity.java | 13 +--
.../wrapper/WrapperExperienceOrbEntity.java | 4 +-
.../entitylib/wrapper/WrapperPlayer.java | 82 +++++++++++++++++++
.../common/AbstractWorldWrapper.java | 6 ++
5 files changed, 98 insertions(+), 16 deletions(-)
create mode 100644 api/src/main/java/me/tofaa/entitylib/wrapper/WrapperPlayer.java
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index d305e06..4139f29 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,13 +5,11 @@
-
-
+
-
-
-
+
+
@@ -296,6 +294,7 @@
+
diff --git a/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java
index ddc4fda..2402c85 100644
--- a/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java
+++ b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java
@@ -28,12 +28,12 @@ public class WrapperEntity implements Tickable {
private boolean ticking;
private Location location;
private Location preRidingLocation;
- private Set viewers;
+ private final Set viewers;
private boolean onGround;
private boolean spawned;
private Vector3d velocity;
private int riding = -1;
- private Set passengers = new HashSet<>();
+ private final Set passengers;
private WorldWrapper> world;
public WrapperEntity(int entityId, UUID uuid, EntityType entityType, EntityMeta entityMeta) {
@@ -43,10 +43,7 @@ public class WrapperEntity implements Tickable {
this.entityMeta = entityMeta;
this.ticking = true;
this.viewers = new HashSet<>();
- }
-
- public boolean spawn(Location location) {
- return spawn(null, location);
+ this.passengers = new HashSet<>();
}
public boolean spawn(WorldWrapper> world, Location location) {
@@ -125,10 +122,6 @@ public class WrapperEntity implements Tickable {
teleport(null, location, onGround);
}
- public void teleport(@NotNull Location location) {
- teleport(null, location, onGround);
- }
-
/**
* Adds a viewer to the viewers set. The viewer will receive all packets and be informed of this addition
* @param uuid the uuid of the user to add
diff --git a/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperExperienceOrbEntity.java b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperExperienceOrbEntity.java
index c880847..27b38b8 100644
--- a/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperExperienceOrbEntity.java
+++ b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperExperienceOrbEntity.java
@@ -25,7 +25,9 @@ public class WrapperExperienceOrbEntity extends WrapperEntity {
* This is an attempt to mimmick the vanilla behavior.
*
*/
- public void updateSliding() {
+ @Override
+ public void tick(long time) {
+ super.tick(time);
if (hasNoGravity()) {
setVelocity(getVelocity().add(0, -0.3f, 0));
}
diff --git a/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperPlayer.java b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperPlayer.java
new file mode 100644
index 0000000..d7d1c57
--- /dev/null
+++ b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperPlayer.java
@@ -0,0 +1,82 @@
+package me.tofaa.entitylib.wrapper;
+
+import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
+import com.github.retrooper.packetevents.protocol.player.*;
+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.WrapperPlayServerPlayerInfoUpdate;
+import me.tofaa.entitylib.meta.EntityMeta;
+import net.kyori.adventure.text.Component;
+
+import java.util.List;
+
+public class WrapperPlayer extends WrapperLivingEntity {
+
+ private final UserProfile profile;
+ private GameMode gameMode = GameMode.CREATIVE;
+ private Component displayName;
+
+
+ public WrapperPlayer(UserProfile profile, int entityId, EntityType entityType, EntityMeta entityMeta) {
+ super(entityId, profile.getUUID(), entityType, entityMeta);
+ this.profile = profile;
+ }
+
+
+ public void setGameMode(GameMode gameMode) {
+ this.gameMode = gameMode;
+ sendPacketsToViewers(new WrapperPlayServerPlayerInfo(
+ WrapperPlayServerPlayerInfo.Action.UPDATE_GAME_MODE,
+ new WrapperPlayServerPlayerInfo.PlayerData(displayName, profile, gameMode, null, -1)));
+ }
+
+ public void setDisplayName(Component displayName) {
+ this.displayName = displayName;
+ sendPacketsToViewers(new WrapperPlayServerPlayerInfo(
+ WrapperPlayServerPlayerInfo.Action.UPDATE_DISPLAY_NAME,
+ new WrapperPlayServerPlayerInfo.PlayerData(displayName, profile, gameMode, null, -1)));
+ }
+
+ public Component getDisplayName() {
+ return displayName;
+ }
+
+ public String getUsername() {
+ return profile.getName();
+ }
+
+ public List getTextureProperties() {
+ return profile.getTextureProperties();
+ }
+
+ public GameMode getGameMode() {
+ return gameMode;
+ }
+
+ @Override
+ public void addViewer(User user) {
+ super.addViewer(user);
+ user.sendPacket(createAddPacket());
+ }
+
+ @Override
+ public void removeViewer(User user) {
+ super.removeViewer(user);
+ user.sendPacket(createRemovePacket());
+ }
+
+ private WrapperPlayServerPlayerInfoUpdate createAddPacket() {
+ return new WrapperPlayServerPlayerInfoUpdate(
+ WrapperPlayServerPlayerInfoUpdate.Action.ADD_PLAYER,
+ new WrapperPlayServerPlayerInfoUpdate.PlayerInfo(
+ profile,
+ true, -1, gameMode, null, null
+ )
+ );
+ }
+
+ private WrapperPlayServerPlayerInfoRemove createRemovePacket() {
+ return new WrapperPlayServerPlayerInfoRemove(getUuid());
+ }
+
+}
diff --git a/common/src/main/java/me/tofaa/entitylib/common/AbstractWorldWrapper.java b/common/src/main/java/me/tofaa/entitylib/common/AbstractWorldWrapper.java
index 0bb678c..4cd4b0a 100644
--- a/common/src/main/java/me/tofaa/entitylib/common/AbstractWorldWrapper.java
+++ b/common/src/main/java/me/tofaa/entitylib/common/AbstractWorldWrapper.java
@@ -6,8 +6,11 @@ import com.github.retrooper.packetevents.protocol.world.Location;
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.WorldWrapper;
import me.tofaa.entitylib.meta.EntityMeta;
+import me.tofaa.entitylib.meta.projectile.ThrownExpBottleMeta;
import me.tofaa.entitylib.meta.types.LivingEntityMeta;
+import me.tofaa.entitylib.meta.types.PlayerMeta;
import me.tofaa.entitylib.wrapper.WrapperEntity;
+import me.tofaa.entitylib.wrapper.WrapperExperienceOrbEntity;
import me.tofaa.entitylib.wrapper.WrapperLivingEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -65,6 +68,9 @@ public abstract class AbstractWorldWrapper implements WorldWrapper {
if (meta instanceof LivingEntityMeta) {
e = new WrapperLivingEntity(entityId, uuid, entityType, meta);
}
+ else if (meta instanceof ThrownExpBottleMeta) {
+ e = new WrapperExperienceOrbEntity(entityId, uuid, entityType, meta);
+ }
else {
e = new WrapperEntity(entityId, uuid, entityType, meta);
}