Skip to content

Commit

Permalink
add tag for defining enchantments that should not be removed
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4ckscor3 committed Feb 21, 2023
1 parent f745378 commit 565c880
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/me/juancarloscp52/entropy/EntropyTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public class EntropyTags {
public static final TagKey<Block> VOID_SIGHT_BREAKS = TagKey.of(Registries.BLOCK.getKey(), new Identifier("entropy", "void_sight_breaks"));
public static final TagKey<Block> SHOWN_DURING_XRAY = TagKey.of(Registries.BLOCK.getKey(), new Identifier("entropy", "shown_during_xray"));
public static final TagKey<Enchantment> DO_NOT_ENCHANT_WITH = TagKey.of(Registries.ENCHANTMENT.getKey(), new Identifier("entropy", "do_not_enchant_with"));
public static final TagKey<Enchantment> DO_NOT_REMOVE = TagKey.of(Registries.ENCHANTMENT.getKey(), new Identifier("entropy", "do_not_remove"));
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package me.juancarloscp52.entropy.events.db;

import me.juancarloscp52.entropy.Entropy;
import me.juancarloscp52.entropy.EntropyTags;
import me.juancarloscp52.entropy.events.AbstractInstantEvent;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;

public class RemoveEnchantmentsEvent extends AbstractInstantEvent {

Expand Down Expand Up @@ -31,7 +34,9 @@ private void removeEnchant(ItemStack itemStack) {
if(nbt==null){return;}
if(Math.random() > 1.0/3){return;} // one chance over 3 to remove an enchantment.

nbt.remove("Enchantments");
itemStack.setNbt(nbt);
var enchantments = EnchantmentHelper.fromNbt(itemStack.getEnchantments());

enchantments.keySet().removeIf(enchantment -> !Registries.ENCHANTMENT.getEntry(enchantment).isIn(EntropyTags.DO_NOT_REMOVE));
EnchantmentHelper.set(enchantments, itemStack);
}
}

0 comments on commit 565c880

Please sign in to comment.