Skip to content

Commit

Permalink
Fixed siren ability not working. Fixed shock no control ticks.
Browse files Browse the repository at this point in the history
  • Loading branch information
LtxProgrammer committed Sep 26, 2024
1 parent f53ec2b commit ec19fe7
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public void activateAbility() {
startedUsing = false;
}

public boolean canKeepUsing() {
return abilityInstance.canKeepUsing();
}

public void tickAbility() {
if (startedUsing) {
holdTicks++;
Expand Down Expand Up @@ -182,7 +186,7 @@ public enum UseType implements UseActivate, UseProgressActive {
HOLD((keyState, oldState, controller) -> {
if (keyState && !oldState)
controller.activateAbility();
else if (keyState)
else if (keyState && controller.canKeepUsing())
controller.tickAbility();
else if (oldState) {
controller.deactivateAbility();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package net.ltxprogrammer.changed.ability;

import net.ltxprogrammer.changed.Changed;
import net.ltxprogrammer.changed.init.ChangedKeyMappings;
import net.ltxprogrammer.changed.network.packet.AbilityPayloadPacket;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraftforge.network.PacketDistributor;

import java.util.function.Supplier;

Expand Down Expand Up @@ -78,6 +83,26 @@ public void readData(CompoundTag tag) {
if (tag.contains("Controller"))
controller.readData(tag.getCompound("Controller"));
}

public void acceptPayload(CompoundTag tag) {}

public void sendPayload(CompoundTag tag) {
if (this.entity.getLevel().isClientSide) {
Changed.PACKET_HANDLER.sendToServer(
new AbilityPayloadPacket(this.entity.getEntity().getId(), this.ability, tag));
} else {
Changed.PACKET_HANDLER.send(PacketDistributor.TRACKING_ENTITY_AND_SELF.with(this.entity::getEntity),
new AbilityPayloadPacket(this.entity.getEntity().getId(), this.ability, tag));
}
}

public void sendPayload(CompoundTag tag, Player destination) {
if (destination instanceof ServerPlayer serverPlayer) {
Changed.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer),
new AbilityPayloadPacket(this.entity.getEntity().getId(), this.ability, tag));
}
}

public AbstractAbility.UseType getUseType() {
return ability.getUseType(entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public SirenSingAbility() {
public UseType getUseType(IAbstractChangedEntity entity) {
return UseType.HOLD;
}

@Override
public int getCoolDown(IAbstractChangedEntity entity) {
return 90 * 20; // 90 Seconds
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package net.ltxprogrammer.changed.ability;

import net.ltxprogrammer.changed.effect.Shock;
import net.ltxprogrammer.changed.entity.beast.LatexSiren;
import net.ltxprogrammer.changed.entity.ChangedEntity;
import net.ltxprogrammer.changed.entity.variant.TransfurVariant;
import net.ltxprogrammer.changed.init.ChangedSounds;
import net.ltxprogrammer.changed.init.ChangedTags;
import net.ltxprogrammer.changed.util.CameraUtil;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;

import javax.annotation.Nullable;
import java.util.Random;

public class SirenSingAbilityInstance extends AbstractAbilityInstance {
Expand All @@ -25,44 +32,62 @@ public boolean canUse() {

@Override
public boolean canKeepUsing() {
return true;
return lastSingTick >= entity.getEntity().tickCount;
}

@Override
public void startUsing() {
final var self = entity.getEntity();
self.playSound(ChangedSounds.SIREN, 1, 1);
lastSingTick = entity.getEntity().tickCount + (8 * 20) + 10;
}

public void applyEffect(@Nullable LivingEntity livingEntity) {
if (livingEntity == null)
return;

Shock.setNoControlTicks(livingEntity, 5);
Random random = new Random(livingEntity.getId() + ((livingEntity.getId() + livingEntity.tickCount) / 10));
Vec3 randomXZdir = (new Vec3(random.nextDouble(-1, 1), 0, random.nextDouble(-1, 1))).normalize();

CameraUtil.tugEntityLookDirection(livingEntity, randomXZdir, 0.125);

if (!livingEntity.isOnGround())
return;
final double moveScale = livingEntity.getSpeed() * 0.8 * (livingEntity instanceof Player ? 10.0 : 1.0);
livingEntity.travel(randomXZdir.multiply(moveScale, 0, moveScale));
}

@Override
public void tick() {
var self = entity.getEntity();
var level = entity.getLevel();

if (!(self instanceof LatexSiren siren) || !siren.wantToSing())
return;

if (lastSingTick < self.tickCount) {
siren.setSilent(self.isSilent());
siren.playSound(ChangedSounds.SIREN, 1, 1);
lastSingTick = self.tickCount + (8 * 20) + 10;
}
self.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SLOWDOWN, 10, 1, false, false, false));

level.getNearbyEntities(LivingEntity.class, TargetingConditions.DEFAULT, self,
new AABB(self.blockPosition()).inflate(8)).forEach(livingEntity -> {
if (TransfurVariant.getEntityVariant(livingEntity) != null)
return;

Shock.setNoControlTicks(livingEntity, 5);
if (!livingEntity.isOnGround())
if (!livingEntity.getType().is(ChangedTags.EntityTypes.HUMANOIDS))
return;
Random random = new Random(livingEntity.getId() + (livingEntity.tickCount / 10));
Vec3 randomXZdir = new Vec3(random.nextDouble(-1, 1), 0, random.nextDouble(-1, 1));
randomXZdir = randomXZdir.normalize();
final double moveScale = livingEntity.getSpeed() * 0.8;
livingEntity.travel(randomXZdir.multiply(moveScale, 0, moveScale));

if (self instanceof ChangedEntity && livingEntity instanceof Player player) {
var tag = new CompoundTag();
tag.putUUID("id", player.getUUID());
this.sendPayload(tag, player);
} else {
applyEffect(livingEntity);
}
});
}

@Override
public void acceptPayload(CompoundTag tag) {
super.acceptPayload(tag);
applyEffect(this.entity.getLevel().getPlayerByUUID(tag.getUUID("id")));
}

@Override
public void stopUsing() {

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/net/ltxprogrammer/changed/client/NullInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.ltxprogrammer.changed.client;

import net.minecraft.client.player.Input;
import net.minecraft.world.phys.Vec2;
import org.jetbrains.annotations.NotNull;

public class NullInput extends Input {
@Override
public void tick(boolean movingSlowly) {
super.tick(movingSlowly);
leftImpulse = 0.0f;
forwardImpulse = 0.0f;
up = false;
down = false;
left = false;
right = false;
jumping = false;
shiftKeyDown = false;
}

@Override
public @NotNull Vec2 getMoveVector() {
return Vec2.ZERO;
}

@Override
public boolean hasForwardImpulse() {
return false;
}
}
8 changes: 7 additions & 1 deletion src/main/java/net/ltxprogrammer/changed/effect/Shock.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public static void setNoControlTicks(LivingEntity entity, int ticks) {
ext.setNoControlTicks(ticks);
}

public static int getNoControlTicks(LivingEntity entity) {
if (entity instanceof LivingEntityDataExtension ext)
return ext.getNoControlTicks();
return 0;
}

public Shock() {
super(MobEffectCategory.HARMFUL, 14688288);
setRegistryName("shock");
Expand All @@ -31,6 +37,6 @@ public boolean isDurationEffectTick(int duration, int amplifier) {
@Override
public void applyEffectTick(LivingEntity livingEntity, int amplifier) {
super.applyEffectTick(livingEntity, amplifier);
livingEntity.makeStuckInBlock(Blocks.AIR.defaultBlockState(), new Vec3(0, 1, 0));
setNoControlTicks(livingEntity, 5);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -516,19 +516,6 @@ private LivingEntity maybeGetUnderlying() {
return underlyingPlayer != null ? underlyingPlayer : this;
}

private static void bonusHurt(LivingEntity entity, DamageSource source, float damage, boolean overrideImmunity) {
if (!entity.isInvulnerableTo(source) || overrideImmunity) {
boolean justHit = entity.invulnerableTime == 20 && entity.hurtDuration == 10;

if (justHit || entity.invulnerableTime <= 0 || overrideImmunity) {
if (entity.getHealth() - damage > 0)
entity.setHealth(entity.getHealth() - damage);
else
entity.hurt(source, Float.MAX_VALUE);
}
}
}

/**
* @param target entity to try to absorb
* @param source abstraction of the attacker
Expand Down Expand Up @@ -963,7 +950,7 @@ public void visualTick(Level level) {
this.tailDragAmountO = this.tailDragAmount;

this.tailDragAmount *= 0.75F;
this.tailDragAmount -= Math.toRadians(this.yBodyRot - this.yBodyRotO) * 0.35F;
this.tailDragAmount -= (float) (Math.toRadians(this.yBodyRot - this.yBodyRotO) * 0.35F);
this.tailDragAmount = Mth.clamp(this.tailDragAmount, -1.1F, 1.1F);

simulatedSprings.forEach((direction, map) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.datafixers.util.Pair;
import net.ltxprogrammer.changed.ability.AbstractAbility;
import net.ltxprogrammer.changed.ability.AbstractAbilityInstance;
import net.ltxprogrammer.changed.ability.IAbstractChangedEntity;
import net.ltxprogrammer.changed.entity.ChangedEntity;
import net.ltxprogrammer.changed.util.Cacheable;
import net.ltxprogrammer.changed.util.CollectionUtil;
Expand All @@ -15,14 +14,12 @@
public class UseAbilityGoal extends Goal {
private final Cacheable<Map<AbstractAbility<?>, Pair<Predicate<AbstractAbilityInstance>, AbstractAbilityInstance>>> abilitiesCache;
private final ChangedEntity latex;
private final IAbstractChangedEntity abstractLatex;
private AbstractAbility<?> selectedAbility = null;
public AbstractAbility<?> selectedAbility = null;

public UseAbilityGoal(
Cacheable<Map<AbstractAbility<?>, Pair<Predicate<AbstractAbilityInstance>, AbstractAbilityInstance>>> abilities, ChangedEntity latex) {
this.abilitiesCache = abilities;
this.latex = latex;
this.abstractLatex = IAbstractChangedEntity.forEntity(latex);
}

public AbstractAbilityInstance getSelectedAbility() {
Expand All @@ -47,15 +44,13 @@ public AbstractAbilityInstance reselectNewAbility() {

if (selectedAbility == null) {
CollectionUtil.shuffle(abilities.entrySet().stream(), latex.level.random).forEach(entry -> {
if (selectedAbility == null && entry.getValue().getFirst().test(entry.getValue().getSecond())) {
if (selectedAbility == null && entry.getValue().getFirst().test(entry.getValue().getSecond()) &&
!entry.getValue().getSecond().getController().isCoolingDown()) {
selectedAbility = entry.getKey();
}
});


if (selectedAbility == null) {
selected = getSelectedAbility();
}
selected = getSelectedAbility();
}

return selected;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.ltxprogrammer.changed.mixin.client;

import net.ltxprogrammer.changed.entity.LivingEntityDataExtension;
import net.minecraft.client.Minecraft;
import net.minecraft.client.MouseHandler;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(MouseHandler.class)
public abstract class MouseHandlerMixin {
@Shadow @Final private Minecraft minecraft;

@Inject(method = "turnPlayer", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/player/LocalPlayer;turn(DD)V"), cancellable = true)
public void orDontTurnPlayer(CallbackInfo ci) {
if (this.minecraft.player instanceof LivingEntityDataExtension ext && ext.getNoControlTicks() > 0)
ci.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ public void tick(CallbackInfo callback) {
move.setWantedPosition(move.getWantedX(), move.getWantedY(), move.getWantedZ(), move.getSpeedModifier());
}

if ((Entity)this instanceof Player player)
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> LocalUtil.mulInputImpulse(player, 0.05F));
/*if ((Entity)this instanceof Player player)
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> LocalUtil.mulInputImpulse(player, 0.05F));*/

--controlDisabledFor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import com.mojang.authlib.GameProfile;
import net.ltxprogrammer.changed.Changed;
import net.ltxprogrammer.changed.client.NullInput;
import net.ltxprogrammer.changed.entity.LivingEntityDataExtension;
import net.ltxprogrammer.changed.entity.PlayerDataExtension;
import net.ltxprogrammer.changed.process.ProcessTransfur;
import net.ltxprogrammer.changed.util.InputWrapper;
Expand All @@ -24,14 +26,15 @@
import org.spongepowered.asm.mixin.Final;
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;

@OnlyIn(Dist.CLIENT)
@Mixin(LocalPlayer.class)
public abstract class LocalPlayerMixin extends AbstractClientPlayer implements PlayerDataExtension {
public abstract class LocalPlayerMixin extends AbstractClientPlayer implements PlayerDataExtension, LivingEntityDataExtension {
public LocalPlayerMixin(ClientLevel p_108548_, GameProfile p_108549_) {
super(p_108548_, p_108549_);
}
Expand Down Expand Up @@ -158,10 +161,24 @@ public void isMovingSlowly(CallbackInfoReturnable<Boolean> callback) {
});
}

@Unique
private Input inputCopy = null;

@Inject(method = "tick", at = @At("HEAD"))
public void tick(CallbackInfo callback) {
var playerMover = getPlayerMover();
if (playerMover != null && playerMover.shouldRemoveMover(this, InputWrapper.from(this), LogicalSide.CLIENT))
setPlayerMover(null);

boolean isNullInput = input instanceof NullInput;
if (this.getNoControlTicks() > 0) {
if (!isNullInput) {
inputCopy = input;
input = new NullInput();
}
} else if (isNullInput) {
input = inputCopy;
inputCopy = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void registerPackets() {
addNetworkMessage(CustomFallable.UpdateFallingBlockEntityData.class, CustomFallable.UpdateFallingBlockEntityData::new);
addNetworkMessage(SeatEntityInfoPacket.class, SeatEntityInfoPacket::new);
addNetworkMessage(TransfurEntityEventPacket.class, TransfurEntityEventPacket::new);
addNetworkMessage(AbilityPayloadPacket.class, AbilityPayloadPacket::new);
}

private <T> void addNetworkMessage(Class<T> messageType, BiConsumer<T, FriendlyByteBuf> encoder, Function<FriendlyByteBuf, T> decoder,
Expand Down
Loading

0 comments on commit ec19fe7

Please sign in to comment.