Skip to content

Commit

Permalink
destroy generator when in proximity to ender dragon
Browse files Browse the repository at this point in the history
  • Loading branch information
omergunr100 committed Nov 17, 2024
1 parent 529dd02 commit 3aba2e0
Showing 1 changed file with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.gregtechceu.gtceu.common.machine.electric;

import com.gregtechceu.gtceu.api.capability.IControllable;
import com.gregtechceu.gtceu.api.capability.IWorkable;
import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity;
import com.gregtechceu.gtceu.api.machine.TickableSubscription;
import com.gregtechceu.gtceu.api.machine.TieredEnergyMachine;
import com.gregtechceu.gtceu.utils.GTUtil;

import com.lowdragmc.lowdraglib.syncdata.annotation.DescSynced;
import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted;
import com.lowdragmc.lowdraglib.syncdata.field.ManagedFieldHolder;

import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.boss.enderdragon.EndCrystal;
import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
Expand All @@ -21,6 +24,8 @@
import net.minecraft.world.level.block.DragonEggBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.feature.SpikeFeature;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
Expand All @@ -31,14 +36,15 @@
import java.util.ArrayList;
import java.util.List;

public class MagicEnergyAbsorberMachine extends TieredEnergyMachine implements IControllable {
public class MagicEnergyAbsorberMachine extends TieredEnergyMachine implements IWorkable {

public static final long BASE_EU_PER_FEATURE = 32;
public static final long AMPLIFIER_MULTIPLIER = 4;

protected static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder(
MagicEnergyAbsorberMachine.class,
TieredEnergyMachine.MANAGED_FIELD_HOLDER);
private static final double DRAGON_DISTANCE_THRESHOLD = 5;

@DescSynced
@Persisted
Expand Down Expand Up @@ -81,11 +87,15 @@ protected long getMaxInputOutputAmperage() {
public void onLoad() {
super.onLoad();
if (!isRemote()) {
onServerTickSubscription = this.subscribeServerTick(() -> onServerTick(false));
updateServerTickSubscription();
onServerTick(true);
}
}

protected void updateServerTickSubscription() {
onServerTickSubscription = this.subscribeServerTick(onServerTickSubscription, () -> onServerTick(false));
}

@Override
public void onUnload() {
super.onUnload();
Expand Down Expand Up @@ -158,20 +168,26 @@ else if (beamTarget == null) {
});
}

protected void checkDragonProximity(EnderDragon dragon) {
if (getPos().getCenter().distanceTo(dragon.position()) < DRAGON_DISTANCE_THRESHOLD)
this.doExplosion(GTUtil.getExplosionPower(tier));
}

protected void updateCrystalTargets() {
if (getLevel() == null) return;
// ender dragon check
List<? extends EnderDragon> dragonsInRange = ((ServerLevel) getLevel()).getDragons();

for (EnderDragon dragon : dragonsInRange) {
checkDragonProximity(dragon);
if (dragon.nearestCrystal != null && connectedFeatures.contains(dragon.nearestCrystal.getId())) {
dragon.nearestCrystal = null;

if (dragon.getPhaseManager().getCurrentPhase().getPhase() == EnderDragonPhase.HOLDING_PATTERN) {
dragon.hurt(dragon.damageSources().explosion(dragon, dragon), 10.0f);
dragon.getPhaseManager().setPhase(EnderDragonPhase.CHARGING_PLAYER);
((DragonChargePlayerPhase) dragon.getPhaseManager().getCurrentPhase())
.setTarget(getPos().getCenter());
.setTarget(getPos().below().below().getCenter());
}
}
}
Expand All @@ -194,4 +210,31 @@ protected void resetConnectedFeatures() {

connectedFeatures.clear();
}

@Override
@SideOnly(Side.CLIENT)
public void animateTick(RandomSource random) {
if (getLevel() == null || !isActive() || this.hasAmplifier) return;
BlockPos pos = getPos();
for (int i = 0; i < 4; i++) {
getLevel().addParticle(ParticleTypes.PORTAL, pos.getX(), pos.getY(), pos.getZ(),
(random.nextFloat() - 0.5f) * 0.5f, (random.nextFloat() - 0.5f) * 0.5f,
(random.nextFloat() - 0.5f) * 0.5f);
}
}

@Override
public int getProgress() {
return 1;
}

@Override
public int getMaxProgress() {
return 1;
}

@Override
public boolean isActive() {
return isWorkingEnabled() && !connectedFeatures.isEmpty();
}
}

0 comments on commit 3aba2e0

Please sign in to comment.