Skip to content

Commit

Permalink
Fix Enchantments
Browse files Browse the repository at this point in the history
Turns out changing enchantments to be data-driven means I need to access them with the data-driven context.
  • Loading branch information
MoSadie committed Jul 28, 2024
1 parent bdadf12 commit e032b78
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.message.ChatVisibility;
import net.minecraft.registry.RegistryOps;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.sound.SoundCategory;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -187,7 +188,12 @@ private void registerClientCommand(CommandDispatcher<FabricClientCommandSource>
return 0;
}

DataResult<JsonElement> dataResult = ItemStack.CODEC.encodeStart(JsonOps.INSTANCE, MinecraftClient.getInstance().player.getMainHandStack());
if (MinecraftClient.getInstance().world == null) {
LOGGER.info("Null world running exportitem, this shouldn't happen!");
return 0;
}

DataResult<JsonElement> dataResult = ItemStack.CODEC.encodeStart(RegistryOps.of(JsonOps.INSTANCE, MinecraftClient.getInstance().world.getRegistryManager()), MinecraftClient.getInstance().player.getMainHandStack());

if (dataResult.isError()) {
receiveChatMessage("[EffectMC] Failed to export held item data: Error encoding JSON.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import com.mosadie.effectmc.core.EffectMCCore;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.toast.Toast;
import net.minecraft.client.toast.ToastManager;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.RegistryOps;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

Expand All @@ -24,7 +26,14 @@ public class ItemToast implements Toast {
private boolean changed = true;

public ItemToast(String itemData, Text title, Text body, EffectMCCore core) {
DataResult<Pair<ItemStack, JsonElement>> dataResult = ItemStack.CODEC.decode(JsonOps.INSTANCE, core.fromJson(itemData));
if (MinecraftClient.getInstance().world == null) {
EffectMC.LOGGER.warn("Error decoding item data: No level");
item = new ItemStack(Items.AIR);
this.title = title;
this.body = body;
return;
}
DataResult<Pair<ItemStack, JsonElement>> dataResult = ItemStack.CODEC.decode(RegistryOps.of(JsonOps.INSTANCE, MinecraftClient.getInstance().world.getRegistryManager()), core.fromJson(itemData));

if (dataResult.error().isPresent()) {
EffectMC.LOGGER.warn("Error decoding item data: " + dataResult.error().get());
Expand Down
6 changes: 3 additions & 3 deletions MinecraftMod/forge/1.21/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ org.gradle.debug=false

#read more on this at https://github.com/neoforged/NeoGradle/blob/NG_7.0/README.md#apply-parchment-mappings
# you can also find the latest versions at: https://parchmentmc.org/docs/getting-started
#neogradle.subsystems.parchment.minecraftVersion=1.20.6
#neogradle.subsystems.parchment.mappingsVersion=2024.05.01
neogradle.subsystems.parchment.minecraftVersion=1.21
neogradle.subsystems.parchment.mappingsVersion=2024.07.07

## Environment Properties

Expand All @@ -17,7 +17,7 @@ minecraft_version=1.21
# as they do not follow standard versioning conventions.
minecraft_version_range=[1.21,1.22)
# The Neo version must agree with the Minecraft version to get a valid artifact
neo_version=21.0.10-beta
neo_version=21.0.140-beta
# The Neo version range can use any version of Neo as bounds or match the loader version range
neo_version_range=[21,)
# The loader version range can only use the major version of Neo/FML as bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@
import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.client.server.IntegratedServer;
import net.minecraft.commands.Commands;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.TagParser;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
Expand All @@ -42,6 +47,7 @@
import net.minecraft.world.item.Items;
import net.minecraft.world.item.WrittenBookItem;
import net.minecraft.world.item.component.WrittenBookContent;
import net.minecraft.world.item.enchantment.Enchantment;
import net.neoforged.fml.ModList;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.client.event.RegisterClientCommandsEvent;
Expand All @@ -65,6 +71,7 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Mod(EffectMC.MODID)
public class EffectMC implements EffectExecutor {
Expand Down Expand Up @@ -175,30 +182,40 @@ public void registerClientCommand(RegisterClientCommandsEvent event) {
LOGGER.info("Null player running exportitem, this shouldn't happen!");
return 0;
}
if (Minecraft.getInstance().level == null) {
LOGGER.info("Null level running exportitem, this shouldn't happen!");
return 0;
}

DataResult<JsonElement> dataResult = ItemStack.CODEC.encodeStart(JsonOps.INSTANCE, Minecraft.getInstance().player.getMainHandItem());
try {
DataResult<JsonElement> dataResult = ItemStack.CODEC.encodeStart(RegistryOps.create(JsonOps.INSTANCE, Minecraft.getInstance().level.registryAccess()), Minecraft.getInstance().player.getMainHandItem());

if (dataResult.isError()) {
receiveChatMessage("[EffectMC] Failed to export held item data: Error encoding JSON.");
if (dataResult.error().isPresent()) {
LOGGER.warn("Error encoding JSON: " + dataResult.error().get().message());
} else {
LOGGER.warn("Error encoding JSON: Unknown error.");
}
return 0;
}

if (dataResult.isError()) {
receiveChatMessage("[EffectMC] Failed to export held item data: Error encoding JSON.");
if (dataResult.error().isPresent()) {
LOGGER.warn("Error encoding JSON: " + dataResult.error().get().message());
} else {
LOGGER.warn("Error encoding JSON: Unknown error.");
if (dataResult.result().isEmpty()) {
receiveChatMessage("[EffectMC] Failed to export held item data: No JSON result.");
return 0;
}
return 0;
}

if (dataResult.result().isEmpty()) {
receiveChatMessage("[EffectMC] Failed to export held item data: No JSON result.");
String json = dataResult.result().get().toString();

LOGGER.info("Held Item JSON: " + json);
showItemToast(json, "Exported", Minecraft.getInstance().player.getMainHandItem().getDisplayName().getString());
receiveChatMessage("[EffectMC] Exported held item data to log file!");
return 0;
} catch (Exception e) {
LOGGER.error("Failed to export item data!", e);
receiveChatMessage("[EffectMC] Failed to export held item data: Internal error.");
return 0;
}

String json = dataResult.result().get().toString();

LOGGER.info("Held Item JSON: " + json);
showItemToast(json, "Exported", Minecraft.getInstance().player.getMainHandItem().getDisplayName().getString());
receiveChatMessage("[EffectMC] Exported held item data to log file!");
return 0;
}))).executes((context -> {
receiveChatMessage("[EffectMC] Available subcommands: exportbook, exportitem, trust");
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import com.mosadie.effectmc.core.EffectMCCore;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.toasts.RecipeToast;
import net.minecraft.client.gui.components.toasts.Toast;
import net.minecraft.client.gui.components.toasts.ToastComponent;
import net.minecraft.nbt.TagParser;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
Expand All @@ -26,7 +28,14 @@ public class ItemToast implements Toast {
private boolean changed = true;

public ItemToast(String itemData, Component title, Component body, EffectMCCore core) {
DataResult<Pair<ItemStack, JsonElement>> dataResult = ItemStack.CODEC.decode(JsonOps.INSTANCE, core.fromJson(itemData));
if (Minecraft.getInstance().level == null) {
EffectMC.LOGGER.warn("Error decoding item data: No level");
item = new ItemStack(Items.AIR);
this.title = title;
this.body = body;
return;
}
DataResult<Pair<ItemStack, JsonElement>> dataResult = ItemStack.CODEC.decode(RegistryOps.create(JsonOps.INSTANCE, Minecraft.getInstance().level.registryAccess()), core.fromJson(itemData));

if (dataResult.error().isPresent()) {
EffectMC.LOGGER.warn("Error decoding item data: " + dataResult.error().get());
Expand Down

0 comments on commit e032b78

Please sign in to comment.