Skip to content

Commit

Permalink
Add wolf_variant registry and start with biome registry
Browse files Browse the repository at this point in the history
Biome registry is required for wolf variant registry to fully work
  • Loading branch information
booky10 committed Jul 1, 2024
1 parent afcdfae commit ba08959
Show file tree
Hide file tree
Showing 21 changed files with 2,248 additions and 48 deletions.
2 changes: 2 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ mappingCompression {
compress("enchantment/enchantment_type_data.json")

compress("stats/statistics.json")

compress("world/biome_data.json")
}

with<JsonArrayCompressionStrategy> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ static void writeDirect(PacketWrapper<?> wrapper, ChatType chatType) {
static ChatType decode(NBT nbt, ClientVersion version, @Nullable TypesBuilderData data) {
NBTCompound compound = (NBTCompound) nbt;
NBTCompound chatTag = compound.getCompoundTagOrThrow("chat");
ChatTypeDecoration chatDeco = ChatTypeDecoration.decode(chatTag, version, data);
ChatTypeDecoration chatDeco = ChatTypeDecoration.decode(chatTag, version);
NBTCompound narrationTag = compound.getCompoundTagOrThrow("narration");
ChatTypeDecoration narrationDeco = ChatTypeDecoration.decode(narrationTag, version, data);
ChatTypeDecoration narrationDeco = ChatTypeDecoration.decode(narrationTag, version);
return new StaticChatType(data, chatDeco, narrationDeco);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@
import com.github.retrooper.packetevents.protocol.nbt.NBTString;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.util.Index;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.function.BiFunction;

import static com.github.retrooper.packetevents.protocol.chat.ChatTypeDecoration.Parameter.CONTENT;
Expand Down Expand Up @@ -71,18 +70,13 @@ public static void write(PacketWrapper<?> wrapper, ChatTypeDecoration decoration
wrapper.writeStyle(decoration.style);
}

public static ChatTypeDecoration decode(NBT nbt, ClientVersion version, @Nullable TypesBuilderData data) {
public static ChatTypeDecoration decode(NBT nbt, ClientVersion version) {
NBTCompound compound = (NBTCompound) nbt;
String translationKey = compound.getStringTagValueOrThrow("translation_key");
List<Parameter> params = new ArrayList<>();
NBTList<NBTString> paramsTag = compound.getStringListTagOrNull("parameters");
if (paramsTag != null) {
for (NBTString paramTag : paramsTag.getTags()) {
Parameter parameter = Parameter.valueByName(paramTag.getValue().toUpperCase());
if (parameter != null) {
params.add(parameter);
}
}
NBTList<NBTString> paramsTag = compound.getStringListTagOrThrow("parameters");
for (NBTString paramTag : paramsTag.getTags()) {
params.add(Parameter.ID_INDEX.valueOrThrow(paramTag.getValue()));
}
NBTCompound styleTag = compound.getCompoundTagOrNull("style");
Style style = styleTag == null ? empty() :
Expand All @@ -93,7 +87,7 @@ public static ChatTypeDecoration decode(NBT nbt, ClientVersion version, @Nullabl
public static NBT encode(ChatTypeDecoration decoration, ClientVersion version) {
NBTList<NBTString> paramsTag = NBTList.createStringList();
for (Parameter param : decoration.parameters) {
paramsTag.addTag(new NBTString(param.name().toLowerCase(Locale.ROOT)));
paramsTag.addTag(new NBTString(param.getId()));
}

NBTCompound compound = new NBTCompound();
Expand Down Expand Up @@ -146,23 +140,30 @@ public Style getStyle() {
}

public enum Parameter {
SENDER((component, type) -> type.getName()),
TARGET((component, type) -> type.getTargetName() != null ? type.getTargetName() : Component.empty()),
CONTENT((component, type) -> component);

SENDER("sender", (component, type) -> type.getName()),
TARGET("target", (component, type) -> type.getTargetName() != null
? type.getTargetName() : Component.empty()),
CONTENT("content", (component, type) -> component);

public static final Index<String, Parameter> ID_INDEX = Index.create(
Parameter.class, Parameter::getId);

private final String id;
private final BiFunction<Component, ChatType.Bound, Component> selector;

Parameter(BiFunction<Component, ChatType.Bound, Component> selector) {
Parameter(String id, BiFunction<Component, ChatType.Bound, Component> selector) {
this.id = id;
this.selector = selector;
}

@Nullable
public static Parameter valueByName(String name) {
try {
return valueOf(name);
} catch (IllegalArgumentException ex) {
return null;
}
public String getId() {
return this.id;
}

@Deprecated
public static @Nullable Parameter valueByName(String id) {
return ID_INDEX.value(id);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ public interface ComponentType<T> extends MappedEntity {

void write(PacketWrapper<?> wrapper, T content);

default T decode(NBT nbt, ClientVersion version) {
throw new UnsupportedOperationException();
}
T decode(NBT nbt, ClientVersion version);

default NBT encode(T value, ClientVersion version) {
throw new UnsupportedOperationException();
}
NBT encode(T value, ClientVersion version);

interface Decoder<T> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2024 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.github.retrooper.packetevents.protocol.entity.wolfvariant;

import com.github.retrooper.packetevents.protocol.mapper.AbstractMappedEntity;
import com.github.retrooper.packetevents.protocol.mapper.MappedEntitySet;
import com.github.retrooper.packetevents.protocol.world.biome.Biome;
import com.github.retrooper.packetevents.resources.ResourceLocation;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import org.jetbrains.annotations.Nullable;

public class StaticWolfVariant extends AbstractMappedEntity implements WolfVariant {

private final ResourceLocation wildTexture;
private final ResourceLocation tameTexture;
private final ResourceLocation angryTexture;
private final MappedEntitySet<Biome> biomes;

public StaticWolfVariant(
ResourceLocation wildTexture,
ResourceLocation tameTexture,
ResourceLocation angryTexture,
MappedEntitySet<Biome> biomes
) {
this(null, wildTexture, tameTexture, angryTexture, biomes);
}

public StaticWolfVariant(
@Nullable TypesBuilderData data,
ResourceLocation wildTexture,
ResourceLocation tameTexture,
ResourceLocation angryTexture,
MappedEntitySet<Biome> biomes
) {
super(data);
this.wildTexture = wildTexture;
this.tameTexture = tameTexture;
this.angryTexture = angryTexture;
this.biomes = biomes;
}

@Override
public WolfVariant copy(@Nullable TypesBuilderData newData) {
return new StaticWolfVariant(newData, this.wildTexture, this.tameTexture, this.angryTexture, this.biomes);
}

@Override
public ResourceLocation getWildTexture() {
return this.wildTexture;
}

@Override
public ResourceLocation getTameTexture() {
return this.tameTexture;
}

@Override
public ResourceLocation getAngryTexture() {
return this.angryTexture;
}

@Override
public MappedEntitySet<Biome> getBiomes() {
return this.biomes;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,62 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2024 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.github.retrooper.packetevents.protocol.entity.wolfvariant;

import com.github.retrooper.packetevents.protocol.mapper.CopyableEntity;
import com.github.retrooper.packetevents.protocol.mapper.MappedEntity;
import com.github.retrooper.packetevents.protocol.mapper.MappedEntitySet;
import com.github.retrooper.packetevents.protocol.nbt.NBT;
import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
import com.github.retrooper.packetevents.protocol.nbt.NBTString;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.world.biome.Biome;
import com.github.retrooper.packetevents.protocol.world.biome.Biomes;
import com.github.retrooper.packetevents.resources.ResourceLocation;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import org.jetbrains.annotations.Nullable;

public interface WolfVariant extends MappedEntity, CopyableEntity<WolfVariant> {

ResourceLocation getWildTexture();

import java.util.Set;
ResourceLocation getTameTexture();

//TODO Finish Wolf-Variant class
public class WolfVariant {
private ResourceLocation wildId, tameId, angryId;
ResourceLocation getAngryTexture();

public WolfVariant(ResourceLocation wildId, ResourceLocation tameId, ResourceLocation angryId) {
MappedEntitySet<Biome> getBiomes();

static WolfVariant decode(NBT nbt, ClientVersion version, @Nullable TypesBuilderData data) {
NBTCompound compound = (NBTCompound) nbt;
ResourceLocation wildTexture = new ResourceLocation(compound.getStringTagValueOrThrow("wild_texture"));
ResourceLocation tameTexture = new ResourceLocation(compound.getStringTagValueOrThrow("tame_texture"));
ResourceLocation angryTexture = new ResourceLocation(compound.getStringTagValueOrThrow("angry_texture"));
MappedEntitySet<Biome> biomes = MappedEntitySet.decode(
compound.getTagOrThrow("biomes"), version, Biomes.getRegistry());
return new StaticWolfVariant(data, wildTexture, tameTexture, angryTexture, biomes);
}

static NBT encode(WolfVariant variant, ClientVersion version) {
NBTCompound compound = new NBTCompound();
compound.setTag("wild_texture", new NBTString(variant.getWildTexture().toString()));
compound.setTag("tame_texture", new NBTString(variant.getTameTexture().toString()));
compound.setTag("angry_texture", new NBTString(variant.getAngryTexture().toString()));
compound.setTag("biomes", MappedEntitySet.encode(variant.getBiomes(), version));
return compound;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2024 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.github.retrooper.packetevents.protocol.entity.wolfvariant;

import com.github.retrooper.packetevents.protocol.mapper.MappedEntitySet;
import com.github.retrooper.packetevents.protocol.world.biome.Biome;
import com.github.retrooper.packetevents.protocol.world.biome.Biomes;
import com.github.retrooper.packetevents.resources.ResourceLocation;
import com.github.retrooper.packetevents.util.mappings.VersionedRegistry;
import org.jetbrains.annotations.ApiStatus;

import java.util.Arrays;

public final class WolfVariants {

private static final VersionedRegistry<WolfVariant> REGISTRY = new VersionedRegistry<>(
"wolf_variant", "entity/wolf_variant_mappings");

private WolfVariants() {
}

@ApiStatus.Internal
public static WolfVariant define(String key, MappedEntitySet<Biome> biomes) {
return define(key, "wolf_" + key, biomes);
}

@ApiStatus.Internal
public static WolfVariant define(String key, String assetId, MappedEntitySet<Biome> biomes) {
return define(key, ResourceLocation.minecraft("entity/wolf/" + assetId),
ResourceLocation.minecraft("entity/wolf/" + assetId + "_tame"),
ResourceLocation.minecraft("entity/wolf/" + assetId + "_angry"), biomes);
}

@ApiStatus.Internal
public static WolfVariant define(
String key,
ResourceLocation wildTexture,
ResourceLocation tameTexture,
ResourceLocation angryTexture,
MappedEntitySet<Biome> biomes
) {
return REGISTRY.define(key, data -> new StaticWolfVariant(
data, wildTexture, tameTexture, angryTexture, biomes));
}

public static VersionedRegistry<WolfVariant> getRegistry() {
return REGISTRY;
}

public static final WolfVariant PALE = define("pale", "wolf",
new MappedEntitySet<>(Arrays.asList(Biomes.TAIGA)));
public static final WolfVariant SPOTTED = define("spotted",
new MappedEntitySet<>(ResourceLocation.minecraft("is_savanna")));
public static final WolfVariant SNOWY = define("snowy",
new MappedEntitySet<>(Arrays.asList(Biomes.GROVE)));
public static final WolfVariant BLACK = define("black",
new MappedEntitySet<>(Arrays.asList(Biomes.OLD_GROWTH_PINE_TAIGA)));
public static final WolfVariant ASHEN = define("ashen",
new MappedEntitySet<>(Arrays.asList(Biomes.SNOWY_TAIGA)));
public static final WolfVariant RUSTY = define("rusty",
new MappedEntitySet<>(ResourceLocation.minecraft("is_jungle")));
public static final WolfVariant WOODS = define("woods",
new MappedEntitySet<>(Arrays.asList(Biomes.FOREST)));
public static final WolfVariant CHESTNUT = define("chestnut",
new MappedEntitySet<>(Arrays.asList(Biomes.OLD_GROWTH_SPRUCE_TAIGA)));
public static final WolfVariant STRIPED = define("striped",
new MappedEntitySet<>(ResourceLocation.minecraft("is_badlands")));

static {
REGISTRY.unloadMappings();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
import com.github.retrooper.packetevents.protocol.nbt.NBTInt;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

Expand All @@ -37,7 +35,7 @@ public EnchantmentCost(int base, int perLevelAboveFirst) {
this.perLevelAboveFirst = perLevelAboveFirst;
}

public static EnchantmentCost decode(NBT nbt, ClientVersion version, @Nullable TypesBuilderData data) {
public static EnchantmentCost decode(NBT nbt, ClientVersion version) {
NBTCompound compound = (NBTCompound) nbt;
int base = compound.getNumberTagOrThrow("base").getAsInt();
int perLevelAboveFirst = compound.getNumberTagOrThrow("per_level_above_first").getAsInt();
Expand Down
Loading

0 comments on commit ba08959

Please sign in to comment.