Skip to content

Commit

Permalink
Change EnchantmentType
Browse files Browse the repository at this point in the history
Add DefaultEnchantmentType, SimpleEnchantmentType, TableExclusionEnchantmentType And FilteredEnchantmentType
Add FilterList, BlackList And WhiteList
Change Some Mixins
Update Quilt Loader
  • Loading branch information
FirstMegaGame4 committed Aug 31, 2023
1 parent 1a8da29 commit 04ca2e1
Show file tree
Hide file tree
Showing 21 changed files with 311 additions and 51 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# The latest versions are available at https://lambdaurora.dev/tools/import_quilt.html
minecraft = "1.19.2"
quilt_mappings = "1.19.2+build.22"
quilt_loader = "0.17.6"
quilt_loader = "0.19.2"
mixin_extras = "0.2.0-beta.11"

quilted_fabric_api = "4.0.0-beta.18+0.64.0-1.19.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public List<List<Path>> getSourcePaths() {
public BasicSourceType getSourceType() {
return mod.getSourceType();
}

@Override
public ClassLoader getClassLoader() {
return mod.getClassLoader();
}
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.mmodding.mmodding_lib.library.enchantments;

import com.mmodding.mmodding_lib.library.items.settings.AdvancedItemSettings;
import com.mmodding.mmodding_lib.library.enchantments.types.EnchantmentType;
import com.mmodding.mmodding_lib.library.items.ItemRegistrable;
import com.mmodding.mmodding_lib.library.utils.TextUtils;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentLevelEntry;
import net.minecraft.item.EnchantedBookItem;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.registry.Registry;

Expand All @@ -17,20 +17,17 @@ public class CustomEnchantedBookItem extends EnchantedBookItem implements ItemRe

private final AtomicBoolean registered = new AtomicBoolean(false);

private EnchantmentType type;
private final EnchantmentType type;

