Skip to content

Commit

Permalink
Porting mixins sucks. Injected interfaces are neat though
Browse files Browse the repository at this point in the history
  • Loading branch information
Electro593 committed Jan 24, 2025
1 parent a9e65c7 commit 8f0be7c
Show file tree
Hide file tree
Showing 40 changed files with 300 additions and 371 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package de.dafuqs.spectrum.api.energy;

import com.google.common.collect.*;
import de.dafuqs.spectrum.api.status_effect.*;
import de.dafuqs.spectrum.helpers.*;
import net.minecraft.entity.attribute.*;
import net.minecraft.entity.effect.*;
import net.minecraft.item.*;
import net.minecraft.nbt.*;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.*;
import net.minecraft.text.*;
import net.minecraft.util.*;
import org.jetbrains.annotations.*;
Expand All @@ -33,9 +32,7 @@ public InkPoweredStatusEffectInstance(StatusEffectInstance statusEffectInstance,
this.customColor = customColor;
this.unidentifiable = unidentifiable;
this.incurable = incurable;

if (incurable)
((Incurable) statusEffectInstance).spectrum$setIncurable(true);
if (incurable) statusEffectInstance.spectrum$setIncurable(true);
}

public StatusEffectInstance getStatusEffectInstance() {
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/de/dafuqs/spectrum/api/item/FermentedItem.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package de.dafuqs.spectrum.api.item;

import de.dafuqs.spectrum.registries.*;
import net.minecraft.item.*;
import net.minecraft.nbt.*;
import net.minecraft.util.*;

public interface FermentedItem {

static boolean isPreviewStack(ItemStack stack) {
NbtCompound nbtCompound = stack.getNbt();
return nbtCompound != null && nbtCompound.getBoolean("Preview");
return stack.contains(SpectrumDataComponentTypes.IS_PREVIEW_ITEM);
}

static void setPreviewStack(ItemStack stack) {
NbtCompound compound = stack.getOrCreateNbt();
compound.putBoolean("Preview", true);
stack.setNbt(compound);
stack.set(SpectrumDataComponentTypes.IS_PREVIEW_ITEM, Unit.INSTANCE);
}

}
20 changes: 0 additions & 20 deletions src/main/java/de/dafuqs/spectrum/api/status_effect/Incurable.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> to
super.appendTooltip(stack, context, tooltip, type);
tooltip.add(Text.translatable("block.spectrum.incandescent_amalgam.tooltip").formatted(Formatting.GRAY));
tooltip.add(Text.translatable("block.spectrum.incandescent_amalgam.tooltip_power", getExplosionPower(stack, false)).formatted(Formatting.GRAY));
if (FermentedItem.isPreviewStack(stack)) {
if (FermentedItem.isPreviewStack(stack))
tooltip.add(Text.translatable("block.spectrum.incandescent_amalgam.tooltip.preview").formatted(Formatting.GRAY));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.dafuqs.spectrum.blocks.fluid;

import de.dafuqs.spectrum.mixin.accessors.*;
import de.dafuqs.spectrum.particle.*;
import de.dafuqs.spectrum.recipe.fluid_converting.*;
import de.dafuqs.spectrum.registries.*;
Expand Down Expand Up @@ -122,7 +121,7 @@ public void onEntityCollision(BlockState state, World world, BlockPos pos, Entit
livingEntity.addStatusEffect(new StatusEffectInstance(SpectrumStatusEffects.LIFE_DRAIN, 600, 0));
}
else if(existingEffect.getDuration() < 500) {
((StatusEffectInstanceAccessor) existingEffect).setDuration(300);
existingEffect.spectrum$setDuration(300);

serverWorld.getChunkManager().sendToNearbyPlayers(livingEntity, new EntityStatusEffectS2CPacket(livingEntity.getId(), existingEffect, true));
}
Expand All @@ -137,7 +136,7 @@ else if(existingEffect.getDuration() < 500) {
if (existingEffect.getDuration() <= cut) {
livingEntity.removeStatusEffect(SpectrumStatusEffects.IMMUNITY);
} else {
((StatusEffectInstanceAccessor) existingEffect).setDuration(existingEffect.getDuration() - cut);
existingEffect.spectrum$setDuration(existingEffect.getDuration() - cut);
serverWorld.getChunkManager().sendToNearbyPlayers(livingEntity, new EntityStatusEffectS2CPacket(livingEntity.getId(), existingEffect, true));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void recall() {
yoink(owner, getPos(), 0.125, 0.165);
}

if (EnchantmentHelper.getLevel(Enchantments.CHANNELING, getTrackedStack()) > 0 && owner != null) {
if (EnchantmentHelper.hasEnchantment(Enchantments.CHANNELING, getTrackedStack()) && owner != null) {
if (!getWorld().isClient()) {
var world = (ServerWorld) getWorld();
for (int i = 0; i < 10; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static <T extends RegistryEntry<Enchantment>> Pair<ItemStack, Integer> re
var removals = new AtomicInteger(0);
var builder = new ItemEnchantmentsComponent.Builder(EnchantmentHelper.getEnchantments(itemStack));
enchantments.forEach(enchantment -> {
if (builder.getLevel(enchantment) > 0) {
if (builder.hasEnchantment(enchantment)) {
builder.set(enchantment, 0);
removals.getAndIncrement();
}
Expand Down Expand Up @@ -201,6 +201,10 @@ public static int getLevel(RegistryWrapper.WrapperLookup registryLookup, Registr
.orElse(0);
}

public static boolean hasEnchantment(RegistryWrapper.WrapperLookup registryLookup, RegistryKey<Enchantment> enchantment, ItemStack stack) {
return getLevel(registryLookup, enchantment, stack) > 0;
}

public static Optional<RegistryWrapper.Impl<Enchantment>> getRegistry(RegistryWrapper.WrapperLookup registryLookup) {
return registryLookup.getOptionalWrapper(RegistryKeys.ENCHANTMENT);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.dafuqs.spectrum.helpers;

import de.dafuqs.spectrum.*;
import de.dafuqs.spectrum.api.status_effect.*;
import de.dafuqs.spectrum.mixin.injectors.*;
import de.dafuqs.spectrum.registries.*;
import net.minecraft.entity.effect.*;
import net.minecraft.util.*;
Expand All @@ -18,7 +18,7 @@ public static Identifier getTexture(Identifier texture, StatusEffectInstance eff
if (type == SpectrumStatusEffects.DIVINITY)
return DIVINITY_EFFECT_BACKGROUNDS;

if (Incurable.isIncurable(effect) && type != SpectrumStatusEffects.ETERNAL_SLUMBER && type != SpectrumStatusEffects.FATAL_SLUMBER) {
if (StatusEffectInstanceInjector.isIncurable(effect) && type != SpectrumStatusEffects.ETERNAL_SLUMBER && type != SpectrumStatusEffects.FATAL_SLUMBER) {
return INCURABLE_EFFECT_BACKGROUNDS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class ExuberanceHelper {

public static float getExuberanceMod(PlayerEntity breakingPlayer) {
if (breakingPlayer != null && EnchantmentHelper.getLevel(SpectrumEnchantments.EXUBERANCE, breakingPlayer.getMainHandStack()) > 0) {
if (breakingPlayer != null && EnchantmentHelper.hasEnchantment(SpectrumEnchantments.EXUBERANCE, breakingPlayer.getMainHandStack())) {
int exuberanceLevel = EnchantmentHelper.getEquipmentLevel(SpectrumEnchantments.EXUBERANCE, breakingPlayer);
return getExuberanceMod(exuberanceLevel);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class InexorableHelper {

public static void checkAndRemoveSlowdownModifiers(LivingEntity entity) {
var armorInexorable = isArmorActive(entity);
var toolInexorable = SpectrumEnchantmentHelper.getLevel(entity.getWorld().getRegistryManager(), SpectrumEnchantments.INEXORABLE, entity.getStackInHand(entity.getActiveHand())) > 0;
var toolInexorable = SpectrumEnchantmentHelper.hasEnchantment(entity.getWorld().getRegistryManager(), SpectrumEnchantments.INEXORABLE, entity.getStackInHand(entity.getActiveHand()));

var armorAttributes = Registries.ATTRIBUTE.getEntryList(SpectrumAttributeTags.INEXORABLE_ARMOR_EFFECTIVE);
var toolAttributes = Registries.ATTRIBUTE.getEntryList(SpectrumAttributeTags.INEXORABLE_HANDHELD_EFFECTIVE);
Expand Down Expand Up @@ -52,6 +52,6 @@ public static void checkAndRemoveSlowdownModifiers(LivingEntity entity) {
}

public static boolean isArmorActive(LivingEntity entity) {
return SpectrumEnchantmentHelper.getLevel(entity.getWorld().getRegistryManager(), SpectrumEnchantments.INEXORABLE, entity.getEquippedStack(EquipmentSlot.CHEST)) > 0;
return SpectrumEnchantmentHelper.hasEnchantment(entity.getWorld().getRegistryManager(), SpectrumEnchantments.INEXORABLE, entity.getEquippedStack(EquipmentSlot.CHEST));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public WorkstaffScreen(WorkstaffScreenHandler handler, PlayerInventory playerInv
GridEntry.item(Items.ENCHANTED_BOOK, Text.translatable("item.spectrum.workstaff.gui.enchantment_group"), (screen) -> screen.selectGrid(ENCHANTMENT_GRID))
));
} else {
GridEntry enchantmentEntry = EnchantmentHelper.getLevel(Enchantments.FORTUNE, mainHandStack) > 0
GridEntry enchantmentEntry = EnchantmentHelper.hasEnchantment(Enchantments.FORTUNE, mainHandStack)
? GridEntry.item(Items.FEATHER, Text.translatable("item.spectrum.workstaff.gui.silk_touch"), (screen) -> WorkstaffScreen.select(WorkstaffItem.GUIToggle.SELECT_SILK_TOUCH))
: GridEntry.item(SpectrumBlocks.FOUR_LEAF_CLOVER.asItem(), Text.translatable("item.spectrum.workstaff.gui.fortune"), (screen) -> WorkstaffScreen.select(WorkstaffItem.GUIToggle.SELECT_FORTUNE));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.minecraft.entity.*;
import net.minecraft.entity.player.*;
import net.minecraft.item.*;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.item.tooltip.*;
import net.minecraft.sound.*;
import net.minecraft.text.*;
import net.minecraft.util.*;
Expand Down Expand Up @@ -75,9 +75,9 @@ public SlotBackgroundEffectProvider.SlotEffect backgroundType(@Nullable PlayerEn
public int getBackgroundColor(@Nullable PlayerEntity player, ItemStack stack, float tickDelta) {
if (player != null) {
var lookup = player.getWorld().getRegistryManager();
var resonance = SpectrumEnchantmentHelper.getLevel(lookup, SpectrumEnchantments.RESONANCE, stack) > 0;
var silkTouch = SpectrumEnchantmentHelper.getLevel(lookup, Enchantments.SILK_TOUCH, stack) > 0;
var fortune = SpectrumEnchantmentHelper.getLevel(lookup, Enchantments.FORTUNE, stack) > 0;
var resonance = SpectrumEnchantmentHelper.hasEnchantment(lookup, SpectrumEnchantments.RESONANCE, stack);
var silkTouch = SpectrumEnchantmentHelper.hasEnchantment(lookup, Enchantments.SILK_TOUCH, stack);
var fortune = SpectrumEnchantmentHelper.hasEnchantment(lookup, Enchantments.FORTUNE, stack);

if (resonance)
return InkColors.WHITE_COLOR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import de.dafuqs.spectrum.api.energy.color.*;
import de.dafuqs.spectrum.api.render.*;
import de.dafuqs.spectrum.api.status_effect.*;
import de.dafuqs.spectrum.registries.*;
import de.dafuqs.spectrum.status_effects.*;
import net.minecraft.enchantment.*;
Expand Down Expand Up @@ -83,7 +82,7 @@ else if (SpectrumStatusEffectTags.hasEffectWithTag(target, SpectrumStatusEffectT
var stolenEffect = target.getStatusEffects()
.stream()
.filter(instance -> instance.getEffectType().value().isBeneficial())
.filter(instance -> !((Incurable) instance).spectrum$isIncurable())
.filter(instance -> !instance.spectrum$isIncurable())
.findFirst();

if (stolenEffect.isEmpty() || !target.removeStatusEffect(stolenEffect.get().getEffectType()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
int exuberanceLevel = SpectrumEnchantmentHelper.getLevel(drm, SpectrumEnchantments.EXUBERANCE, itemStack);
int bigCatchLevel = SpectrumEnchantmentHelper.getLevel(drm, SpectrumEnchantments.BIG_CATCH, itemStack);
int serendipityReelLevel = SpectrumEnchantmentHelper.getLevel(drm, SpectrumEnchantments.SERENDIPITY_REEL, itemStack);
boolean inventoryInsertion = SpectrumEnchantmentHelper.getLevel(drm, SpectrumEnchantments.INVENTORY_INSERTION, itemStack) > 0;
boolean inventoryInsertion = SpectrumEnchantmentHelper.hasEnchantment(drm, SpectrumEnchantments.INVENTORY_INSERTION, itemStack);
boolean shouldSmeltDrops = shouldSmeltDrops(itemStack);
spawnBobber(user, world, luckOfTheSeaLevel, lureLevel, exuberanceLevel, bigCatchLevel, serendipityReelLevel, inventoryInsertion, shouldSmeltDrops);
}
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/de/dafuqs/spectrum/mixin/CrossbowItemMixin.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package de.dafuqs.spectrum.mixin;

import de.dafuqs.spectrum.helpers.SpectrumEnchantmentHelper;
import de.dafuqs.spectrum.*;
import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.registries.*;
import net.minecraft.enchantment.*;
import net.minecraft.entity.*;
import net.minecraft.item.*;
import net.minecraft.util.*;
import net.minecraft.world.*;
import org.jetbrains.annotations.*;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.*;

@Mixin(CrossbowItem.class)
public abstract class CrossbowItemMixin {

@Inject(method = "getSpeed(Lnet/minecraft/item/ItemStack;)F", at = @At("RETURN"), cancellable = true)
private static void getSpeed(ItemStack stack, CallbackInfoReturnable<Float> cir) {
int snipingLevel = SpectrumEnchantmentHelper.getLevel(drm, SpectrumEnchantments.SNIPING, stack);
if (snipingLevel > 0) {
cir.setReturnValue(cir.getReturnValue() + 1.0F * snipingLevel);
}
@ModifyArg(method = "use(Lnet/minecraft/world/World;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/Hand;)Lnet/minecraft/util/TypedActionResult;", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/CrossbowItem;shootAll(Lnet/minecraft/world/World;Lnet/minecraft/entity/LivingEntity;Lnet/minecraft/util/Hand;Lnet/minecraft/item/ItemStack;FFLnet/minecraft/entity/LivingEntity;)V"), index = 4)
private float spectrum$applySnipingSpeed(World world, LivingEntity shooter, Hand hand, ItemStack stack, float speed, float divergence, @Nullable LivingEntity target) {
return SpectrumCommon.getRegistryLookup()
.map(lookup -> SpectrumEnchantmentHelper.getLevel(lookup, SpectrumEnchantments.SNIPING, stack))
.map(level -> speed * 1.0f * level)
.orElse(speed);
}

}
31 changes: 13 additions & 18 deletions src/main/java/de/dafuqs/spectrum/mixin/EffectCommandMixin.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package de.dafuqs.spectrum.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import de.dafuqs.spectrum.api.status_effect.Incurable;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.command.EffectCommand;
import net.minecraft.server.command.ServerCommandSource;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import com.llamalad7.mixinextras.sugar.*;
import net.minecraft.entity.*;
import net.minecraft.entity.effect.*;
import net.minecraft.registry.entry.*;
import net.minecraft.server.command.*;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.*;

import java.util.Collection;
import java.util.*;

@Mixin(EffectCommand.class)
public class EffectCommandMixin {
Expand All @@ -23,8 +18,8 @@ public class EffectCommandMixin {
private static void clearIncurableEffects(ServerCommandSource source, Collection<? extends Entity> targets, CallbackInfoReturnable<Integer> cir, @Local Entity target) {
if (target instanceof LivingEntity living) {
for (StatusEffectInstance effect : living.getStatusEffects()) {
if (((Incurable) effect).spectrum$isIncurable())
((Incurable) effect).spectrum$setIncurable(false);
if (effect.spectrum$isIncurable())
effect.spectrum$setIncurable(false);
}
}
}
Expand All @@ -34,8 +29,8 @@ private static void clearIncurableEffects(ServerCommandSource source, Collection
if (target instanceof LivingEntity living) {
var effect = living.getStatusEffect(ref);
if (effect != null) {
if (((Incurable) effect).spectrum$isIncurable())
((Incurable) effect).spectrum$setIncurable(false);
if (effect.spectrum$isIncurable())
effect.spectrum$setIncurable(false);
}
}
}
Expand Down

This file was deleted.

Loading

0 comments on commit 8f0be7c

Please sign in to comment.