3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-09 09:40:10 +01:00

Merge pull request #1484 from KennyTV/abstraction

Item rewriter cleanup
Dieser Commit ist enthalten in:
Myles 2019-10-12 10:06:55 +01:00 committet von GitHub
Commit 28b7f6febc
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
19 geänderte Dateien mit 197 neuen und 529 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,111 @@
package us.myles.ViaVersion.api.rewriters;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
// If any of these methods become outdated, just create a new rewriter overriding the methods
public class ItemRewriter {
private final Protocol protocol;
private final RewriteFunction toClient;
private final RewriteFunction toServer;
public ItemRewriter(Protocol protocol, RewriteFunction toClient, RewriteFunction toServer) {
this.protocol = protocol;
this.toClient = toClient;
this.toServer = toServer;
}
public void registerWindowItems(Type<Item[]> type, int oldPacketId, int newPacketId) {
protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(type); // 1 - Window Values
handler(itemArrayHandler(type));
}
});
}
public void registerSetSlot(Type<Item> type, int oldPacketId, int newPacketId) {
protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot ID
map(type); // 2 - Slot Value
handler(itemToClientHandler(type));
}
});
}
public void registerEntityEquipment(Type<Item> type, int oldPacketId, int newPacketId) {
protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Type.VAR_INT); // 1 - Slot ID
map(type); // 2 - Item
handler(itemToClientHandler(type));
}
});
}
public void registerCreativeInvAction(Type<Item> type, int oldPacketId, int newPacketId) {
protocol.registerIncoming(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(type); // 1 - Clicked Item
handler(itemToServerHandler(type));
}
});
}
public void registerClickWindow(Type<Item> type, int oldPacketId, int newPacketId) {
protocol.registerIncoming(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot
map(Type.BYTE); // 2 - Button
map(Type.SHORT); // 3 - Action number
map(Type.VAR_INT); // 4 - Mode
map(type); // 5 - Clicked Item
handler(itemToServerHandler(type));
}
});
}
// Only sent to the client
public PacketHandler itemArrayHandler(Type<Item[]> type) {
return wrapper -> {
Item[] items = wrapper.get(type, 0);
for (Item item : items) {
toClient.rewrite(item);
}
};
}
public PacketHandler itemToClientHandler(Type<Item> type) {
return wrapper -> toClient.rewrite(wrapper.get(type, 0));
}
public PacketHandler itemToServerHandler(Type<Item> type) {
return wrapper -> toServer.rewrite(wrapper.get(type, 0));
}
@FunctionalInterface
public interface RewriteFunction {
void rewrite(Item item);
}
}

Datei anzeigen