public CustomEnchantedBookItem(AdvancedItemSettings settings) {
public CustomEnchantedBookItem(EnchantmentType type, Settings settings) {
super(settings);
}

public void setType(EnchantmentType type) {
this.type = type;
}

public ItemStack forCustomEnchantment(EnchantmentLevelEntry info) {
ItemStack stack = new ItemStack(this.type.bookItem());
stack.setCustomName(Text.of(this.type.prefix() + " " + stack.getName()));
addEnchantment(stack, info);
ItemStack stack = new ItemStack(this.type.getEnchantedBook());
stack.setCustomName(TextUtils.spaceBetween(this.type.getPrefix().copy(), stack.getName()));
CustomEnchantedBookItem.addEnchantment(stack, info);
return stack;
}

Expand All @@ -48,7 +45,8 @@ public void appendStacks(ItemGroup group, DefaultedList<ItemStack> stacks) {
}
}
}
} else if (group.getEnchantments().length != 0) {
}
else if (group.getEnchantments().length != 0) {
for(Enchantment enchantment : Registry.ENCHANTMENT) {
if (group.containsEnchantments(enchantment.type)) {
if (enchantment instanceof CustomEnchantment customEnchantment) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mmodding.mmodding_lib.library.enchantments;

import com.mmodding.mmodding_lib.library.enchantments.types.EnchantmentType;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.entity.EquipmentSlot;
Expand All @@ -9,6 +10,7 @@
public class CustomEnchantment extends Enchantment implements EnchantmentRegistrable {

private final AtomicBoolean registered = new AtomicBoolean(false);

private final EnchantmentType type;

public CustomEnchantment(Rarity rarity, EnchantmentTarget enchantmentTarget, EquipmentSlot... equipmentSlots) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mmodding.mmodding_lib.library.enchantments.types;

import net.minecraft.item.EnchantedBookItem;
import net.minecraft.item.Items;
import net.minecraft.text.Text;

public class DefaultEnchantmentType implements EnchantmentType {

DefaultEnchantmentType() {}

@Override
public String getQualifier() {
return "default";
}

@Override
public Text getPrefix() {
return Text.of("");
}

@Override
public EnchantedBookItem getEnchantedBook() {
return (EnchantedBookItem) Items.ENCHANTED_BOOK;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.mmodding.mmodding_lib.library.enchantments.types;

import com.mmodding.mmodding_lib.library.enchantments.CustomEnchantedBookItem;
import com.mmodding.mmodding_lib.library.items.settings.AdvancedItemSettings;
import com.mmodding.mmodding_lib.library.utils.FilterList;
import net.minecraft.item.EnchantedBookItem;
import net.minecraft.item.Item;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Rarity;

public interface EnchantmentType {

EnchantmentType DEFAULT = new DefaultEnchantmentType();

static EnchantmentType of(String qualifier, Text prefix) {
return EnchantmentType.of(qualifier, prefix, new AdvancedItemSettings().maxCount(1).rarity(Rarity.UNCOMMON));
}

static EnchantmentType of(String qualifier, Text prefix, boolean inEnchantingTable) {
return new TableExclusionEnchantmentType(qualifier, prefix, new AdvancedItemSettings().maxCount(1).rarity(Rarity.UNCOMMON), inEnchantingTable);
}

static EnchantmentType of(String qualifier, Text prefix, boolean inEnchantingTable, FilterList<EnchantmentType> filter) {
return new FilteredEnchantmentType(qualifier, prefix, new AdvancedItemSettings().maxCount(1).rarity(Rarity.UNCOMMON), inEnchantingTable, filter);
}

static EnchantmentType of(String qualifier, Text prefix, Item.Settings enchantedBookSettings) {
return new SimpleEnchantmentType(qualifier, prefix, enchantedBookSettings);
}

static EnchantmentType of(String qualifier, Text prefix, Item.Settings enchantedBookSettings, boolean inEnchantingTable) {
return new TableExclusionEnchantmentType(qualifier, prefix, enchantedBookSettings, inEnchantingTable);
}

static EnchantmentType of(String qualifier, Text prefix, Item.Settings enchantedBookSettings, boolean inEnchantingTable, FilterList<EnchantmentType> filter) {
return new FilteredEnchantmentType(qualifier, prefix, enchantedBookSettings, inEnchantingTable, filter);
}

String getQualifier();

Text getPrefix();

EnchantedBookItem getEnchantedBook();

default boolean isInEnchantingTable() {
return true;
}

default FilterList<EnchantmentType> typeCompatibilities() {
return FilterList.always();
}

default void register(Identifier identifier) {
if (this.getEnchantedBook() instanceof CustomEnchantedBookItem item) {
item.register(identifier);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mmodding.mmodding_lib.library.enchantments.types;

import com.mmodding.mmodding_lib.library.utils.FilterList;
import net.minecraft.item.Item;
import net.minecraft.text.Text;

public class FilteredEnchantmentType extends TableExclusionEnchantmentType {

private final FilterList<EnchantmentType> filterList;

FilteredEnchantmentType(String qualifier, Text prefix, Item.Settings enchantedBookSettings, boolean inEnchantingTable, FilterList<EnchantmentType> filterList) {
super(qualifier, prefix, enchantedBookSettings, inEnchantingTable);
this.filterList = filterList;
}

@Override
public FilterList<EnchantmentType> typeCompatibilities() {
return this.filterList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mmodding.mmodding_lib.library.enchantments.types;

import com.mmodding.mmodding_lib.library.enchantments.CustomEnchantedBookItem;
import net.minecraft.item.EnchantedBookItem;
import net.minecraft.item.Item;
import net.minecraft.text.Text;

public class SimpleEnchantmentType implements EnchantmentType {

private final String qualifier;
private final Text prefix;
private final EnchantedBookItem enchantedBook;

SimpleEnchantmentType(String qualifier, Text prefix, Item.Settings enchantedBookSettings) {
this.qualifier = qualifier;
this.prefix = prefix;
this.enchantedBook = new CustomEnchantedBookItem(this, enchantedBookSettings);
}

@Override
public String getQualifier() {
return this.qualifier;
}

@Override
public Text getPrefix() {
return this.prefix;
}

@Override
public EnchantedBookItem getEnchantedBook() {
return this.enchantedBook;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mmodding.mmodding_lib.library.enchantments.types;

import net.minecraft.item.Item;
import net.minecraft.text.Text;

public class TableExclusionEnchantmentType extends SimpleEnchantmentType {

private final boolean inEnchantingTable;

TableExclusionEnchantmentType(String qualifier, Text prefix, Item.Settings enchantedBookSettings, boolean inEnchantingTable) {
super(qualifier, prefix, enchantedBookSettings);
this.inEnchantingTable = inEnchantingTable;
}

@Override
public boolean isInEnchantingTable() {
return this.inEnchantingTable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public FluidGroup(CustomFluid still, CustomFluid flowing, AbstractBlock.Settings
this.still.getDefaultState().with(FlowableFluid.FALLING, Boolean.FALSE),
value -> this.flowing.getDefaultState().with(FlowableFluid.LEVEL, value == 8 ? 8 : 8 - value).with(FlowableFluid.FALLING, value == 8 ? Boolean.TRUE : Boolean.FALSE)
);
this.bucket = new CustomBucketItem(this.still, itemSettings);
this.bucket = itemSettings != null ? new CustomBucketItem(this.still, itemSettings) : null;
}

public CustomFluid getStill() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mmodding.mmodding_lib.library.utils;

import java.util.List;

public class BlackList<E> implements FilterList<E> {

private final List<E> elements;

@SafeVarargs
public BlackList(E... elements) {
this.elements = List.of(elements);
}

@Override
public boolean check(E element) {
return !this.elements.contains(element);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mmodding.mmodding_lib.library.utils;

public interface FilterList<E> {

static <E> FilterList<E> always() {
return element -> true;
}

static <E> FilterList<E> never() {
return element -> false;
}

boolean check(E element);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mmodding.mmodding_lib.library.utils;

import java.util.List;

public class WhiteList<E> implements FilterList<E> {

private final List<E> elements;

@SafeVarargs
public WhiteList(E... elements) {
this.elements = List.of(elements);
}

@Override
public boolean check(E element) {
return this.elements.contains(element);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.mmodding.mmodding_lib.mixin.injectors;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mmodding.mmodding_lib.library.enchantments.CustomEnchantedBookItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.AnvilScreenHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(AnvilScreenHandler.class)
public class AnvilScreenHandlerMixin {

@WrapOperation(method = "updateResult", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isOf(Lnet/minecraft/item/Item;)Z", ordinal = 0))
private boolean updateResultFirst(ItemStack stack, Item item, Operation<Boolean> original) {
return stack.getItem() instanceof CustomEnchantedBookItem || original.call(stack, item);
}

@WrapOperation(method = "updateResult", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isOf(Lnet/minecraft/item/Item;)Z", ordinal = 2))
private boolean updateResultSecond(ItemStack stack, Item item, Operation<Boolean> original) {
return stack.getItem() instanceof CustomEnchantedBookItem || original.call(stack, item);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mmodding.mmodding_lib.mixin.injectors;

import com.google.common.collect.Iterators;
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.mmodding.mmodding_lib.library.enchantments.CustomEnchantment;
import com.mmodding.mmodding_lib.library.enchantments.types.EnchantmentType;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.item.EnchantedBookItem;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.util.Iterator;

@Mixin(EnchantedBookItem.class)
public class EnchantedBookItemMixin {

@ModifyExpressionValue(method = "appendStacks", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/registry/Registry;iterator()Ljava/util/Iterator;"))
private Iterator<Enchantment> appendStacks(Iterator<Enchantment> original) {
return Iterators.filter(
original, enchantment -> !(enchantment instanceof CustomEnchantment customEnchantment) || customEnchantment.getType() == EnchantmentType.DEFAULT
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class EnchantmentHelperMixin {
@Inject(method = "getPossibleEntries", at = @At(value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z", shift = At.Shift.AFTER), locals = LocalCapture.CAPTURE_FAILEXCEPTION, cancellable = true)
private static void getPossibleEntries(int power, ItemStack stack, boolean treasureAllowed, CallbackInfoReturnable<List<EnchantmentLevelEntry>> cir, List<EnchantmentLevelEntry> list, Item item, boolean bl, Iterator<Enchantment> iterator, Enchantment enchantment, int i) {
if (enchantment instanceof CustomEnchantment customEnchantment) {
if (!customEnchantment.getType().inEnchantingTable()) {
if (!customEnchantment.getType().isInEnchantingTable()) {
if (iterator.hasNext()) iterator.next(); cir.setReturnValue(list);
}
}
Expand Down
Loading

0 comments on commit 04ca2e1

Please sign in to comment.