Skip to content

Commit

Permalink
Merge pull request #1006 from booky10/fix/viaversion-map-deco-errors
Browse files Browse the repository at this point in the history
Fix errors with viaversion and map decorations
  • Loading branch information
retrooper authored Sep 24, 2024
2 parents 0d86b3b + d902406 commit ed28da3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ public String toString() {
int maxAmount = getType().getMaxAmount();
return "ItemStack[type=" + identifier + ", amount=" + amount + "/" + maxAmount
+ ", nbt tag names: " + (nbt != null ? nbt.getTagNames() : "[null]")
+ ", legacyData=" + legacyData + ", components=" + components + "]";
+ ", legacyData=" + legacyData + ", components=" + (components != null ? components.getPatches() : null) + "]";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,17 @@

import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.resources.ResourceLocation;
import com.github.retrooper.packetevents.util.mappings.MappingHelper;
import com.github.retrooper.packetevents.util.mappings.TypesBuilder;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import com.github.retrooper.packetevents.util.mappings.VersionedRegistry;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static com.github.retrooper.packetevents.resources.ResourceLocation.minecraft;

public class MapDecorationTypes {

private static final Map<String, MapDecorationType> DECORATION_TYPE_MAP = new HashMap<>();
private static final Map<Byte, Map<Integer, MapDecorationType>> DECORATION_TYPE_ID_MAP = new HashMap<>();

private static final TypesBuilder TYPES_BUILDER = new TypesBuilder("item/item_map_decoration_type_mappings");
private static final VersionedRegistry<MapDecorationType> REGISTRY =
new VersionedRegistry<>("map_decoration_type", "item/item_map_decoration_type_mappings");

public static MapDecorationType define(String key, boolean showOnItemFrame, boolean trackCount) {
return define(key, minecraft(key), showOnItemFrame, trackCount);
Expand Down Expand Up @@ -65,8 +58,7 @@ public static MapDecorationType define(
boolean showOnItemFrame, int mapColor,
boolean explorationMapElement, boolean trackCount
) {
TypesBuilderData data = TYPES_BUILDER.define(key);
MapDecorationType potionType = new MapDecorationType() {
return REGISTRY.define(key, data -> new MapDecorationType() {
@Override
public ResourceLocation getAssetId() {
return assetId;
Expand Down Expand Up @@ -99,7 +91,7 @@ public ResourceLocation getName() {

@Override
public int getId(ClientVersion version) {
return MappingHelper.getId(version, TYPES_BUILDER, data);
return data.getId(version);
}

@Override
Expand All @@ -109,23 +101,19 @@ public boolean equals(Object obj) {
}
return false;
}
};
MappingHelper.registerMapping(TYPES_BUILDER, DECORATION_TYPE_MAP, DECORATION_TYPE_ID_MAP, potionType);
return potionType;
});
}

public static @Nullable MapDecorationType getByName(String name) {
return DECORATION_TYPE_MAP.get(name);
return REGISTRY.getByName(name);
}

public static @Nullable MapDecorationType getById(int id, ClientVersion version) {
return getById(version, id);
return REGISTRY.getById(version, id);
}

public static @Nullable MapDecorationType getById(ClientVersion version, int id) {
int index = TYPES_BUILDER.getDataIndex(version);
Map<Integer, MapDecorationType> idMap = DECORATION_TYPE_ID_MAP.get((byte) index);
return idMap.get(id);
return REGISTRY.getById(version, id);
}

// color constants used by vanilla
Expand Down Expand Up @@ -209,13 +197,14 @@ public boolean equals(Object obj) {

/**
* Returns an immutable view of the map decoration types.
*
* @return Map Decoration Types
*/
public static Collection<MapDecorationType> values() {
return Collections.unmodifiableCollection(DECORATION_TYPE_MAP.values());
return REGISTRY.getEntries();
}

static {
TYPES_BUILDER.unloadFileMappings();
REGISTRY.unloadMappings();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.util.Objects;

public class ResourceLocation {

public static final String VANILLA_NAMESPACE = "minecraft";

protected final String namespace;
protected final String key;

Expand All @@ -30,7 +33,7 @@ public ResourceLocation(String namespace, String key) {
}

public ResourceLocation(String location) {
String[] array = new String[]{"minecraft", location};
String[] array = new String[]{VANILLA_NAMESPACE, location};
int index = location.indexOf(":");
if (index != -1) {
array[1] = location.substring(index + 1);
Expand All @@ -42,6 +45,19 @@ public ResourceLocation(String location) {
this.key = array[1];
}

public static String normString(String location) {
int index = location.indexOf(':');
if (index > 0) {
return location; // namespace already set
} else if (index == -1) {
// prepend namespace and delimiter
return VANILLA_NAMESPACE + ":" + location;
} else { // index == 0
// treat prepending delimiter as no namespace
return VANILLA_NAMESPACE + location;
}
}

public String getNamespace() {
return namespace;
}
Expand Down Expand Up @@ -70,6 +86,6 @@ public String toString() {
}

public static ResourceLocation minecraft(String key) {
return new ResourceLocation("minecraft", key);
return new ResourceLocation(VANILLA_NAMESPACE, key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,15 @@ public void unloadMappings() {
this.typesBuilder.unloadFileMappings();
}

@Override
public @Nullable T getByName(ResourceLocation name) {
return this.typeMap.get(name.toString()); // skip norming call
}

@Override
public @Nullable T getByName(String name) {
return this.typeMap.get(name);
// prepend "minecraft:" prefix if no other namespace has been specified
return this.typeMap.get(ResourceLocation.normString(name));
}

@Override
Expand Down

0 comments on commit ed28da3

Please sign in to comment.