diff --git a/src/main/java/xiamomc/morph/backends/DisguiseWrapper.java b/src/main/java/xiamomc/morph/backends/DisguiseWrapper.java index 520f2206..95747f51 100644 --- a/src/main/java/xiamomc/morph/backends/DisguiseWrapper.java +++ b/src/main/java/xiamomc/morph/backends/DisguiseWrapper.java @@ -114,14 +114,6 @@ public boolean isMobDisguise() return getEntityType().isAlive() && getEntityType() != EntityType.PLAYER; } - /** - * Gets a {@link BoundingBox} matching the current disguise - * @return A {@link BoundingBox} matching the current disguise - * @deprecated We use {@link DisguiseWrapper#getDimensions()} or {@link DisguiseWrapper#getBoundingBoxAt(double, double, double)} now - */ - @Deprecated - public abstract BoundingBox getBoundingBox(); - /** * Gets a {@link AABB} matching the current disguise at an exact position * @return A {@link AABB} matching the current disguise at the exact position @@ -154,6 +146,14 @@ public double getExceptingEyeHeight() private EntityDimensions dimensions; + /** + * 重置此Wrapper已缓存的Dimensions + */ + protected void resetDimensions() + { + this.dimensions = null; + } + private void ensureDimensionPresent() { if (dimensions != null) return; @@ -179,7 +179,8 @@ else if (getEntityType() == EntityType.ITEM_DISPLAY) if (getEntityType() != EntityType.SLIME && getEntityType() != EntityType.MAGMA_CUBE) return; - this.dimensions = EntityDimensions.fixed(0.51F * getSlimeDimensionScale(), 0.51F * getSlimeDimensionScale()); + var dimScale = getSlimeDimensionScale(); + this.dimensions = EntityDimensions.fixed(0.51F * dimScale, 0.51F * dimScale); } /** @@ -195,11 +196,12 @@ public EntityDimensions getDimensions() : dimensions; } - private Boolean ageable = null; - protected abstract boolean isBaby(); - protected abstract float getSlimeDimensionScale(); + protected float getSlimeDimensionScale() + { + return Math.max(1, getCompound().getInt("Size")); + } public abstract void setGlowingColor(ChatColor glowingColor); diff --git a/src/main/java/xiamomc/morph/backends/fallback/NilWrapper.java b/src/main/java/xiamomc/morph/backends/fallback/NilWrapper.java index ea01dfa3..d0d053c9 100644 --- a/src/main/java/xiamomc/morph/backends/fallback/NilWrapper.java +++ b/src/main/java/xiamomc/morph/backends/fallback/NilWrapper.java @@ -35,6 +35,9 @@ public void mergeCompound(CompoundTag compoundTag) { this.instance.compoundTag.merge(compoundTag); this.instance.isBaby = NbtUtils.isBabyForType(getEntityType(), compoundTag); + + if (this.getEntityType() == EntityType.MAGMA_CUBE || this.getEntityType() == EntityType.SLIME) + resetDimensions(); } @Override @@ -139,25 +142,12 @@ public void setDisguiseName(String name) this.instance.name = name; } - @Deprecated - @Override - public BoundingBox getBoundingBox() - { - return new BoundingBox(); - } - @Override protected boolean isBaby() { return instance.isBaby; } - @Override - protected float getSlimeDimensionScale() - { - return 4; - } - @Override public void setGlowingColor(ChatColor glowingColor) { diff --git a/src/main/java/xiamomc/morph/backends/libsdisg/LibsDisguiseWrapper.java b/src/main/java/xiamomc/morph/backends/libsdisg/LibsDisguiseWrapper.java index 0ed0d7ff..9096f78e 100644 --- a/src/main/java/xiamomc/morph/backends/libsdisg/LibsDisguiseWrapper.java +++ b/src/main/java/xiamomc/morph/backends/libsdisg/LibsDisguiseWrapper.java @@ -123,22 +123,6 @@ public void setDisguiseName(String name) invalidateCompound(); } - @Deprecated - @Override - public BoundingBox getBoundingBox() - { - FakeBoundingBox box; - var mobDisguise = (MobDisguise) instance; - var values = DisguiseValues.getDisguiseValues(instance.getType()); - - if (!mobDisguise.isAdult() && values.getBabyBox() != null) - box = values.getBabyBox(); - else - box = values.getAdultBox(); - - return new BoundingBox(0, 0, 0, box.getX(), box.getY(), box.getZ()); - } - private boolean isBaby; @Override @@ -147,13 +131,6 @@ protected boolean isBaby() return isBaby; } - @Override - protected float getSlimeDimensionScale() - { - if (!(watcher instanceof SlimeWatcher slimeWatcher)) return 1; - return slimeWatcher.getSize(); - } - @Override public void setGlowingColor(ChatColor glowingColor) { @@ -404,6 +381,17 @@ public void mergeCompound(CompoundTag compoundTag) this.isBaby = NbtUtils.isBabyForType(getEntityType(), compoundTag); invalidateCompound(); + + var watcher = instance.getWatcher(); + switch (getEntityType()) + { + case SLIME, MAGMA_CUBE -> + { + var size = compoundTag.getInt("Size"); + ((SlimeWatcher)watcher).setSize(size + 1); + resetDimensions(); + } + } } @Override diff --git a/src/main/java/xiamomc/morph/providers/VanillaDisguiseProvider.java b/src/main/java/xiamomc/morph/providers/VanillaDisguiseProvider.java index 44442274..c94560ac 100644 --- a/src/main/java/xiamomc/morph/providers/VanillaDisguiseProvider.java +++ b/src/main/java/xiamomc/morph/providers/VanillaDisguiseProvider.java @@ -35,6 +35,8 @@ import xiamomc.pluginbase.Bindables.Bindable; import java.util.List; +import java.util.Objects; +import java.util.Random; import java.util.stream.Collectors; public class VanillaDisguiseProvider extends DefaultDisguiseProvider @@ -121,9 +123,30 @@ public DisguiseResult makeWrapper(Player player, DisguiseMeta disguiseMeta, @Nul ? copyResult.wrapperInstance() //copyResult.success() -> wrapperInstance() != null : backend.createInstance(entityType); + // Make IDE happy + Objects.requireNonNull(constructedDisguise); + // 检查是否有足够的空间 if (modifyBoundingBoxes.get() && checkSpaceBoundingBox.get()) { + //手动指定史莱姆和岩浆怪的大小 + if (entityType == EntityType.SLIME || entityType == EntityType.MAGMA_CUBE) + { + var canCons = canConstruct(disguiseMeta, targetEntity, null); + + if (canCons) + { + var size = targetEntity != null + ? NbtUtils.getRawTagCompound(targetEntity).getInt("Size") + : new Random().nextInt(1, 4); + + var initialTag = new CompoundTag(); + + initialTag.putInt("Size", size); + constructedDisguise.mergeCompound(initialTag); + } + } + var loc = player.getLocation(); var box = constructedDisguise.getBoundingBoxAt(loc.x(), loc.y(), loc.z()); @@ -364,11 +387,11 @@ public boolean validForClient(DisguiseState state) } @Override - public boolean canConstruct(DisguiseMeta info, Entity targetEntity, DisguiseState theirState) + public boolean canConstruct(DisguiseMeta info, @Nullable Entity targetEntity, DisguiseState theirState) { return theirState != null ? theirState.getDisguiseWrapper().getEntityType().equals(info.getEntityType()) - : targetEntity.getType().equals(info.getEntityType()); + : targetEntity == null || targetEntity.getType().equals(info.getEntityType()); } @Override diff --git a/src/main/java/xiamomc/morph/utilities/NbtUtils.java b/src/main/java/xiamomc/morph/utilities/NbtUtils.java index 88a198c8..6b892e98 100644 --- a/src/main/java/xiamomc/morph/utilities/NbtUtils.java +++ b/src/main/java/xiamomc/morph/utilities/NbtUtils.java @@ -8,6 +8,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; @@ -19,7 +20,7 @@ public class NbtUtils * @param entity 目标实体 * @return 此实体的NBT数据,当实体为null或不为 {@link CraftEntity} 的实例时返回null */ - @Nullable + @NotNull public static CompoundTag getRawTagCompound(Entity entity) { if (entity instanceof CraftEntity craftEntity) @@ -31,7 +32,7 @@ public static CompoundTag getRawTagCompound(Entity entity) return entityDataObject.getData(); } - return null; + return new CompoundTag(); } /**