| 1 | package net.minecraft.world.level.gameevent.vibrations;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.function.ToIntFunction;
|
| 9 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Direction;
|
| 12 | import net.minecraft.core.Holder;
|
| 13 | import net.minecraft.core.particles.VibrationParticleOption;
|
| 14 | import net.minecraft.resources.ResourceKey;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.server.level.ServerPlayer;
|
| 17 | import net.minecraft.tags.BlockTags;
|
| 18 | import net.minecraft.tags.GameEventTags;
|
| 19 | import net.minecraft.tags.TagKey;
|
| 20 | import net.minecraft.util.ExtraCodecs;
|
| 21 | import net.minecraft.util.Mth;
|
| 22 | import net.minecraft.util.Util;
|
| 23 | import net.minecraft.world.entity.Entity;
|
| 24 | import net.minecraft.world.level.ChunkPos;
|
| 25 | import net.minecraft.world.level.ClipBlockStateContext;
|
| 26 | import net.minecraft.world.level.Level;
|
| 27 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 28 | import net.minecraft.world.level.gameevent.GameEventListener;
|
| 29 | import net.minecraft.world.level.gameevent.PositionSource;
|
| 30 | import net.minecraft.world.phys.HitResult;
|
| 31 | import net.minecraft.world.phys.Vec3;
|
| 32 | import org.jspecify.annotations.Nullable;
|
| 33 |
|
| 34 | public interface VibrationSystem {
|
| 35 | List<ResourceKey<GameEvent>> RESONANCE_EVENTS = List.of(
|
| 36 | GameEvent.RESONATE_1.key(),
|
| 37 | GameEvent.RESONATE_2.key(),
|
| 38 | GameEvent.RESONATE_3.key(),
|
| 39 | GameEvent.RESONATE_4.key(),
|
| 40 | GameEvent.RESONATE_5.key(),
|
| 41 | GameEvent.RESONATE_6.key(),
|
| 42 | GameEvent.RESONATE_7.key(),
|
| 43 | GameEvent.RESONATE_8.key(),
|
| 44 | GameEvent.RESONATE_9.key(),
|
| 45 | GameEvent.RESONATE_10.key(),
|
| 46 | GameEvent.RESONATE_11.key(),
|
| 47 | GameEvent.RESONATE_12.key(),
|
| 48 | GameEvent.RESONATE_13.key(),
|
| 49 | GameEvent.RESONATE_14.key(),
|
| 50 | GameEvent.RESONATE_15.key()
|
| 51 | );
|
| 52 | int NO_VIBRATION_FREQUENCY = 0;
|
| 53 | ToIntFunction<ResourceKey<GameEvent>> VIBRATION_FREQUENCY_FOR_EVENT = Util.make(new Reference2IntOpenHashMap<>(), map -> {
|
| 54 | map.defaultReturnValue(0);
|
| 55 | map.put(GameEvent.STEP.key(), 1);
|
| 56 | map.put(GameEvent.SWIM.key(), 1);
|
| 57 | map.put(GameEvent.FLAP.key(), 1);
|
| 58 | map.put(GameEvent.PROJECTILE_LAND.key(), 2);
|
| 59 | map.put(GameEvent.HIT_GROUND.key(), 2);
|
| 60 | map.put(GameEvent.SPLASH.key(), 2);
|
| 61 | map.put(GameEvent.BOUNCE.key(), 2);
|
| 62 | map.put(GameEvent.ITEM_INTERACT_FINISH.key(), 3);
|
| 63 | map.put(GameEvent.PROJECTILE_SHOOT.key(), 3);
|
| 64 | map.put(GameEvent.INSTRUMENT_PLAY.key(), 3);
|
| 65 | map.put(GameEvent.ENTITY_ACTION.key(), 4);
|
| 66 | map.put(GameEvent.ELYTRA_GLIDE.key(), 4);
|
| 67 | map.put(GameEvent.UNEQUIP.key(), 4);
|
| 68 | map.put(GameEvent.ENTITY_DISMOUNT.key(), 5);
|
| 69 | map.put(GameEvent.EQUIP.key(), 5);
|
| 70 | map.put(GameEvent.ENTITY_INTERACT.key(), 6);
|
| 71 | map.put(GameEvent.SHEAR.key(), 6);
|
| 72 | map.put(GameEvent.ENTITY_MOUNT.key(), 6);
|
| 73 | map.put(GameEvent.ENTITY_DAMAGE.key(), 7);
|
| 74 | map.put(GameEvent.DRINK.key(), 8);
|
| 75 | map.put(GameEvent.EAT.key(), 8);
|
| 76 | map.put(GameEvent.CONTAINER_CLOSE.key(), 9);
|
| 77 | map.put(GameEvent.BLOCK_CLOSE.key(), 9);
|
| 78 | map.put(GameEvent.BLOCK_DEACTIVATE.key(), 9);
|
| 79 | map.put(GameEvent.BLOCK_DETACH.key(), 9);
|
| 80 | map.put(GameEvent.CONTAINER_OPEN.key(), 10);
|
| 81 | map.put(GameEvent.BLOCK_OPEN.key(), 10);
|
| 82 | map.put(GameEvent.BLOCK_ACTIVATE.key(), 10);
|
| 83 | map.put(GameEvent.BLOCK_ATTACH.key(), 10);
|
| 84 | map.put(GameEvent.PRIME_FUSE.key(), 10);
|
| 85 | map.put(GameEvent.NOTE_BLOCK_PLAY.key(), 10);
|
| 86 | map.put(GameEvent.BLOCK_CHANGE.key(), 11);
|
| 87 | map.put(GameEvent.BLOCK_DESTROY.key(), 12);
|
| 88 | map.put(GameEvent.FLUID_PICKUP.key(), 12);
|
| 89 | map.put(GameEvent.BLOCK_PLACE.key(), 13);
|
| 90 | map.put(GameEvent.FLUID_PLACE.key(), 13);
|
| 91 | map.put(GameEvent.ENTITY_PLACE.key(), 14);
|
| 92 | map.put(GameEvent.LIGHTNING_STRIKE.key(), 14);
|
| 93 | map.put(GameEvent.TELEPORT.key(), 14);
|
| 94 | map.put(GameEvent.ENTITY_DIE.key(), 15);
|
| 95 | map.put(GameEvent.EXPLODE.key(), 15);
|
| 96 |
|
| 97 | for (int i = 1; i <= 15; i++) {
|
| 98 | map.put(getResonanceEventByFrequency(i), i);
|
| 99 | }
|
| 100 | });
|
| 101 |
|
| 102 | VibrationSystem.Data getVibrationData();
|
| 103 |
|
| 104 | VibrationSystem.User getVibrationUser();
|
| 105 |
|
| 106 | static int getGameEventFrequency(final Holder<GameEvent> event) {
|
| 107 | return event.unwrapKey().map(VibrationSystem::getGameEventFrequency).orElse(0);
|
| 108 | }
|
| 109 |
|
| 110 | static int getGameEventFrequency(final ResourceKey<GameEvent> event) {
|
| 111 | return VIBRATION_FREQUENCY_FOR_EVENT.applyAsInt(event);
|
| 112 | }
|
| 113 |
|
| 114 | static ResourceKey<GameEvent> getResonanceEventByFrequency(final int vibrationFrequency) {
|
| 115 | return RESONANCE_EVENTS.get(vibrationFrequency - 1);
|
| 116 | }
|
| 117 |
|
| 118 | static int getRedstoneStrengthForDistance(final float distance, final int listenerRadius) {
|
| 119 | double powerScale = 15.0 / listenerRadius;
|
| 120 | return Math.max(1, 15 - Mth.floor(powerScale * distance));
|
| 121 | }
|
| 122 |
|
| 123 | final class Data {
|
| 124 | public static final Codec<VibrationSystem.Data> CODEC = RecordCodecBuilder.create(
|
| 125 | i -> i.group(
|
| 126 | VibrationInfo.CODEC.lenientOptionalFieldOf("event").forGetter(o -> Optional.ofNullable(o.currentVibration)),
|
| 127 | VibrationSelector.CODEC.fieldOf("selector").forGetter(VibrationSystem.Data::getSelectionStrategy),
|
| 128 | ExtraCodecs.optionalAlwaysPresentFieldOf(ExtraCodecs.NON_NEGATIVE_INT, "event_delay", 0).forGetter(VibrationSystem.Data::getTravelTimeInTicks)
|
| 129 | )
|
| 130 | .apply(
|
| 131 | i,
|
| 132 | (currentVibration, selectionStrategy, travelTimeInTicks) -> new VibrationSystem.Data(
|
| 133 | currentVibration.orElse(null), selectionStrategy, travelTimeInTicks, true
|
| 134 | )
|
| 135 | )
|
| 136 | );
|
| 137 | public static final String NBT_TAG_KEY = "listener";
|
| 138 | private @Nullable VibrationInfo currentVibration;
|
| 139 | private int travelTimeInTicks;
|
| 140 | private final VibrationSelector selectionStrategy;
|
| 141 | private boolean reloadVibrationParticle;
|
| 142 |
|
| 143 | private Data(
|
| 144 | final @Nullable VibrationInfo currentVibration,
|
| 145 | final VibrationSelector selectionStrategy,
|
| 146 | final int travelTimeInTicks,
|
| 147 | final boolean reloadVibrationParticle
|
| 148 | ) {
|
| 149 | this.currentVibration = currentVibration;
|
| 150 | this.travelTimeInTicks = travelTimeInTicks;
|
| 151 | this.selectionStrategy = selectionStrategy;
|
| 152 | this.reloadVibrationParticle = reloadVibrationParticle;
|
| 153 | }
|
| 154 |
|
| 155 | public Data() {
|
| 156 | this(null, new VibrationSelector(), 0, false);
|
| 157 | }
|
| 158 |
|
| 159 | public VibrationSelector getSelectionStrategy() {
|
| 160 | return this.selectionStrategy;
|
| 161 | }
|
| 162 |
|
| 163 | public @Nullable VibrationInfo getCurrentVibration() {
|
| 164 | return this.currentVibration;
|
| 165 | }
|
| 166 |
|
| 167 | public void setCurrentVibration(final @Nullable VibrationInfo currentVibration) {
|
| 168 | this.currentVibration = currentVibration;
|
| 169 | }
|
| 170 |
|
| 171 | public int getTravelTimeInTicks() {
|
| 172 | return this.travelTimeInTicks;
|
| 173 | }
|
| 174 |
|
| 175 | public void setTravelTimeInTicks(final int travelTimeInTicks) {
|
| 176 | this.travelTimeInTicks = travelTimeInTicks;
|
| 177 | }
|
| 178 |
|
| 179 | public void decrementTravelTime() {
|
| 180 | this.travelTimeInTicks = Math.max(0, this.travelTimeInTicks - 1);
|
| 181 | }
|
| 182 |
|
| 183 | public boolean shouldReloadVibrationParticle() {
|
| 184 | return this.reloadVibrationParticle;
|
| 185 | }
|
| 186 |
|
| 187 | public void setReloadVibrationParticle(final boolean reloadVibrationParticle) {
|
| 188 | this.reloadVibrationParticle = reloadVibrationParticle;
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | class Listener implements GameEventListener {
|
| 193 | private final VibrationSystem system;
|
| 194 |
|
| 195 | public Listener(final VibrationSystem system) {
|
| 196 | this.system = system;
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public PositionSource getListenerSource() {
|
| 201 | return this.system.getVibrationUser().getPositionSource();
|
| 202 | }
|
| 203 |
|
| 204 | @Override
|
| 205 | public int getListenerRadius() {
|
| 206 | return this.system.getVibrationUser().getListenerRadius();
|
| 207 | }
|
| 208 |
|
| 209 | @Override
|
| 210 | public boolean handleGameEvent(final ServerLevel level, final Holder<GameEvent> event, final GameEvent.Context context, final Vec3 sourcePosition) {
|
| 211 | VibrationSystem.Data data = this.system.getVibrationData();
|
| 212 | VibrationSystem.User user = this.system.getVibrationUser();
|
| 213 | if (data.getCurrentVibration() != null) {
|
| 214 | return false;
|
| 215 | }
|
| 216 |
|
| 217 | if (!user.isValidVibration(event, context)) {
|
| 218 | return false;
|
| 219 | }
|
| 220 |
|
| 221 | Optional<Vec3> listenerSourcePos = user.getPositionSource().getPosition(level);
|
| 222 | if (listenerSourcePos.isEmpty()) {
|
| 223 | return false;
|
| 224 | }
|
| 225 |
|
| 226 | Vec3 destination = listenerSourcePos.get();
|
| 227 | if (!user.canReceiveVibration(level, BlockPos.containing(sourcePosition), event, context)) {
|
| 228 | return false;
|
| 229 | }
|
| 230 |
|
| 231 | if (isOccluded(level, sourcePosition, destination)) {
|
| 232 | return false;
|
| 233 | }
|
| 234 |
|
| 235 | this.scheduleVibration(level, data, event, context, sourcePosition, destination);
|
| 236 | return true;
|
| 237 | }
|
| 238 |
|
| 239 | public void forceScheduleVibration(final ServerLevel level, final Holder<GameEvent> event, final GameEvent.Context context, final Vec3 origin) {
|
| 240 | this.system
|
| 241 | .getVibrationUser()
|
| 242 | .getPositionSource()
|
| 243 | .getPosition(level)
|
| 244 | .ifPresent(p -> this.scheduleVibration(level, this.system.getVibrationData(), event, context, origin, p));
|
| 245 | }
|
| 246 |
|
| 247 | private void scheduleVibration(
|
| 248 | final ServerLevel level, final VibrationSystem.Data data, final Holder<GameEvent> event, final GameEvent.Context context, final Vec3 origin, final Vec3 dest
|
| 249 | ) {
|
| 250 | data.selectionStrategy.addCandidate(new VibrationInfo(event, (float)origin.distanceTo(dest), origin, context.sourceEntity()), level.getGameTime());
|
| 251 | }
|
| 252 |
|
| 253 | public static float distanceBetweenInBlocks(final BlockPos origin, final BlockPos dest) {
|
| 254 | return (float)Math.sqrt(origin.distSqr(dest));
|
| 255 | }
|
| 256 |
|
| 257 | private static boolean isOccluded(final Level level, final Vec3 origin, final Vec3 dest) {
|
| 258 | Vec3 from = new Vec3(Mth.floor(origin.x) + 0.5, Mth.floor(origin.y) + 0.5, Mth.floor(origin.z) + 0.5);
|
| 259 | Vec3 to = new Vec3(Mth.floor(dest.x) + 0.5, Mth.floor(dest.y) + 0.5, Mth.floor(dest.z) + 0.5);
|
| 260 |
|
| 261 | for (Direction direction : Direction.values()) {
|
| 262 | Vec3 nudgedSource = from.relative(direction, 1.0E-5F);
|
| 263 | if (level.isBlockInLine(new ClipBlockStateContext(nudgedSource, to, state -> state.is(BlockTags.OCCLUDES_VIBRATION_SIGNALS))).getType()
|
| 264 | != HitResult.Type.BLOCK) {
|
| 265 | return false;
|
| 266 | }
|
| 267 | }
|
| 268 |
|
| 269 | return true;
|
| 270 | }
|
| 271 | }
|
| 272 |
|
| 273 | interface Ticker {
|
| 274 | static void tick(final Level level, final VibrationSystem.Data data, final VibrationSystem.User user) {
|
| 275 | if (level instanceof ServerLevel serverLevel) {
|
| 276 | if (data.currentVibration == null) {
|
| 277 | trySelectAndScheduleVibration(serverLevel, data, user);
|
| 278 | }
|
| 279 |
|
| 280 | if (data.currentVibration != null) {
|
| 281 | boolean hasChanged = data.getTravelTimeInTicks() > 0;
|
| 282 | tryReloadVibrationParticle(serverLevel, data, user);
|
| 283 | data.decrementTravelTime();
|
| 284 | if (data.getTravelTimeInTicks() <= 0) {
|
| 285 | hasChanged = receiveVibration(serverLevel, data, user, data.currentVibration);
|
| 286 | }
|
| 287 |
|
| 288 | if (hasChanged) {
|
| 289 | user.onDataChanged();
|
| 290 | }
|
| 291 | }
|
| 292 | }
|
| 293 | }
|
| 294 |
|
| 295 | private static void trySelectAndScheduleVibration(final ServerLevel serverLevel, final VibrationSystem.Data data, final VibrationSystem.User user) {
|
| 296 | data.getSelectionStrategy()
|
| 297 | .chosenCandidate(serverLevel.getGameTime())
|
| 298 | .ifPresent(
|
| 299 | context -> {
|
| 300 | data.setCurrentVibration(context);
|
| 301 | Vec3 origin = context.pos();
|
| 302 | data.setTravelTimeInTicks(user.calculateTravelTimeInTicks(context.distance()));
|
| 303 | serverLevel.sendParticles(
|
| 304 | new VibrationParticleOption(user.getPositionSource(), data.getTravelTimeInTicks()), origin.x, origin.y, origin.z, 1, 0.0, 0.0, 0.0, 0.0
|
| 305 | );
|
| 306 | user.onDataChanged();
|
| 307 | data.getSelectionStrategy().startOver();
|
| 308 | }
|
| 309 | );
|
| 310 | }
|
| 311 |
|
| 312 | private static void tryReloadVibrationParticle(final ServerLevel level, final VibrationSystem.Data data, final VibrationSystem.User user) {
|
| 313 | if (data.shouldReloadVibrationParticle()) {
|
| 314 | if (data.currentVibration == null) {
|
| 315 | data.setReloadVibrationParticle(false);
|
| 316 | } else {
|
| 317 | Vec3 origin = data.currentVibration.pos();
|
| 318 | PositionSource positionSource = user.getPositionSource();
|
| 319 | Vec3 destination = positionSource.getPosition(level).orElse(origin);
|
| 320 | int travelTimeInTicks = data.getTravelTimeInTicks();
|
| 321 | int initialTravelTime = user.calculateTravelTimeInTicks(data.currentVibration.distance());
|
| 322 | double alpha = 1.0 - (double)travelTimeInTicks / initialTravelTime;
|
| 323 | double newInitialX = Mth.lerp(alpha, origin.x, destination.x);
|
| 324 | double newInitialY = Mth.lerp(alpha, origin.y, destination.y);
|
| 325 | double newInitialZ = Mth.lerp(alpha, origin.z, destination.z);
|
| 326 | boolean particleWasSent = level.sendParticles(
|
| 327 | new VibrationParticleOption(positionSource, travelTimeInTicks), newInitialX, newInitialY, newInitialZ, 1, 0.0, 0.0, 0.0, 0.0
|
| 328 | )
|
| 329 | > 0;
|
| 330 | if (particleWasSent) {
|
| 331 | data.setReloadVibrationParticle(false);
|
| 332 | }
|
| 333 | }
|
| 334 | }
|
| 335 | }
|
| 336 |
|
| 337 | private static boolean receiveVibration(
|
| 338 | final ServerLevel serverLevel, final VibrationSystem.Data data, final VibrationSystem.User user, final VibrationInfo currentVibration
|
| 339 | ) {
|
| 340 | BlockPos origin = BlockPos.containing(currentVibration.pos());
|
| 341 | BlockPos destination = user.getPositionSource().getPosition(serverLevel).map(BlockPos::containing).orElse(origin);
|
| 342 | if (user.requiresAdjacentChunksToBeTicking() && !areAdjacentChunksTicking(serverLevel, destination)) {
|
| 343 | return false;
|
| 344 | }
|
| 345 |
|
| 346 | user.onReceiveVibration(
|
| 347 | serverLevel,
|
| 348 | origin,
|
| 349 | currentVibration.gameEvent(),
|
| 350 | currentVibration.getEntity(serverLevel).orElse(null),
|
| 351 | currentVibration.getProjectileOwner(serverLevel).orElse(null),
|
| 352 | VibrationSystem.Listener.distanceBetweenInBlocks(origin, destination)
|
| 353 | );
|
| 354 | data.setCurrentVibration(null);
|
| 355 | return true;
|
| 356 | }
|
| 357 |
|
| 358 | private static boolean areAdjacentChunksTicking(final Level level, final BlockPos listenerPos) {
|
| 359 | ChunkPos listenerChunkPos = ChunkPos.containing(listenerPos);
|
| 360 |
|
| 361 | for (int x = listenerChunkPos.x() - 1; x <= listenerChunkPos.x() + 1; x++) {
|
| 362 | for (int z = listenerChunkPos.z() - 1; z <= listenerChunkPos.z() + 1; z++) {
|
| 363 | if (!level.shouldTickBlocksAt(ChunkPos.pack(x, z)) || level.getChunkSource().getChunkNow(x, z) == null) {
|
| 364 | return false;
|
| 365 | }
|
| 366 | }
|
| 367 | }
|
| 368 |
|
| 369 | return true;
|
| 370 | }
|
| 371 | }
|
| 372 |
|
| 373 | interface User {
|
| 374 | int getListenerRadius();
|
| 375 |
|
| 376 | PositionSource getPositionSource();
|
| 377 |
|
| 378 | boolean canReceiveVibration(ServerLevel level, BlockPos pos, Holder<GameEvent> event, GameEvent.Context context);
|
| 379 |
|
| 380 | void onReceiveVibration(
|
| 381 | ServerLevel level, BlockPos pos, Holder<GameEvent> event, @Nullable Entity sourceEntity, @Nullable Entity projectileOwner, float receivingDistance
|
| 382 | );
|
| 383 |
|
| 384 | default TagKey<GameEvent> getListenableEvents() {
|
| 385 | return GameEventTags.VIBRATIONS;
|
| 386 | }
|
| 387 |
|
| 388 | default boolean canTriggerAvoidVibration() {
|
| 389 | return false;
|
| 390 | }
|
| 391 |
|
| 392 | default boolean requiresAdjacentChunksToBeTicking() {
|
| 393 | return false;
|
| 394 | }
|
| 395 |
|
| 396 | default int calculateTravelTimeInTicks(final float distanceToDestination) {
|
| 397 | return Mth.floor(distanceToDestination);
|
| 398 | }
|
| 399 |
|
| 400 | default boolean isValidVibration(final Holder<GameEvent> event, final GameEvent.Context context) {
|
| 401 | if (!event.is(this.getListenableEvents())) {
|
| 402 | return false;
|
| 403 | }
|
| 404 |
|
| 405 | Entity sourceEntity = context.sourceEntity();
|
| 406 | if (sourceEntity != null) {
|
| 407 | if (sourceEntity.isSpectator()) {
|
| 408 | return false;
|
| 409 | }
|
| 410 |
|
| 411 | if (sourceEntity.isSteppingCarefully() && event.is(GameEventTags.IGNORE_VIBRATIONS_SNEAKING)) {
|
| 412 | if (this.canTriggerAvoidVibration() && sourceEntity instanceof ServerPlayer player) {
|
| 413 | CriteriaTriggers.AVOID_VIBRATION.trigger(player);
|
| 414 | }
|
| 415 |
|
| 416 | return false;
|
| 417 | }
|
| 418 |
|
| 419 | if (sourceEntity.dampensVibrations()) {
|
| 420 | return false;
|
| 421 | }
|
| 422 | }
|
| 423 |
|
| 424 | return context.affectedState() != null ? !context.affectedState().is(BlockTags.DAMPENS_VIBRATIONS) : true;
|
| 425 | }
|
| 426 |
|
| 427 | default void onDataChanged() {
|
| 428 | }
|
| 429 | }
|
| 430 | }
|
| 431 |
|