Skip to content

Commit

Permalink
chore: 1.20.6 port
Browse files Browse the repository at this point in the history
Compiles and runs, not heavily tested yet

ItemStack "NBT" editing still works, even with components; component data is serialized
to NBT for the NBT editor, and deserialized back to the stack if changes are made

Arch FluidStack.empty() currently broken (returning null, causes crashes in fluid selection
screens)

Won't work with NeoForge 20.6.3-beta or later due to arch/neoforge mappings issues
(see architectury/architectury-loom#207)

No MinecraftForge support at all yet
  • Loading branch information
desht committed Apr 30, 2024
1 parent 253a96a commit a9ae6b7
Show file tree
Hide file tree
Showing 26 changed files with 234 additions and 189 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id "architectury-plugin" version "3.4-SNAPSHOT"
id "dev.architectury.loom" version "1.5-SNAPSHOT" apply false
id "dev.architectury.loom" version "1.6-SNAPSHOT" apply false
}

architectury {
Expand Down Expand Up @@ -33,7 +33,7 @@ allprojects {
// needs to be done AFTER version is set
apply from: "https://raw.githubusercontent.com/FTBTeam/mods-meta/main/gradle/publishing.gradle"

sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = 17
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = 21

compileJava {
options.encoding = "UTF-8"
Expand Down
8 changes: 5 additions & 3 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ dependencies {
exclude group: "dev.architectury"
}

modCompileOnly("mezz.jei:jei-${minecraft_version}-common-api:${jei_version}")
// modCompileOnly("mezz.jei:jei-${minecraft_version}-common-api:${jei_version}")
modCompileOnly("mezz.jei:jei-1.20.4-common-api:${jei_version}")

modCompileOnly "dev.emi:emi-xplat-intermediary:${emi_version}+${minecraft_version}:api"
// modCompileOnly "dev.emi:emi-xplat-intermediary:${emi_version}+${minecraft_version}:api"
modCompileOnly "dev.emi:emi-xplat-intermediary:${emi_version}+1.20.4:api"
}

def ENV = System.getenv()
Expand All @@ -23,7 +25,7 @@ test {
}

architectury {
common("forge", "fabric", "neoforge")
common(/*"forge",*/ "fabric", "neoforge")
}

loom {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.Nameable;
Expand Down Expand Up @@ -104,31 +106,31 @@ public static void registerCommands(CommandDispatcher<CommandSourceStack> dispat
info.putInt("x", pos.getX());
info.putInt("y", pos.getY());
info.putInt("z", pos.getZ());
tag.merge(blockEntity.saveWithFullMetadata());
tag.merge(blockEntity.saveWithFullMetadata(context.getSource().getLevel().registryAccess()));
tag.remove("x");
tag.remove("y");
tag.remove("z");
info.putString("id", tag.getString("id"));
tag.remove("id");

var list = new ListTag();
addInfo(list, Component.literal("Class"), Component.literal(blockEntity.getClass().getName()));
var key = RegistrarManager.getId(blockEntity.getType(), Registries.BLOCK_ENTITY_TYPE);
addInfo(list, Component.literal("ID"), Component.literal(key == null ? "null" : key.toString()));
addInfo(list, Component.literal("Block"), Component.literal(String.valueOf(RegistrarManager.getId(blockEntity.getBlockState().getBlock(), Registries.BLOCK))));
addInfo(list, Component.literal("Block Class"), Component.literal(blockEntity.getBlockState().getBlock().getClass().getName()));
addInfo(list, Component.literal("Position"), Component.literal("[" + pos.getX() + ", " + pos.getY() + ", " + pos.getZ() + "]"));
addInfo(list, Component.literal("Mod"), Component.literal(key == null ? "null" : Platform.getOptionalMod(key.getNamespace()).map(Mod::getName).orElse("Unknown")));
addInfo(list, Component.literal("Ticking"), Component.literal(blockEntity instanceof TickingBlockEntity ? "true" : "false"));
info.put("text", list);
info.put("text", InfoAppender.create(context)
.add(Component.literal("Class"), Component.literal(blockEntity.getClass().getName()))
.add(Component.literal("ID"), Component.literal(key == null ? "null" : key.toString()))
.add(Component.literal("Block"), Component.literal(String.valueOf(RegistrarManager.getId(blockEntity.getBlockState().getBlock(), Registries.BLOCK))))
.add(Component.literal("Block Class"), Component.literal(blockEntity.getBlockState().getBlock().getClass().getName()))
.add(Component.literal("Position"), Component.literal("[" + pos.getX() + ", " + pos.getY() + ", " + pos.getZ() + "]"))
.add(Component.literal("Mod"), Component.literal(key == null ? "null" : Platform.getOptionalMod(key.getNamespace()).map(Mod::getName).orElse("Unknown")))
.add(Component.literal("Ticking"), Component.literal(blockEntity instanceof TickingBlockEntity ? "true" : "false"))
.list);

var title = blockEntity instanceof Nameable ? ((Nameable) blockEntity).getDisplayName() : null;

if (title == null) {
title = Component.literal(blockEntity.getClass().getSimpleName());
}

info.putString("title", Component.Serializer.toJson(title));
info.putString("title", Component.Serializer.toJson(title, context.getSource().registryAccess()));
}))
)
)
Expand All @@ -146,13 +148,13 @@ public static void registerCommands(CommandDispatcher<CommandSourceStack> dispat

entity.save(tag);

var list = new ListTag();
addInfo(list, Component.literal("Class"), Component.literal(entity.getClass().getName()));
var key = RegistrarManager.getId(entity.getType(), Registries.ENTITY_TYPE);
addInfo(list, Component.literal("ID"), Component.literal(key == null ? "null" : key.toString()));
addInfo(list, Component.literal("Mod"), Component.literal(key == null ? "null" : Platform.getOptionalMod(key.getNamespace()).map(Mod::getName).orElse("Unknown")));
info.put("text", list);
info.putString("title", Component.Serializer.toJson(entity.getDisplayName()));
info.put("text", InfoAppender.create(context)
.add(Component.literal("Class"), Component.literal(entity.getClass().getName()))
.add(Component.literal("ID"), Component.literal(key == null ? "null" : key.toString()))
.add(Component.literal("Mod"), Component.literal(key == null ? "null" : Platform.getOptionalMod(key.getNamespace()).map(Mod::getName).orElse("Unknown")))
.list());
info.putString("title", Component.Serializer.toJson(entity.getDisplayName(), entity.level().registryAccess()));
}))
)
)
Expand All @@ -167,21 +169,21 @@ public static void registerCommands(CommandDispatcher<CommandSourceStack> dispat
player.saveWithoutId(tag);
tag.remove("id");

var list = new ListTag();
addInfo(list, Component.literal("Name"), player.getName());
addInfo(list, Component.literal("Display Name"), player.getDisplayName());
addInfo(list, Component.literal("UUID"), Component.literal(player.getUUID().toString()));
// addInfo(list, Component.literal("FTB Library Team"), Component.literal(p.team.getId()));
info.put("text", list);
info.putString("title", Component.Serializer.toJson(player.getDisplayName()));
info.put("text", InfoAppender.create(context)
.add(Component.literal("Name"), player.getName())
.add(Component.literal("Display Name"), player.getDisplayName())
.add(Component.literal("UUID"), Component.literal(player.getUUID().toString()))
.list());
info.putString("title", Component.Serializer.toJson(player.getDisplayName(), player.level().registryAccess()));
}))
)
)
.then(Commands.literal("item")
.executes(context -> editNBT(context, (info, tag) -> {
var player = context.getSource().getPlayerOrException();
info.putString("type", "item");
player.getItemInHand(InteractionHand.MAIN_HAND).save(tag);
Tag res = player.getItemInHand(InteractionHand.MAIN_HAND).save(player.level().registryAccess(), tag);
if (res instanceof CompoundTag t) tag.merge(t);
}))
)
)
Expand Down Expand Up @@ -209,8 +211,12 @@ public static void registerCommands(CommandDispatcher<CommandSourceStack> dispat
dispatcher.register(command);
}