@ -15,11 +15,12 @@ import us.myles.ViaVersion.packets.State;
import java.util.*; import java.util.*;
import java.util.logging.Logger; import java.util.logging.Logger;
public abstract class MetadataRewriter<T extends Protocol> extends Rewriter<T> { public abstract class MetadataRewriter {
private final Class<? extends EntityTracker> entityTrackerClass; private final Class<? extends EntityTracker> entityTrackerClass;
private final Protocol protocol;
protected MetadataRewriter(T protocol, Class<? extends EntityTracker> entityTrackerClass) { protected MetadataRewriter(Protocol protocol, Class<? extends EntityTracker> entityTrackerClass) {
super(protocol); this.protocol = protocol;
this.entityTrackerClass = entityTrackerClass; this.entityTrackerClass = entityTrackerClass;
protocol.put(this); protocol.put(this);
} }
@ -53,7 +54,7 @@ public abstract class MetadataRewriter<T extends Protocol> extends Rewriter<T> {
} }
public void registerMetadataRewriter(int oldPacketId, int newPacketId, Type<List<Metadata>> oldMetaType, Type<List<Metadata>> newMetaType) { public void registerMetadataRewriter(int oldPacketId, int newPacketId, Type<List<Metadata>> oldMetaType, Type<List<Metadata>> newMetaType) {
getProtocol().registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID map(Type.VAR_INT); // 0 - Entity ID
@ -146,7 +147,7 @@ public abstract class MetadataRewriter<T extends Protocol> extends Rewriter<T> {
} }
public void registerEntityDestroy(int oldPacketId, int newPacketId) { public void registerEntityDestroy(int oldPacketId, int newPacketId) {
getProtocol().registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.VAR_INT_ARRAY); // 0 - Entity ids map(Type.VAR_INT_ARRAY); // 0 - Entity ids

Datei anzeigen

@ -1,15 +0,0 @@
package us.myles.ViaVersion.api.rewriters;
import us.myles.ViaVersion.api.protocol.Protocol;
public abstract class Rewriter<T extends Protocol> {
protected final T protocol;
protected Rewriter(T protocol) {
this.protocol = protocol;
}
public T getProtocol() {
return protocol;
}
}

Datei anzeigen

@ -18,7 +18,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
public class MetadataRewriter1_11To1_10 extends MetadataRewriter<Protocol1_11To1_10> { public class MetadataRewriter1_11To1_10 extends MetadataRewriter {
public MetadataRewriter1_11To1_10(Protocol1_11To1_10 protocol) { public MetadataRewriter1_11To1_10(Protocol1_11To1_10 protocol) {
super(protocol, EntityTracker1_11.class); super(protocol, EntityTracker1_11.class);

Datei anzeigen

@ -1,73 +1,27 @@
package us.myles.ViaVersion.protocols.protocol1_11to1_10.packets; package us.myles.ViaVersion.protocols.protocol1_11to1_10.packets;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_11to1_10.EntityIdRewriter; import us.myles.ViaVersion.protocols.protocol1_11to1_10.EntityIdRewriter;
import us.myles.ViaVersion.protocols.protocol1_11to1_10.Protocol1_11To1_10; import us.myles.ViaVersion.protocols.protocol1_11to1_10.Protocol1_11To1_10;
public class InventoryPackets { public class InventoryPackets {
public static void register(Protocol1_11To1_10 protocol) { public static void register(Protocol1_11To1_10 protocol) {
/* ItemRewriter itemRewriter = new ItemRewriter(protocol, EntityIdRewriter::toClientItem, EntityIdRewriter::toServerItem);
Incoming packets
*/
// Set slot packet // Set slot packet
protocol.registerOutgoing(State.PLAY, 0x16, 0x16, new PacketRemapper() { itemRewriter.registerSetSlot(Type.ITEM, 0x16, 0x16);
@Override
public void registerMap() {
map(Type.BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot ID
map(Type.ITEM); // 2 - Slot Value
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
EntityIdRewriter.toClientItem(stack);
}
});
}
});
// Window items packet // Window items packet
protocol.registerOutgoing(State.PLAY, 0x14, 0x14, new PacketRemapper() { itemRewriter.registerWindowItems(Type.ITEM_ARRAY, 0x14, 0x14);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.ITEM_ARRAY); // 1 - Window Values
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.ITEM_ARRAY, 0);
for (Item stack : stacks)
EntityIdRewriter.toClientItem(stack);
}
});
}
});
// Entity Equipment Packet // Entity Equipment Packet
protocol.registerOutgoing(State.PLAY, 0x3C, 0x3C, new PacketRemapper() { itemRewriter.registerEntityEquipment(Type.ITEM, 0x3C, 0x3C);
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Type.VAR_INT); // 1 - Slot ID
map(Type.ITEM); // 2 - Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
EntityIdRewriter.toClientItem(stack);
}
});
}
});
// Plugin message Packet -> Trading // Plugin message Packet -> Trading
protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() {
@ -100,48 +54,11 @@ public class InventoryPackets {
} }
}); });
/*
Incoming packets
*/
// Click window packet // Click window packet
protocol.registerIncoming(State.PLAY, 0x07, 0x07, new PacketRemapper() { itemRewriter.registerClickWindow(Type.ITEM, 0x07, 0x07);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot
map(Type.BYTE); // 2 - Button
map(Type.SHORT); // 3 - Action number
map(Type.VAR_INT); // 4 - Mode
map(Type.ITEM); // 5 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
EntityIdRewriter.toServerItem(item);
}
});
}
}
);
// Creative Inventory Action // Creative Inventory Action
protocol.registerIncoming(State.PLAY, 0x18, 0x18, new PacketRemapper() { itemRewriter.registerCreativeInvAction(Type.ITEM, 0x18, 0x18);
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(Type.ITEM); // 1 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
EntityIdRewriter.toServerItem(item);
}
});
}
}
);
} }
} }

