Add trade list rewriter functions to ItemRewriter (#3926)

This commit is contained in:
EnZaXD 2024-06-11 15:02:04 +02:00 committed by GitHub
parent d0ed528788
commit ae3042074e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 42 additions and 112 deletions

View File

@ -21,7 +21,6 @@ import com.viaversion.nbt.tag.ByteTag;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.protocols.v1_10to1_11.Protocol1_10To1_11;
import com.viaversion.viaversion.protocols.v1_10to1_11.data.EntityMappings1_11;
@ -40,36 +39,7 @@ public class ItemPacketRewriter1_11 extends ItemRewriter<ClientboundPackets1_9_3
registerSetSlot(ClientboundPackets1_9_3.CONTAINER_SET_SLOT);
registerSetContent(ClientboundPackets1_9_3.CONTAINER_SET_CONTENT);
registerSetEquippedItem(ClientboundPackets1_9_3.SET_EQUIPPED_ITEM);
// Plugin message Packet -> Trading
protocol.registerClientbound(ClientboundPackets1_9_3.CUSTOM_PAYLOAD, new PacketHandlers() {
@Override
public void register() {
map(Types.STRING); // 0 - Channel
handlerSoftFail(wrapper -> {
if (wrapper.get(Types.STRING, 0).equals("MC|TrList")) {
wrapper.passthrough(Types.INT); // Passthrough Window ID
int size = wrapper.passthrough(Types.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Input Item
handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Output Item
boolean secondItem = wrapper.passthrough(Types.BOOLEAN); // Has second item
if (secondItem) {
handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Second Item
}
wrapper.passthrough(Types.BOOLEAN); // Trade disabled
wrapper.passthrough(Types.INT); // Number of tools uses
wrapper.passthrough(Types.INT); // Maximum number of trade uses
}
}
});
}
});
registerCustomPayloadTradeList(ClientboundPackets1_9_3.CUSTOM_PAYLOAD);
registerContainerClick(ServerboundPackets1_9_3.CONTAINER_CLICK);
registerSetCreativeModeSlot(ServerboundPackets1_9_3.SET_CREATIVE_MODE_SLOT);
}

View File

