Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Oct 15, 2024
2 parents e218e32 + cd40b81 commit 1aa30ba
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
public class ClientModelPredicates {

public static void register() {
ModelPredicateProviderRegistry.register(
new MModdingIdentifier("broken"),
(stack, world, entity, seed) -> stack.getDamage() >= stack.getMaxDamage() ? 1.0f : 0.0f
);
ModelPredicateProviderRegistry.register(
new MModdingIdentifier("throwing"),
(stack, world, livingEntity, i) -> livingEntity != null && livingEntity.isUsingItem() && livingEntity.getActiveItem() == stack ? 1.0f : 0.0f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class AdvancedItemSettings extends QuiltItemSettings {
public static final CustomItemSetting<GlintPackView> GLINT_PACK = CustomItemSetting.create(() -> null);
public static final CustomItemSetting<Boolean> EATABLE = CustomItemSetting.create(Boolean.FALSE);
public static final CustomItemSetting<Boolean> DRINKABLE = CustomItemSetting.create(Boolean.FALSE);
public static final CustomItemSetting<Boolean> HAS_BROKEN_STATE = CustomItemSetting.create(Boolean.FALSE);
public static final CustomItemSetting<ItemUse> ITEM_USE = CustomItemSetting.create(() -> null);
public static final CustomItemSetting<ItemFinishUsing> ITEM_FINISH_USING = CustomItemSetting.create(() -> null);
public static final CustomItemSetting<ItemUseOnBlock> ITEM_USE_ON_BLOCK = CustomItemSetting.create(() -> null);
Expand Down Expand Up @@ -63,6 +64,10 @@ public AdvancedItemSettings drinkable() {
return this.customSetting(DRINKABLE, true);
}

public AdvancedItemSettings hasBrokenState() {
return this.customSetting(HAS_BROKEN_STATE, true);
}

public AdvancedItemSettings itemUse(ItemUse itemUseSetting) {
return this.customSetting(ITEM_USE, itemUseSetting);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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.fabricmc.fabric.api.item.v1.FabricItem;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -21,14 +22,15 @@
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Optional;

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

@Shadow
public abstract boolean isFood();
Expand Down Expand Up @@ -117,4 +119,14 @@ private void getUseAction(ItemStack stack, CallbackInfoReturnable<UseAction> cir
boolean drinkable = AdvancedItemSettings.DRINKABLE.get(this.getObject());
if (drinkable) cir.setReturnValue(UseAction.DRINK);
}

@Unique
protected boolean isBroken(ItemStack stack) {
if (AdvancedItemSettings.HAS_BROKEN_STATE.get(this.getObject())) {
return stack.getDamage() >= stack.getMaxDamage();
}
else {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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.items.settings.AdvancedItemSettings;
import com.mmodding.mmodding_lib.library.items.settings.ItemPostHit;
import com.mmodding.mmodding_lib.library.items.settings.ItemPostMine;
Expand All @@ -14,18 +16,57 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.function.Consumer;

@Mixin(MiningToolItem.class)
public class MiningToolItemMixin {
public abstract class MiningToolItemMixin extends ItemMixin {

@Inject(method = "getMiningSpeedMultiplier", at = @At(value = "HEAD"), cancellable = true)
private void cancelIfBroken(ItemStack stack, BlockState state, CallbackInfoReturnable<Float> cir) {
if (this.isBroken(stack)) {
cir.setReturnValue(1.0f);
}
}

@WrapOperation(method = "postHit", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;damage(ILnet/minecraft/entity/LivingEntity;Ljava/util/function/Consumer;)V"))
private void cancelHitStackDamageIfBroken(ItemStack instance, int amount, LivingEntity entity, Consumer<LivingEntity> breakCallback, Operation<Void> original) {
if (AdvancedItemSettings.HAS_BROKEN_STATE.get(this.getObject())) {
instance.setDamage(Math.min(instance.getDamage() + amount, instance.getMaxDamage()));
}
else {
original.call(instance, amount, entity, breakCallback);
}
}

@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((MiningToolItem) (Object) this);
if (itemPostHit != null) itemPostHit.apply(stack, target, attacker);
}

@WrapOperation(method = "postMine", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;damage(ILnet/minecraft/entity/LivingEntity;Ljava/util/function/Consumer;)V"))
private void cancelMineStackDamageIfBroken(ItemStack instance, int amount, LivingEntity entity, Consumer<LivingEntity> breakCallback, Operation<Void> original) {
if (AdvancedItemSettings.HAS_BROKEN_STATE.get(this.getObject())) {
instance.setDamage(Math.min(instance.getDamage() + amount, instance.getMaxDamage()));
}
else {
original.call(instance, amount, entity, breakCallback);
}
}

@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((MiningToolItem) (Object) this);
if (itemPostMine != null) itemPostMine.apply(stack, world, state, pos, miner);
}

@Override
public boolean isSuitableFor(ItemStack stack, BlockState state) {
if (this.isBroken(stack)) {
return false;
}
else {
return super.isSuitableFor(stack, state);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.mmodding.mmodding_lib.mixin.injectors;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mmodding.mmodding_lib.library.items.settings.AdvancedItemSettings;
import com.mmodding.mmodding_lib.library.items.settings.ItemPostHit;
import com.mmodding.mmodding_lib.library.items.settings.ItemPostMine;
import net.minecraft.block.BlockState;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;
import net.minecraft.util.math.BlockPos;
Expand All @@ -14,18 +21,67 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.function.Consumer;

@Mixin(SwordItem.class)
public class SwordItemMixin {
public abstract class SwordItemMixin extends ItemMixin {

@Inject(method = "getMiningSpeedMultiplier", at = @At(value = "HEAD"), cancellable = true)
private void cancelIfBroken(ItemStack stack, BlockState state, CallbackInfoReturnable<Float> cir) {
if (this.isBroken(stack)) {
cir.setReturnValue(1.0f);
}
}

@WrapOperation(method = "postHit", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;damage(ILnet/minecraft/entity/LivingEntity;Ljava/util/function/Consumer;)V"))
private void cancelHitStackDamageIfBroken(ItemStack instance, int amount, LivingEntity entity, Consumer<LivingEntity> breakCallback, Operation<Void> original) {
if (AdvancedItemSettings.HAS_BROKEN_STATE.get(this.getObject())) {
instance.setDamage(Math.min(instance.getDamage() + amount, instance.getMaxDamage()));
}
else {
original.call(instance, amount, entity, breakCallback);
}
}

@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((SwordItem) (Object) this);
if (itemPostHit != null) itemPostHit.apply(stack, target, attacker);
}

@WrapOperation(method = "postMine", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;damage(ILnet/minecraft/entity/LivingEntity;Ljava/util/function/Consumer;)V"))
private void cancelMineStackDamageIfBroken(ItemStack instance, int amount, LivingEntity entity, Consumer<LivingEntity> breakCallback, Operation<Void> original) {
if (AdvancedItemSettings.HAS_BROKEN_STATE.get(this.getObject())) {
instance.setDamage(Math.min(instance.getDamage() + amount, instance.getMaxDamage()));
}
else {
original.call(instance, amount, entity, breakCallback);
}
}

@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((SwordItem) (Object) this);
if (itemPostMine != null) itemPostMine.apply(stack, world, state, pos, miner);
}

@Override
public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(ItemStack stack, EquipmentSlot slot) {
if (this.isBroken(stack)) {
return ImmutableMultimap.of();
}
else {
return super.getAttributeModifiers(stack, slot);
}
}

@Override
public boolean isSuitableFor(ItemStack stack, BlockState state) {
if (this.isBroken(stack)) {
return false;
}
else {
return super.isSuitableFor(stack, state);
}
}
}

0 comments on commit 1aa30ba

Please sign in to comment.