-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
004d38e
commit 04aba26
Showing
7 changed files
with
232 additions
and
20 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
api/src/main/java/de/oliver/fancysitula/api/packets/FS_ClientboundSetEntityDataPacket.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,44 @@ | ||
package de.oliver.fancysitula.api.packets; | ||
|
||
import java.util.List; | ||
|
||
public abstract class FS_ClientboundSetEntityDataPacket implements FS_ClientboundPacket { | ||
|
||
protected int entityId; | ||
protected List<EntityData> entityData; | ||
|
||
public FS_ClientboundSetEntityDataPacket(int entityId, List<EntityData> entityData) { | ||
this.entityId = entityId; | ||
this.entityData = entityData; | ||
} | ||
|
||
public int getEntityId() { | ||
return entityId; | ||
} | ||
|
||
public void setEntityId(int entityId) { | ||
this.entityId = entityId; | ||
} | ||
|
||
public List<EntityData> getEntityData() { | ||
return entityData; | ||
} | ||
|
||
public void setEntityData(List<EntityData> entityData) { | ||
this.entityData = entityData; | ||
} | ||
|
||
/** | ||
* @param accessor can be found in {@link de.oliver.fancysitula.api.utils.entityData} | ||
* @param value must be the correct type for the accessor (see accessor javadoc) | ||
*/ | ||
public record EntityData(EntityDataAccessor accessor, Object value) { | ||
} | ||
|
||
/** | ||
* @param entityClassName the class name of the entity (e.g. "net.minecraft.world.entity.Display$TextDisplay") | ||
* @param accessorFieldName the field name of the accessor (typically starts with "DATA_" and ends with "_ID") | ||
*/ | ||
public record EntityDataAccessor(String entityClassName, String accessorFieldName) { | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
api/src/main/java/de/oliver/fancysitula/api/utils/entityData/TextDisplayData.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,41 @@ | ||
package de.oliver.fancysitula.api.utils.entityData; | ||
|
||
import de.oliver.fancysitula.api.packets.FS_ClientboundSetEntityDataPacket; | ||
|
||
public enum TextDisplayData { | ||
/** | ||
* Use {@link net.kyori.adventure.text.Component} as value | ||
*/ | ||
TEXT(new FS_ClientboundSetEntityDataPacket.EntityDataAccessor("net.minecraft.world.entity.Display$TextDisplay", "DATA_TEXT_ID")), | ||
|
||
/** | ||
* Use {@link Integer} as value | ||
*/ | ||
LINE_WIDTH(new FS_ClientboundSetEntityDataPacket.EntityDataAccessor("net.minecraft.world.entity.Display$TextDisplay", "DATA_LINE_WIDTH_ID")), | ||
|
||
/** | ||
* Use {@link Integer} as value | ||
*/ | ||
BACKGROUND(new FS_ClientboundSetEntityDataPacket.EntityDataAccessor("net.minecraft.world.entity.Display$TextDisplay", "DATA_BACKGROUND_COLOR_ID")), | ||
|
||
/** | ||
* Use {@link Byte} as value | ||
*/ | ||
TEXT_OPACITY(new FS_ClientboundSetEntityDataPacket.EntityDataAccessor("net.minecraft.world.entity.Display$TextDisplay", "DATA_TEXT_OPACITY_ID")), | ||
|
||
/** | ||
* Use {@link Byte} as value | ||
*/ | ||
STYLE_FLAGS(new FS_ClientboundSetEntityDataPacket.EntityDataAccessor("net.minecraft.world.entity.Display$TextDisplay", "DATA_STYLE_FLAGS_ID")), | ||
; | ||
|
||
private final FS_ClientboundSetEntityDataPacket.EntityDataAccessor accessor; | ||
|
||
TextDisplayData(FS_ClientboundSetEntityDataPacket.EntityDataAccessor accessor) { | ||
this.accessor = accessor; | ||
} | ||
|
||
public FS_ClientboundSetEntityDataPacket.EntityDataAccessor get() { | ||
return accessor; | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
...va/de/oliver/fancysitula/versions/v1_20_6/packets/ClientboundSetEntityDataPacketImpl.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,52 @@ | ||
package de.oliver.fancysitula.versions.v1_20_6.packets; | ||
|
||
import de.oliver.fancysitula.api.entities.FS_RealPlayer; | ||
import de.oliver.fancysitula.api.packets.FS_ClientboundSetEntityDataPacket; | ||
import de.oliver.fancysitula.api.utils.reflections.ReflectionUtils; | ||
import de.oliver.fancysitula.versions.v1_20_6.utils.VanillaPlayerAdapter; | ||
import io.papermc.paper.adventure.PaperAdventure; | ||
import net.kyori.adventure.text.Component; | ||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; | ||
import net.minecraft.network.syncher.SynchedEntityData; | ||
import net.minecraft.server.level.ServerPlayer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ClientboundSetEntityDataPacketImpl extends FS_ClientboundSetEntityDataPacket { | ||
|
||
public ClientboundSetEntityDataPacketImpl(int entityId, List<EntityData> entityData) { | ||
super(entityId, entityData); | ||
} | ||
|
||
@Override | ||
public Object createPacket() { | ||
List<SynchedEntityData.DataValue<?>> dataValues = new ArrayList<>(); | ||
for (EntityData data : entityData) { | ||
try { | ||
Class<?> entityClass = Class.forName(data.accessor().entityClassName()); | ||
net.minecraft.network.syncher.EntityDataAccessor<Object> accessor = ReflectionUtils.getStaticField(entityClass, data.accessor().accessorFieldName()); | ||
|
||
Object vanillaValue = data.value(); | ||
|
||
if (data.value() instanceof Component c) { | ||
vanillaValue = PaperAdventure.asVanilla(c); | ||
} | ||
|
||
dataValues.add(SynchedEntityData.DataValue.create(accessor, vanillaValue)); | ||
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
return new ClientboundSetEntityDataPacket(entityId, dataValues); | ||
} | ||
|
||
@Override | ||
public void send(FS_RealPlayer player) { | ||
ClientboundSetEntityDataPacket packet = (ClientboundSetEntityDataPacket) createPacket(); | ||
|
||
ServerPlayer vanillaPlayer = VanillaPlayerAdapter.asVanilla(player.getBukkitPlayer()); | ||
vanillaPlayer.connection.send(packet); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...e/oliver/fancysitula/versions/v1_20_6/packets/ClientboundSetEntityDataPacketImplTest.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,28 @@ | ||
package de.oliver.fancysitula.versions.v1_20_6.packets; | ||
|
||
import de.oliver.fancysitula.api.packets.FS_ClientboundSetEntityDataPacket; | ||
import de.oliver.fancysitula.api.utils.entityData.TextDisplayData; | ||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; | ||
|
||
import java.util.List; | ||
|
||
class ClientboundSetEntityDataPacketImplTest { | ||
|
||
//TODO: Fix this test (using registry) | ||
// @Test | ||
void createPacket() { | ||
int entityId = 712; | ||
List<FS_ClientboundSetEntityDataPacket.EntityData> entityData = List.of( | ||
new FS_ClientboundSetEntityDataPacket.EntityData( | ||
TextDisplayData.TEXT.get(), | ||
"Hello, World!" | ||
) | ||
); | ||
|
||
ClientboundSetEntityDataPacketImpl packet = new ClientboundSetEntityDataPacketImpl(entityId, entityData); | ||
ClientboundSetEntityDataPacket createdPacket = (ClientboundSetEntityDataPacket) packet.createPacket(); | ||
|
||
assert createdPacket.id() == entityId; | ||
assert createdPacket.packedItems().size() == 1; | ||
} | ||
} |
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