Skip to content

Commit

Permalink
Add AdvancedItemSettings#NAME_FORMATTINGS
Browse files Browse the repository at this point in the history
Modify CustomEnchantment
  • Loading branch information
FirstMegaGame4 committed Sep 10, 2023
1 parent 41c64e0 commit 46b764d
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

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 Down Expand Up @@ -59,11 +57,6 @@ else if (group.getEnchantments().length != 0) {
}
}

@Override
public Text getName() {
return this.type.getPrefix().isSpaced() ? TextUtils.spaceBetween(this.type.getPrefix(), super.getName()) : type.getPrefix().append(super.getName());
}

@Override
public boolean isNotRegistered() {
return !this.registered.get();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.mmodding.mmodding_lib.library.enchantments;

import com.mmodding.mmodding_lib.library.enchantments.types.EnchantmentType;
import com.mmodding.mmodding_lib.library.utils.TextUtils;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -26,6 +30,21 @@ public EnchantmentType getType() {
return this.type;
}

@Override
public Text getName(int level) {
MutableText enchantment = Text.translatable(this.getTranslationKey());

if (this.isCursed()) {
enchantment.formatted(Formatting.RED);
}

if (level != 1 || this.getMaxLevel() != 1) {
TextUtils.spaceBetween(enchantment, Text.translatable("enchantment.level." + level));
}

return this.type.getPrefix().isSpaced() ? TextUtils.spaceBetween(this.type.getPrefix().copy(), enchantment) : this.type.getPrefix().copy().append(enchantment);
}

@Override
public boolean isNotRegistered() {
return !this.registered.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

public class FilteredEnchantmentType extends TableExclusionEnchantmentType {

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

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

public class TableExclusionEnchantmentType extends SimpleEnchantmentType {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Rarity;
import org.quiltmc.qsl.item.setting.api.*;

import java.util.Collections;
import java.util.List;

public class AdvancedItemSettings extends QuiltItemSettings {

public static final CustomItemSetting<List<Formatting>> NAME_FORMATTINGS = CustomItemSetting.create(Collections::emptyList);
public static final CustomItemSetting<Text[]> DESCRIPTION_LINES = CustomItemSetting.create(() -> null);
public static final CustomItemSetting<Boolean> GLINT = CustomItemSetting.create(Boolean.FALSE);
public static final CustomItemSetting<GlintPackView> GLINT_PACK = CustomItemSetting.create(() -> null);
Expand All @@ -24,6 +29,10 @@ public class AdvancedItemSettings extends QuiltItemSettings {
public static final CustomItemSetting<ItemPostHit> ITEM_POST_HIT = CustomItemSetting.create(() -> null);
public static final CustomItemSetting<ItemPostMine> ITEM_POST_MINE = CustomItemSetting.create(() -> null);

public AdvancedItemSettings nameFormattings(Formatting... formattings) {
return this.customSetting(NAME_FORMATTINGS, List.of(formattings));
}

public AdvancedItemSettings descriptionLines(Text... descriptionLines) {
return this.customSetting(DESCRIPTION_LINES, descriptionLines);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mmodding.mmodding_lib.library.utils;

/**
* A utility interface that allow fast access to the real object for mixin classes.
* A utility interface that allows fast access to the real object for mixin classes.
* @param <T> the real object type
*/
public interface Self<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import com.mmodding.mmodding_lib.library.portals.Ignition;
import com.mmodding.mmodding_lib.library.portals.squared.AbstractSquaredPortal;
import com.mmodding.mmodding_lib.library.utils.MModdingGlobalMaps;
import com.mmodding.mmodding_lib.library.utils.Self;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.sound.SoundCategory;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
Expand All @@ -29,29 +32,29 @@
import java.util.Optional;

@Mixin(Item.class)
public abstract class ItemMixin implements ItemGlintPack {
public abstract class ItemMixin implements ItemGlintPack, Self<Item> {

@Shadow
public abstract boolean isFood();

@Inject(method = "hasGlint", at = @At("TAIL"), cancellable = true)
private void hasGlint(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
if (AdvancedItemSettings.GLINT.get((Item) (Object) this)) {
if (AdvancedItemSettings.GLINT.get(this.getObject())) {
cir.setReturnValue(true);
}
}

@Inject(method = "use", at = @At("HEAD"))
private void use(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> cir) {
ItemUse itemUse = AdvancedItemSettings.ITEM_USE.get((Item) (Object) this);
ItemUse itemUse = AdvancedItemSettings.ITEM_USE.get(this.getObject());
if (itemUse != null) {
itemUse.apply(world, user, hand);
}
}

@Inject(method = "finishUsing", at = @At("HEAD"), cancellable = true)
private void finishUsing(ItemStack stack, World world, LivingEntity user, CallbackInfoReturnable<ItemStack> cir) {
ItemFinishUsing itemFinishUsing = AdvancedItemSettings.ITEM_FINISH_USING.get((Item) (Object) this);
ItemFinishUsing itemFinishUsing = AdvancedItemSettings.ITEM_FINISH_USING.get(this.getObject());
if (itemFinishUsing != null) {
cir.setReturnValue(itemFinishUsing.apply(this.isFood() ? user.eatFood(world, stack) : stack, world, user));
}
Expand Down Expand Up @@ -85,43 +88,43 @@ private void useOnBlock(ItemUsageContext context, CallbackInfoReturnable<ActionR
}
}

ItemUseOnBlock itemUseOnBlock = AdvancedItemSettings.ITEM_USE_ON_BLOCK.get((Item) (Object) this);
ItemUseOnBlock itemUseOnBlock = AdvancedItemSettings.ITEM_USE_ON_BLOCK.get(this.getObject());
if (itemUseOnBlock != null) {
itemUseOnBlock.apply(context);
}
}

@Inject(method = "useOnEntity", at = @At("HEAD"))
private void useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
ItemUseOnEntity itemUseOnEntity = AdvancedItemSettings.ITEM_USE_ON_ENTITY.get((Item) (Object) this);
ItemUseOnEntity itemUseOnEntity = AdvancedItemSettings.ITEM_USE_ON_ENTITY.get(this.getObject());
if (itemUseOnEntity != null) {
itemUseOnEntity.apply(stack, user, entity, hand);
}
}

@Inject(method = "postHit", at = @At("TAIL"))
private void postHit(ItemStack stack, LivingEntity target, LivingEntity attacker, CallbackInfoReturnable<Boolean> cir) {
ItemPostHit itemPostHit = AdvancedItemSettings.ITEM_POST_HIT.get((Item) (Object) this);
ItemPostHit itemPostHit = AdvancedItemSettings.ITEM_POST_HIT.get(this.getObject());
if (itemPostHit != null) itemPostHit.apply(stack, target, attacker);
}

@Inject(method = "postMine", at = @At("TAIL"))
private void postMine(ItemStack stack, World world, BlockState state, BlockPos pos, LivingEntity miner, CallbackInfoReturnable<Boolean> cir) {
ItemPostMine itemPostMine = AdvancedItemSettings.ITEM_POST_MINE.get((Item) (Object) this);
ItemPostMine itemPostMine = AdvancedItemSettings.ITEM_POST_MINE.get(this.getObject());
if (itemPostMine != null) itemPostMine.apply(stack, world, state, pos, miner);
}

@Inject(method = "getUseAction", at = @At("TAIL"), cancellable = true)
private void getUseAction(ItemStack stack, CallbackInfoReturnable<UseAction> cir) {
boolean eatable = AdvancedItemSettings.EATABLE.get((Item) (Object) this);
boolean eatable = AdvancedItemSettings.EATABLE.get(this.getObject());
if (eatable) cir.setReturnValue(UseAction.EAT);
boolean drinkable = AdvancedItemSettings.DRINKABLE.get((Item) (Object) this);
boolean drinkable = AdvancedItemSettings.DRINKABLE.get(this.getObject());
if (drinkable) cir.setReturnValue(UseAction.DRINK);
}

@Nullable
@Override
public GlintPackView getGlintPackView() {
return AdvancedItemSettings.GLINT_PACK.get((Item) (Object) this);
return AdvancedItemSettings.GLINT_PACK.get(this.getObject());
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
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.glint.GlintPackView;
import com.mmodding.mmodding_lib.interface_injections.ItemGlintPack;
import com.mmodding.mmodding_lib.library.items.settings.AdvancedItemSettings;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.MutableText;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;

import java.util.List;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin implements ItemGlintPack {

@Shadow
public abstract Item getItem();

@WrapOperation(method = "getTooltip", at = @At(value = "INVOKE", target = "Lnet/minecraft/text/MutableText;formatted(Lnet/minecraft/util/Formatting;)Lnet/minecraft/text/MutableText;", ordinal = 0))
private MutableText getTooltip(MutableText mutableText, Formatting formatting, Operation<MutableText> original) {
List<Formatting> formattings = AdvancedItemSettings.NAME_FORMATTINGS.get(this.getItem());
if (!formattings.isEmpty()) {
formattings.forEach(mutableText::formatted);
return mutableText;
}
else {
return original.call(mutableText, formatting);
}
}

@WrapOperation(method = "toHoverableText", at = @At(value = "INVOKE", target = "Lnet/minecraft/text/MutableText;formatted(Lnet/minecraft/util/Formatting;)Lnet/minecraft/text/MutableText;", ordinal = 1))
private MutableText toHoverableText(MutableText mutableText, Formatting formatting, Operation<MutableText> original) {
List<Formatting> formattings = AdvancedItemSettings.NAME_FORMATTINGS.get(this.getItem());
if (!formattings.isEmpty()) {
formattings.forEach(mutableText::formatted);
return mutableText;
}
else {
return original.call(mutableText, formatting);
}
}

@Nullable
@Override
public GlintPackView getGlintPackView() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.mmodding.mmodding_lib.mixin.injectors.client;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mmodding.mmodding_lib.ducks.EntityDuckInterface;
import com.mmodding.mmodding_lib.library.items.settings.AdvancedItemSettings;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.Tessellator;
Expand All @@ -9,13 +12,18 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.texture.Sprite;
import net.minecraft.item.ItemStack;
import net.minecraft.text.MutableText;
import net.minecraft.util.Formatting;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
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;

import java.util.List;

@Mixin(InGameHud.class)
public class InGameHudMixin {

Expand All @@ -29,6 +37,21 @@ public class InGameHudMixin {
@Final
private MinecraftClient client;

@Shadow
private ItemStack currentStack;

@WrapOperation(method = "renderHeldItemTooltip", at = @At(value = "INVOKE", target = "Lnet/minecraft/text/MutableText;formatted(Lnet/minecraft/util/Formatting;)Lnet/minecraft/text/MutableText;", ordinal = 0))
private MutableText renderHeldItemTooltip(MutableText mutableText, Formatting formatting, Operation<MutableText> original) {
List<Formatting> formattings = AdvancedItemSettings.NAME_FORMATTINGS.get(this.currentStack.getItem());
if (!formattings.isEmpty()) {
formattings.forEach(mutableText::formatted);
return mutableText;
}
else {
return original.call(mutableText, formatting);
}
}

@Inject(method = "renderPortalOverlay", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/block/BlockModels;getModelParticleSprite(Lnet/minecraft/block/BlockState;)Lnet/minecraft/client/texture/Sprite;"), cancellable = true)
private void renderPortalOverlay(float nauseaStrength, CallbackInfo ci) {
assert client.player != null;
Expand Down

0 comments on commit 46b764d

Please sign in to comment.