Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.20.2' into 1.20.2-folia
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Nov 1, 2023
2 parents 58bcdcd + 7a98ace commit 88b2c87
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 139 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project_version=0.13.1
project_version=0.13.3

# FM Protocols
protocols_version=8aeff50
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/xiamomc/morph/MorphManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ private void perPlayerUpdate(Player bindingPlayer, DisguiseState state)

unMorph(nilCommandSource, bindingPlayer, true, true);
}

state.getSoundHandler().update();
}

private void update()
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/xiamomc/morph/backends/DisguiseWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.util.BoundingBox;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xiamomc.morph.misc.CollisionBoxRecord;
Expand All @@ -20,13 +19,13 @@

/**
* A wrapper that holds the underlying disguise instance
* @param <T> Type of the disguise instance
* @param <TInstance> Type of the disguise instance
*/
public abstract class DisguiseWrapper<T>
public abstract class DisguiseWrapper<TInstance>
{
protected T instance;
protected TInstance instance;

public DisguiseWrapper(@NotNull T instance, DisguiseBackend<T, ? extends DisguiseWrapper<T>> backend)
public DisguiseWrapper(@NotNull TInstance instance, DisguiseBackend<TInstance, ? extends DisguiseWrapper<TInstance>> backend)
{
this.instance = instance;
this.backend = backend;
Expand All @@ -36,14 +35,14 @@ public DisguiseWrapper(@NotNull T instance, DisguiseBackend<T, ? extends Disguis
* Gets the underlying disguise instance
* @return The underlying disguise instance
*/
public T getInstance()
public TInstance getInstance()
{
return instance;
}

private final DisguiseBackend<T, ? extends DisguiseWrapper<T>> backend;
private final DisguiseBackend<TInstance, ? extends DisguiseWrapper<TInstance>> backend;

public DisguiseBackend<T, ? extends DisguiseWrapper<T>> getBackend()
public DisguiseBackend<TInstance, ? extends DisguiseWrapper<TInstance>> getBackend()
{
return backend;
}
Expand Down Expand Up @@ -76,13 +75,13 @@ public T getInstance()
* Clone the underlying disguise instance
* @return A new instance cloned from the underlying disguise
*/
public abstract T copyInstance();
public abstract TInstance copyInstance();

/**
* Clone this wrapper
* @return A new wrapper cloned from this instance, everything in the new instance should not have any reference with this wrapper
*/
public abstract DisguiseWrapper<T> clone();
public abstract DisguiseWrapper<TInstance> clone();

/**
* 返回此伪装的名称
Expand Down Expand Up @@ -196,7 +195,7 @@ public EntityDimensions getDimensions()
: dimensions;
}

protected abstract boolean isBaby();
public abstract boolean isBaby();

protected float getSlimeDimensionScale()
{
Expand Down Expand Up @@ -292,8 +291,6 @@ public void dispose()

//region Temp

public abstract void resetAmbientSoundInterval();

public void showArms(boolean showarms)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ public int getNetworkEntityId()
return -1;
}

@Override
public void resetAmbientSoundInterval()
{
}

@Nullable
@Override
public <R extends Tag> R getTag(@NotNull String path, TagType<R> type)
Expand Down Expand Up @@ -143,7 +138,7 @@ public void setDisguiseName(String name)
}

@Override
protected boolean isBaby()
public boolean isBaby()
{
return instance.isBaby;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public LibsDisguiseWrapper(@NotNull Disguise instance, LibsBackend backend)
var config = depMgr.get(MorphConfigManager.class, true);

if (config == null) return;
soundFrequency = MathUtils.clamp(0, 2, config.getBindable(Double.class, ConfigOption.AMBIENT_FREQUENCY).get());
}

private final FlagWatcher watcher;
Expand Down Expand Up @@ -94,10 +93,6 @@ public DisguiseWrapper<Disguise> clone()
var newWrapper = new LibsDisguiseWrapper(instance.clone(), (LibsBackend) getBackend());
newWrapper.compoundTag.merge(this.compoundTag);

newWrapper.ambientInterval = this.ambientInterval;
newWrapper.ambientSoundPrimary = this.ambientSoundPrimary;
newWrapper.ambientSoundSecondary = this.ambientSoundSecondary;

return newWrapper;
}

Expand Down Expand Up @@ -126,7 +121,7 @@ public void setDisguiseName(String name)
private boolean isBaby;

@Override
protected boolean isBaby()
public boolean isBaby()
{
return isBaby;
}
Expand Down Expand Up @@ -230,52 +225,16 @@ public void onPostConstructDisguise(DisguiseState state, @Nullable Entity target
}

instance.setKeepDisguiseOnPlayerDeath(true);

initializeSounds();
}

private void initializeSounds()
{
var entityType = getEntityType();
var soundEvent = EntityTypeUtils.getSoundEvent(entityType);

var sound = soundEvent.sound();
if (sound == null) return;

this.ambientInterval = soundEvent.interval();
var pitch = isBaby() ? 1.5F : 1F;

this.ambientSoundPrimary = SoundUtils.toBukkitSound(soundEvent, pitch);

if (entityType == EntityType.ALLAY)
{
var allaySecondary = SoundEvents.ALLAY_AMBIENT_WITH_ITEM;
var secSi = new EntityTypeUtils.SoundInfo(allaySecondary, SoundSource.NEUTRAL, ambientInterval, soundEvent.volume());
this.ambientSoundSecondary = SoundUtils.toBukkitSound(secSi, pitch);
}
}

private final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();

public BiConsumer<FlagWatcher, Player> preUpdate;

public int ambientInterval = 0;
public Sound ambientSoundPrimary;
public Sound ambientSoundSecondary;
private int soundTime;

@Override
public void resetAmbientSoundInterval()
{
soundTime = 0;
}

private final Random random = new Random();

private net.minecraft.world.entity.player.Player nmsPlayer;

private double soundFrequency = 0D;

@Override
public void update(boolean isClone, DisguiseState state, Player player)
{
Expand All @@ -293,42 +252,6 @@ public void update(boolean isClone, DisguiseState state, Player player)
if (preUpdate != null)
preUpdate.accept(watcher, player);

soundTime++;

// Java中浮点数除以0是正或负无穷
// 因为soundFrequency永远大于等于0,而分子是1,因此frequencyScale的最大值是正无穷
// 除非soundTime最后也加到了大于等于正无穷,否则不需要额外的判断,但这真的会发生吗(
double frequencyScale = 1.0D / soundFrequency;

//logger.info("Sound: %s <-- %s(%s) --> %s".formatted(soundTime, frequency, soundFrequency, ambientInterval * frequency));
if (ambientInterval != 0 && soundTime >= ambientInterval * frequencyScale && !player.isSneaking())
{
var loc = player.getLocation();
boolean playSecondary = false;

if (getEntityType() == EntityType.ALLAY)
{
var eq = player.getEquipment();
if (!eq.getItemInMainHand().getType().isAir()) playSecondary = true;
}

Sound sound = playSecondary ? ambientSoundSecondary : ambientSoundPrimary;

var isSpectator = nmsPlayer.isSpectator();

// 和原版行为保持一致, 并且不要为旁观者播放音效:
// net.minecraft.world.entity.Mob#baseTick()
if (isSpectator)
{
soundTime = -(int)(ambientInterval * frequencyScale);
}
else if (sound != null && random.nextInt((int)(1000 * frequencyScale)) < soundTime)
{
soundTime = -(int)(ambientInterval * frequencyScale);
player.getWorld().playSound(sound, loc.getX(), loc.getY(), loc.getZ());
}
}

//对克隆的伪装手动更新一些属性
if (!isClone) return;

Expand Down Expand Up @@ -371,7 +294,6 @@ else if (sound != null && random.nextInt((int)(1000 * frequencyScale)) < soundTi
private void invalidateCompound()
{
this.tagValid = false;
MorphPlugin.getInstance().schedule(this::initializeSounds);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void onPlayerTookDamage(EntityDamageEvent e)

if (state != null)
{
state.getDisguiseWrapper().resetAmbientSoundInterval();
state.getSoundHandler().resetAmbientSoundInterval();

//如果伤害是0,那么取消事件
if (e.getDamage() > 0d)
Expand Down
Loading

0 comments on commit 88b2c87

Please sign in to comment.