Datei anzeigen

@ -12,7 +12,7 @@ import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.EntityTracker1
import java.util.List; import java.util.List;
public class MetadataRewriter1_12To1_11_1 extends MetadataRewriter<Protocol1_12To1_11_1> { public class MetadataRewriter1_12To1_11_1 extends MetadataRewriter {
public MetadataRewriter1_12To1_11_1(Protocol1_12To1_11_1 protocol) { public MetadataRewriter1_12To1_11_1(Protocol1_12To1_11_1 protocol) {
super(protocol, EntityTracker1_12.class); super(protocol, EntityTracker1_12.class);

Datei anzeigen

@ -5,6 +5,7 @@ import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.BedRewriter; import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.BedRewriter;
@ -12,64 +13,18 @@ import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.Protocol1_12To1_11_1;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider; import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider;
public class InventoryPackets { public class InventoryPackets {
public static void register(Protocol1_12To1_11_1 protocol) { public static void register(Protocol1_12To1_11_1 protocol) {
/* ItemRewriter itemRewriter = new ItemRewriter(protocol, BedRewriter::toClientItem, BedRewriter::toServerItem);
Outgoing packets
*/
// Set slot packet // Set slot packet
protocol.registerOutgoing(State.PLAY, 0x16, 0x16, new PacketRemapper() { itemRewriter.registerSetSlot(Type.ITEM, 0x16, 0x16);
@Override
public void registerMap() {
map(Type.BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot ID
map(Type.ITEM); // 2 - Slot Value
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
BedRewriter.toClientItem(stack);
}
});
}
});
// Window items packet // Window items packet
protocol.registerOutgoing(State.PLAY, 0x14, 0x14, new PacketRemapper() { itemRewriter.registerWindowItems(Type.ITEM_ARRAY, 0x14, 0x14);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.ITEM_ARRAY); // 1 - Window Values
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.ITEM_ARRAY, 0);
for (Item stack : stacks)
BedRewriter.toClientItem(stack);
}
});
}
});
// Entity Equipment Packet // Entity Equipment Packet
protocol.registerOutgoing(State.PLAY, 0x3C, 0x3E, new PacketRemapper() { itemRewriter.registerEntityEquipment(Type.ITEM, 0x3C, 0x3E);
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Type.VAR_INT); // 1 - Slot ID
map(Type.ITEM); // 2 - Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
BedRewriter.toClientItem(stack);
}
});
}
});
// Plugin message Packet -> Trading // Plugin message Packet -> Trading
protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() {
@ -102,9 +57,6 @@ public class InventoryPackets {
} }
}); });
/*
Incoming packets
*/
// Click window packet // Click window packet
protocol.registerIncoming(State.PLAY, 0x07, 0x08, new PacketRemapper() { protocol.registerIncoming(State.PLAY, 0x07, 0x08, new PacketRemapper() {
@ -148,21 +100,6 @@ public class InventoryPackets {
); );
// Creative Inventory Action // Creative Inventory Action
protocol.registerIncoming(State.PLAY, 0x18, 0x1b, new PacketRemapper() { itemRewriter.registerCreativeInvAction(Type.ITEM, 0x18, 0x1b);
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(Type.ITEM); // 1 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
BedRewriter.toServerItem(item);
}
});
}
}
);
} }
} }

Datei anzeigen

