Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-12-26 16:12:42 +01:00
Add packet cancelling, implement quite a few of the todo's
Dieser Commit ist enthalten in:
Ursprung
0357d8e6aa
Commit
fce7f1740e
@ -1,7 +1,9 @@
|
||||
package us.myles.ViaVersion2.api;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import us.myles.ViaVersion2.api.data.UserConnection;
|
||||
import us.myles.ViaVersion2.api.remapper.ValueCreator;
|
||||
import us.myles.ViaVersion2.api.type.Type;
|
||||
import us.myles.ViaVersion2.api.util.Pair;
|
||||
|
||||
@ -11,6 +13,7 @@ import java.util.List;
|
||||
public class PacketWrapper {
|
||||
private final ByteBuf inputBuffer;
|
||||
private final UserConnection userConnection;
|
||||
private boolean send = true;
|
||||
private List<Pair<Type, Object>> packetValues = new ArrayList<>();
|
||||
|
||||
public PacketWrapper(ByteBuf inputBuffer, UserConnection userConnection) {
|
||||
@ -46,6 +49,7 @@ public class PacketWrapper {
|
||||
}
|
||||
|
||||
public <T> T read(Type<T> type) throws Exception {
|
||||
Preconditions.checkNotNull(inputBuffer, "This packet does not have an input buffer.");
|
||||
System.out.println("Reading: " + type.getTypeName());
|
||||
// We could in the future log input read values, but honestly for things like bulk maps, mem waste D:
|
||||
return type.read(inputBuffer);
|
||||
@ -70,8 +74,36 @@ public class PacketWrapper {
|
||||
}
|
||||
|
||||
public void writeRemaining(ByteBuf output) {
|
||||
System.out.println("Writing remaining: " + output.readableBytes());
|
||||
output.writeBytes(inputBuffer);
|
||||
if (inputBuffer != null) {
|
||||
System.out.println("Writing remaining: " + output.readableBytes());
|
||||
output.writeBytes(inputBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
public void send() throws Exception {
|
||||
ByteBuf output = inputBuffer.alloc().buffer();
|
||||
writeToBuffer(output);
|
||||
writeRemaining(output);
|
||||
|
||||
user().sendRawPacket(output);
|
||||
}
|
||||
|
||||
public PacketWrapper create() throws Exception {
|
||||
return new PacketWrapper(null, user());
|
||||
}
|
||||
|
||||
public PacketWrapper create(ValueCreator init) throws Exception {
|
||||
PacketWrapper wrapper = create();
|
||||
init.write(wrapper);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
this.send = false;
|
||||
}
|
||||
|
||||
public boolean isCancelled() {
|
||||
return !this.send;
|
||||
}
|
||||
|
||||
public UserConnection user() {
|
||||
|
@ -1,11 +1,20 @@
|
||||
package us.myles.ViaVersion2.api.data;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class UserConnection {
|
||||
private final SocketChannel channel;
|
||||
List<StoredObject> storedObjects = new ArrayList<>();
|
||||
|
||||
public UserConnection(SocketChannel socketChannel) {
|
||||
this.channel = socketChannel;
|
||||
}
|
||||
|
||||
public <T extends StoredObject> T get(Class<T> objectClass) {
|
||||
for (StoredObject o : storedObjects) {
|
||||
if (o.getClass().equals(objectClass))
|
||||
@ -25,4 +34,22 @@ public class UserConnection {
|
||||
public void put(StoredObject object) {
|
||||
storedObjects.add(object);
|
||||
}
|
||||
|
||||
public void sendRawPacket(final ByteBuf packet, boolean currentThread) {
|
||||
final ChannelHandler handler = channel.pipeline().get("encoder");
|
||||
if (currentThread) {
|
||||
channel.pipeline().context(handler).writeAndFlush(packet);
|
||||
} else {
|
||||
channel.eventLoop().submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
channel.pipeline().context(handler).writeAndFlush(packet);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void sendRawPacket(final ByteBuf packet) {
|
||||
sendRawPacket(packet, false);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package us.myles.ViaVersion2.api.protocol;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import us.myles.ViaVersion.CancelException;
|
||||
import us.myles.ViaVersion.packets.Direction;
|
||||
import us.myles.ViaVersion.packets.State;
|
||||
import us.myles.ViaVersion2.api.PacketWrapper;
|
||||
@ -66,6 +67,8 @@ public abstract class Protocol {
|
||||
// remap
|
||||
if (protocolPacket.getRemapper() != null) {
|
||||
protocolPacket.getRemapper().remap(packetWrapper);
|
||||
if(packetWrapper.isCancelled())
|
||||
throw new CancelException();
|
||||
// write to output
|
||||
packetWrapper.writeToBuffer(output);
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
package us.myles.ViaVersion2.api.protocol1_9to1_8.packets;
|
||||
|
||||
import us.myles.ViaVersion.ViaVersionPlugin;
|
||||
import us.myles.ViaVersion.api.ViaVersion;
|
||||
import us.myles.ViaVersion.packets.State;
|
||||
import us.myles.ViaVersion2.api.PacketWrapper;
|
||||
import us.myles.ViaVersion2.api.protocol.Protocol;
|
||||
import us.myles.ViaVersion2.api.protocol1_9to1_8.Protocol1_9TO1_8;
|
||||
import us.myles.ViaVersion2.api.protocol1_9to1_8.storage.EntityTracker;
|
||||
import us.myles.ViaVersion2.api.remapper.PacketHandler;
|
||||
import us.myles.ViaVersion2.api.remapper.PacketRemapper;
|
||||
import us.myles.ViaVersion2.api.remapper.ValueTransformer;
|
||||
@ -29,9 +32,26 @@ public class EntityPackets {
|
||||
// Leash boolean is removed in new versions
|
||||
map(Type.BOOLEAN, new ValueTransformer<Boolean, Void>(Type.NOTHING) {
|
||||
@Override
|
||||
public Void transform(PacketWrapper wrapper, Boolean inputValue) {
|
||||
public Void transform(PacketWrapper wrapper, Boolean inputValue) throws Exception {
|
||||
EntityTracker tracker = wrapper.user().get(EntityTracker.class);
|
||||
if (!inputValue) {
|
||||
// TODO: Write Set Passengers packet
|
||||
int passenger = wrapper.get(Type.VAR_INT, 0);
|
||||
int vehicle = wrapper.get(Type.VAR_INT, 1);
|
||||
|
||||
wrapper.cancel(); // Don't send current packet
|
||||
|
||||
PacketWrapper passengerPacket = wrapper.create();
|
||||
passengerPacket.write(Type.VAR_INT, 0x40); // Passenger Packet ID
|
||||
if (vehicle == -1) {
|
||||
if (!tracker.getVehicleMap().containsKey(passenger))
|
||||
return null; // Cancel
|
||||
passengerPacket.write(Type.VAR_INT, tracker.getVehicleMap().remove(passenger));
|
||||
passengerPacket.write(Type.VAR_INT_ARRAY, new Integer[]{});
|
||||
} else {
|
||||
passengerPacket.write(Type.VAR_INT, vehicle);
|
||||
passengerPacket.write(Type.VAR_INT_ARRAY, new Integer[]{passenger});
|
||||
}
|
||||
passengerPacket.send(); // Send the packet
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -53,7 +73,20 @@ public class EntityPackets {
|
||||
|
||||
map(Type.BOOLEAN); // 6 - On Ground
|
||||
|
||||
// TODO: Move holograms up on Y by offset (*32)
|
||||
handler(new PacketHandler() {
|
||||
@Override
|
||||
public void handle(PacketWrapper wrapper) throws Exception {
|
||||
int entityID = wrapper.get(Type.VAR_INT, 0);
|
||||
if (((ViaVersionPlugin) ViaVersion.getInstance()).isHologramPatch()) {
|
||||
EntityTracker tracker = wrapper.user().get(EntityTracker.class);
|
||||
if (tracker.getKnownHolograms().contains(entityID)) {
|
||||
Double newValue = wrapper.get(Type.DOUBLE, 1);
|
||||
newValue += (32D * ((ViaVersionPlugin) ViaVersion.getInstance()).getHologramYOffset());
|
||||
wrapper.set(Type.DOUBLE, 1, newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
// Entity Look Move Packet
|
||||
@ -71,7 +104,20 @@ public class EntityPackets {
|
||||
|
||||
map(Type.BOOLEAN); // 6 - On Ground
|
||||
|
||||
// TODO: Hologram patch moves down by 1 in Y
|
||||
handler(new PacketHandler() {
|
||||
@Override
|
||||
public void handle(PacketWrapper wrapper) throws Exception {
|
||||
int entityID = wrapper.get(Type.VAR_INT, 0);
|
||||
if (((ViaVersionPlugin) ViaVersion.getInstance()).isHologramPatch()) {
|
||||
EntityTracker tracker = wrapper.user().get(EntityTracker.class);
|
||||
if (tracker.getKnownHolograms().contains(entityID)) {
|
||||
Short newValue = wrapper.get(Type.SHORT, 1);
|
||||
newValue = (short) (newValue + (((ViaVersionPlugin) ViaVersion.getInstance()).getHologramYOffset()));
|
||||
wrapper.set(Type.SHORT, 1, newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
// Entity Relative Move Packet
|
||||
@ -86,7 +132,20 @@ public class EntityPackets {
|
||||
|
||||
map(Type.BOOLEAN); // 4 - On Ground
|
||||
|
||||
// TODO: Hologram patch moves down by 1 in Y
|
||||
handler(new PacketHandler() {
|
||||
@Override
|
||||
public void handle(PacketWrapper wrapper) throws Exception {
|
||||
int entityID = wrapper.get(Type.VAR_INT, 0);
|
||||
if (((ViaVersionPlugin) ViaVersion.getInstance()).isHologramPatch()) {
|
||||
EntityTracker tracker = wrapper.user().get(EntityTracker.class);
|
||||
if (tracker.getKnownHolograms().contains(entityID)) {
|
||||
Short newValue = wrapper.get(Type.SHORT, 1);
|
||||
newValue = (short) (newValue + (((ViaVersionPlugin) ViaVersion.getInstance()).getHologramYOffset()));
|
||||
wrapper.set(Type.SHORT, 1, newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
// Entity Equipment Packet
|
||||
@ -158,9 +217,17 @@ public class EntityPackets {
|
||||
map(Type.VAR_INT); // 0 - Player ID
|
||||
map(Type.VAR_INT); // 1 - Action
|
||||
map(Type.VAR_INT); // 2 - Jump
|
||||
|
||||
// TODO: If action is 6 or 8 cancel
|
||||
// If action is 7 change to 6
|
||||
handler(new PacketHandler() {
|
||||
@Override
|
||||
public void handle(PacketWrapper wrapper) throws Exception {
|
||||
int action = wrapper.get(Type.VAR_INT, 1);
|
||||
if (action == 6 || action == 8)
|
||||
wrapper.cancel();
|
||||
if (action == 7) {
|
||||
wrapper.set(Type.VAR_INT, 1, 6);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -136,8 +136,15 @@ public class PlayerPackets {
|
||||
map(Type.UNSIGNED_BYTE); // 4 - Max Players (Tab)
|
||||
map(Type.STRING); // 5 - Level Type
|
||||
map(Type.BOOLEAN); // 6 - Reduced Debug info
|
||||
|
||||
// TODO Register player ID as self ID
|
||||
|
||||
handler(new PacketHandler() {
|
||||
@Override
|
||||
public void handle(PacketWrapper wrapper) throws Exception {
|
||||
int myID = wrapper.get(Type.INT, 0);
|
||||
EntityTracker tracker = wrapper.user().get(EntityTracker.class);
|
||||
tracker.setEntityID(myID);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -97,8 +97,14 @@ public class WorldPackets {
|
||||
map(Type.UNSIGNED_BYTE); // 0 - Status
|
||||
map(Type.POSITION); // 1 - Position
|
||||
map(Type.UNSIGNED_BYTE); // 2 - Face
|
||||
|
||||
// TODO: Cancel if status == 6
|
||||
handler(new PacketHandler() {
|
||||
@Override
|
||||
public void handle(PacketWrapper wrapper) throws Exception {
|
||||
int status = wrapper.get(Type.UNSIGNED_BYTE, 0);
|
||||
if(status == 6)
|
||||
wrapper.cancel();
|
||||
}
|
||||
});
|
||||
// TODO: Blocking patch stopped if blocking and 5
|
||||
}
|
||||
});
|
||||
|
@ -1,18 +1,23 @@
|
||||
package us.myles.ViaVersion2.api.protocol1_9to1_8.storage;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import us.myles.ViaVersion.api.boss.BossBar;
|
||||
import us.myles.ViaVersion2.api.data.StoredObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
@Getter
|
||||
public class EntityTracker extends StoredObject{
|
||||
private final Map<Integer, UUID> uuidMap = new HashMap<>();
|
||||
private final Map<Integer, EntityType> clientEntityTypes = new HashMap<>();
|
||||
private final Map<Integer, Integer> vehicleMap = new HashMap<>();
|
||||
private final Map<Integer, BossBar> bossBarMap = new HashMap<>();
|
||||
private final Set<Integer> validBlocking = new HashSet<>();
|
||||
private final Set<Integer> knownHolograms = new HashSet<>();
|
||||
@Setter
|
||||
private int entityID;
|
||||
|
||||
public UUID getEntityUUID(int id) {
|
||||
if (uuidMap.containsKey(id)) {
|
||||
@ -26,5 +31,14 @@ public class EntityTracker extends StoredObject{
|
||||
|
||||
public void removeEntity(Integer entityID) {
|
||||
clientEntityTypes.remove(entityID);
|
||||
vehicleMap.remove(entityID);
|
||||
uuidMap.remove(entityID);
|
||||
validBlocking.remove(entityID);
|
||||
knownHolograms.remove(entityID);
|
||||
|
||||
BossBar bar = bossBarMap.remove(entityID);
|
||||
if (bar != null) {
|
||||
bar.hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,10 +10,10 @@ public abstract class ValueTransformer<T1, T2> implements ValueWriter<T1> {
|
||||
this.outputType = outputType;
|
||||
}
|
||||
|
||||
public abstract T2 transform(PacketWrapper wrapper, T1 inputValue);
|
||||
public abstract T2 transform(PacketWrapper wrapper, T1 inputValue) throws Exception;
|
||||
|
||||
@Override
|
||||
public void write(PacketWrapper writer, T1 inputValue) {
|
||||
public void write(PacketWrapper writer, T1 inputValue) throws Exception {
|
||||
writer.write(outputType, transform(writer, inputValue));
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren