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;
|
|
|
|
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-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()) {
|
|
|
|
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;
|
|
|
|
}
|
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) {
|
|
|
|
if (isTouching(block -> block instanceof BeamBlock) && !saving) {
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Inject(at = @At("RETURN"), method = "tick")
|
|
|
|
public void tick(CallbackInfo info) {
|
|
|
|
if (!isTouching(block -> block == EnergonRelics.ENERGY_PORTAL_BLOCK) && energyPortalCooldown > 0) {
|
|
|
|
energyPortalCooldown--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void resetEnergyPortalCooldown() {
|
|
|
|
energyPortalCooldown = HardcodedConfig.ENERGY_PORTAL_COOLDOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnergyPortalCooldown() {
|
|
|
|
return energyPortalCooldown > 0;
|
|
|
|
}
|
2020-07-28 03:10:16 +00:00
|
|
|
}
|