@ -13,7 +13,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1
import java.util.List; import java.util.List;
public class MetadataRewriter1_13_1To1_13 extends MetadataRewriter<Protocol1_13_1To1_13> { public class MetadataRewriter1_13_1To1_13 extends MetadataRewriter {
public MetadataRewriter1_13_1To1_13(Protocol1_13_1To1_13 protocol) { public MetadataRewriter1_13_1To1_13(Protocol1_13_1To1_13 protocol) {
super(protocol, EntityTracker1_13.class); super(protocol, EntityTracker1_13.class);

Datei anzeigen

@ -5,52 +5,20 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
public class InventoryPackets { public class InventoryPackets {
public static void register(Protocol protocol) { public static void register(Protocol protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
/*
Outgoing packets
*/
// Set slot packet // Set slot packet
protocol.registerOutgoing(State.PLAY, 0x17, 0x17, new PacketRemapper() { itemRewriter.registerSetSlot(Type.FLAT_ITEM, 0x17, 0x17);
@Override
public void registerMap() {
map(Type.BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot ID
map(Type.FLAT_ITEM, Type.FLAT_ITEM); // 2 - Slot Value
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.FLAT_ITEM, 0);
toClient(stack);
}
});
}
});
// Window items packet // Window items packet
protocol.registerOutgoing(State.PLAY, 0x15, 0x15, new PacketRemapper() { itemRewriter.registerWindowItems(Type.FLAT_ITEM_ARRAY, 0x15, 0x15);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.FLAT_ITEM_ARRAY, Type.FLAT_ITEM_ARRAY); // 1 - Window Values
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.FLAT_ITEM_ARRAY, 0);
for (Item stack : stacks)
toClient(stack);
}
});
}
});
// Plugin message // Plugin message
protocol.registerOutgoing(State.PLAY, 0x19, 0x19, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x19, 0x19, new PacketRemapper() {
@ -88,22 +56,7 @@ public class InventoryPackets {
}); });
// Entity Equipment Packet // Entity Equipment Packet
protocol.registerOutgoing(State.PLAY, 0x42, 0x42, new PacketRemapper() { itemRewriter.registerEntityEquipment(Type.FLAT_ITEM, 0x42, 0x42);
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Type.VAR_INT); // 1 - Slot ID
map(Type.FLAT_ITEM, Type.FLAT_ITEM); // 2 - Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.FLAT_ITEM, 0);
toClient(stack);
}
});
}
});
// Declare Recipes // Declare Recipes
protocol.registerOutgoing(State.PLAY, 0x54, 0x54, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x54, 0x54, new PacketRemapper() {
@ -155,49 +108,11 @@ public class InventoryPackets {
}); });
/*
Incoming packets
*/
// Click window packet // Click window packet
protocol.registerIncoming(State.PLAY, 0x08, 0x08, new PacketRemapper() { itemRewriter.registerClickWindow(Type.FLAT_ITEM, 0x08, 0x08);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot
map(Type.BYTE); // 2 - Button
map(Type.SHORT); // 3 - Action number
map(Type.VAR_INT); // 4 - Mode
map(Type.FLAT_ITEM, Type.FLAT_ITEM); // 5 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.FLAT_ITEM, 0);
toServer(item);
}
});
}
}
);
// Creative Inventory Action // Creative Inventory Action
protocol.registerIncoming(State.PLAY, 0x24, 0x24, new PacketRemapper() { itemRewriter.registerCreativeInvAction(Type.FLAT_ITEM, 0x24, 0x24);
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(Type.FLAT_ITEM, Type.FLAT_ITEM); // 1 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.FLAT_ITEM, 0);
toServer(item);
}
});
}
}
);
} }
public static void toClient(Item item) { public static void toClient(Item item) {

Datei anzeigen

@ -10,11 +10,6 @@ import us.myles.ViaVersion.packets.State;
public class InventoryPackets { public class InventoryPackets {
public static void register(Protocol protocol) { public static void register(Protocol protocol) {
/*
Outgoing packets
*/
// Set slot packet // Set slot packet
protocol.registerOutgoing(State.PLAY, 0x17, 0x17, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x17, 0x17, new PacketRemapper() {
@Override @Override
@ -118,11 +113,6 @@ public class InventoryPackets {
} }
}); });
/*
Incoming packets
*/
// Click window packet // Click window packet
protocol.registerIncoming(State.PLAY, 0x08, 0x08, new PacketRemapper() { protocol.registerIncoming(State.PLAY, 0x08, 0x08, new PacketRemapper() {
@Override @Override

Datei anzeigen

@ -19,7 +19,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter<Protocol1_13To1_12_2> { public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter {
public MetadataRewriter1_13To1_12_2(Protocol1_13To1_12_2 protocol) { public MetadataRewriter1_13To1_12_2(Protocol1_13To1_12_2 protocol) {
super(protocol, EntityTracker1_13.class); super(protocol, EntityTracker1_13.class);

Datei anzeigen

@ -10,9 +10,11 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.BlockIdData; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.BlockIdData;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.SoundSource; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.SoundSource;
@ -25,14 +27,10 @@ import java.util.Locale;
import java.util.Optional; import java.util.Optional;
public class InventoryPackets { public class InventoryPackets {
private static String NBT_TAG_NAME; private static final String NBT_TAG_NAME = "ViaVersion|" + Protocol1_13To1_12_2.class.getSimpleName();
public static void register(Protocol protocol) { public static void register(Protocol protocol) {
NBT_TAG_NAME = "ViaVersion|" + protocol.getClass().getSimpleName(); ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
/*
Outgoing packets
*/
// Set slot packet // Set slot packet
protocol.registerOutgoing(State.PLAY, 0x16, 0x17, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x16, 0x17, new PacketRemapper() {
@ -42,13 +40,7 @@ public class InventoryPackets {
map(Type.SHORT); // 1 - Slot ID map(Type.SHORT); // 1 - Slot ID
map(Type.ITEM, Type.FLAT_ITEM); // 2 - Slot Value map(Type.ITEM, Type.FLAT_ITEM); // 2 - Slot Value
handler(new PacketHandler() { handler(itemRewriter.itemToClientHandler(Type.FLAT_ITEM));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.FLAT_ITEM, 0);
toClient(stack);
}
});
} }
}); });
@ -59,14 +51,7 @@ public class InventoryPackets {
map(Type.UNSIGNED_BYTE); // 0 - Window ID map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.ITEM_ARRAY, Type.FLAT_ITEM_ARRAY); // 1 - Window Values map(Type.ITEM_ARRAY, Type.FLAT_ITEM_ARRAY); // 1 - Window Values
handler(new PacketHandler() { handler(itemRewriter.itemArrayHandler(Type.FLAT_ITEM_ARRAY));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.FLAT_ITEM_ARRAY, 0);
for (Item stack : stacks)
toClient(stack);
}
});
} }
}); });
@ -77,14 +62,13 @@ public class InventoryPackets {
map(Type.UNSIGNED_BYTE); // Window id map(Type.UNSIGNED_BYTE); // Window id
map(Type.SHORT); // Property map(Type.SHORT); // Property
map(Type.SHORT); // Value map(Type.SHORT); // Value
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
short property = wrapper.get(Type.SHORT, 0); short property = wrapper.get(Type.SHORT, 0);
if (property >= 4 && property <= 6) { // Enchantment id if (property >= 4 && property <= 6) { // Enchantment id
wrapper.set(Type.SHORT, 1, (short) MappingData.enchantmentMappings.getNewId( wrapper.set(Type.SHORT, 1, (short) MappingData.enchantmentMappings.getNewId(wrapper.get(Type.SHORT, 1)));
wrapper.get(Type.SHORT, 1)
));
} }
} }
}); });
@ -202,21 +186,11 @@ public class InventoryPackets {
map(Type.VAR_INT); // 1 - Slot ID map(Type.VAR_INT); // 1 - Slot ID
map(Type.ITEM, Type.FLAT_ITEM); // 2 - Item map(Type.ITEM, Type.FLAT_ITEM); // 2 - Item
handler(new PacketHandler() { handler(itemRewriter.itemToClientHandler(Type.FLAT_ITEM));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.FLAT_ITEM, 0);
toClient(stack);
}
});
} }
}); });
/*
Incoming packets
*/
// Click window packet // Click window packet
protocol.registerIncoming(State.PLAY, 0x07, 0x08, new PacketRemapper() { protocol.registerIncoming(State.PLAY, 0x07, 0x08, new PacketRemapper() {
@Override @Override
@ -228,16 +202,9 @@ public class InventoryPackets {
map(Type.VAR_INT); // 4 - Mode map(Type.VAR_INT); // 4 - Mode
map(Type.FLAT_ITEM, Type.ITEM); // 5 - Clicked Item map(Type.FLAT_ITEM, Type.ITEM); // 5 - Clicked Item
handler(new PacketHandler() { handler(itemRewriter.itemToServerHandler(Type.ITEM));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
toServer(item);
} }
}); });
}
}
);
// Plugin message // Plugin message
protocol.registerIncoming(State.PLAY, 0x09, 0x0A, new PacketRemapper() { protocol.registerIncoming(State.PLAY, 0x09, 0x0A, new PacketRemapper() {
@ -282,17 +249,10 @@ public class InventoryPackets {
map(Type.SHORT); // 0 - Slot map(Type.SHORT); // 0 - Slot
map(Type.FLAT_ITEM, Type.ITEM); // 1 - Clicked Item map(Type.FLAT_ITEM, Type.ITEM); // 1 - Clicked Item
handler(new PacketHandler() { handler(itemRewriter.itemToServerHandler(Type.ITEM));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
toServer(item);
} }
}); });
} }
}
);
}
// TODO CLEANUP / SMARTER REWRITE SYSTEM // TODO CLEANUP / SMARTER REWRITE SYSTEM
// TODO Rewrite identifiers // TODO Rewrite identifiers

