Unicopia/src/main/java/com/minelittlepony/unicopia/client/ClientInteractionManager.java

163 lines
7.2 KiB
Java
Raw Normal View History

2020-04-25 13:32:33 +02:00
package com.minelittlepony.unicopia.client;
2023-10-09 23:05:41 +02:00
import java.lang.ref.WeakReference;
import java.util.Map;
2022-10-07 16:52:35 +02:00
import java.util.Optional;
2022-09-18 23:05:28 +02:00
import java.util.function.Predicate;
2023-10-09 23:05:41 +02:00
import java.util.function.Supplier;
2022-09-18 23:05:28 +02:00
2021-08-04 15:38:03 +02:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
2020-04-25 13:32:33 +02:00
2023-10-09 23:05:41 +02:00
import com.minelittlepony.unicopia.EquinePredicates;
2021-08-17 20:55:35 +02:00
import com.minelittlepony.unicopia.FlightType;
2020-04-25 13:32:33 +02:00
import com.minelittlepony.unicopia.InteractionManager;
2021-08-13 14:46:54 +02:00
import com.minelittlepony.unicopia.USounds;
2022-10-07 16:52:35 +02:00
import com.minelittlepony.unicopia.ability.magic.CasterView;
2021-12-31 23:00:18 +01:00
import com.minelittlepony.unicopia.client.gui.DismissSpellScreen;
import com.minelittlepony.unicopia.client.gui.spellbook.ClientChapters;
import com.minelittlepony.unicopia.client.sound.*;
2021-08-17 20:55:35 +02:00
import com.minelittlepony.unicopia.entity.player.PlayerPhysics;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.entity.player.dummy.DummyClientPlayerEntity;
2023-04-30 11:46:33 +02:00
import com.minelittlepony.unicopia.server.world.Ether;
2020-04-25 13:32:33 +02:00
import com.mojang.authlib.GameProfile;
2023-10-09 23:05:41 +02:00
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
2020-04-25 13:32:33 +02:00
import net.minecraft.client.MinecraftClient;
2021-08-13 14:46:54 +02:00
import net.minecraft.client.sound.AggressiveBeeSoundInstance;
import net.minecraft.client.sound.MovingMinecartSoundInstance;
import net.minecraft.client.sound.PassiveBeeSoundInstance;
2023-10-09 23:05:41 +02:00
import net.minecraft.client.sound.TickableSoundInstance;
2020-04-25 13:32:33 +02:00
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
2021-08-13 14:46:54 +02:00
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.passive.BeeEntity;
2020-04-25 13:32:33 +02:00
import net.minecraft.entity.player.PlayerEntity;
2021-08-13 14:46:54 +02:00
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
import net.minecraft.network.PacketByteBuf;
2022-10-07 16:52:35 +02:00
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.Identifier;
2022-06-25 00:19:55 +02:00
import net.minecraft.util.math.random.Random;
2022-10-07 16:52:35 +02:00
import net.minecraft.world.BlockView;
2021-08-18 17:30:59 +02:00
import net.minecraft.world.World;
2020-04-25 13:32:33 +02:00
public class ClientInteractionManager extends InteractionManager {
2021-08-13 14:46:54 +02:00
private final MinecraftClient client = MinecraftClient.getInstance();
2022-10-07 16:52:35 +02:00
private final Optional<CasterView> clientWorld = Optional.of(() -> MinecraftClient.getInstance().world);
2023-10-09 23:05:41 +02:00
private final Int2ObjectMap<WeakReference<TickableSoundInstance>> playingSounds = new Int2ObjectOpenHashMap<>();
2022-10-07 16:52:35 +02:00
@Override
public Optional<CasterView> getCasterView(BlockView view) {
if (view instanceof ServerWorld world) {
return Optional.of(Ether.get(world));
}
return clientWorld;
}
2021-08-13 14:46:54 +02:00
@Override
public Map<Identifier, ?> readChapters(PacketByteBuf buffer) {
return buffer.readMap(PacketByteBuf::readIdentifier, ClientChapters::loadChapter);
2021-08-13 14:46:54 +02:00
}
@Override
2022-06-25 00:19:55 +02:00
public void playLoopingSound(Entity source, int type, long seed) {
client.execute(() -> {
2023-08-06 20:53:06 +02:00
if (type == SOUND_EARS_RINGING && source instanceof LivingEntity living) {
2023-10-09 23:05:41 +02:00
play(type, () -> new LoopingSoundInstance<>(living,
createTicker(100).and(e -> !e.isRemoved()),
2022-09-18 23:05:28 +02:00
USounds.ENTITY_PLAYER_EARS_RINGING, 0.01F, 2, Random.create(seed)).setFadeIn()
);
2023-08-06 20:53:06 +02:00
} else if (type == SOUND_BEE && source instanceof BeeEntity bee) {
2023-10-09 23:05:41 +02:00
play(type, () ->
2023-08-06 20:53:06 +02:00
bee.hasAngerTime()
? new AggressiveBeeSoundInstance(bee)
: new PassiveBeeSoundInstance(bee)
);
2023-08-06 20:53:06 +02:00
} else if (type == SOUND_MINECART && source instanceof AbstractMinecartEntity minecart) {
2023-10-09 23:05:41 +02:00
play(type, () -> new MovingMinecartSoundInstance(minecart));
2023-08-06 20:53:06 +02:00
} else if (type == SOUND_CHANGELING_BUZZ && source instanceof PlayerEntity player) {
2023-10-09 23:05:41 +02:00
play(type, () -> new MotionBasedSoundInstance<>(USounds.ENTITY_PLAYER_CHANGELING_BUZZ, player, e -> {
PlayerPhysics physics = Pony.of(e).getPhysics();
return physics.isFlying() && physics.getFlightType() == FlightType.INSECTOID;
2023-08-06 20:53:06 +02:00
}, 0.25F, 0.5F, 0.66F, Random.create(seed)));
} else if (type == SOUND_GLIDING && source instanceof PlayerEntity player && isClientPlayer(player)) {
2023-10-09 23:05:41 +02:00
play(type, () -> new MotionBasedSoundInstance<>(USounds.Vanilla.ITEM_ELYTRA_FLYING, player, e -> {
2023-08-06 20:53:06 +02:00
Pony pony = Pony.of(e);
return pony.getPhysics().isFlying() && pony.getPhysics().getFlightType().isAvian();
}, 0, 1, 1, Random.create(seed)));
} else if (type == SOUND_GLIDING && source instanceof PlayerEntity player) {
2023-10-09 23:05:41 +02:00
play(type, () -> new MotionBasedSoundInstance<>(USounds.ENTITY_PLAYER_PEGASUS_FLYING, player, e -> {
2023-08-06 20:53:06 +02:00
Pony pony = Pony.of(e);
return pony.getPhysics().isFlying() && pony.getPhysics().getFlightType().isAvian();
}, 0, 1, 1, Random.create(seed)));
2022-01-11 19:42:50 +01:00
} else if (type == SOUND_MAGIC_BEAM) {
2023-10-09 23:05:41 +02:00
play(type, () -> new LoopedEntityTrackingSoundInstance(USounds.SPELL_CAST_SHOOT, 0.3F, 1F, source, seed));
2022-12-26 20:21:18 +01:00
} else if (type == SOUND_HEART_BEAT) {
2023-10-09 23:05:41 +02:00
play(type, () -> new NonLoopingFadeOutSoundInstance(USounds.ENTITY_PLAYER_HEARTBEAT_LOOP, SoundCategory.PLAYERS, 0.3F, Random.create(seed), 80L));
} else if (type == SOUND_KIRIN_RAGE) {
play(type, () -> new FadeOutSoundInstance(USounds.ENTITY_PLAYER_KIRIN_RAGE_LOOP, SoundCategory.AMBIENT, 0.3F, Random.create(seed)) {
@Override
protected boolean shouldKeepPlaying() {
return EquinePredicates.RAGING.test(source);
}
});
}
});
2021-08-13 14:46:54 +02:00
}
2023-10-09 23:05:41 +02:00
private void play(int type, Supplier<TickableSoundInstance> soundSupplier) {
WeakReference<TickableSoundInstance> activeSound = playingSounds.get(type);
TickableSoundInstance existing;
if (activeSound == null || (existing = activeSound.get()) == null || existing.isDone()) {
existing = soundSupplier.get();
playingSounds.put(type, new WeakReference<>(existing));
playNow(existing);
}
}
private void playNow(TickableSoundInstance sound) {
client.getSoundManager().playNextTick(sound);
}
2021-08-13 14:46:54 +02:00
2022-09-18 23:05:28 +02:00
static Predicate<LivingEntity> createTicker(int ticks) {
int[] ticker = new int[] {ticks};
return entity -> ticker[0]-- > 0;
}
2021-12-31 23:00:18 +01:00
@Override
public void openScreen(int type) {
client.execute(() -> {
if (type == SCREEN_DISPELL_ABILITY) {
client.setScreen(new DismissSpellScreen());
}
});
}
2020-04-25 13:32:33 +02:00
@Override
2021-08-04 15:38:03 +02:00
@NotNull
2021-08-18 17:30:59 +02:00
public PlayerEntity createPlayer(World world, GameProfile profile) {
if (world instanceof ClientWorld) {
return new DummyClientPlayerEntity((ClientWorld)world, profile);
2020-04-25 13:32:33 +02:00
}
2021-08-18 17:30:59 +02:00
return super.createPlayer(world, profile);
2020-04-25 13:32:33 +02:00
}
@Override
public boolean isClientPlayer(@Nullable PlayerEntity player) {
2021-08-13 14:46:54 +02:00
return (client.player != null && player != null)
&& (client.player == player
|| Pony.equal(client.player, player));
2020-04-25 13:32:33 +02:00
}
@Override
public int getViewMode() {
2021-08-13 14:46:54 +02:00
return client.options.getPerspective().ordinal();
2020-04-25 13:32:33 +02:00
}
}