@ -40,36 +40,7 @@ public class ItemPacketRewriter1_12 extends ItemRewriter<ClientboundPackets1_9_3
registerSetSlot(ClientboundPackets1_9_3.CONTAINER_SET_SLOT);
registerSetContent(ClientboundPackets1_9_3.CONTAINER_SET_CONTENT);
registerSetEquippedItem(ClientboundPackets1_9_3.SET_EQUIPPED_ITEM);
// Plugin message -> Trading
protocol.registerClientbound(ClientboundPackets1_9_3.CUSTOM_PAYLOAD, new PacketHandlers() {
@Override
public void register() {
map(Types.STRING); // 0 - Channel
handlerSoftFail(wrapper -> {
if (wrapper.get(Types.STRING, 0).equals("MC|TrList")) {
wrapper.passthrough(Types.INT); // Passthrough Window ID
int size = wrapper.passthrough(Types.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Input Item
handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Output Item
boolean secondItem = wrapper.passthrough(Types.BOOLEAN); // Has second item
if (secondItem) {
handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Second Item
}
wrapper.passthrough(Types.BOOLEAN); // Trade disabled
wrapper.passthrough(Types.INT); // Number of tools uses
wrapper.passthrough(Types.INT); // Maximum number of trade uses
}
}
});
}
});
registerCustomPayloadTradeList(ClientboundPackets1_9_3.CUSTOM_PAYLOAD);
protocol.registerServerbound(ServerboundPackets1_12.CONTAINER_CLICK, new PacketHandlers() {
@Override

View File

@ -51,7 +51,7 @@ import java.util.Optional;
public class ItemPacketRewriter1_13 extends ItemRewriter<ClientboundPackets1_12_1, ServerboundPackets1_13, Protocol1_12_2To1_13> {
public ItemPacketRewriter1_13(Protocol1_12_2To1_13 protocol) {
super(protocol, null, null);
super(protocol, Types.ITEM1_8, null, Types.ITEM1_13, null);
}
@Override
@ -136,31 +136,7 @@ public class ItemPacketRewriter1_13 extends ItemRewriter<ClientboundPackets1_12_
return;
} else if (channel.equals("MC|TrList")) {
channel = "minecraft:trader_list";
wrapper.passthrough(Types.INT); // Passthrough Window ID
int size = wrapper.passthrough(Types.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
// Input Item
Item input = wrapper.read(Types.ITEM1_8);
handleItemToClient(wrapper.user(), input);
wrapper.write(Types.ITEM1_13, input);
// Output Item
Item output = wrapper.read(Types.ITEM1_8);
handleItemToClient(wrapper.user(), output);
wrapper.write(Types.ITEM1_13, output);
boolean secondItem = wrapper.passthrough(Types.BOOLEAN); // Has second item
if (secondItem) {
// Second Item
Item second = wrapper.read(Types.ITEM1_8);
handleItemToClient(wrapper.user(), second);
wrapper.write(Types.ITEM1_13, second);
}
wrapper.passthrough(Types.BOOLEAN); // Trade disabled
wrapper.passthrough(Types.INT); // Number of tools uses
wrapper.passthrough(Types.INT); // Maximum number of trade uses
}
handleTradeList(wrapper);
} else {
String old = channel;
channel = getNewPluginChannelId(channel);

View File

@ -43,7 +43,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public class ItemPacketRewriter1_9 extends ItemRewriter<ClientboundPackets1_8, ServerboundPackets1_9, Protocol1_8To1_9> {
public ItemPacketRewriter1_9(final Protocol1_8To1_9 protocol) {
super(protocol, null, null);
super(protocol, Types.ITEM1_8, null);
}
@Override

View File

@ -276,33 +276,11 @@ public class PlayerPacketRewriter1_9 {
public void register() {
map(Types.STRING); // 0 - Channel Name
handlerSoftFail(wrapper -> {
String name = wrapper.get(Types.STRING, 0);
final String name = wrapper.get(Types.STRING, 0);
if (name.equals("MC|BOpen")) {
wrapper.write(Types.VAR_INT, 0);
} else if (name.equals("MC|TrList")) {
wrapper.passthrough(Types.INT); // ID
Short size = wrapper.passthrough(Types.UNSIGNED_BYTE);
for (int i = 0; i < size; ++i) {
Item item1 = wrapper.passthrough(Types.ITEM1_8);
protocol.getItemRewriter().handleItemToClient(wrapper.user(), item1);
Item item2 = wrapper.passthrough(Types.ITEM1_8);
protocol.getItemRewriter().handleItemToClient(wrapper.user(), item2);
boolean present = wrapper.passthrough(Types.BOOLEAN);
if (present) {
Item item3 = wrapper.passthrough(Types.ITEM1_8);
protocol.getItemRewriter().handleItemToClient(wrapper.user(), item3);
}
wrapper.passthrough(Types.BOOLEAN);
wrapper.passthrough(Types.INT);
wrapper.passthrough(Types.INT);
}
protocol.getItemRewriter().handleTradeList(wrapper);
}
});
}

View File

@ -234,6 +234,41 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
});
}
public void registerCustomPayloadTradeList(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
protected void register() {
map(Types.STRING); // 0 - Channel
handlerSoftFail(wrapper -> {
final String channel = wrapper.get(Types.STRING, 0);
if (channel.equals("MC|TrList")) {
handleTradeList(wrapper);
}
});
}
});
}
public void handleTradeList(final PacketWrapper wrapper) {
wrapper.passthrough(Types.INT); // Window ID
final int size = wrapper.passthrough(Types.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
handleClientboundItem(wrapper); // Input Item
handleClientboundItem(wrapper); // Output Item
if (wrapper.passthrough(Types.BOOLEAN)) {
handleClientboundItem(wrapper); // Second Item
}
wrapper.passthrough(Types.BOOLEAN); // Trade disabled
wrapper.passthrough(Types.INT); // Number of tools uses
wrapper.passthrough(Types.INT); // Maximum number of trade uses
}
}
// 1.14.4+
public void registerMerchantOffers(C packetType) {
protocol.registerClientbound(packetType, wrapper -> {