Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-12-28 00:50:13 +01:00
Add basic handling for items within item components
No changes so far, but will most likely be needed rather sooner than later
Dieser Commit ist enthalten in:
Ursprung
182148dbe8
Commit
e3312b1c80
@ -45,4 +45,16 @@ public record ArmorTrim(Holder<ArmorTrimMaterial> material, Holder<ArmorTrimPatt
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public ArmorTrim rewrite(final Int2IntFunction idRewriteFunction) {
|
||||||
|
Holder<ArmorTrimMaterial> material = this.material;
|
||||||
|
if (material.isDirect()) {
|
||||||
|
material = Holder.of(material.value().rewrite(idRewriteFunction));
|
||||||
|
}
|
||||||
|
|
||||||
|
Holder<ArmorTrimPattern> pattern = this.pattern;
|
||||||
|
if (pattern.isDirect()) {
|
||||||
|
pattern = Holder.of(pattern.value().rewrite(idRewriteFunction));
|
||||||
|
}
|
||||||
|
return new ArmorTrim(material, pattern, showInTooltip);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,4 +67,7 @@ public record ArmorTrimMaterial(String assetName, int itemId, float itemModelInd
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public ArmorTrimMaterial rewrite(final Int2IntFunction idRewriteFunction) {
|
||||||
|
return new ArmorTrimMaterial(assetName, idRewriteFunction.applyAsInt(itemId), itemModelIndex, overrideArmorMaterials, description);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,4 +48,7 @@ public record ArmorTrimPattern(String assetName, int itemId, Tag description, bo
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public ArmorTrimPattern rewrite(final Int2IntFunction idRewriteFunction) {
|
||||||
|
return new ArmorTrimPattern(assetName, idRewriteFunction.applyAsInt(itemId), description, decal);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,9 +22,11 @@
|
|||||||
*/
|
*/
|
||||||
package com.viaversion.viaversion.api.minecraft.item.data;
|
package com.viaversion.viaversion.api.minecraft.item.data;
|
||||||
|
|
||||||
|
import com.viaversion.viaversion.api.data.MappingData;
|
||||||
import com.viaversion.viaversion.api.type.Type;
|
import com.viaversion.viaversion.api.type.Type;
|
||||||
import com.viaversion.viaversion.api.type.Types;
|
import com.viaversion.viaversion.api.type.Types;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
|
||||||
|
|
||||||
public final class PotDecorations {
|
public final class PotDecorations {
|
||||||
|
|
||||||
@ -73,4 +75,12 @@ public final class PotDecorations {
|
|||||||
private int item(final int index) {
|
private int item(final int index) {
|
||||||
return index < 0 || index >= itemIds.length ? -1 : itemIds[index];
|
return index < 0 || index >= itemIds.length ? -1 : itemIds[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PotDecorations rewrite(final Int2IntFunction idRewriteFunction) {
|
||||||
|
final int[] newItems = new int[itemIds.length];
|
||||||
|
for (int i = 0; i < itemIds.length; i++) {
|
||||||
|
newItems[i] = idRewriteFunction.applyAsInt(itemIds[i]);
|
||||||
|
}
|
||||||
|
return new PotDecorations(newItems);
|
||||||
|
}
|
||||||
}
|
}
|
@ -50,7 +50,7 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter<ClientboundPa
|
|||||||
protocol.registerClientbound(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA, wrapper -> {
|
protocol.registerClientbound(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA, wrapper -> {
|
||||||
final String type = Key.stripMinecraftNamespace(wrapper.passthrough(Type.STRING));
|
final String type = Key.stripMinecraftNamespace(wrapper.passthrough(Type.STRING));
|
||||||
final RegistryEntry[] entries = wrapper.passthrough(Type.REGISTRY_ENTRY_ARRAY);
|
final RegistryEntry[] entries = wrapper.passthrough(Type.REGISTRY_ENTRY_ARRAY);
|
||||||
if (Key.stripMinecraftNamespace(type).equals("damage_type")) {
|
if (type.equals("damage_type")) {
|
||||||
// Add required damage type
|
// Add required damage type
|
||||||
final CompoundTag campfireDamageType = new CompoundTag();
|
final CompoundTag campfireDamageType = new CompoundTag();
|
||||||
campfireDamageType.putString("scaling", "when_caused_by_living_non_player");
|
campfireDamageType.putString("scaling", "when_caused_by_living_non_player");
|
||||||
|
@ -19,11 +19,18 @@ package com.viaversion.viaversion.rewriter;
|
|||||||
|
|
||||||
import com.viaversion.viaversion.api.connection.UserConnection;
|
import com.viaversion.viaversion.api.connection.UserConnection;
|
||||||
import com.viaversion.viaversion.api.data.MappingData;
|
import com.viaversion.viaversion.api.data.MappingData;
|
||||||
|
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
|
||||||
|
import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
|
||||||
|
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
|
||||||
import com.viaversion.viaversion.api.minecraft.item.Item;
|
import com.viaversion.viaversion.api.minecraft.item.Item;
|
||||||
|
import com.viaversion.viaversion.api.minecraft.item.data.ArmorTrim;
|
||||||
|
import com.viaversion.viaversion.api.minecraft.item.data.PotDecorations;
|
||||||
import com.viaversion.viaversion.api.protocol.Protocol;
|
import com.viaversion.viaversion.api.protocol.Protocol;
|
||||||
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
|
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
|
||||||
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
|
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
|
||||||
import com.viaversion.viaversion.api.type.Type;
|
import com.viaversion.viaversion.api.type.Type;
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class StructuredItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType,
|
public class StructuredItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType,
|
||||||
T extends Protocol<C, ?, ?, S>> extends ItemRewriter<C, S, T> {
|
T extends Protocol<C, ?, ?, S>> extends ItemRewriter<C, S, T> {
|
||||||
@ -51,6 +58,8 @@ public class StructuredItemRewriter<C extends ClientboundPacketType, S extends S
|
|||||||
item.dataContainer().setIdLookup(protocol, true);
|
item.dataContainer().setIdLookup(protocol, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateItemComponents(connection, item.dataContainer(), this::handleItemToClient, mappingData != null ? mappingData::getNewItemId : null);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,6 +78,51 @@ public class StructuredItemRewriter<C extends ClientboundPacketType, S extends S
|
|||||||
item.dataContainer().setIdLookup(protocol, false);
|
item.dataContainer().setIdLookup(protocol, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateItemComponents(connection, item.dataContainer(), this::handleItemToServer, mappingData != null ? mappingData::getOldItemId : null);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void updateItemComponents(UserConnection connection, StructuredDataContainer container, ItemHandler itemHandler, @Nullable Int2IntFunction idRewriter) {
|
||||||
|
// Specific types that need deep handling
|
||||||
|
final StructuredData<ArmorTrim> trimData = container.getNonEmpty(StructuredDataKey.TRIM);
|
||||||
|
if (trimData != null && idRewriter != null) {
|
||||||
|
trimData.setValue(trimData.value().rewrite(idRewriter));
|
||||||
|
}
|
||||||
|
|
||||||
|
final StructuredData<PotDecorations> potDecorationsData = container.getNonEmpty(StructuredDataKey.POT_DECORATIONS);
|
||||||
|
if (potDecorationsData != null && idRewriter != null) {
|
||||||
|
potDecorationsData.setValue(potDecorationsData.value().rewrite(idRewriter));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for item types
|
||||||
|
for (final Map.Entry<StructuredDataKey<?>, StructuredData<?>> entry : container.data().entrySet()) {
|
||||||
|
final StructuredData<?> data = entry.getValue();
|
||||||
|
if (data.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
final StructuredDataKey<?> key = entry.getKey();
|
||||||
|
final Class<?> outputClass = key.type().getOutputClass();
|
||||||
|
if (outputClass == Item.class) {
|
||||||
|
//noinspection unchecked
|
||||||
|
final StructuredData<Item> itemData = (StructuredData<Item>) data;
|
||||||
|
itemData.setValue(itemHandler.rewrite(connection, itemData.value()));
|
||||||
|
} else if (outputClass == Item[].class) {
|
||||||
|
//noinspection unchecked
|
||||||
|
final StructuredData<Item[]> itemArrayData = (StructuredData<Item[]>) data;
|
||||||
|
final Item[] items = itemArrayData.value();
|
||||||
|
for (int i = 0; i < items.length; i++) {
|
||||||
|
items[i] = itemHandler.rewrite(connection, items[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface ItemHandler {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
Item rewrite(UserConnection connection, @Nullable Item item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren