Skip to content

Commit

Permalink
Update 1.1.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
MerchantPug committed Jun 28, 2024
1 parent 8f4557d commit 142c4f9
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 64 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
- Allowed creative mode tab code to reload on the server.
## Changes
- Any Enchanted Books that have no enchantments as a result of this mod will now turn into regular Books.

## Bugfixes
- Caught more edge cases where a disabled enchantment can be obtained from containers when playing with the mod serverside.
- Fixed /enchant and the enchanting table not functioning.
- Fixed items stacking incorrectly when moved to different inventories.

## Internal
- Moved EnchantmentDisableTag#DISABLED_ENCHANTMENT_TAG field to EnchantmentDisabledTags#DISABLED.
- This was done to match the 1.20.1 version.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.ItemEnchantments;

Expand All @@ -14,36 +13,51 @@
public class EnchantmentDisableTag {
public static final String MOD_ID = "enchantmentdisabletag";

public static final TagKey<Enchantment> DISABLED_ENCHANTMENT_TAG = TagKey.create(Registries.ENCHANTMENT, asResource("disabled"));
public static boolean reloaded = false;
private static boolean setToBook = false;

public static void removeDisabledEnchantments(ItemStack stack) {
if (stack.has(DataComponents.ENCHANTMENTS)) {
public static ItemStack removeDisabledEnchantments(ItemStack stack) {
if (stack.has(DataComponents.ENCHANTMENTS) && !stack.getEnchantments().isEmpty()) {
ItemEnchantments.Mutable itemEnchantments = new ItemEnchantments.Mutable(ItemEnchantments.EMPTY);
for (Map.Entry<Holder<Enchantment>, Integer> entry : stack.getOrDefault(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY).entrySet())
if (entry.getKey().isBound() && !entry.getKey().is(EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG))
if (entry.getKey().isBound() && !entry.getKey().is(EnchantmentDisableTags.DISABLED))
itemEnchantments.set(entry.getKey().value(), entry.getValue());
if (itemEnchantments.keySet().isEmpty())
stack.remove(DataComponents.ENCHANTMENTS);
else
stack.set(DataComponents.ENCHANTMENTS, itemEnchantments.toImmutable());
}
if (stack.has(DataComponents.STORED_ENCHANTMENTS)) {
if (stack.has(DataComponents.STORED_ENCHANTMENTS) && !stack.get(DataComponents.STORED_ENCHANTMENTS).isEmpty()) {
ItemEnchantments.Mutable itemEnchantments = new ItemEnchantments.Mutable(ItemEnchantments.EMPTY);
for (Map.Entry<Holder<Enchantment>, Integer> entry : stack.getOrDefault(DataComponents.STORED_ENCHANTMENTS, ItemEnchantments.EMPTY).entrySet())
if (entry.getKey().isBound() && !entry.getKey().is(EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG))
if (entry.getKey().isBound() && !entry.getKey().is(EnchantmentDisableTags.DISABLED))
itemEnchantments.set(entry.getKey().value(), entry.getValue());
if (itemEnchantments.keySet().isEmpty())
stack.set(DataComponents.STORED_ENCHANTMENTS, ItemEnchantments.EMPTY);
else
if (itemEnchantments.keySet().isEmpty()) {
stack.remove(DataComponents.STORED_ENCHANTMENTS);
if (stack.is(Items.ENCHANTED_BOOK)) {
ItemStack book = new ItemStack(Items.BOOK);
book.applyComponents(stack.getComponentsPatch());
return book;
}
} else
stack.set(DataComponents.STORED_ENCHANTMENTS, itemEnchantments.toImmutable());
}
return stack;
}

public static boolean shouldSetToBookAndResetState() {
boolean retValue = setToBook;
setToBook = false;
return retValue;
}

public static void setBookState() {
setToBook = true;
}

public static boolean getAndResetReloadState() {
boolean retValue = reloaded;
if (reloaded)
reloaded = false;
reloaded = false;
return retValue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.greenhouseteam.enchantmentdisabletag;

import net.minecraft.core.registries.Registries;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.enchantment.Enchantment;

public class EnchantmentDisableTags {
public static final TagKey<Enchantment> DISABLED = TagKey.create(Registries.ENCHANTMENT, EnchantmentDisableTag.asResource("disabled"));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.greenhouseteam.enchantmentdisabletag.mixin;

import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTag;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

@Mixin(AbstractContainerMenu.class)
public class AbstractContainerMenuMixin {
@ModifyVariable(method = "setCarried", at = @At("HEAD"), argsOnly = true)
private ItemStack enchantmentdisabletag$removeFromCarried(ItemStack stack) {
return EnchantmentDisableTag.removeDisabledEnchantments(stack);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.greenhouseteam.enchantmentdisabletag.mixin;

import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTag;
import com.mojang.serialization.Codec;
import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTags;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.world.item.enchantment.Enchantment;
Expand All @@ -27,7 +27,7 @@ public class EnchantRandomlyFunctionMixin {
ENCHANTMENT_SET_CODEC.xmap(holders -> {
List<Holder<Enchantment>> enchantmentHolders = new ArrayList<>();
for (int i = 0; i < holders.size(); ++i) {
if (!holders.get(i).is(EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG))
if (!holders.get(i).is(EnchantmentDisableTags.DISABLED))
enchantmentHolders.add(holders.get(i));
}
return HolderSet.direct(enchantmentHolders);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.greenhouseteam.enchantmentdisabletag.mixin;

import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTag;
import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTags;
import dev.greenhouseteam.enchantmentdisabletag.access.ItemEnchantmentsAccess;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
Expand Down Expand Up @@ -34,18 +35,25 @@ public abstract class ItemEnchantmentsMixin implements ItemEnchantmentsAccess {
@Inject(method = "<clinit>", at = @At("TAIL"))
private static void enchantmentdisabletag$validateEnchantmentsInCodec(CallbackInfo ci) {
CODEC = CODEC.flatXmap(itemEnchantments -> {
List<Holder<Enchantment>> disabledHolders = new ArrayList<>(itemEnchantments.keySet().stream().filter(holder -> holder.is(EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG)).toList());
List<Holder<Enchantment>> disabledHolders = new ArrayList<>(itemEnchantments.keySet().stream().filter(holder -> holder.is(EnchantmentDisableTags.DISABLED)).toList());
if (!disabledHolders.isEmpty()) {
Object2IntOpenHashMap<Holder<Enchantment>> potentialNewMap = new Object2IntOpenHashMap<>();
for (Object2IntMap.Entry<Holder<Enchantment>> entry : itemEnchantments.entrySet()) {
if (!entry.getKey().is(EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG))
if (!entry.getKey().is(EnchantmentDisableTags.DISABLED))
potentialNewMap.addTo(entry.getKey(), entry.getIntValue());
}
var newItemEnchantments = new ItemEnchantments(potentialNewMap, ((ItemEnchantmentsMixin) (Object) itemEnchantments).enchantmentdisabletag$getShowInTooltip());
if (!itemEnchantments.isEmpty() && newItemEnchantments.isEmpty()) {
newItemEnchantments = ItemEnchantments.EMPTY;
EnchantmentDisableTag.setBookState();
}
if (disabledHolders.isEmpty())
return DataResult.success(newItemEnchantments);
if (disabledHolders.size() == 1)
if (disabledHolders.size() == 1) {
if (newItemEnchantments.isEmpty())
return DataResult.error(() -> "Enchantment " + disabledHolders.getFirst().getRegisteredName() + " has been disabled via the enchantmentdisabledtag:disabled enchantment tag.");
return DataResult.error(() -> "Enchantment " + disabledHolders.getFirst().getRegisteredName() + " has been disabled via the enchantmentdisabledtag:disabled enchantment tag.", newItemEnchantments);
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < disabledHolders.size(); ++i) {
if (i == disabledHolders.size() - 1)
Expand All @@ -54,6 +62,8 @@ public abstract class ItemEnchantmentsMixin implements ItemEnchantmentsAccess {
if (i < disabledHolders.size() - 1)
builder.append(", ");
}
if (newItemEnchantments.isEmpty())
return DataResult.error(() -> "Enchantments " + builder.toString() + " have been disabled via the enchantmentdisabledtag:disabled enchantment tag.");
return DataResult.error(() -> "Enchantments " + builder.toString() + " have been disabled via the enchantmentdisabledtag:disabled enchantment tag.", newItemEnchantments);
}
return DataResult.success(itemEnchantments);
Expand All @@ -63,7 +73,7 @@ public abstract class ItemEnchantmentsMixin implements ItemEnchantmentsAccess {
@Override
public void enchantmentdisabletag$validate() {
for (Object2IntMap.Entry<Holder<Enchantment>> entry : this.enchantments.object2IntEntrySet()) {
if (entry.getKey().is(EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG)) {
if (entry.getKey().is(EnchantmentDisableTags.DISABLED)) {
this.enchantments.remove(entry, entry.getIntValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package dev.greenhouseteam.enchantmentdisabletag.mixin;

import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTag;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.PatchedDataComponentMap;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin {
@Mutable
@Shadow @Final @Deprecated @Nullable
private Item item;

@Shadow public abstract boolean is(Item item);

@Mutable
@Shadow @Final private PatchedDataComponentMap components;

@Inject(method = "<init>(Lnet/minecraft/world/level/ItemLike;ILnet/minecraft/core/component/PatchedDataComponentMap;)V", at = @At("TAIL"))
private void enchantmentdisabletag$unenchantBookIfDisabledInit(ItemLike item, int count, PatchedDataComponentMap map, CallbackInfo ci) {
if (EnchantmentDisableTag.shouldSetToBookAndResetState() && this.is(Items.ENCHANTED_BOOK)) {
this.item = Items.BOOK;
var components = new PatchedDataComponentMap(item.asItem().components());
components.applyPatch(map.asPatch());
this.components = components;
}
}

@Inject(method = "applyComponentsAndValidate", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/ItemStack;validateStrict(Lnet/minecraft/world/item/ItemStack;)Lcom/mojang/serialization/DataResult;"))
private void enchantmentdisabletag$unenchantBookIfAllDisabledAndValidate(DataComponentPatch patch, CallbackInfo ci) {
if (EnchantmentDisableTag.shouldSetToBookAndResetState() && this.is(Items.ENCHANTED_BOOK)) {
this.item = Items.BOOK;
var components = new PatchedDataComponentMap(item.components());
components.applyPatch(this.components.asPatch());
this.components = components;
}
}

@Inject(method = "applyComponents(Lnet/minecraft/core/component/DataComponentPatch;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/Item;verifyComponentsAfterLoad(Lnet/minecraft/world/item/ItemStack;)V"))
private void enchantmentdisabletag$unenchantBookIfAllDisabled(DataComponentPatch patch, CallbackInfo ci) {
if (EnchantmentDisableTag.shouldSetToBookAndResetState() && this.is(Items.ENCHANTED_BOOK)) {
this.item = Items.BOOK;
var components = new PatchedDataComponentMap(item.components());
components.applyPatch(this.components.asPatch());
this.components = components;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.greenhouseteam.enchantmentdisabletag.mixin;

import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTag;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTags;
import net.minecraft.core.Holder;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry;
Expand Down Expand Up @@ -39,7 +39,7 @@ public abstract class MappedRegistryMixin<T> {

return original.stream().filter(t -> {
var optionalHolder = this.getHolder(t);
return optionalHolder.isEmpty() || !optionalHolder.get().is((TagKey<T>) EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG);
return optionalHolder.isEmpty() || !optionalHolder.get().is((TagKey<T>) EnchantmentDisableTags.DISABLED);
}).collect(Collectors.toSet());
}

Expand All @@ -50,7 +50,7 @@ public abstract class MappedRegistryMixin<T> {

return original.stream().filter(t -> {
var optionalHolder = this.getHolder(t);
return optionalHolder.isEmpty() || !optionalHolder.get().is((TagKey<T>) EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG);
return optionalHolder.isEmpty() || !optionalHolder.get().is((TagKey<T>) EnchantmentDisableTags.DISABLED);
}).collect(Collectors.toSet());
}

Expand All @@ -59,13 +59,13 @@ public abstract class MappedRegistryMixin<T> {
if (this.key() != (ResourceKey<? extends Registry<?>>) Registries.ENCHANTMENT)
return original;

return original.entrySet().stream().filter(entry -> entry.getValue().is((TagKey<T>) EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return original.entrySet().stream().filter(entry -> entry.getValue().is((TagKey<T>) EnchantmentDisableTags.DISABLED)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

@ModifyReturnValue(method = "holders", at = @At("RETURN"))
private Stream<Holder.Reference<T>> enchantmentdisabletag$disableFromHolders(Stream<Holder.Reference<T>> original) {
// ref can be null on NeoForge. No clue how it happens, but hey, we have to compensate sometimes.
return original.filter(ref -> ref != null && (!ref.key().isFor(Registries.ENCHANTMENT) || !ref.is((TagKey<T>) EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG)));
return original.filter(ref -> ref != null && (!ref.key().isFor(Registries.ENCHANTMENT) || !ref.is((TagKey<T>) EnchantmentDisableTags.DISABLED)));
}

@ModifyArg(method = "iterator", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/Iterators;transform(Ljava/util/Iterator;Lcom/google/common/base/Function;)Ljava/util/Iterator;"))
Expand All @@ -77,7 +77,7 @@ public abstract class MappedRegistryMixin<T> {
List<T> list = new ArrayList<>();
while (original.hasNext()) {
T it = original.next();
if (it instanceof Holder.Reference<?> reference && reference.key().isFor(Registries.ENCHANTMENT) && !reference.is((TagKey) EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG))
if (it instanceof Holder.Reference<?> reference && reference.key().isFor(Registries.ENCHANTMENT) && !reference.is((TagKey) EnchantmentDisableTags.DISABLED))
list.add(it);
}
return list.iterator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dev.greenhouseteam.enchantmentdisabletag.mixin;

import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTag;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTags;
import net.minecraft.commands.arguments.ResourceArgument;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
Expand All @@ -16,7 +16,7 @@
public class ResourceArgumentMixin<T> {
@ModifyReturnValue(method = "parse(Lcom/mojang/brigadier/StringReader;)Lnet/minecraft/core/Holder$Reference;", at = @At(value = "RETURN"))
private Holder.Reference<T> enchantmentdisabletag$throwOnDisabled(Holder.Reference<T> original) throws CommandSyntaxException {
if (original.key().isFor(Registries.ENCHANTMENT) && original.is((TagKey<T>) EnchantmentDisableTag.DISABLED_ENCHANTMENT_TAG))
if (original.key().isFor(Registries.ENCHANTMENT) && original.is((TagKey<T>) EnchantmentDisableTags.DISABLED))
throw new DynamicCommandExceptionType(
key -> Component.translatableWithFallback("command.enchantmentdisabledtag.disabled", "Enchantment " + key + " has been disabled via the enchantmentdisabletag:disabled enchantment tag.", key)
).create(original.key().location().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Slot.class)
public class SlotMixin {
@ModifyVariable(method = "set", at = @At("HEAD"), argsOnly = true)
private ItemStack enchantmentdisabletag$removeDisabledEnchantments(ItemStack stack) {
EnchantmentDisableTag.removeDisabledEnchantments(stack);
return stack;
return EnchantmentDisableTag.removeDisabledEnchantments(stack);
}
}
Loading

0 comments on commit 142c4f9

Please sign in to comment.