Skip to content

Commit

Permalink
Add modify_slipperiness power
Browse files Browse the repository at this point in the history
  • Loading branch information
Alluysl committed Jul 6, 2021
1 parent d2f9b45 commit 4737664
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/main/java/alluysl/alluyslorigins/mixin/BoatEntityMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package alluysl.alluyslorigins.mixin;

import alluysl.alluyslorigins.power.ModifySlipperinessPower;
import io.github.apace100.origins.component.OriginComponent;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.vehicle.BoatEntity;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
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 org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(BoatEntity.class)
public abstract class BoatEntityMixin extends Entity {

public BoatEntityMixin(EntityType<?> type, World world){
super(type, world);
}

@Shadow @Nullable public abstract Entity getPrimaryPassenger();

@Redirect(method = "method_7548", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;getSlipperiness()F"))
private float modifySlipperiness(Block block){
float base = block.getSlipperiness();

Entity passenger = getPrimaryPassenger();

if (passenger instanceof LivingEntity)
for (ModifySlipperinessPower power : OriginComponent.getPowers(passenger, ModifySlipperinessPower.class))
if (power.doesModify(getVelocityAffectingPos(), true))
base = MathHelper.lerp(power.getValue(), base, 1.0F);

return base;
}
}
32 changes: 32 additions & 0 deletions src/main/java/alluysl/alluyslorigins/mixin/LivingEntityMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package alluysl.alluyslorigins.mixin;

import alluysl.alluyslorigins.power.ModifySlipperinessPower;
import io.github.apace100.origins.component.OriginComponent;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(LivingEntity.class)
public abstract class LivingEntityMixin extends Entity {

public LivingEntityMixin(EntityType<?> type, World world){
super(type, world);
}

@Redirect(method = "travel", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;getSlipperiness()F"))
private float modifySlipperiness(Block block){
float base = block.getSlipperiness();

for (ModifySlipperinessPower power : OriginComponent.getPowers(this, ModifySlipperinessPower.class))
if (power.doesModify(getVelocityAffectingPos(), false))
base = MathHelper.lerp(power.getValue(), base, 1.0F);

return base;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import alluysl.alluyslorigins.AlluyslOrigins;
import io.github.apace100.origins.power.factory.PowerFactory;
import io.github.apace100.origins.power.factory.condition.ConditionFactory;
import io.github.apace100.origins.registry.ModRegistries;
import io.github.apace100.origins.util.SerializableData;
import io.github.apace100.origins.util.SerializableDataType;
import net.minecraft.block.pattern.CachedBlockPosition;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

Expand Down Expand Up @@ -109,6 +111,19 @@ public static void register(){
data.getBoolean("static_ratio_threshold_inclusive"))
).allowCondition());

//noinspection unchecked
register(new PowerFactory<>(
new Identifier(AlluyslOrigins.MODID, "modify_slipperiness"),
new SerializableData()
.add("value", SerializableDataType.FLOAT)
.add("block_condition", SerializableDataType.BLOCK_CONDITION, null)
.add("affect_boats", SerializableDataType.BOOLEAN, false),
data -> (type, player) -> new ModifySlipperinessPower(type, player,
data.getFloat("value"),
(ConditionFactory<CachedBlockPosition>.Instance)data.get("block_condition"),
data.getBoolean("affect_boats"))
).allowCondition());

System.out.println("[Alluysl's Origins] Powers registered.");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package alluysl.alluyslorigins.power;

import io.github.apace100.origins.power.Power;
import io.github.apace100.origins.power.PowerType;
import io.github.apace100.origins.power.factory.condition.ConditionFactory;
import net.minecraft.block.pattern.CachedBlockPosition;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;

import java.util.function.Predicate;

public class ModifySlipperinessPower extends Power {

private final float value;
private final Predicate<CachedBlockPosition> blockCondition;
private final boolean affectBoats;

public ModifySlipperinessPower(PowerType<Power> type, PlayerEntity player, float value, ConditionFactory<CachedBlockPosition>.Instance blockCondition, boolean affectBoats){
super(type, player);
this.value = value;
this.blockCondition = blockCondition;
this.affectBoats = affectBoats;
}

public float getValue(){
return value;
}

public boolean doesModify(BlockPos pos, boolean isBoat){
return doesModify(new CachedBlockPosition(player.world, pos, true), isBoat);
}

public boolean doesModify(CachedBlockPosition pos, boolean isBoat){
if (isBoat && !affectBoats)
return false;
if (blockCondition == null)
return true;
return blockCondition.test(pos);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/alluyslorigins.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"package": "alluysl.alluyslorigins.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
"BoatEntityMixin",
"LivingEntityMixin"
],
"client": [
"GameRendererMixin"
Expand Down

1 comment on commit 4737664

@Alluysl
Copy link
Owner Author

@Alluysl Alluysl commented on 4737664 Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(The builds are unsuccessful due to TerraformersMC's Maven being down)

Please sign in to comment.