Datei anzeigen

@ -10,7 +10,7 @@ import us.myles.ViaVersion.protocols.protocol1_14_1to1_14.storage.EntityTracker1
import java.util.List; import java.util.List;
public class MetadataRewriter1_14_1To1_14 extends MetadataRewriter<Protocol1_14_1To1_14> { public class MetadataRewriter1_14_1To1_14 extends MetadataRewriter {
public MetadataRewriter1_14_1To1_14(Protocol1_14_1To1_14 protocol) { public MetadataRewriter1_14_1To1_14(Protocol1_14_1To1_14 protocol) {
super(protocol, EntityTracker1_14_1.class); super(protocol, EntityTracker1_14_1.class);

Datei anzeigen

@ -19,7 +19,7 @@ import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1
import java.util.List; import java.util.List;
public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter<Protocol1_14To1_13_2> { public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter {
public MetadataRewriter1_14To1_13_2(Protocol1_14To1_13_2 protocol) { public MetadataRewriter1_14To1_13_2(Protocol1_14To1_13_2 protocol) {
super(protocol, EntityTracker1_14.class); super(protocol, EntityTracker1_14.class);

Datei anzeigen

@ -9,6 +9,7 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
@ -21,14 +22,11 @@ import java.util.Set;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
public class InventoryPackets { public class InventoryPackets {
private static String NBT_TAG_NAME; private static final String NBT_TAG_NAME = "ViaVersion|" + Protocol1_14To1_13_2.class.getSimpleName();
private static final Set<String> REMOVED_RECIPE_TYPES = Sets.newHashSet("crafting_special_banneraddpattern", "crafting_special_repairitem"); private static final Set<String> REMOVED_RECIPE_TYPES = Sets.newHashSet("crafting_special_banneraddpattern", "crafting_special_repairitem");
public static void register(Protocol protocol) { public static void register(Protocol protocol) {
NBT_TAG_NAME = "ViaVersion|" + protocol.getClass().getSimpleName(); ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
/*
Outgoing packets
*/
// Open Inventory // Open Inventory
protocol.registerOutgoing(State.PLAY, 0x14, -1, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x14, -1, new PacketRemapper() {
@ -105,38 +103,10 @@ public class InventoryPackets {
}); });
// Window items packet // Window items packet
protocol.registerOutgoing(State.PLAY, 0x15, 0x14, new PacketRemapper() { itemRewriter.registerWindowItems(Type.FLAT_VAR_INT_ITEM_ARRAY, 0x15, 0x14);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.FLAT_VAR_INT_ITEM_ARRAY); // 1 - Window Values
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.FLAT_VAR_INT_ITEM_ARRAY, 0);
for (Item stack : stacks) toClient(stack);
}
});
}
});
// Set slot packet // Set slot packet
protocol.registerOutgoing(State.PLAY, 0x17, 0x16, new PacketRemapper() { itemRewriter.registerSetSlot(Type.FLAT_VAR_INT_ITEM, 0x17, 0x16);
@Override
public void registerMap() {
map(Type.BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot ID
map(Type.FLAT_VAR_INT_ITEM); // 2 - Slot Value
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
toClient(wrapper.get(Type.FLAT_VAR_INT_ITEM, 0));
}
});
}
});
// Plugin message // Plugin message
protocol.registerOutgoing(State.PLAY, 0x19, 0x18, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x19, 0x18, new PacketRemapper() {
@ -192,21 +162,7 @@ public class InventoryPackets {
}); });
// Entity Equipment Packet // Entity Equipment Packet
protocol.registerOutgoing(State.PLAY, 0x42, 0x46, new PacketRemapper() { itemRewriter.registerEntityEquipment(Type.FLAT_VAR_INT_ITEM, 0x42, 0x46);
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Type.VAR_INT); // 1 - Slot ID
map(Type.FLAT_VAR_INT_ITEM); // 2 - Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
toClient(wrapper.get(Type.FLAT_VAR_INT_ITEM, 0));
}
});
}
});
// Declare Recipes // Declare Recipes
protocol.registerOutgoing(State.PLAY, 0x54, 0x5A, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x54, 0x5A, new PacketRemapper() {
@ -259,29 +215,8 @@ public class InventoryPackets {
}); });
/*
Incoming packets
*/
// Click window packet // Click window packet
protocol.registerIncoming(State.PLAY, 0x08, 0x09, new PacketRemapper() { itemRewriter.registerClickWindow(Type.FLAT_VAR_INT_ITEM, 0x08, 0x09);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot
map(Type.BYTE); // 2 - Button
map(Type.SHORT); // 3 - Action number
map(Type.VAR_INT); // 4 - Mode
map(Type.FLAT_VAR_INT_ITEM); // 5 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
toServer(wrapper.get(Type.FLAT_VAR_INT_ITEM, 0));
}
});
}
});
// Select trade // Select trade
protocol.registerIncoming(State.PLAY, 0x1F, 0x21, new PacketRemapper() { protocol.registerIncoming(State.PLAY, 0x1F, 0x21, new PacketRemapper() {
@ -307,20 +242,7 @@ public class InventoryPackets {
}); });
// Creative Inventory Action // Creative Inventory Action
protocol.registerIncoming(State.PLAY, 0x24, 0x26, new PacketRemapper() { itemRewriter.registerCreativeInvAction(Type.FLAT_VAR_INT_ITEM, 0x24, 0x26);
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(Type.FLAT_VAR_INT_ITEM); // 1 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
toServer(wrapper.get(Type.FLAT_VAR_INT_ITEM, 0));
}
});
}
});
} }

