Skip to content

Commit

Permalink
refactor: grenades now use datatracker to store power
Browse files Browse the repository at this point in the history
  • Loading branch information
fenn7 committed Sep 22, 2023
1 parent 8087acd commit 34b2c76
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

public class HiddenExplosiveBlockEntity extends BlockEntity implements IAnimatable, ExtendedScreenHandlerFactory, ImplementedInventory {
public static final int MAX_ARMING_TICKS = 40;
private static final float INCREASED_POWER_PER_RANGE = 0.2F;
private static final float INCREASED_POWER_PER_RANGE = 0.15F;
private static final float INCREASED_POWER_BASE = 1.5F;
private static final String NBT_TAG = "configuration.data";
private static final String LAST_USER = "last.user";
Expand Down Expand Up @@ -128,7 +128,7 @@ public void detonate(World world, BlockPos pos) {
grenadeEntity.setNoGravity(true);
BlockPos potentialPos = pos.offset(Direction.byId(this.directionID));
grenadeEntity.setPosition(Vec3d.ofCenter(this.directionID > 0 ? (!world.getBlockState(potentialPos).isSolidBlock(world, pos) ? potentialPos : pos) : pos));
grenadeEntity.setPower(grenadeEntity.getPower() * (INCREASED_POWER_BASE + (1.1F - (this.detectRange * INCREASED_POWER_PER_RANGE))));
grenadeEntity.setPower(grenadeEntity.getPower() * (INCREASED_POWER_BASE + (0.9F - (MathHelper.clamp(this.detectRange, 1, 4) * INCREASED_POWER_PER_RANGE))));
world.spawnEntity(grenadeEntity);
world.breakBlock(pos, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ protected void handleParticleEffects() {

@Override
protected void handleDiscard() {
this.explode(this.power);
this.explode();
super.handleDiscard();
}

@Override
protected void explode(float power) {
super.explode(power);
protected void explode() {
super.explode();
if (this.state == LingeringState.DISCARDED) {
var entities = this.getEntitiesFromBlocks(this.getAffectedBlocksAtRange(power));
var entities = this.getEntitiesFromBlocks(this.getAffectedBlocksAtRange(this.getPower()));
entities.forEach(entity -> this.handleDisplacement(entity, this.getBlockPos(), entities));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.stream.Collectors;

import fenn7.grenadesandgadgets.client.GrenadesModClientUtil;
import fenn7.grenadesandgadgets.commonside.GrenadesMod;
import fenn7.grenadesandgadgets.commonside.item.recipe.custom.GrenadeModifierRecipe;
import fenn7.grenadesandgadgets.commonside.status.GrenadesModStatus;
import fenn7.grenadesandgadgets.commonside.util.GrenadesModEntityData;
Expand Down Expand Up @@ -66,8 +65,8 @@ public abstract class AbstractGrenadeEntity extends ThrownItemEntity implements
protected static TrackedData<Boolean> BOUNCE_FLAG = DataTracker.registerData(AbstractGrenadeEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
protected static TrackedData<Float> BOUNCE_MULTIPLIER = DataTracker.registerData(AbstractGrenadeEntity.class, TrackedDataHandlerRegistry.FLOAT);
protected static TrackedData<Integer> MAX_AGE = DataTracker.registerData(AbstractGrenadeEntity.class, TrackedDataHandlerRegistry.INTEGER);
protected static TrackedData<Float> POWER = DataTracker.registerData(AbstractGrenadeEntity.class, TrackedDataHandlerRegistry.FLOAT);
protected final AnimationFactory factory = GrenadesModUtil.getAnimationFactoryFor(this);
protected float power;
protected ParticleEffect explosionEffect;
protected GrenadesModSoundProfile explosionSoundProfile;

Expand All @@ -92,6 +91,7 @@ protected void initDataTracker() {
this.dataTracker.startTracking(BOUNCE_FLAG, true);
this.dataTracker.startTracking(BOUNCE_MULTIPLIER, 2F / 3F);
this.dataTracker.startTracking(MAX_AGE, 100);
this.dataTracker.startTracking(POWER, 0.0F);
}

@Override
Expand All @@ -102,7 +102,7 @@ public void tick() {
}
if (this.age >= this.getMaxAgeTicks() && !(this instanceof AbstractLingeringGrenadeEntity lingering
&& lingering.state != AbstractLingeringGrenadeEntity.LingeringState.UNEXPLODED)) {
this.explodeWithEffects(this.power);
this.explodeWithEffects();
}
Vec3d velocity = this.getVelocity();
super.tick();
Expand All @@ -118,7 +118,7 @@ private void tickMolten() {
this.setOnFireFor(1);
}
if (this.isSubmergedInWater()) {
this.explodeWithEffects(this.power);
this.explodeWithEffects();
}
}

Expand All @@ -145,7 +145,7 @@ private void updateStickyPosition() {
if (entity != null && entity.isAlive()) {
this.setPos(entity.getX(), entity.getBodyY(0.5D), entity.getZ());
} else {
this.explodeWithEffects(this.power);
this.explodeWithEffects();
}
}
}
Expand Down Expand Up @@ -173,21 +173,21 @@ protected void onBlockHit(BlockHitResult blockHitResult) {
if (velocity.length() >= MINIMUM_CATACLYSMIC_THRESHOLD) {
var copy = this.spawnCopyAtLocation();
if (copy != null) {
copy.setPower(this.power * CATACLYSMIC_MULTIPLIER);
copy.setPower(this.getPower() * CATACLYSMIC_MULTIPLIER);
copy.setMaxAgeTicks(0);
}
}
}
case AQUATIC -> {
if (this.isSubmergedInWater()) {
this.setPower(this.power * AQUATIC_MULTIPLIER);
this.explodeWithEffects(this.power);
this.setPower(this.getPower() * AQUATIC_MULTIPLIER);
this.explodeWithEffects();
}
}
}
} else {
switch (this.getModifierName()) {
case REACTIVE, ECHOING -> this.explodeWithEffects(this.power);
case REACTIVE, ECHOING -> this.explodeWithEffects();
case STICKY -> {
NbtCompound nbt = ((GrenadesModEntityData) this).getPersistentData();
if (!nbt.contains(STICK_TARGET)) {
Expand Down Expand Up @@ -219,31 +219,31 @@ protected void onEntityHit(EntityHitResult entityHitResult) {
}
case AQUATIC -> {
if (this.isSubmergedInWater()) {
this.setPower(this.power * AQUATIC_MULTIPLIER);
this.setPower(this.getPower() * AQUATIC_MULTIPLIER);
}
this.explodeWithEffects(this.power);
this.explodeWithEffects();
}
case MOLTEN -> {
target.setOnFireFor(5);
this.explodeWithEffects(this.power);
this.explodeWithEffects();
}
case GRAVITY -> {
if (target instanceof LivingEntity alive) {
alive.addStatusEffect(new StatusEffectInstance(GrenadesModStatus.DECELERATE, 20, 4));
throwDmg += 2.0F;
}
this.explodeWithEffects(this.power);
this.explodeWithEffects();
}
case CATACLYSMIC -> {
if (this.getVelocity().length() >= MINIMUM_CATACLYSMIC_THRESHOLD) {
var copy = this.spawnCopyAtLocation();
if (copy != null) {
copy.setPower(this.power * CATACLYSMIC_MULTIPLIER);
copy.setPower(this.getPower() * CATACLYSMIC_MULTIPLIER);
copy.setMaxAgeTicks(0);
}
}
}
default -> this.explodeWithEffects(this.power);
default -> this.explodeWithEffects();
}
target.damage(DamageSource.thrownProjectile(this, this.getOwner()), throwDmg);
super.onEntityHit(entityHitResult);
Expand All @@ -253,7 +253,7 @@ protected void onEntityHit(EntityHitResult entityHitResult) {
public NbtCompound writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
nbt.putInt("current_age", this.age);
nbt.putFloat("explosion_power", this.power);
nbt.putFloat("explosion_power", this.getPower());
nbt.putInt("max_age", this.getMaxAgeTicks());
nbt.putBoolean("should_bounce", this.getShouldBounce());
nbt.putFloat("bounce_multiplier", this.getBounceMultiplier());
Expand All @@ -263,7 +263,7 @@ public NbtCompound writeNbt(NbtCompound nbt) {
@Override
public void readNbt(NbtCompound nbt) {
this.age = nbt.getInt("current_age");
this.power = nbt.getFloat("explosion_power");
this.dataTracker.set(POWER, nbt.getFloat("explosion_power"));
this.dataTracker.set(MAX_AGE, nbt.getInt("max_age"));
this.dataTracker.set(BOUNCE_FLAG, nbt.getBoolean("should_bounce"));
this.dataTracker.set(BOUNCE_MULTIPLIER, nbt.getFloat("bounce_multiplier"));
Expand All @@ -275,19 +275,19 @@ public void remove(RemovalReason reason) {
if (this.getModifierName().equals(ECHOING)) {
var clone = this.spawnCopyAtLocation();
if (clone != null) {
clone.setPower(this.power * ECHOING_MULTIPLIER);
clone.setPower(this.getPower() * ECHOING_MULTIPLIER);
clone.setMaxAgeTicks(20);
}
}
super.remove(reason);
}

protected void explodeWithEffects(float power) {
protected void explodeWithEffects() {
this.world.sendEntityStatus(this, STATUS_BYTE);
this.explode(power);
this.explode();
}

protected abstract void explode(float power);
protected abstract void explode();

protected abstract void initialise();

Expand Down Expand Up @@ -338,11 +338,11 @@ public void setBounceMultiplier(float bounceMultiplier) {
}

public void setPower(float power) {
this.power = power;
this.dataTracker.set(POWER, power);
}

public float getPower() {
return this.power;
return this.dataTracker.get(POWER);
}

public float getBounceMultiplier() {
Expand All @@ -369,7 +369,7 @@ public void setExplosionSoundProfile(GrenadesModSoundProfile sound) {
public void handleStatus(byte status) {
if (status == STATUS_BYTE) {
if (this.explosionEffect != null) {
GrenadesModClientUtil.createExplosionEffects(this.world, this.explosionEffect, this.getPos(), 3, this.power);
GrenadesModClientUtil.createExplosionEffects(this.world, this.explosionEffect, this.getPos(), 3, this.getPower());
}
if (this.explosionSoundProfile != null) {
GrenadesModClientUtil.playExplosionSound(this.world, this.explosionSoundProfile, this.getPos());
Expand All @@ -379,7 +379,7 @@ public void handleStatus(byte status) {
}

protected float proportionalDistanceTo(Entity entity) {
return this.distanceTo(entity) / this.power;
return this.distanceTo(entity) / this.getPower();
}

protected double blockDistanceTo(BlockPos position) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package fenn7.grenadesandgadgets.commonside.entity.grenades;

import fenn7.grenadesandgadgets.commonside.GrenadesMod;
import fenn7.grenadesandgadgets.commonside.entity.GrenadesModEntities;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity;
Expand Down Expand Up @@ -110,7 +108,7 @@ public void pushAwayFrom(Entity entity) {
}

@Override
protected void explode(float power) {
protected void explode() {
if (this.state == LingeringState.UNEXPLODED) {
this.setInactive();
this.setState(LingeringState.LINGERING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ protected void initialise() {
@Override
protected void handleParticleEffects() {
if (this.world.isClient) {
double randomX = this.random.nextDouble(this.power);
double randomX = this.random.nextDouble(this.getPower());
double randomY = this.random.nextDouble(-0.5, 0.5);
double correspondingZ = Math.sqrt(Math.pow(this.power, 2) - Math.pow(randomX, 2));
double correspondingZ = Math.sqrt(Math.pow(this.getPower(), 2) - Math.pow(randomX, 2));
boolean shouldNegate = this.random.nextBoolean();

double spawnX = shouldNegate ? this.getX() - randomX : this.getX() + randomX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ protected void initialise() {

@Override
@SuppressWarnings("ConstantConditions")
protected void explode(float power) {
protected void explode() {
if (this.getOwner() instanceof PlayerEntity player) {
DecoyEntity decoyEntity = new DecoyEntity(this.world, player, power);
DecoyEntity decoyEntity = new DecoyEntity(this.world, player, this.getPower());
decoyEntity.getAttributeInstance(EntityAttributes.GENERIC_MAX_HEALTH).addPersistentModifier(
new EntityAttributeModifier("bonus_health", power * ABSORPTION_MULTIPLIER, EntityAttributeModifier.Operation.ADDITION));
new EntityAttributeModifier("bonus_health", this.getPower() * ABSORPTION_MULTIPLIER, EntityAttributeModifier.Operation.ADDITION));
decoyEntity.setHealth(decoyEntity.getMaxHealth());
decoyEntity.setPosition(this.getPos());
this.world.spawnEntity(decoyEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ protected void initialise() {
@Override
protected void handleParticleEffects() {
if (this.world.isClient) {
double randomX = this.random.nextDouble(this.power);
double randomX = this.random.nextDouble(this.getPower());
double randomY = this.random.nextDouble(-0.5, 0.5);
double correspondingZ = Math.sqrt(Math.pow(this.power, 2) - Math.pow(randomX, 2));
double correspondingZ = Math.sqrt(Math.pow(this.getPower(), 2) - Math.pow(randomX, 2));
boolean shouldNegate = this.random.nextBoolean();

double spawnX = shouldNegate ? this.getX() - randomX : this.getX() + randomX;
Expand All @@ -70,7 +70,7 @@ protected void handleParticleEffects() {

@Override
protected void handleDisplacement(Entity entity, BlockPos pos, Set<Entity> entities) {
entity.move(MovementType.SELF, entity.getPos().subtract(this.getPos()).normalize().multiply(this.power));
entity.move(MovementType.SELF, entity.getPos().subtract(this.getPos()).normalize().multiply(this.getPower()));
if (entity instanceof LivingEntity alive) {
alive.damage(DamageSource.thrownProjectile(this, this.getOwner()), DISPLACEMENT_DAMAGE_PER_ENTITY);
alive.addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 40));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import net.minecraft.world.World;

public class FireGrenadeEntity extends AbstractLingeringGrenadeEntity {
private static final float FIRE_RANGE = 2.8F;
private static final float FIRE_RANGE = 1.9F;
private static final float MAX_IMPACT_DAMAGE = 4.0F;
private static final float MAX_DAMAGE_PROPORTION_RANGE = 0.4F;
private static final int MAX_LINGERING_TICKS = 10;
Expand Down Expand Up @@ -50,14 +50,15 @@ protected void initialise() {
@Override
protected void onCollision(HitResult hitResult) {
if (!this.world.isClient()) {
this.explodeWithEffects(this.power * 0.66F);
this.explodeWithEffects();
}
super.onCollision(hitResult);
}

@Override
protected void handleDiscard() {
this.explode(this.power);
this.setPower(this.getPower() * 1.5F);
this.explode();
super.handleDiscard();
}

Expand All @@ -67,9 +68,9 @@ protected Item getDefaultItem() {
}

@Override
protected void explode(float power) {
super.explode(power);
this.getAffectedBlocksAtRange(power).forEach(pos -> {
protected void explode() {
super.explode();
this.getAffectedBlocksAtRange(this.getPower()).forEach(pos -> {
this.world.getNonSpectatingEntities(LivingEntity.class, new Box(pos)).forEach(entity -> {
if (!entity.isFireImmune() && this.state != LingeringState.DISCARDED) {
entity.damage(DamageSource.LAVA, this.handleImpactDamage(entity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ protected void initialise() {


@Override
protected void explode(float power) {
protected void explode() {
if (!this.world.isClient) {
this.world.createExplosion(null, this.getX(), this.getY(), this.getZ(), power, Explosion.DestructionType.NONE);
this.world.createExplosion(null, this.getX(), this.getY(), this.getZ(), this.getPower(), Explosion.DestructionType.NONE);
NbtCompound nbt = this.getItem().getOrCreateNbt();
if (nbt.contains(FRAGMENTS) && nbt.get(FRAGMENTS) instanceof NbtList) {
NbtList nbtList = nbt.getList(FRAGMENTS, 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ protected void onCollision(HitResult hitResult) {
}

@Override
protected void explode(float power) {
protected void explode() {
if (!this.world.isClient()) {
Set<LivingEntity> list =
GrenadesModUtil.getLivingEntitiesAtRangeFromEntity(this.world, this, power);
GrenadesModUtil.getLivingEntitiesAtRangeFromEntity(this.world, this, this.getPower());
list.stream().forEach(e -> e.damage(DamageSource.thrownProjectile(this, this.getOwner()), PROXIMITY_DAMAGE));
this.world.createExplosion(null, this.getX(), this.getY(), this.getZ(), power, Explosion.DestructionType.NONE);
this.world.createExplosion(null, this.getX(), this.getY(), this.getZ(), this.getPower(), Explosion.DestructionType.NONE);
}
this.discard();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ protected void initialise() {
}

@Override
protected void explode(float power) {
protected void explode() {
if (!this.world.isClient()) {
this.world.createExplosion(null, this.getX(), this.getY(), this.getZ(), power, Explosion.DestructionType.NONE);
Set<LivingEntity> list = GrenadesModUtil.getLivingEntitiesAtRangeFromEntity(this.world, this, power);
this.world.createExplosion(null, this.getX(), this.getY(), this.getZ(), this.getPower(), Explosion.DestructionType.NONE);
Set<LivingEntity> list = GrenadesModUtil.getLivingEntitiesAtRangeFromEntity(this.world, this, this.getPower());
list.forEach(e -> {
e.damage(DamageSource.thrownProjectile(this, this.getOwner()), PROXIMITY_DAMAGE);
StatusEffectInstance currentAB = e.getStatusEffect(GrenadesModStatus.ARMOUR_BREAK);
Expand Down
Loading

0 comments on commit 34b2c76

Please sign in to comment.