Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
290dafd
commit 659377d
Showing
4 changed files
with
139 additions
and
129 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/java/org/auioc/mcmod/arnicalib/game/enchantment/EnchUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.auioc.mcmod.arnicalib.game.enchantment; | ||
|
||
import org.apache.commons.lang3.RandomUtils; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.nbt.ListTag; | ||
|
||
public interface EnchUtils { | ||
|
||
static void enchantOne(ListTag enchantments, int index, int level) { | ||
CompoundTag nbt = enchantments.getCompound(index); | ||
nbt.putShort("lvl", (short) (nbt.getShort("lvl") + level)); | ||
} | ||
|
||
static void enchantOne(CompoundTag enchantment, int level) { | ||
enchantment.putShort("lvl", (short) (enchantment.getShort("lvl") + level)); | ||
} | ||
|
||
static void enchantAll(ListTag enchantments, int level) { | ||
for (int i = 0; i < enchantments.size(); i++) { | ||
enchantOne(enchantments.getCompound(i), level); | ||
} | ||
} | ||
|
||
static void enchantRandom(ListTag enchantments, int level) { | ||
enchantOne(enchantments.getCompound(RandomUtils.nextInt(0, enchantments.size())), level); | ||
} | ||
|
||
|
||
static CompoundTag getHighestEnchantment(ListTag enchantments) { | ||
int index = 0; | ||
short highestLevel = 0; | ||
for (int i = 0, l = enchantments.size(); i < l; i++) { | ||
short lvl = enchantments.getCompound(i).getShort("lvl"); | ||
if (lvl > highestLevel) { | ||
index = i; | ||
highestLevel = lvl; | ||
} | ||
} | ||
return enchantments.getCompound(index); | ||
} | ||
|
||
static boolean isOverLimit(ListTag enchantments) { | ||
for (int i = 0, l = enchantments.size(); i < l; i++) { | ||
CompoundTag ench = enchantments.getCompound(i); | ||
if (ench.getShort("lvl") > (EnchantmentRegistry.getOrElseThrow(ench.getString("id"))).getMaxLevel()) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/org/auioc/mcmod/arnicalib/game/enchantment/EnchantmentIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.auioc.mcmod.arnicalib.game.enchantment; | ||
|
||
import java.util.Map; | ||
import java.util.function.Predicate; | ||
import org.auioc.mcmod.arnicalib.game.enchantment.EnchantmentVisitor.BiEnchantmentVisitor; | ||
import org.auioc.mcmod.arnicalib.game.enchantment.EnchantmentVisitor.QuadEnchantmentVisitor; | ||
import org.auioc.mcmod.arnicalib.game.enchantment.EnchantmentVisitor.TriEnchantmentVisitor; | ||
import net.minecraft.world.entity.EquipmentSlot; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.enchantment.Enchantment; | ||
import net.minecraft.world.item.enchantment.EnchantmentHelper; | ||
|
||
public class EnchantmentIterator { | ||
|
||
public static void run(BiEnchantmentVisitor visitor, Map<Enchantment, Integer> enchMap) { | ||
for (var enchEntry : enchMap.entrySet()) visitor.accept(enchEntry.getKey(), enchEntry.getValue()); | ||
} | ||
|
||
public static void runOnItem(BiEnchantmentVisitor visitor, ItemStack itemStack) { | ||
if (!itemStack.isEmpty()) run(visitor, EnchantmentHelper.getEnchantments(itemStack)); | ||
} | ||
|
||
public static void runOnItems(TriEnchantmentVisitor visitor, Iterable<ItemStack> itemStacks, Predicate<ItemStack> predicate) { | ||
for (var itemStack : itemStacks) { | ||
if (!itemStack.isEmpty() && predicate.test(itemStack)) { | ||
runOnItem((ench, lvl) -> visitor.accept(itemStack, ench, lvl), itemStack); | ||
} | ||
} | ||
} | ||
|
||
public static void runOnItems(TriEnchantmentVisitor visitor, Iterable<ItemStack> itemStacks) { | ||
runOnItems(visitor, itemStacks, (o) -> true); | ||
} | ||
|
||
public static void runOnLiving(QuadEnchantmentVisitor visitor, LivingEntity living, EquipmentSlot[] slots) { | ||
for (EquipmentSlot slot : slots) { | ||
var itemStack = living.getItemBySlot(slot); | ||
runOnItem((ench, lvl) -> { | ||
if (ench instanceof IValidSlotsVisibleEnchantment _ench) { | ||
if (_ench.isValidSlot(slot)) { | ||
visitor.accept(slot, itemStack, ench, lvl); | ||
} | ||
} else { | ||
visitor.accept(slot, itemStack, ench, lvl); | ||
} | ||
}, itemStack); | ||
} | ||
} | ||
|
||
public static void runOnLiving(QuadEnchantmentVisitor visitor, LivingEntity living) { | ||
runOnLiving(visitor, living, EquipmentSlot.values()); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/org/auioc/mcmod/arnicalib/game/enchantment/EnchantmentRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.auioc.mcmod.arnicalib.game.enchantment; | ||
|
||
import java.util.Optional; | ||
import javax.annotation.Nonnull; | ||
import org.auioc.mcmod.arnicalib.game.registry.RegistryEntryException; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.item.enchantment.Enchantment; | ||
import net.minecraftforge.registries.ForgeRegistries; | ||
|
||
public class EnchantmentRegistry { | ||
|
||
@Nonnull | ||
public static Optional<Enchantment> get(ResourceLocation id) { | ||
return Optional.ofNullable(ForgeRegistries.ENCHANTMENTS.containsKey(id) ? ForgeRegistries.ENCHANTMENTS.getValue(id) : null); | ||
} | ||
|
||
@Nonnull | ||
public static Optional<Enchantment> get(String id) { | ||
return get(new ResourceLocation(id)); | ||
} | ||
|
||
@Nonnull | ||
public static Enchantment getOrElseThrow(ResourceLocation id) { | ||
return get(id).orElseThrow(RegistryEntryException.UNKNOWN_ENCHANTMENT.create(id.toString())); | ||
} | ||
|
||
@Nonnull | ||
public static Enchantment getOrElseThrow(String id) { | ||
return get(id).orElseThrow(RegistryEntryException.UNKNOWN_ENCHANTMENT.create(id.toString())); | ||
} | ||
|
||
} |
129 changes: 0 additions & 129 deletions
129
src/main/java/org/auioc/mcmod/arnicalib/utils/game/EnchUtils.java
This file was deleted.
Oops, something went wrong.