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

119 lines
4.1 KiB
Java
Raw Normal View History

2020-07-28 03:10:16 +00:00
package com.thebrokenrail.energonrelics.mixin;
2020-08-01 20:22:32 +00:00
import com.thebrokenrail.energonrelics.EnergonRelics;
2020-07-28 03:10:16 +00:00
import com.thebrokenrail.energonrelics.block.forcefield.util.BeamBlock;
2020-08-01 20:22:32 +00:00
import com.thebrokenrail.energonrelics.block.portal.PortalCooldownEntity;
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
2020-07-28 03:10:16 +00:00
import net.minecraft.block.Block;
2020-09-06 14:41:30 +00:00
import net.minecraft.block.BlockState;
2020-07-28 03:10:16 +00:00
import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag;
2020-07-30 22:57:20 +00:00
import net.minecraft.server.MinecraftServer;
2020-07-28 03:10:16 +00:00
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;
2020-08-01 20:22:32 +00:00
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
2020-07-28 03:10:16 +00:00
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.function.Predicate;
@Mixin(Entity.class)
2020-08-01 20:22:32 +00:00
public abstract class MixinEntity implements PortalCooldownEntity {
@Unique
private int energyPortalCooldown = 0;
2020-09-06 14:41:30 +00:00
@Unique
private boolean touchingBeam = false;
2020-08-01 20:22:32 +00:00
2020-07-30 22:57:20 +00:00
@Shadow
public abstract Box getBoundingBox();
2020-07-28 03:10:16 +00:00
2020-07-30 22:57:20 +00:00
@Shadow
public abstract World getEntityWorld();
@Shadow
public abstract MinecraftServer getServer();
2020-07-28 03:10:16 +00:00
@Unique
private boolean isTouching(Predicate<Block> test) {
2020-07-30 22:57:20 +00:00
if (getServer() == null || getServer().getThread() == Thread.currentThread()) {
2020-08-07 02:29:44 +00:00
getEntityWorld().getProfiler().push("energonrelics:isTouching");
2020-07-30 22:57:20 +00:00
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);
2020-08-07 02:12:48 +00:00
BlockPos.Mutable pos = new BlockPos.Mutable();
2020-07-30 22:57:20 +00:00
for (int p = i; p < j; ++p) {
for (int q = k; q < l; ++q) {
for (int r = m; r < n; ++r) {
2020-08-07 02:12:48 +00:00
pos.set(p, q, r);
2020-09-06 14:41:30 +00:00
BlockState state = getEntityWorld().getBlockState(pos);
if (state.getCollisionShape(getEntityWorld(), pos).isEmpty() && test.test(state.getBlock())) {
2020-08-07 02:29:44 +00:00
getEntityWorld().getProfiler().pop();
2020-07-30 22:57:20 +00:00
return true;
}
2020-07-28 03:10:16 +00:00
}
}
}
}
2020-08-07 02:29:44 +00:00
getEntityWorld().getProfiler().pop();
2020-07-28 03:10:16 +00:00
return false;
}
@Unique
private boolean saving = false;
@Inject(at = @At("HEAD"), method = "hasNoGravity", cancellable = true)
public void hasNoGravity(CallbackInfoReturnable<Boolean> info) {
2020-09-06 14:41:30 +00:00
if (!saving && touchingBeam) {
2020-07-28 03:10:16 +00:00
info.setReturnValue(true);
}
}
@Inject(at = @At("HEAD"), method = "toTag")
2020-08-01 20:22:32 +00:00
public void toTagHead(CompoundTag tag, CallbackInfoReturnable<CompoundTag> info) {
2020-07-28 03:10:16 +00:00
saving = true;
2020-08-01 20:22:32 +00:00
tag.putInt(EnergonRelics.NAMESPACE + ":EnergyPortalCooldown", energyPortalCooldown);
2020-07-28 03:10:16 +00:00
}
@Inject(at = @At("RETURN"), method = "toTag")
2020-08-01 20:22:32 +00:00
public void toTagReturn(CompoundTag tag, CallbackInfoReturnable<CompoundTag> info) {
2020-07-28 03:10:16 +00:00
saving = false;
}
2020-08-01 20:22:32 +00:00
@Inject(at = @At("HEAD"), method = "fromTag")
public void fromTag(CompoundTag tag, CallbackInfo info) {
energyPortalCooldown = tag.getInt(EnergonRelics.NAMESPACE + ":EnergyPortalCooldown");
}
2020-09-06 14:41:30 +00:00
@Inject(at = @At("HEAD"), method = "tick")
public void tickHead(CallbackInfo info) {
touchingBeam = isTouching(block -> block instanceof BeamBlock);
}
2020-08-01 20:22:32 +00:00
@Inject(at = @At("RETURN"), method = "tick")
2020-09-06 14:41:30 +00:00
public void tickReturn(CallbackInfo info) {
2020-08-07 02:07:10 +00:00
energyPortalCooldown--;
2020-08-01 20:22:32 +00:00
}
@Override
public void resetEnergyPortalCooldown() {
energyPortalCooldown = HardcodedConfig.ENERGY_PORTAL_COOLDOWN;
}
@Override
public boolean isEnergyPortalCooldown() {
return energyPortalCooldown > 0;
}
2020-07-28 03:10:16 +00:00
}