Skip to content

Commit

Permalink
fix: 修复岩浆怪碰撞箱不正常的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Oct 22, 2023
1 parent 76b9458 commit ee238cc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 52 deletions.
26 changes: 14 additions & 12 deletions src/main/java/xiamomc/morph/backends/DisguiseWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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);

Expand Down
16 changes: 3 additions & 13 deletions src/main/java/xiamomc/morph/backends/fallback/NilWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/xiamomc/morph/providers/VanillaDisguiseProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/xiamomc/morph/utilities/NbtUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -31,7 +32,7 @@ public static CompoundTag getRawTagCompound(Entity entity)
return entityDataObject.getData();
}

return null;
return new CompoundTag();
}

/**
Expand Down

0 comments on commit ee238cc

Please sign in to comment.