Datei anzeigen

@ -14,7 +14,7 @@ import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.storage.EntityTracker1
import java.util.List; import java.util.List;
public class MetadataRewriter1_15To1_14_4 extends MetadataRewriter<Protocol1_15To1_14_4> { public class MetadataRewriter1_15To1_14_4 extends MetadataRewriter {
public MetadataRewriter1_15To1_14_4(Protocol1_15To1_14_4 protocol) { public MetadataRewriter1_15To1_14_4(Protocol1_15To1_14_4 protocol) {
super(protocol, EntityTracker1_15.class); super(protocol, EntityTracker1_15.class);

Datei anzeigen

@ -6,6 +6,7 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data.MappingData;
@ -13,22 +14,10 @@ import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data.MappingData;
public class InventoryPackets { public class InventoryPackets {
public static void register(Protocol protocol) { public static void register(Protocol protocol) {
// Window items packet ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
protocol.registerOutgoing(State.PLAY, 0x14, 0x15, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.FLAT_VAR_INT_ITEM_ARRAY); // 1 - Window Values
handler(new PacketHandler() { // Window items packet
@Override itemRewriter.registerWindowItems(Type.FLAT_VAR_INT_ITEM_ARRAY, 0x14, 0x15);
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.FLAT_VAR_INT_ITEM_ARRAY, 0);
for (Item stack : stacks) toClient(stack);
}
});
}
});
// Trade list packet // Trade list packet
protocol.registerOutgoing(State.PLAY, 0x27, 0x28, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x27, 0x28, new PacketRemapper() {
@ -70,38 +59,10 @@ public class InventoryPackets {
}); });
// Set slot packet // Set slot packet
protocol.registerOutgoing(State.PLAY, 0x16, 0x17, new PacketRemapper() { itemRewriter.registerSetSlot(Type.FLAT_VAR_INT_ITEM, 0x16, 0x17);
@Override
public void registerMap() {
map(Type.BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot ID
map(Type.FLAT_VAR_INT_ITEM); // 2 - Slot Value
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
toClient(wrapper.get(Type.FLAT_VAR_INT_ITEM, 0));
}
});
}
});
// Entity Equipment Packet // Entity Equipment Packet
protocol.registerOutgoing(State.PLAY, 0x46, 0x47, new PacketRemapper() { itemRewriter.registerEntityEquipment(Type.FLAT_VAR_INT_ITEM, 0x46, 0x47);
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Type.VAR_INT); // 1 - Slot ID
map(Type.FLAT_VAR_INT_ITEM); // 2 - Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
toClient(wrapper.get(Type.FLAT_VAR_INT_ITEM, 0));
}
});
}
});
// Declare Recipes // Declare Recipes
protocol.registerOutgoing(State.PLAY, 0x5A, 0x5B, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x5A, 0x5B, new PacketRemapper() {
@ -171,40 +132,10 @@ public class InventoryPackets {
}); });
// Click window packet // Click window packet
protocol.registerIncoming(State.PLAY, 0x09, 0x09, new PacketRemapper() { itemRewriter.registerClickWindow(Type.FLAT_VAR_INT_ITEM, 0x09, 0x09);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot
map(Type.BYTE); // 2 - Button
map(Type.SHORT); // 3 - Action number
map(Type.VAR_INT); // 4 - Mode
map(Type.FLAT_VAR_INT_ITEM); // 5 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
toServer(wrapper.get(Type.FLAT_VAR_INT_ITEM, 0));
}
});
}
});
// Creative Inventory Action // Creative Inventory Action
protocol.registerIncoming(State.PLAY, 0x26, 0x26, new PacketRemapper() { itemRewriter.registerCreativeInvAction(Type.FLAT_VAR_INT_ITEM, 0x26, 0x26);
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(Type.FLAT_VAR_INT_ITEM); // 1 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
toServer(wrapper.get(Type.FLAT_VAR_INT_ITEM, 0));
}
});
}
});
} }
public static void toClient(Item item) { public static void toClient(Item item) {

Datei anzeigen

@ -19,7 +19,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
public class MetadataRewriter1_9To1_8 extends MetadataRewriter<Protocol1_9To1_8> { public class MetadataRewriter1_9To1_8 extends MetadataRewriter {
public MetadataRewriter1_9To1_8(Protocol1_9To1_8 protocol) { public MetadataRewriter1_9To1_8(Protocol1_9To1_8 protocol) {
super(protocol, EntityTracker1_9.class); super(protocol, EntityTracker1_9.class);

Datei anzeigen

@ -14,6 +14,7 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.InventoryTracker; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.InventoryTracker;
public class InventoryPackets { public class InventoryPackets {
public static void register(Protocol protocol) { public static void register(Protocol protocol) {
// Window Property Packet // Window Property Packet
protocol.registerOutgoing(State.PLAY, 0x31, 0x15, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x31, 0x15, new PacketRemapper() {
@ -334,7 +335,5 @@ public class InventoryPackets {
protocol.registerIncoming(State.PLAY, 0x0F, 0x05); // Confirm Transaction Packet protocol.registerIncoming(State.PLAY, 0x0F, 0x05); // Confirm Transaction Packet
protocol.registerIncoming(State.PLAY, 0x11, 0x06); // Enchant Item Packet protocol.registerIncoming(State.PLAY, 0x11, 0x06); // Enchant Item Packet
} }
} }