Skip to content

Commit

Permalink
Add UserConnection param to item rewriter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytv committed Apr 21, 2024
1 parent c64e9ac commit 2a4bfb9
Show file tree
Hide file tree
Showing 40 changed files with 175 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.BackwardsMappings;
import com.viaversion.viabackwards.api.data.MappedItem;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
Expand All @@ -45,7 +46,7 @@ public BackwardsStructuredItemRewriter(final T protocol, final Type<Item> itemTy
}

@Override
public @Nullable Item handleItemToClient(@Nullable final Item item) {
public @Nullable Item handleItemToClient(final UserConnection connection, @Nullable final Item item) {
if (item == null) {
return null;
}
Expand Down Expand Up @@ -80,7 +81,7 @@ public BackwardsStructuredItemRewriter(final T protocol, final Type<Item> itemTy
}

@Override
public @Nullable Item handleItemToServer(@Nullable final Item item) {
public @Nullable Item handleItemToServer(final UserConnection connection, @Nullable final Item item) {
if (item == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void registerMetaTypeHandler(
filter().handler((event, meta) -> {
MetaType type = meta.metaType();
if (type == itemType) {
protocol.getItemRewriter().handleItemToClient(meta.value());
protocol.getItemRewriter().handleItemToClient(event.user(), meta.value());
} else if (type == blockStateType) {
int data = meta.value();
meta.setValue(protocol.getMappingData().getNewBlockStateId(data));
Expand All @@ -201,7 +201,7 @@ public void registerMetaTypeHandler(
meta.setValue(protocol.getMappingData().getNewBlockStateId(data));
}
} else if (type == particleType) {
rewriteParticle(meta.value());
rewriteParticle(event.user(), meta.value());
} else if (type == optionalComponentType || type == componentType) {
JsonElement text = meta.value();
protocol.getTranslatableRewriter().processText(text);
Expand All @@ -220,7 +220,7 @@ public void registerMetaTypeHandler1_20_3(
filter().handler((event, meta) -> {
MetaType type = meta.metaType();
if (type == itemType) {
protocol.getItemRewriter().handleItemToClient(meta.value());
protocol.getItemRewriter().handleItemToClient(event.user(), meta.value());
} else if (type == blockStateType) {
int data = meta.value();
meta.setValue(protocol.getMappingData().getNewBlockStateId(data));
Expand All @@ -230,7 +230,7 @@ public void registerMetaTypeHandler1_20_3(
meta.setValue(protocol.getMappingData().getNewBlockStateId(data));
}
} else if (type == particleType) {
rewriteParticle(meta.value());
rewriteParticle(event.user(), meta.value());
} else if (type == optionalComponentType || type == componentType) {
protocol.getTranslatableRewriter().processTag(meta.value());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.MappedItem;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
Expand Down Expand Up @@ -46,7 +47,7 @@ public ItemRewriter(T protocol, Type<Item> itemType, Type<Item[]> itemArrayType,
}

@Override
public @Nullable Item handleItemToClient(@Nullable Item item) {
public @Nullable Item handleItemToClient(UserConnection connection, @Nullable Item item) {
if (item == null) {
return null;
}
Expand Down Expand Up @@ -83,7 +84,7 @@ public ItemRewriter(T protocol, Type<Item> itemType, Type<Item[]> itemArrayType,
MappedItem data = protocol.getMappingData() != null ? protocol.getMappingData().getMappedItem(item.identifier()) : null;
if (data == null) {
// Just rewrite the id
return super.handleItemToClient(item);
return super.handleItemToClient(connection, item);
}

if (item.tag() == null) {
Expand Down Expand Up @@ -111,10 +112,10 @@ public ItemRewriter(T protocol, Type<Item> itemType, Type<Item[]> itemArrayType,
}

@Override
public @Nullable Item handleItemToServer(@Nullable Item item) {
public @Nullable Item handleItemToServer(UserConnection connection, @Nullable Item item) {
if (item == null) return null;

super.handleItemToServer(item);
super.handleItemToServer(connection, item);
if (item.tag() != null) {
Tag originalId = item.tag().remove(nbtTagName("id"));
if (originalId instanceof IntTag) {
Expand Down Expand Up @@ -150,7 +151,7 @@ public void register() {
translatableRewriter.processText(description);
}

final Item icon = handleItemToClient(wrapper.read(itemType()));
final Item icon = handleItemToClient(wrapper.user(), wrapper.read(itemType()));
wrapper.write(mappedItemType(), icon);

wrapper.passthrough(Type.VAR_INT); // Frame type
Expand Down Expand Up @@ -198,7 +199,7 @@ public void registerAdvancements1_20_3(final C packetType) {
translatableRewriter.processTag(description);
}

final Item icon = handleItemToClient(wrapper.read(itemType()));
final Item icon = handleItemToClient(wrapper.user(), wrapper.read(itemType()));
wrapper.write(mappedItemType(), icon);

wrapper.passthrough(Type.VAR_INT); // Frame type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.viaversion.viabackwards.api.rewriters;

import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
Expand All @@ -44,9 +45,9 @@ protected ItemRewriterBase(T protocol, Type<Item> itemType, Type<Item[]> itemArr
}

@Override
public @Nullable Item handleItemToServer(@Nullable Item item) {
public @Nullable Item handleItemToServer(UserConnection connection, @Nullable Item item) {
if (item == null) return null;
super.handleItemToServer(item);
super.handleItemToServer(connection, item);

restoreDisplayTag(item);
return item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.viaversion.viabackwards.api.data.MappedLegacyBlockItem;
import com.viaversion.viabackwards.api.data.BackwardsMappingDataLoader;
import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.BlockColors;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection;
Expand Down Expand Up @@ -141,13 +142,13 @@ public void register() {
}

@Override
public @Nullable Item handleItemToClient(@Nullable Item item) {
public @Nullable Item handleItemToClient(UserConnection connection, @Nullable Item item) {
if (item == null) return null;

MappedLegacyBlockItem data = getMappedBlockItem(item.identifier(), item.data());
if (data == null) {
// Just rewrite the id
return super.handleItemToClient(item);
return super.handleItemToClient(connection, item);
}
if (item.tag() == null) {
item.setTag(new CompoundTag());
Expand Down Expand Up @@ -186,9 +187,9 @@ public void register() {
}

@Override
public @Nullable Item handleItemToServer(@Nullable final Item item) {
public @Nullable Item handleItemToServer(UserConnection connection, @Nullable final Item item) {
if (item == null) return null;
super.handleItemToServer(item);
super.handleItemToServer(connection, item);
if (item.tag() != null) {
Tag originalId = item.tag().remove(nbtTagName("id"));
if (originalId instanceof IntTag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void register() {
map(Type.SHORT); // 1 - Slot ID
map(Type.ITEM1_8); // 2 - Slot Value

handler(wrapper -> handleItemToClient(wrapper.get(Type.ITEM1_8, 0)));
handler(wrapper -> handleItemToClient(wrapper.user(), wrapper.get(Type.ITEM1_8, 0)));

// Handle Llama
handler(wrapper -> {
Expand All @@ -90,7 +90,7 @@ public void register() {
handler(wrapper -> {
Item[] stacks = wrapper.get(Type.ITEM1_8_SHORT_ARRAY, 0);
for (int i = 0; i < stacks.length; i++)
stacks[i] = handleItemToClient(stacks[i]);
stacks[i] = handleItemToClient(wrapper.user(), stacks[i]);

if (isLlama(wrapper.user())) {
Optional<ChestedHorseStorage> horse = getChestedHorse(wrapper.user());
Expand Down Expand Up @@ -123,12 +123,12 @@ public void register() {

int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.read(Type.ITEM1_8))); // Input Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.read(Type.ITEM1_8))); // Output Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.user(), wrapper.read(Type.ITEM1_8))); // Input Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.user(), wrapper.read(Type.ITEM1_8))); // Output Item

boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item
if (secondItem) {
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.read(Type.ITEM1_8))); // Second Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.user(), wrapper.read(Type.ITEM1_8))); // Second Item
}

wrapper.passthrough(Type.BOOLEAN); // Trade disabled
Expand All @@ -150,7 +150,7 @@ public void register() {
map(Type.VAR_INT); // 4 - Mode
map(Type.ITEM1_8); // 5 - Clicked Item

handler(wrapper -> handleItemToServer(wrapper.get(Type.ITEM1_8, 0)));
handler(wrapper -> handleItemToServer(wrapper.user(), wrapper.get(Type.ITEM1_8, 0)));

// Llama slot
handler(wrapper -> {
Expand Down Expand Up @@ -267,7 +267,7 @@ public void register() {

protocol.getEntityRewriter().filter().handler((event, meta) -> {
if (meta.metaType().type().equals(Type.ITEM1_8)) // Is Item
meta.setValue(handleItemToClient((Item) meta.getValue()));
meta.setValue(handleItemToClient(event.user(), (Item) meta.getValue()));
});
}

Expand All @@ -288,9 +288,9 @@ protected void registerRewrites() {
}

@Override
public Item handleItemToClient(Item item) {
public Item handleItemToClient(UserConnection connection, Item item) {
if (item == null) return null;
super.handleItemToClient(item);
super.handleItemToClient(connection, item);

CompoundTag tag = item.tag();
if (tag == null) return item;
Expand All @@ -303,9 +303,9 @@ public Item handleItemToClient(Item item) {
}

@Override
public Item handleItemToServer(Item item) {
public Item handleItemToServer(UserConnection connection, Item item) {
if (item == null) return null;
super.handleItemToServer(item);
super.handleItemToServer(connection, item);

CompoundTag tag = item.tag();
if (tag == null) return item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected void registerPackets() {
int action = wrapper.passthrough(Type.VAR_INT);
if (action >= 0 && action <= 2) {
JsonElement component = wrapper.read(Type.COMPONENT);
wrapper.write(Type.COMPONENT, Protocol1_9To1_8.fixJson(component.toString()));
wrapper.write(Type.COMPONENT, Protocol1_9To1_8.STRING_TO_JSON.transform(wrapper, component.toString()));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public ShoulderTracker(UserConnection user) {
public void update() {
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_12.CHAT_MESSAGE, null, getUser());

wrapper.write(Type.COMPONENT, Protocol1_9To1_8.fixJson(generateString()));
try {
wrapper.write(Type.COMPONENT, Protocol1_9To1_8.STRING_TO_JSON.transform(wrapper, generateString()));
} catch (final Exception e) {
throw new RuntimeException(e);
}
wrapper.write(Type.BYTE, (byte) 2);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.viaversion.viabackwards.api.rewriters.LegacyBlockItemRewriter;
import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12;
import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.MapColorMapping;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
Expand Down Expand Up @@ -99,12 +100,12 @@ public void register() {

int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.read(Type.ITEM1_8))); // Input Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.read(Type.ITEM1_8))); // Output Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.user(), wrapper.read(Type.ITEM1_8))); // Input Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.user(), wrapper.read(Type.ITEM1_8))); // Output Item

boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item
if (secondItem)
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.read(Type.ITEM1_8))); // Second Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.user(), wrapper.read(Type.ITEM1_8))); // Second Item

wrapper.passthrough(Type.BOOLEAN); // Trade disabled
wrapper.passthrough(Type.INT); // Number of tools uses
Expand Down Expand Up @@ -146,7 +147,7 @@ public void register() {

}
Item item = wrapper.get(Type.ITEM1_8, 0);
handleItemToServer(item);
handleItemToServer(wrapper.user(), item);
});
}
});
Expand Down Expand Up @@ -179,7 +180,7 @@ public void register() {

protocol.getEntityRewriter().filter().handler((event, meta) -> {
if (meta.metaType().type().equals(Type.ITEM1_8)) // Is Item
meta.setValue(handleItemToClient((Item) meta.getValue()));
meta.setValue(handleItemToClient(event.user(), (Item) meta.getValue()));
});

protocol.registerServerbound(ServerboundPackets1_9_3.CLIENT_STATUS, new PacketHandlers() {
Expand All @@ -198,9 +199,9 @@ public void register() {
}

@Override
public @Nullable Item handleItemToClient(Item item) {
public @Nullable Item handleItemToClient(UserConnection connection, Item item) {
if (item == null) return null;
super.handleItemToClient(item);
super.handleItemToClient(connection, item);

if (item.tag() != null) {
CompoundTag backupTag = new CompoundTag();
Expand Down Expand Up @@ -233,9 +234,9 @@ private boolean handleNbtToClient(CompoundTag compoundTag, CompoundTag backupTag
}

@Override
public @Nullable Item handleItemToServer(Item item) {
public @Nullable Item handleItemToServer(UserConnection connection, Item item) {
if (item == null) return null;
super.handleItemToServer(item);
super.handleItemToServer(connection, item);

if (item.tag() != null) {
Tag tag = item.tag().remove("Via|LongArrayTags");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.viaversion.viabackwards.api.rewriters.LegacyBlockItemRewriter;
import com.viaversion.viabackwards.api.rewriters.LegacyEnchantmentRewriter;
import com.viaversion.viabackwards.protocol.protocol1_11to1_11_1.Protocol1_11To1_11_1;
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.Type;
Expand Down Expand Up @@ -53,12 +54,12 @@ public void register() {

int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.read(Type.ITEM1_8))); // Input Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.read(Type.ITEM1_8))); // Output Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.user(), wrapper.read(Type.ITEM1_8))); // Input Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.user(), wrapper.read(Type.ITEM1_8))); // Output Item

boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item
if (secondItem) {
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.read(Type.ITEM1_8))); // Second Item
wrapper.write(Type.ITEM1_8, handleItemToClient(wrapper.user(), wrapper.read(Type.ITEM1_8))); // Second Item
}

wrapper.passthrough(Type.BOOLEAN); // Trade disabled
Expand All @@ -76,7 +77,7 @@ public void register() {
// Handle item metadata
protocol.getEntityRewriter().filter().handler((event, meta) -> {
if (meta.metaType().type().equals(Type.ITEM1_8)) { // Is Item
meta.setValue(handleItemToClient((Item) meta.getValue()));
meta.setValue(handleItemToClient(event.user(), (Item) meta.getValue()));
}
});
}
Expand All @@ -88,18 +89,18 @@ protected void registerRewrites() {
}

@Override
public Item handleItemToClient(Item item) {
public Item handleItemToClient(UserConnection connection, Item item) {
if (item == null) return null;
super.handleItemToClient(item);
super.handleItemToClient(connection, item);

enchantmentRewriter.handleToClient(item);
return item;
}

@Override
public Item handleItemToServer(Item item) {
public Item handleItemToServer(UserConnection connection, Item item) {
if (item == null) return null;
super.handleItemToServer(item);
super.handleItemToServer(connection, item);

enchantmentRewriter.handleToServer(item);
return item;
Expand Down

0 comments on commit 2a4bfb9

Please sign in to comment.