Skip to content

Commit

Permalink
Introduce game rule for long reach increment
Browse files Browse the repository at this point in the history
This feature allows players to configure the increment of the long reach effect on a per-world basis. The default value is now '2' as opposed to the previously hardcoded '5', because I felt it was too powerful.
  • Loading branch information
ChloeDawn committed Sep 29, 2021
1 parent 21cdf61 commit 4810fff
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 24 deletions.
46 changes: 37 additions & 9 deletions src/main/java/dev/sapphic/beacons/BeaconMobEffects.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,57 @@
import com.google.common.collect.ObjectArrays;
import com.jamieswhiteshirt.reachentityattributes.ReachEntityAttributes;
import dev.sapphic.beacons.mixin.BeaconBlockEntityAccessor;
import dev.sapphic.beacons.mixin.MobEffectAccessor;
import dev.sapphic.beacons.mixin.GameRulesAccessor;
import dev.sapphic.beacons.mixin.IntegerValueAccessor;
import net.fabricmc.api.ModInitializer;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeMap;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.block.entity.BeaconBlockEntity;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Arrays;
import java.util.stream.Collectors;

public final class BeaconMobEffects implements ModInitializer {
public static final MobEffect LONG_REACH = MobEffectAccessor.newMobEffect(MobEffectCategory.BENEFICIAL, 0xDEF58F)
.addAttributeModifier(ReachEntityAttributes.REACH, "C20A0A8F-83DF-4C37-BC34-3678C24C3F01", 5.0, Operation.ADDITION)
.addAttributeModifier(ReachEntityAttributes.ATTACK_RANGE, "C764C44F-FC32-498B-98EB-B3262BA58B3B", 5.0, Operation.ADDITION);
public static final GameRules.Key<GameRules.IntegerValue> LONG_REACH_INCREMENT = GameRulesAccessor.callRegister(
"longReachIncrement", GameRules.Category.PLAYER, IntegerValueAccessor.callCreate(2));

public static final MobEffect LONG_REACH = new MobEffect(MobEffectCategory.BENEFICIAL, 0xDEF58F) {
private static double getLongReachAmount(final LivingEntity entity, final int mul) {
return Math.max(0, entity.level.getGameRules().getInt(LONG_REACH_INCREMENT)) * (mul + 1);
}

@Override
public void addAttributeModifiers(final LivingEntity entity, final AttributeMap attributes, final int mul) {
for (final var entry : this.getAttributeModifiers().entrySet()) {
final @Nullable AttributeInstance instance = attributes.getInstance(entry.getKey());

if (instance != null) {
final var modifier = entry.getValue();

instance.removeModifier(modifier);
instance.addPermanentModifier(new AttributeModifier(
modifier.getId(), this.getDescriptionId() + ' ' + mul,
getLongReachAmount(entity, mul), modifier.getOperation()
));
}
}
}
}.addAttributeModifier(ReachEntityAttributes.ATTACK_RANGE,
"C764C44F-FC32-498B-98EB-B3262BA58B3B", Double.NaN, Operation.ADDITION
).addAttributeModifier(ReachEntityAttributes.REACH,
"C20A0A8F-83DF-4C37-BC34-3678C24C3F01", Double.NaN, Operation.ADDITION
);

public static final MobEffect NUTRITION = new MobEffect(MobEffectCategory.BENEFICIAL, 0xC75F79) {
@Override
Expand All @@ -39,11 +71,7 @@ public boolean isDurationEffectTick(final int duration, final int amplifier) {

static final String NAMESPACE = "beaconoverhaul";

public BeaconMobEffects() {
appendAdditionalEffects();
}

private static void appendAdditionalEffects() {
static {
final var effects = BeaconBlockEntity.BEACON_EFFECTS;

effects[0] = ObjectArrays.concat(effects[0], MobEffects.NIGHT_VISION);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/dev/sapphic/beacons/mixin/GameRulesAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.sapphic.beacons.mixin;

import net.minecraft.world.level.GameRules;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(GameRules.class)
public interface GameRulesAccessor {
@Invoker
static <T extends GameRules.Value<T>> GameRules.Key<T> callRegister(
final String name, final GameRules.Category category, final GameRules.Type<T> type
) {
throw new AssertionError();
}
}
13 changes: 13 additions & 0 deletions src/main/java/dev/sapphic/beacons/mixin/IntegerValueAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.sapphic.beacons.mixin;

import net.minecraft.world.level.GameRules;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(GameRules.IntegerValue.class)
public interface IntegerValueAccessor {
@Invoker
static GameRules.Type<GameRules.IntegerValue> callCreate(final int defaultValue) {
throw new AssertionError();
}
}
14 changes: 0 additions & 14 deletions src/main/java/dev/sapphic/beacons/mixin/MobEffectAccessor.java

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/resources/mixins/beaconoverhaul/mixins.server.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"BeaconBlockEntityMixin",
"BeaconBlockEntityMixin$DataAccessMixin",
"BeaconMenuMixin",
"GameRulesAccessor",
"IntegerValueAccessor",
"LivingEntityMixin",
"MobEffectAccessor",
"PlayerMixin"
],
"refmap": "mixins/beaconoverhaul/refmap.json"
Expand Down

0 comments on commit 4810fff

Please sign in to comment.