Skip to content

Commit

Permalink
Merge pull request jcm236#25 from zyxkad/main
Browse files Browse the repository at this point in the history
fix magnet boot
  • Loading branch information
blockninja124 authored Jan 23, 2025
2 parents 67b5689 + 9671581 commit 03f0d1c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 27 deletions.
Binary file removed lib/cosmic-horizons-0.0.6-WIP.jar.disabled
Binary file not shown.
Binary file removed lib/cosmichorizons-0.0.6.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/java/net/jcm/vsch/config/VSCHConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class VSCHConfig {
CANCEL_ASSEMBLY = BUILDER.comment("Cancel multi-block assemblies when above world height. This is a temporary fix, but for now ships made above world height have issues with starlance.").define("cancel_assembly", true);

MAGNET_BOOT_DISTANCE = BUILDER.comment("Distance (in blocks) at which magnet boots will pull you in").define("magnet_boot_distance", 6);
MAGNET_BOOT_MAX_FORCE = BUILDER.comment("Max acceleration magnet boots will apply at close distances to move the player downwards.").define("magnet_boot_max_force", 0.1);
MAGNET_BOOT_MAX_FORCE = BUILDER.comment("Max acceleration magnet boots will apply at close distances to move the player downwards.").define("magnet_boot_max_force", 0.09);

BUILDER.pop();

Expand Down
42 changes: 16 additions & 26 deletions src/main/java/net/jcm/vsch/items/custom/MagnetBootItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.minecraft.nbt.DoubleTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ArmorMaterial;
Expand All @@ -16,7 +18,7 @@
import net.minecraft.world.phys.Vec3;

public class MagnetBootItem extends ArmorItem {
private static final String TAG_ENABLED = "Enabled";
private static final String TAG_DISABLED = "Disabled";
private static final String TAG_READY = "Ready";
private static final String TAG_DIRECTION = "Direction";
private static final double MIN_FORCE = 0.01;
Expand All @@ -38,7 +40,7 @@ public boolean getEnabled(ItemStack stack) {
return false;
}
CompoundTag tag = stack.getTag();
return tag != null && tag.getBoolean(TAG_ENABLED);
return tag == null || !tag.getBoolean(TAG_DISABLED);
}

public boolean getReady(ItemStack stack) {
Expand All @@ -61,48 +63,38 @@ public Vec3 getDirection(ItemStack stack) {
}

@Override
public void onCraftedBy(ItemStack stack, Level level, Player player) {
if (!(level instanceof ServerLevel)) {
public void inventoryTick(ItemStack stack, Level level, Entity entity, int slot, boolean selected) {
if (!(entity instanceof LivingEntity livingEntity)) {
return;
}
CompoundTag tag = stack.getOrCreateTag();
tag.putBoolean(TAG_ENABLED, true);
}

@Override
public void onArmorTick(ItemStack stack, Level level, Player player) {
if (!(level instanceof ServerLevel)) {
if (livingEntity.getItemBySlot(this.getEquipmentSlot()) != stack) {
return;
}

// Ignore spectator mode
// I don't exactly know what this var does, but it trigger in spectator mode.
// If it causes problems, replace it with 'isSpectator()'
if (player.noPhysics) {
// Ignore no physics entities
if (entity.noPhysics || entity.isPassenger()) {
return;
}

// I don't know why there isn't a simpler check for this
if (player.getAbilities().flying) {
if (entity instanceof Player player && player.getAbilities().flying) {
return;
}

CompoundTag tag = stack.getOrCreateTag();
boolean enabled = tag.getBoolean(TAG_ENABLED);
boolean disabled = tag.getBoolean(TAG_DISABLED);
boolean wasReady = tag.getBoolean(TAG_READY);

double maxDistance = getAttractDistance();

Vec3 direction = new Vec3(0, -1, 0); // TODO: maybe we can change the direction to match the ship that player stands on?
Vec3 startPos = player.position(); // Starting position (player's position)
Vec3 startPos = entity.position(); // Starting position (player's position)
Vec3 endPos = startPos.add(direction.scale(maxDistance)); // End position (straight down)

HitResult hitResult = level.clip(new ClipContext(
startPos,
endPos,
ClipContext.Block.COLLIDER, // Raycast considers block collision shapes, maybe we don't want this?
ClipContext.Fluid.NONE, // Ignore fluids
player
entity
));

if (hitResult.getType() != HitResult.Type.BLOCK) {
Expand All @@ -116,7 +108,7 @@ public void onArmorTick(ItemStack stack, Level level, Player player) {
tag.putBoolean(TAG_READY, true);
Vec3.CODEC.encodeStart(NbtOps.INSTANCE, direction).result().ifPresent(pos -> tag.put(TAG_DIRECTION, pos));
}
if (!enabled) {
if (disabled) {
return;
}

Expand All @@ -125,12 +117,10 @@ public void onArmorTick(ItemStack stack, Level level, Player player) {
double scaledForce = Math.min(maxDistance * maxDistance / distance * MIN_FORCE, getMaxForce());

Vec3 force = direction.scale(scaledForce);
tag.putDouble("Force", scaledForce);

player.setDeltaMovement(player.getDeltaMovement().add(force));

//System.out.println("Hit block");
entity.push(force.x, force.y, force.z);

//System.out.println(slotId);
//level.addParticle(ParticleTypes.HEART, player.getX(), player.getY(), player.getZ(), 0, 0, 0);
}
}
40 changes: 40 additions & 0 deletions src/main/java/net/jcm/vsch/mixin/MixinMob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.jcm.vsch.mixin;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
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 javax.annotation.Nullable;
import java.util.Set;

@Mixin(Mob.class)
public abstract class MixinMob extends Entity {
private MixinMob() {
super(null, null);
}

@Shadow
public abstract Iterable<ItemStack> getArmorSlots();

@Inject(method = "baseTick()V", at = @At(value = "HEAD"))
private void tickArmors(CallbackInfo cb) {
Level level = this.level();
int i = 0;
for (ItemStack stack : this.getArmorSlots()) {
if (!stack.isEmpty()) {
Item item = stack.getItem();
if (item != null) {
item.inventoryTick(stack, level, this, i, false);
}
}
i++;
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/vsch.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"MixinEntity",
"MixinLightRenderer",
"MixinLivingEntityProcedure",
"MixinMob",
"MixinShipAssemblyKt",
"MixinSpacesuitwornLogicProcedure",
"MixinVSGameUtilsKt",
Expand Down

0 comments on commit 03f0d1c

Please sign in to comment.