mirror of
https://github.com/ViaVersion/ViaBackwards.git
synced 2024-12-23 17:08:00 +01:00
Some metadata and entity rewriter cleanup
This commit is contained in:
parent
cf5e5590ad
commit
004d0d2b23
@ -77,19 +77,17 @@ public class MetaHandlerSettings {
|
||||
return filterFamily;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the metadata should be handled by this object.
|
||||
*
|
||||
* @param type entity type
|
||||
* @param metadata metadata
|
||||
* @return true if gucci
|
||||
*/
|
||||
public boolean isGucci(EntityType type, Metadata metadata) {
|
||||
if (!hasHandler()) return false;
|
||||
|
||||
if (hasType()) {
|
||||
if (filterFamily) {
|
||||
if (!type.isOrHasParent(filterType)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!filterType.is(type)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (hasType() && (filterFamily ? !type.isOrHasParent(filterType) : !filterType.is(type))) {
|
||||
return false;
|
||||
}
|
||||
return !hasIndex() || metadata.getId() == filterIndex;
|
||||
}
|
||||
|
@ -18,7 +18,8 @@ public class RemovedValueException extends IOException {
|
||||
* May be cached since it is never actually printed, only checked.
|
||||
*/
|
||||
public static final RemovedValueException EX = new RemovedValueException() {
|
||||
public synchronized Throwable fillInStackTrace() {
|
||||
@Override
|
||||
public Throwable fillInStackTrace() {
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
@ -24,10 +24,12 @@ import us.myles.ViaVersion.packets.State;
|
||||
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Entity rewriter base class.
|
||||
@ -138,60 +140,52 @@ public abstract class EntityRewriterBase<T extends BackwardsProtocol> extends Re
|
||||
}
|
||||
|
||||
EntityType type = storedEntity.getType();
|
||||
List<Metadata> newList = new ArrayList<>();
|
||||
for (MetaHandlerSettings settings : metaHandlers) {
|
||||
List<Metadata> extraData = null;
|
||||
for (Metadata md : storage.getMetaDataList()) {
|
||||
Metadata nmd = md;
|
||||
List<Metadata> newData = new ArrayList<>();
|
||||
for (Metadata meta : storage.getMetaDataList()) {
|
||||
MetaHandlerEvent event = null;
|
||||
try {
|
||||
if (settings.isGucci(type, nmd)) {
|
||||
event = new MetaHandlerEvent(user, storedEntity, nmd.getId(), nmd, storage);
|
||||
nmd = settings.getHandler().handle(event);
|
||||
Metadata modifiedMeta = meta;
|
||||
if (settings.isGucci(type, meta)) {
|
||||
event = new MetaHandlerEvent(user, storedEntity, meta.getId(), meta, storage);
|
||||
modifiedMeta = settings.getHandler().handle(event);
|
||||
|
||||
if (event.getExtraData() != null) {
|
||||
(extraData != null ? extraData : (extraData = new ArrayList<>())).addAll(event.getExtraData());
|
||||
newData.addAll(event.getExtraData());
|
||||
event.clearExtraData();
|
||||
}
|
||||
}
|
||||
|
||||
if (nmd == null) {
|
||||
if (modifiedMeta == null) {
|
||||
throw RemovedValueException.EX;
|
||||
}
|
||||
|
||||
newList.add(nmd);
|
||||
newData.add(modifiedMeta);
|
||||
} catch (RemovedValueException e) {
|
||||
// add the additionally created data here in case of an interruption
|
||||
// Add the additionally created data here in case of an interruption
|
||||
if (event != null && event.getExtraData() != null) {
|
||||
(extraData != null ? extraData : (extraData = new ArrayList<>())).addAll(event.getExtraData());
|
||||
newData.addAll(event.getExtraData());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Logger log = ViaBackwards.getPlatform().getLogger();
|
||||
log.warning("Unable to handle metadata " + nmd);
|
||||
log.warning("Full metadata list " + storage);
|
||||
log.warning("Unable to handle metadata " + meta + " for entity type " + type);
|
||||
log.warning(storage.getMetaDataList().stream().sorted(Comparator.comparingInt(Metadata::getId))
|
||||
.map(Metadata::toString).collect(Collectors.joining("\n", "Full metadata list: ", "")));
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
List<Metadata> newData = new ArrayList<>(newList);
|
||||
if (extraData != null) {
|
||||
newData.addAll(extraData);
|
||||
}
|
||||
|
||||
storage.setMetaDataList(newData);
|
||||
newList.clear();
|
||||
}
|
||||
|
||||
// Handle Entity Name
|
||||
Metadata data = storage.get(displayNameIndex);
|
||||
if (data != null) {
|
||||
EntityData entityData = getEntityData(type);
|
||||
if (entityData != null) {
|
||||
if (entityData.getMobName() != null &&
|
||||
(data.getValue() == null || ((String) data.getValue()).isEmpty()) &&
|
||||
data.getMetaType().getTypeID() == displayNameMetaType.getTypeID()) {
|
||||
data.setValue(entityData.getMobName());
|
||||
}
|
||||
if (entityData != null && entityData.getMobName() != null
|
||||
&& (data.getValue() == null || ((String) data.getValue()).isEmpty())
|
||||
&& data.getMetaType().getTypeID() == displayNameMetaType.getTypeID()) {
|
||||
data.setValue(entityData.getMobName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -166,26 +166,10 @@ public class EntityPackets1_11 extends LegacyEntityRewriter<Protocol1_10To1_11>
|
||||
registerExtraTracker(0x04, Entity1_11Types.EntityType.PAINTING);
|
||||
|
||||
// Join game
|
||||
protocol.registerOutgoing(State.PLAY, 0x23, 0x23, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.INT); // 0 - Entity ID
|
||||
map(Type.UNSIGNED_BYTE); // 1 - Gamemode
|
||||
map(Type.INT); // 2 - Dimension
|
||||
registerJoinGame(0x23, 0x23, Entity1_11Types.EntityType.PLAYER);
|
||||
|
||||
handler(getTrackerHandler(Entity1_11Types.EntityType.PLAYER, Type.INT));
|
||||
handler(getDimensionHandler(1));
|
||||
}
|
||||
});
|
||||
|
||||
// Respawn Packet (save dimension id)
|
||||
protocol.registerOutgoing(State.PLAY, 0x33, 0x33, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.INT); // 0 - Dimension ID
|
||||
handler(getDimensionHandler(0));
|
||||
}
|
||||
});
|
||||
// Respawn Packet
|
||||
registerRespawn(0x33, 0x33);
|
||||
|
||||
// Spawn Player
|
||||
protocol.registerOutgoing(State.PLAY, 0x05, 0x05, new PacketRemapper() {
|
||||
|
@ -165,14 +165,8 @@ public class EntityPackets1_12 extends LegacyEntityRewriter<Protocol1_11_1To1_12
|
||||
}
|
||||
});
|
||||
|
||||
// Respawn Packet (save dimension id)
|
||||
protocol.registerOutgoing(State.PLAY, 0x34, 0x33, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.INT); // 0 - Dimension ID
|
||||
handler(getDimensionHandler(0));
|
||||
}
|
||||
});
|
||||
// Respawn Packet
|
||||
registerRespawn(0x34, 0x33);
|
||||
|
||||
// Destroy entities
|
||||
registerEntityDestroy(0x31, 0x30);
|
||||
|
@ -83,26 +83,10 @@ public class EntityPackets1_11_1 extends LegacyEntityRewriter<Protocol1_11To1_11
|
||||
registerExtraTracker(0x04, Entity1_11Types.EntityType.PAINTING);
|
||||
|
||||
// Join game
|
||||
protocol.registerOutgoing(State.PLAY, 0x23, 0x23, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.INT); // 0 - Entity ID
|
||||
map(Type.UNSIGNED_BYTE); // 1 - Gamemode
|
||||
map(Type.INT); // 2 - Dimension
|
||||
registerJoinGame(0x23, 0x23, Entity1_11Types.EntityType.PLAYER);
|
||||
|
||||
handler(getTrackerHandler(Entity1_11Types.EntityType.PLAYER, Type.INT));
|
||||
handler(getDimensionHandler(1));
|
||||
}
|
||||
});
|
||||
|
||||
// Respawn Packet (save dimension id)
|
||||
protocol.registerOutgoing(State.PLAY, 0x33, 0x33, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.INT); // 0 - Dimension ID
|
||||
handler(getDimensionHandler(0));
|
||||
}
|
||||
});
|
||||
// Respawn Packet
|
||||
registerRespawn(0x33, 0x33);
|
||||
|
||||
// Spawn Player
|
||||
protocol.registerOutgoing(State.PLAY, 0x05, 0x05, new PacketRemapper() {
|
||||
|
@ -198,19 +198,9 @@ public class EntityPackets1_13 extends LegacyEntityRewriter<Protocol1_12_2To1_13
|
||||
});
|
||||
|
||||
// Join game
|
||||
protocol.out(State.PLAY, 0x25, 0x23, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.INT); // 0 - Entity ID
|
||||
map(Type.UNSIGNED_BYTE); // 1 - Gamemode
|
||||
map(Type.INT); // 2 - Dimension
|
||||
registerJoinGame(0x25, 0x23, Entity1_13Types.EntityType.PLAYER);
|
||||
|
||||
handler(getTrackerHandler(Entity1_13Types.EntityType.PLAYER, Type.INT));
|
||||
handler(getDimensionHandler(1));
|
||||
}
|
||||
});
|
||||
|
||||
// Respawn Packet (save dimension id)
|
||||
// Respawn Packet
|
||||
protocol.registerOutgoing(State.PLAY, 0x38, 0x35, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
|
@ -126,28 +126,10 @@ public class EntityPackets1_13_1 extends LegacyEntityRewriter<Protocol1_13To1_13
|
||||
registerExtraTracker(0x04, Entity1_13Types.EntityType.PAINTING);
|
||||
|
||||
// Join Game
|
||||
protocol.registerOutgoing(State.PLAY, 0x25, 0x25, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.INT); // 0 - Entity ID
|
||||
map(Type.UNSIGNED_BYTE); // 1 - Gamemode
|
||||
map(Type.INT); // 2 - Dimension
|
||||
|
||||
handler(getTrackerHandler(Entity1_13Types.EntityType.PLAYER, Type.INT));
|
||||
handler(getDimensionHandler(1));
|
||||
}
|
||||
});
|
||||
registerJoinGame(0x25, 0x25, Entity1_13Types.EntityType.PLAYER);
|
||||
|
||||
// Respawn
|
||||
protocol.out(State.PLAY, 0x38, 0x38, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.INT); // 0 - Dimension ID\
|
||||
|
||||
// Save dimension
|
||||
handler(getDimensionHandler(0));
|
||||
}
|
||||
});
|
||||
registerRespawn(0x38, 0x38);
|
||||
|
||||
// Destroy entities
|
||||
registerEntityDestroy(0x35);
|
||||
|
@ -18,7 +18,6 @@ import us.myles.ViaVersion.api.type.Type;
|
||||
import us.myles.ViaVersion.api.type.types.Particle;
|
||||
import us.myles.ViaVersion.api.type.types.version.Types1_14;
|
||||
import us.myles.ViaVersion.packets.State;
|
||||
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@ -97,7 +96,7 @@ public class EntityPackets1_15 extends EntityRewriter<Protocol1_14_4To1_15> {
|
||||
public void registerMap() {
|
||||
map(Type.INT);
|
||||
map(Type.LONG, Type.NOTHING); // Seed
|
||||
handler(wrapper -> wrapper.user().get(ClientWorld.class).setEnvironment(wrapper.get(Type.INT, 0)));
|
||||
handler(getDimensionHandler(0));
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -142,26 +142,10 @@ public class EntityPackets1_10 extends LegacyEntityRewriter<Protocol1_9_4To1_10>
|
||||
registerExtraTracker(0x04, Entity1_10Types.EntityType.PAINTING);
|
||||
|
||||
// Join game
|
||||
protocol.registerOutgoing(State.PLAY, 0x23, 0x23, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.INT); // 0 - Entity ID
|
||||
map(Type.UNSIGNED_BYTE); // 1 - Gamemode
|
||||
map(Type.INT); // 2 - Dimension
|
||||
registerJoinGame(0x23, 0x23, Entity1_10Types.EntityType.PLAYER);
|
||||
|
||||
handler(getTrackerHandler(Entity1_10Types.EntityType.PLAYER, Type.INT));
|
||||
handler(getDimensionHandler(1));
|
||||
}
|
||||
});
|
||||
|
||||
// Respawn Packet (save dimension id)
|
||||
protocol.registerOutgoing(State.PLAY, 0x33, 0x33, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.INT); // 0 - Dimension ID
|
||||
handler(getDimensionHandler(0));
|
||||
}
|
||||
});
|
||||
// Respawn Packet
|
||||
registerRespawn(0x33, 0x33);
|
||||
|
||||
// Spawn Player
|
||||
protocol.registerOutgoing(State.PLAY, 0x05, 0x05, new PacketRemapper() {
|
||||
|
Loading…
Reference in New Issue
Block a user