This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/mixin/MixinEntity.java

104 lines
3.5 KiB
Java

package com.thebrokenrail.energonrelics.mixin;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.forcefield.util.BeamBlock;
import com.thebrokenrail.energonrelics.block.portal.PortalCooldownEntity;
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.MathHelper;
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.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.function.Predicate;
@Mixin(Entity.class)
public abstract class MixinEntity implements PortalCooldownEntity {
@Unique
private int energyPortalCooldown = 0;
@Shadow
public abstract Box getBoundingBox();
@Shadow
public abstract World getEntityWorld();
@Shadow
public abstract MinecraftServer getServer();
@Unique
private boolean isTouching(Predicate<Block> test) {
if (getServer() == null || getServer().getThread() == Thread.currentThread()) {
Box box = getBoundingBox();
int i = MathHelper.floor(box.minX);
int j = MathHelper.ceil(box.maxX);
int k = MathHelper.floor(box.minY);
int l = MathHelper.ceil(box.maxY);
int m = MathHelper.floor(box.minZ);
int n = MathHelper.ceil(box.maxZ);
for (int p = i; p < j; ++p) {
for (int q = k; q < l; ++q) {
for (int r = m; r < n; ++r) {
BlockPos pos = new BlockPos(p, q, r);
if (test.test(getEntityWorld().getBlockState(pos).getBlock())) {
return true;
}
}
}
}
}
return false;
}
@Unique
private boolean saving = false;
@Inject(at = @At("HEAD"), method = "hasNoGravity", cancellable = true)
public void hasNoGravity(CallbackInfoReturnable<Boolean> info) {
if (!saving && isTouching(block -> block instanceof BeamBlock)) {
info.setReturnValue(true);
}
}
@Inject(at = @At("HEAD"), method = "toTag")
public void toTagHead(CompoundTag tag, CallbackInfoReturnable<CompoundTag> info) {
saving = true;
tag.putInt(EnergonRelics.NAMESPACE + ":EnergyPortalCooldown", energyPortalCooldown);
}
@Inject(at = @At("RETURN"), method = "toTag")
public void toTagReturn(CompoundTag tag, CallbackInfoReturnable<CompoundTag> info) {
saving = false;
}
@Inject(at = @At("HEAD"), method = "fromTag")
public void fromTag(CompoundTag tag, CallbackInfo info) {
energyPortalCooldown = tag.getInt(EnergonRelics.NAMESPACE + ":EnergyPortalCooldown");
}
@Inject(at = @At("RETURN"), method = "tick")
public void tick(CallbackInfo info) {
energyPortalCooldown--;
}
@Override
public void resetEnergyPortalCooldown() {
energyPortalCooldown = HardcodedConfig.ENERGY_PORTAL_COOLDOWN;
}
@Override
public boolean isEnergyPortalCooldown() {
return energyPortalCooldown > 0;
}
}