3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-27 00:22:51 +01:00

Add trade list rewriter functions to ItemRewriter (#3926)

Dieser Commit ist enthalten in:
EnZaXD 2024-06-11 15:02:04 +02:00 committet von GitHub
Ursprung d0ed528788
Commit ae3042074e
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
6 geänderte Dateien mit 42 neuen und 112 gelöschten Zeilen

Datei anzeigen

@ -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);
}

Datei anzeigen

@ -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

Datei anzeigen

@ -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);

Datei anzeigen

@ -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

Datei anzeigen

@ -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);
}
});
}

Datei anzeigen

@ -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 -> {