Rewrite metadata changes en add missing entity track

This commit is contained in:
Matsv 2019-01-19 15:53:31 +01:00
parent 87f217ca37
commit 842631feef
2 changed files with 307 additions and 228 deletions

View File

@ -60,6 +60,10 @@ public abstract class EntityRewriter<T extends BackwardsProtocol> extends Rewrit
getEntityTracker(connection).trackEntityType(entityId, type);
}
protected boolean hasData(AbstractEntityType type) {
return entityTypes.containsKey(type);
}
protected Optional<EntityData> getEntityData(AbstractEntityType type) {
if (!entityTypes.containsKey(type))
return Optional.empty();
@ -144,8 +148,9 @@ public abstract class EntityRewriter<T extends BackwardsProtocol> extends Rewrit
EntityData entData = opEd.get();
if (entData.getMobName() != null &&
(data.getValue() == null || ((String) data.getValue()).isEmpty()) &&
data.getMetaType().getTypeID() == getDisplayNameMetaType().getTypeID())
data.getMetaType().getTypeID() == getDisplayNameMetaType().getTypeID()) {
data.setValue(entData.getMobName());
}
}
}

View File

@ -4,6 +4,7 @@ import nl.matsv.viabackwards.ViaBackwards;
import nl.matsv.viabackwards.api.entities.storage.EntityData;
import nl.matsv.viabackwards.api.entities.storage.MetaStorage;
import nl.matsv.viabackwards.api.entities.types.AbstractEntityType;
import nl.matsv.viabackwards.api.entities.types.EntityType1_12;
import nl.matsv.viabackwards.api.entities.types.EntityType1_13;
import nl.matsv.viabackwards.api.entities.types.EntityType1_13.EntityType;
import nl.matsv.viabackwards.api.rewriters.EntityRewriter;
@ -19,152 +20,153 @@ import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_12;
import us.myles.ViaVersion.api.type.types.version.Types1_13;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import java.util.Optional;
public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
@Override
protected void registerPackets(Protocol1_12_2To1_13 protocol) {
@Override
protected void registerPackets(Protocol1_12_2To1_13 protocol) {
//Spawn Object
protocol.out(State.PLAY, 0x00, 0x00, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.UUID);
map(Type.BYTE);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.BYTE);
map(Type.BYTE);
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
byte type = wrapper.get(Type.BYTE, 0);
EntityType entityType = EntityType1_13.getTypeFromId(type, true);
if (entityType == null) {
ViaBackwards.getPlatform().getLogger().warning("Could not find 1.13 entity type " + type);
return;
}
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
entityType
);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Optional<EntityType1_13.ObjectType> type = EntityType1_13.ObjectType.findById(wrapper.get(Type.BYTE, 0));
if (type.isPresent() && type.get() == EntityType1_13.ObjectType.FALLING_BLOCK) {
int blockState = wrapper.get(Type.INT, 0);
int combined = BlockItemPackets1_13.toOldId(blockState);
combined = ((combined >> 4) & 0xFFF) | ((combined & 0xF) << 12);
wrapper.set(Type.INT, 0, combined);
} else if (type.isPresent() && type.get() == EntityType1_13.ObjectType.ITEM_FRAME) {
int data = wrapper.get(Type.INT, 0);
switch (data) {
case 3:
data = 0;
break;
case 4:
data = 1;
break;
case 5:
data = 3;
break;
}
wrapper.set(Type.INT, 0, data);
}
}
});
}
});
//Spawn Object
protocol.out(State.PLAY, 0x00, 0x00, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.UUID);
map(Type.BYTE);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.BYTE);
map(Type.BYTE);
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
byte type = wrapper.get(Type.BYTE, 0);
EntityType entityType = EntityType1_13.getTypeFromId(type, true);
if (entityType == null) {
ViaBackwards.getPlatform().getLogger().warning("Could not find 1.13 entity type " + type);
return;
}
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
entityType
);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Optional<EntityType1_13.ObjectType> type = EntityType1_13.ObjectType.findById(wrapper.get(Type.BYTE, 0));
if (type.isPresent() && type.get() == EntityType1_13.ObjectType.FALLING_BLOCK) {
int blockState = wrapper.get(Type.INT, 0);
int combined = BlockItemPackets1_13.toOldId(blockState);
combined = ((combined >> 4) & 0xFFF) | ((combined & 0xF) << 12);
wrapper.set(Type.INT, 0, combined);
} else if (type.isPresent() && type.get() == EntityType1_13.ObjectType.ITEM_FRAME) {
int data = wrapper.get(Type.INT, 0);
switch (data) {
case 3:
data = 0;
break;
case 4:
data = 1;
break;
case 5:
data = 3;
break;
}
wrapper.set(Type.INT, 0, data);
}
}
});
}
});
//Spawn Experience Orb
protocol.out(State.PLAY, 0x01, 0x01, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
EntityType.XP_ORB
);
}
});
}
});
//Spawn Experience Orb
protocol.out(State.PLAY, 0x01, 0x01, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
EntityType.XP_ORB
);
}
});
}
});
//Spawn Global Entity
protocol.out(State.PLAY, 0x02, 0x02, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.BYTE);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
EntityType.LIGHTNING_BOLT
);
}
});
}
});
//Spawn Global Entity
protocol.out(State.PLAY, 0x02, 0x02, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.BYTE);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
EntityType.LIGHTNING_BOLT
);
}
});
}
});
//Spawn Mob
protocol.out(State.PLAY, 0x03, 0x03, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.UUID);
map(Type.VAR_INT);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.BYTE);
map(Type.BYTE);
map(Type.BYTE);
map(Type.SHORT);
map(Type.SHORT);
map(Type.SHORT);
map(Types1_13.METADATA_LIST, Types1_12.METADATA_LIST);
//Spawn Mob
protocol.out(State.PLAY, 0x03, 0x03, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.UUID);
map(Type.VAR_INT);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.BYTE);
map(Type.BYTE);
map(Type.BYTE);
map(Type.SHORT);
map(Type.SHORT);
map(Type.SHORT);
map(Types1_13.METADATA_LIST, Types1_12.METADATA_LIST);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int type = wrapper.get(Type.VAR_INT, 1);
EntityType entityType = EntityType1_13.getTypeFromId(type, false);
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
entityType
);
Optional<Integer> oldId = EntityTypeMapping.getOldId(type);
if (!oldId.isPresent()) {
ViaBackwards.getPlatform().getLogger().warning("Could not find 1.12 entity type for 1.13 entity type " + type + "/" + entityType);
return;
} else {
wrapper.set(Type.VAR_INT, 1, oldId.get());
}
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int type = wrapper.get(Type.VAR_INT, 1);
EntityType entityType = EntityType1_13.getTypeFromId(type, false);
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
entityType
);
Optional<Integer> oldId = EntityTypeMapping.getOldId(type);
if (!oldId.isPresent()) {
if (!hasData(entityType))
ViaBackwards.getPlatform().getLogger().warning("Could not find 1.12 entity type for 1.13 entity type " + type + "/" + entityType);
} else {
wrapper.set(Type.VAR_INT, 1, oldId.get());
}
}
});
// Handle entity type & metadata
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
AbstractEntityType type = getEntityType(wrapper.user(), entityId);
@ -176,12 +178,16 @@ public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
);
Optional<EntityData> optEntDat = getEntityData(type);
System.out.println(optEntDat + " OPTENT");
if (optEntDat.isPresent()) {
EntityData data = optEntDat.get();
wrapper.set(Type.VAR_INT, 1, data.getReplacementId());
Optional<Integer> replacementId = EntityTypeMapping.getOldId(data.getReplacementId());
wrapper.set(Type.VAR_INT, 1, replacementId.orElse(EntityType1_12.EntityType.ZOMBIE.getId()));
if (data.hasBaseMeta())
data.getDefaultMeta().handle(storage);
}
System.out.println(storage.getMetaDataList());
// Rewrite Metadata
wrapper.set(
@ -190,34 +196,34 @@ public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
storage.getMetaDataList()
);
}
});
}
});
});
}
});
// Spawn Player
protocol.out(State.PLAY, 0x05, 0x05, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.UUID);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.BYTE);
map(Type.BYTE);
map(Types1_13.METADATA_LIST, Types1_12.METADATA_LIST);
// Spawn Player
protocol.out(State.PLAY, 0x05, 0x05, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.UUID);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.BYTE);
map(Type.BYTE);
map(Types1_13.METADATA_LIST, Types1_12.METADATA_LIST);
// Track Entity
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
EntityType.PLAYER
);
}
});
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
EntityType.PLAYER
);
}
});
// Rewrite Metadata
handler(new PacketHandler() {
@ -235,34 +241,34 @@ public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
}
});
}
});
});
//Spawn Painting
protocol.out(State.PLAY, 0x04, 0x04, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.UUID);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
EntityType.PAINTING
);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int motive = wrapper.read(Type.VAR_INT);
String title = PaintingMapping.getStringId(motive);
wrapper.write(Type.STRING, title);
}
});
}
});
//Spawn Painting
protocol.out(State.PLAY, 0x04, 0x04, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.UUID);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
EntityType.PAINTING
);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int motive = wrapper.read(Type.VAR_INT);
String title = PaintingMapping.getStringId(motive);
wrapper.write(Type.STRING, title);
}
});
}
});
// Join game
protocol.out(State.PLAY, 0x25, 0x23, new PacketRemapper() {
@ -272,6 +278,19 @@ public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
map(Type.UNSIGNED_BYTE); // 1 - Gamemode
map(Type.INT); // 2 - Dimension
// Track Entity
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.INT, 0),
EntityType1_12.EntityType.PLAYER
);
}
});
// Save dimension
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
@ -321,70 +340,125 @@ public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
protocol.registerOutgoing(State.PLAY, 0x3F, 0x3C, new PacketRemapper() {
@Override
public void registerMap() {
// TODO HANDLE
map(Type.VAR_INT); // 0 - Entity ID
map(Types1_13.METADATA_LIST, Types1_12.METADATA_LIST); // 1 - Metadata list
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.cancel();
wrapper.set(
Types1_12.METADATA_LIST,
0,
handleMeta(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
new MetaStorage(wrapper.get(Types1_12.METADATA_LIST, 0))
).getMetaDataList()
);
}
});
}
});
}
@Override
protected void registerRewrites() {
// Rewrite new Entity 'drowned'
regEntType(EntityType.DROWNED, EntityType.ZOMBIE_VILLAGER).mobName("Drowned");
@Override
protected void registerRewrites() {
// Rewrite new Entity 'drowned'
regEntType(EntityType.DROWNED, EntityType.ZOMBIE_VILLAGER).mobName("Drowned");
// Fishy
regEntType(EntityType.COD_MOB, EntityType.SQUID).mobName("Cod");
regEntType(EntityType.SALMON_MOB, EntityType.SQUID).mobName("Salmon");
regEntType(EntityType.PUFFER_FISH, EntityType.SQUID).mobName("Puffer Fish");
regEntType(EntityType.TROPICAL_FISH, EntityType.SQUID).mobName("Tropical Fish");
// Fishy
regEntType(EntityType.COD_MOB, EntityType.SQUID).mobName("Cod");
regEntType(EntityType.SALMON_MOB, EntityType.SQUID).mobName("Salmon");
regEntType(EntityType.PUFFER_FISH, EntityType.SQUID).mobName("Puffer Fish");
regEntType(EntityType.TROPICAL_FISH, EntityType.SQUID).mobName("Tropical Fish");
// Phantom
regEntType(EntityType.PHANTOM, EntityType.PARROT).mobName("Phantom");
// Phantom
regEntType(EntityType.PHANTOM, EntityType.PARROT).mobName("Phantom").spawnMetadata(storage -> {
// The phantom is grey/blue so let's do yellow/blue
storage.add(new Metadata(15, MetaType1_12.VarInt, 3));
});
// Dolphin
regEntType(EntityType.DOLPHIN, EntityType.SQUID).mobName("Dolphin");
// Dolphin
regEntType(EntityType.DOLPHIN, EntityType.SQUID).mobName("Dolphin");
// Turtle
regEntType(EntityType.TURTLE, EntityType.OCELOT).mobName("Turtle");
// Turtle
regEntType(EntityType.TURTLE, EntityType.OCELOT).mobName("Turtle");
// Rewrite Meta types
this.registerMetaHandler().handle(e -> {
Metadata meta = e.getData();
int typeId = meta.getMetaType().getTypeID();
// Rewrite Meta types
registerMetaHandler().handle(e -> {
Metadata meta = e.getData();
int typeId = meta.getMetaType().getTypeID();
// Rewrite optional chat to chat
if (typeId == 5) {
meta.setMetaType(MetaType1_12.Chat);
// Rewrite optional chat to chat
if (typeId == 5) {
meta.setMetaType(MetaType1_12.String);
if (meta.getValue() == null) {
meta.setValue("");
}
}
if (meta.getValue() == null) {
meta.setValue("");
}
}
// Discontinue particles
else if (typeId == 15) {
meta.setMetaType(MetaType1_12.Discontinued);
}
// Discontinue particles
else if (typeId == 15) {
meta.setMetaType(MetaType1_12.Discontinued);
}
// Rewrite to 1.12 ids
else if (typeId > 5) {
meta.setMetaType(MetaType1_12.byId(
typeId - 1
));
}
// Rewrite to 1.12 ids
else if (typeId > 5) {
meta.setMetaType(MetaType1_12.byId(
typeId - 1
));
}
return e.getData();
});
return meta;
});
// TODO Remove everything for now
this.registerMetaHandler().removed();
// Rewrite Custom Name from Chat to String
registerMetaHandler().filter(EntityType.ENTITY, true, 2).handle(e -> {
Metadata meta = e.getData();
}
meta.setValue(
ChatRewriter.jsonTextToLegacy(
(String) meta.getValue()
)
);
return meta;
});
// Handle zombie metadata
registerMetaHandler().filter(EntityType.ZOMBIE, true, 15).removed();
registerMetaHandler().filter(EntityType.ZOMBIE, true).handle(e -> {
Metadata meta = e.getData();
if (meta.getId() > 15) {
meta.setId(meta.getId() - 1);
}
return meta;
});
// Handle turtle metadata (Remove them all for now)
registerMetaHandler().filter(EntityType.TURTLE, 13).removed(); // Home pos
registerMetaHandler().filter(EntityType.TURTLE, 14).removed(); // Has egg
registerMetaHandler().filter(EntityType.TURTLE, 15).removed(); // Laying egg
registerMetaHandler().filter(EntityType.TURTLE, 16).removed(); // Travel pos
registerMetaHandler().filter(EntityType.TURTLE, 17).removed(); // Going home
registerMetaHandler().filter(EntityType.TURTLE, 18).removed(); // Traveling
// Remove additional fish meta
registerMetaHandler().filter(EntityType.ABSTRACT_FISHES, true, 12).removed();
registerMetaHandler().filter(EntityType.ABSTRACT_FISHES, true, 13).removed();
// Remove phantom size
registerMetaHandler().filter(EntityType.PHANTOM, 12).removed();
// Remove boat splash timer
registerMetaHandler().filter(EntityType.BOAT, 12).removed();
// Remove Trident special loyalty level
registerMetaHandler().filter(EntityType.TRIDENT, 7).removed();
}
}