private static void addInfo(ListTag list, Component key, Component value) {
list.add(StringTag.valueOf(Component.Serializer.toJson(key.copy().withStyle(ChatFormatting.BLUE).append(": ").append(value.copy().withStyle(ChatFormatting.GOLD)))));
private static void addInfo(ListTag list, Component key, Component value, HolderLookup.Provider provider) {
list.add(StringTag.valueOf(Component.Serializer.toJson(
key.copy().withStyle(ChatFormatting.BLUE).append(": ").append(value.copy().withStyle(ChatFormatting.GOLD)),
provider)
)
);
}

private static int editNBT(CommandContext<CommandSourceStack> context, NBTEditCallback data) throws CommandSyntaxException {
Expand All @@ -227,4 +233,19 @@ private static int editNBT(CommandContext<CommandSourceStack> context, NBTEditCa

return 0;
}

private record InfoAppender(ListTag list, HolderLookup.Provider provider) {
static InfoAppender create(CommandContext<CommandSourceStack> context) {
return new InfoAppender(new ListTag(), context.getSource().registryAccess());
}

private InfoAppender add(Component key, Component value) {
list.add(StringTag.valueOf(Component.Serializer.toJson(
key.copy().withStyle(ChatFormatting.BLUE).append(": ").append(value.copy().withStyle(ChatFormatting.GOLD)),
provider)
)
);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public NBTButton(Panel panel) {
public void onClicked(MouseButton button) {
playClickSound();

CompoundTag toEdit = Objects.requireNonNullElse(selectedStack.getTag(), new CompoundTag());
CompoundTag toEdit = Objects.requireNonNullElse(selectedStack.getComponentsTag(), new CompoundTag());
if (button.isLeft()) {
StringConfig config = new StringConfig();
SNBTCompoundTag snbt = SNBTCompoundTag.of(toEdit);
Expand All @@ -322,7 +322,7 @@ public void onClicked(MouseButton button) {
CompoundTag info = Util.make(new CompoundTag(), tag -> tag.putString("type", "item"));
new NBTEditorScreen(info, toEdit, (accepted, tag) -> {
if (accepted) {
selectedStack.setTag(tag.copy());
selectedStack.applyComponentsTag(tag.copy());
ResourceSelectorScreen.this.openGuiLater();
}
}).openGui();
Expand All @@ -334,7 +334,7 @@ private EditMultilineStringConfigOverlay makeMultilineEditPanel(StringConfig con
var panel = new EditMultilineStringConfigOverlay(ResourceSelectorScreen.this, config, accepted -> {
if (accepted) {
try {
selectedStack.setTag(SNBT.readLines(List.of(config.getValue())));
selectedStack.applyComponentsTag(SNBT.readLines(List.of(config.getValue())));
} catch (SNBTSyntaxException e) {
SimpleToast.error(Component.translatable("ftblibrary.gui.error"), Component.literal(e.getMessage()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;

Expand Down Expand Up @@ -52,7 +53,7 @@ public boolean shouldAdd(String search) {
public void addMouseOverText(TooltipList list) {
if (!getStack().isEmpty()) {
TooltipFlag flag = Minecraft.getInstance().options.advancedItemTooltips ? TooltipFlag.ADVANCED : TooltipFlag.NORMAL;
getStack().getTooltipLines(Minecraft.getInstance().player, flag).forEach(list::add);
getStack().getTooltipLines(Item.TooltipContext.of(Minecraft.getInstance().level), Minecraft.getInstance().player, flag).forEach(list::add);
if (FTBLibraryClientConfig.ITEM_MODNAME.get()) {
ModUtils.getModName(getStack().getItem()).ifPresent(name ->
list.add(Component.literal(name).withStyle(ChatFormatting.BLUE, ChatFormatting.ITALIC)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import dev.ftb.mods.ftblibrary.icon.ItemIcon;
import dev.ftb.mods.ftblibrary.util.client.ClientUtils;
import net.minecraft.ChatFormatting;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -39,9 +41,9 @@ static SelectableResource<FluidStack> fluid(FluidStack stack) {
return new FluidStackResource(stack);
}

CompoundTag getTag();
CompoundTag getComponentsTag();

void setTag(CompoundTag tag);
void applyComponentsTag(CompoundTag tag);

void setCount(int count);

Expand All @@ -67,13 +69,16 @@ public SelectableResource<ItemStack> copyWithCount(long count) {
}

@Override
public CompoundTag getTag() {
return stack.getTag();
public CompoundTag getComponentsTag() {
Tag tag = DataComponentMap.CODEC.encodeStart(NbtOps.INSTANCE, stack.getComponents()).result()
.orElse(new CompoundTag());
return tag instanceof CompoundTag t ? t : null;
}

@Override
public void setTag(CompoundTag tag) {
stack.setTag(tag);
public void applyComponentsTag(CompoundTag tag) {
DataComponentMap.CODEC.parse(NbtOps.INSTANCE, tag).result()
.ifPresent(stack::applyComponents);
}

@Override
Expand Down Expand Up @@ -104,13 +109,16 @@ public SelectableResource<FluidStack> copyWithCount(long count) {
}

@Override
public CompoundTag getTag() {
return stack.getTag();
public CompoundTag getComponentsTag() {
Tag tag = DataComponentMap.CODEC.encodeStart(NbtOps.INSTANCE, stack.getComponents()).result()
.orElse(new CompoundTag());
return tag instanceof CompoundTag t ? t : null;
}

@Override
public void setTag(CompoundTag tag) {
stack.setTag(tag);
public void applyComponentsTag(CompoundTag tag) {
DataComponentMap.CODEC.parse(NbtOps.INSTANCE, tag).result()
.ifPresent(stack::applyComponents);
}

@Override
Expand Down Expand Up @@ -159,12 +167,12 @@ public SelectableResource<ResourceLocation> copyWithCount(long count) {
}

@Override
public CompoundTag getTag() {
public CompoundTag getComponentsTag() {
return null;
}

@Override
public void setTag(CompoundTag tag) {
public void applyComponentsTag(CompoundTag tag) {
}

@Override
Expand Down
30 changes: 15 additions & 15 deletions common/src/main/java/dev/ftb/mods/ftblibrary/icon/ItemIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.TagParser;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -20,8 +23,6 @@
import net.minecraft.world.item.Items;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class ItemIcon extends Icon implements IResourceIcon {
private final ItemStack stack;

Expand Down Expand Up @@ -58,15 +59,17 @@ public static Icon getItemIcon(String lazyStackString) {

if (s.length >= 4 && !s[3].equals("null")) {
try {
stack.setTag(TagParser.parseTag(s[3]));
DataComponentMap.CODEC.parse(NbtOps.INSTANCE, TagParser.parseTag(s[3]))
.ifSuccess(stack::applyComponents);
} catch (CommandSyntaxException ex) {
ex.printStackTrace();
}
}

if (stack.isEmpty()) {
stack = new ItemStack(Items.BARRIER);
stack.setHoverName(Component.literal(lazyStackString));
ItemStack fallback = new ItemStack(Items.BARRIER);
fallback.set(DataComponents.CUSTOM_NAME, Component.literal(lazyStackString));
return getItemIcon(fallback);
}

return getItemIcon(stack);
Expand Down Expand Up @@ -131,12 +134,13 @@ public void draw3D(GuiGraphics graphics) {
}

public String toString() {
var is = getStack();
var stack = getStack();
var builder = new StringBuilder("item:");
builder.append(RegistrarManager.getId(is.getItem(), Registries.ITEM));
var count = is.getCount();
var damage = is.getDamageValue();
var nbt = is.getTag();
builder.append(RegistrarManager.getId(stack.getItem(), Registries.ITEM));
var count = stack.getCount();
var damage = stack.getDamageValue();
var nbt = DataComponentMap.CODEC.encodeStart(NbtOps.INSTANCE, stack.getComponents()).result()
.orElse(null);

if (count > 1 || damage > 0 || nbt != null) {
builder.append(' ');
Expand All @@ -157,11 +161,7 @@ public String toString() {
}

public int hashCode() {
var stack = getStack();
var h = stack.getItem().hashCode();
h = h * 31 + stack.getCount();
h = h * 31 + Objects.hashCode(stack.getTag());
return h;
return ItemStack.hashItemAndComponents(getStack());
}

public boolean equals(Object o) {
Expand Down
Loading

0 comments on commit a9ae6b7

Please sign in to comment.