Skip to content

Commit

Permalink
Initial rewrite of entity emulation system
Browse files Browse the repository at this point in the history
Don't buffer Entity Metadata
  • Loading branch information
FlorianMichael committed Oct 8, 2023
1 parent a7e865e commit 15769e5
Show file tree
Hide file tree
Showing 29 changed files with 415 additions and 397 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* This file is part of ViaRewind - https://github.com/ViaVersion/ViaRewind
* Copyright (C) 2016-2023 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viarewind.api.minecraft;

import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.protocol.Protocol;

import java.util.List;

/**
* Represents a model that is replacing an entity on the client side (e.g. armor stand)
*/
public abstract class EntityModel<T extends Protocol<?, ?, ?, ?>> {

protected final UserConnection user;
protected final T protocol;

public EntityModel(final UserConnection user, final T protocol) {
this.user = user;
this.protocol = protocol;
}

/**
* @return The entity id of the entity this model is replacing
*/
public abstract int getEntityId();

/**
* Updates the position of the entity this model is replacing
*
* @param x The new x position
* @param y The new y position
* @param z The new z position
*/
public abstract void updateReplacementPosition(final double x, final double y, final double z);

/**
* Handles the original movement packet of the entity this model is replacing
*
* @param x The x position
* @param y The y position
* @param z The z position
*/
public abstract void handleOriginalMovementPacket(final double x, final double y, final double z);

/**
* Sets the yaw and pitch of the entity this model is replacing
*
* @param yaw The new yaw
* @param pitch The new pitch
*/
public abstract void setYawPitch(final float yaw, final float pitch);

/**
* Sets the head yaw of the entity this model is replacing
*
* @param yaw The new head yaw
*/
public abstract void setHeadYaw(final float yaw);

/**
* Spawns the entity this model is replacing
*/
public abstract void sendSpawnPacket();

/**
* Destroys the entity this model is replacing
*/
public abstract void deleteEntity();

/**
* Updates the metadata of the entity this model is replacing
*
* @param metadataList The new metadata
*/
public abstract void updateMetadata(final List<Metadata> metadataList);

public UserConnection getUser() {
return user;
}

public T getProtocol() {
return protocol;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viarewind.replacement;
package com.viaversion.viarewind.api.rewriter;

import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
Expand All @@ -26,25 +26,27 @@ public class Replacement {
private final int id;
private final int data;
private final String name;

private String resetName;
private String bracketName;

public Replacement(int id) {
public Replacement(final int id) {
this(id, -1);
}

public Replacement(int id, int data) {
public Replacement(final int id, final int data) {
this(id, data, null);
}

public Replacement(int id, String name) {
public Replacement(final int id, final String name) {
this(id, -1, name);
}

public Replacement(int id, int data, String name) {
public Replacement(final int id, final int data, final String name) {
this.id = id;
this.data = data;
this.name = name;

if (name != null) {
this.resetName = "§r" + name;
this.bracketName = " §r§7(" + name + "§r§7)";
Expand All @@ -63,25 +65,38 @@ public String getName() {
return name;
}

/**
* @param item The item to replace
* @return The replacement for the item or the item if not found
*/
public Item replace(Item item) {
item.setIdentifier(id);
if (data != -1) item.setData((short) data);
if (name != null) {
CompoundTag compoundTag = item.tag() == null ? new CompoundTag() : item.tag();
if (!compoundTag.contains("display")) compoundTag.put("display", new CompoundTag());
CompoundTag display = compoundTag.get("display");
item.setIdentifier(id); // Set the new id
if (data != -1) {
item.setData((short) data); // Set the new data
}
if (name != null) { // Set the new name
CompoundTag rootTag = item.tag() == null ? new CompoundTag() : item.tag(); // Get root tag or create new one if not exists

if (!rootTag.contains("display")) rootTag.put("display", new CompoundTag()); // Create display tag if not exists

final CompoundTag display = rootTag.get("display");
if (display.contains("Name")) {
StringTag name = display.get("Name");
if (!name.getValue().equals(resetName) && !name.getValue().endsWith(bracketName))
name.setValue(name.getValue() + bracketName);
final StringTag name = display.get("Name");
if (!name.getValue().equals(resetName) && !name.getValue().endsWith(bracketName)) {
name.setValue(name.getValue() + bracketName); // Append the new name tag
}
} else {
display.put("Name", new StringTag(resetName));
display.put("Name", new StringTag(resetName)); // Set the new name tag
}
item.setTag(compoundTag);
item.setTag(rootTag);
}
return item;
}

/**
* @param data The data of the item/block
* @return The replacement data for the item/block
*/
public int replaceData(int data) {
return this.data == -1 ? data : this.data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package com.viaversion.viarewind.api.rewriter;

import com.viaversion.viarewind.api.minecraft.IdDataCombine;
import com.viaversion.viarewind.replacement.Replacement;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ public void init(UserConnection userConnection) {

userConnection.put(new Windows(userConnection));
userConnection.put(new EntityTracker(userConnection, this));
userConnection.put(new PlayerPosition(userConnection));
userConnection.put(new PlayerPositionTracker(userConnection));
userConnection.put(new GameProfileStorage(userConnection));
userConnection.put(new Scoreboard(userConnection));
userConnection.put(new CompressionSendStorage(userConnection));
userConnection.put(new CompressionStatusTracker(userConnection));
userConnection.put(new WorldBorder(userConnection));
userConnection.put(new PlayerAbilities(userConnection));
userConnection.put(new PlayerAbilitiesTracker(userConnection));
userConnection.put(new ClientWorld(userConnection));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.entityreplacements;
package com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.emulator;

import com.viaversion.viarewind.protocol.protocol1_7_2_5to1_7_6_10.ClientboundPackets1_7_2_5;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.Protocol1_7_6_10To1_8;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.metadata.MetadataRewriter;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.types.MetaType1_7_6_10;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.types.Types1_7_6_10;
import com.viaversion.viarewind.utils.PacketUtil;
Expand All @@ -36,7 +35,7 @@
import java.util.ArrayList;
import java.util.List;

public class ArmorStandReplacement extends EntityReplacement1_7to1_8 {
public class ArmorStandModel extends EntityModel1_7_6_10 {
private final int entityId;
private final List<Metadata> datawatcher = new ArrayList<>();
private int[] entityIds = null;
Expand All @@ -49,8 +48,8 @@ public class ArmorStandReplacement extends EntityReplacement1_7to1_8 {
private boolean small = false;
private boolean marker = false;

public ArmorStandReplacement(Protocol1_7_6_10To1_8 protocol, UserConnection user, int entityId) {
super(protocol, user);
public ArmorStandModel(UserConnection user, Protocol1_7_6_10To1_8 protocol, int entityId) {
super(user, protocol);
this.entityId = entityId;
}

Expand All @@ -60,7 +59,7 @@ public int getEntityId() {
}

@Override
public void setLocation(double x, double y, double z) {
public void updateReplacementPosition(double x, double y, double z) {
if (x != this.locX || y != this.locY || z != this.locZ) {
this.locX = x;
this.locY = y;
Expand All @@ -70,7 +69,7 @@ public void setLocation(double x, double y, double z) {
}

@Override
public void relMove(double x, double y, double z) {
public void handleOriginalMovementPacket(double x, double y, double z) {
if (x == 0.0 && y == 0.0 && z == 0.0) return;
this.locX += x;
this.locY += y;
Expand Down Expand Up @@ -123,15 +122,15 @@ public void updateState() {
marker = (armorStandFlags & 0x10) != 0;

State prevState = currentState;
if (invisible && (name != null || marker)) {
if (invisible && name != null) {
currentState = State.HOLOGRAM;
} else {
currentState = State.ZOMBIE;
}

if (currentState != prevState) {
despawn();
spawn();
deleteEntity();
sendSpawnPacket();
} else {
updateMetadata();
updateLocation(false);
Expand All @@ -149,7 +148,7 @@ public void updateLocation(boolean remount) {
}

private void updateZombieLocation() {
sendTeleportWithHead(entityId, locX, locY, locZ, yaw, pitch, headYaw);
teleportAndUpdate(entityId, locX, locY, locZ, yaw, pitch, headYaw);
}

private void updateHologramLocation(boolean remount) {
Expand All @@ -162,10 +161,10 @@ private void updateHologramLocation(boolean remount) {
}

// Don't ask me where this offset is coming from
sendTeleport(entityIds[0], locX, (locY + (marker ? 54.85 : small ? 56 : 57)), locZ, 0, 0); // Skull
teleportEntity(entityIds[0], locX, (locY + (marker ? 54.85 : small ? 56 : 57)), locZ, 0, 0); // Skull

if (remount) {
sendTeleport(entityIds[1], locX, locY + 56.75, locZ, 0, 0); // Horse
teleportEntity(entityIds[1], locX, locY + 56.75, locZ, 0, 0); // Horse

PacketWrapper attach = PacketWrapper.create(ClientboundPackets1_7_2_5.ATTACH_ENTITY, null, user);
attach.write(Type.INT, entityIds[1]);
Expand Down Expand Up @@ -200,7 +199,7 @@ private void writeZombieMeta(PacketWrapper metadataPacket) {
metadataList.add(new Metadata(metadata.id(), metadata.metaType(), metadata.getValue()));
}
if (small) metadataList.add(new Metadata(12, MetaType1_8.Byte, (byte) 1));
protocol.getMetadataRewriter().transform(Entity1_10Types.EntityType.ZOMBIE, metadataList);
getProtocol().getMetadataRewriter().transform(Entity1_10Types.EntityType.ZOMBIE, metadataList);

metadataPacket.write(Types1_7_6_10.METADATA_LIST, metadataList);
}
Expand All @@ -217,8 +216,8 @@ private void writeHologramMeta(PacketWrapper metadataPacket) {
}

@Override
public void spawn() {
if (entityIds != null) despawn();
public void sendSpawnPacket() {
if (entityIds != null) deleteEntity();

if (currentState == State.ZOMBIE) {
spawnZombie();
Expand All @@ -231,7 +230,7 @@ public void spawn() {
}

private void spawnZombie() {
sendSpawn(entityId, 54, locX, locY, locZ);
spawnEntity(entityId, 54, locX, locY, locZ);

entityIds = new int[]{entityId};
}
Expand All @@ -250,7 +249,7 @@ private void spawnHologram() {
spawnSkull.write(Type.INT, 0);
PacketUtil.sendPacket(spawnSkull, Protocol1_7_6_10To1_8.class, true, true);

sendSpawn(entityIds[1], 100, locX, locY, locZ); // Horse
spawnEntity(entityIds[1], 100, locX, locY, locZ); // Horse

this.entityIds = entityIds;
}
Expand All @@ -268,7 +267,7 @@ public AABB getBoundingBox() {
}

@Override
public void despawn() {
public void deleteEntity() {
if (entityIds == null) return;
PacketWrapper despawn = PacketWrapper.create(ClientboundPackets1_7_2_5.DESTROY_ENTITIES, null, user);
despawn.write(Type.BYTE, (byte) entityIds.length);
Expand Down
Loading

0 comments on commit 15769e5

Please sign in to comment.