Skip to content

Commit

Permalink
Fix reading/writing of item attribute modifier component in 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
booky10 committed Jul 10, 2024
1 parent 149483e commit 245b7a0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@

package com.github.retrooper.packetevents.protocol.component.builtin.item;

import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.attribute.Attribute;
import com.github.retrooper.packetevents.protocol.attribute.AttributeOperation;
import com.github.retrooper.packetevents.protocol.attribute.Attributes;
import com.github.retrooper.packetevents.resources.ResourceLocation;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerUpdateAttributes.PropertyModifier;
import org.jetbrains.annotations.ApiStatus;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -168,11 +172,16 @@ public String toString() {

public static class Modifier {

private UUID id;
private String name;
private UUID id; // removed in 1.21
private String name; // ResourceLocation since 1.21
private double value;
private AttributeOperation operation;

public Modifier(ResourceLocation name, double value, AttributeOperation operation) {
this(PropertyModifier.generateSemiUniqueId(name), name.toString(), value, operation);
}

@ApiStatus.Obsolete
public Modifier(UUID id, String name, double value, AttributeOperation operation) {
this.id = id;
this.name = name;
Expand All @@ -181,32 +190,56 @@ public Modifier(UUID id, String name, double value, AttributeOperation operation
}

public static Modifier read(PacketWrapper<?> wrapper) {
UUID id = wrapper.readUUID();
String name = wrapper.readString();
UUID id;
String name;
if (wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_21)) {
ResourceLocation nameKey = wrapper.readIdentifier();
name = nameKey.toString();
id = PropertyModifier.generateSemiUniqueId(nameKey);
} else {
id = wrapper.readUUID();
name = wrapper.readString();
}
double value = wrapper.readDouble();
AttributeOperation operation = wrapper.readEnum(AttributeOperation.values());
return new Modifier(id, name, value, operation);
}

public static void write(PacketWrapper<?> wrapper, Modifier modifier) {
wrapper.writeUUID(modifier.id);
wrapper.writeString(modifier.name);
if (wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_21)) {
wrapper.writeIdentifier(new ResourceLocation(modifier.name));
} else {
wrapper.writeUUID(modifier.id);
wrapper.writeString(modifier.name);
}
wrapper.writeDouble(modifier.value);
wrapper.writeEnum(modifier.operation);
}

public ResourceLocation getNameKey() {
return new ResourceLocation(this.name);
}

public void setNameKey(ResourceLocation nameKey) {
this.name = nameKey.toString();
}

@ApiStatus.Obsolete
public UUID getId() {
return this.id;
}

@ApiStatus.Obsolete
public void setId(UUID id) {
this.id = id;
}

@ApiStatus.Obsolete
public String getName() {
return this.name;
}

@ApiStatus.Obsolete
public void setName(String name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ public PropertyModifier(ResourceLocation name, UUID uuid, double amount, Operati
this.operation = operation;
}

private static UUID generateSemiUniqueId(ResourceLocation name) {
@ApiStatus.Internal
public static UUID generateSemiUniqueId(ResourceLocation name) {
String extendedName = "packetevents_" + name.toString();
return UUID.nameUUIDFromBytes(extendedName.getBytes(StandardCharsets.UTF_8));
}
Expand Down

0 comments on commit 245b7a0

Please sign in to comment.