Skip to content

Commit

Permalink
Use getWidth and getHeight for Bukkit entities, once available.
Browse files Browse the repository at this point in the history
* Simplify MC version string.
  • Loading branch information
asofold committed Apr 2, 2017
1 parent c017d00 commit 0491fa7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion NCPCompatBukkit/pom.xml
Expand Up @@ -17,7 +17,7 @@
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.9.4-R0.1-SNAPSHOT</version>
<version>1.11.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Expand Up @@ -38,6 +38,7 @@
public class MCAccessBukkitBase implements MCAccess {

// private AlmostBoolean entityPlayerAvailable = AlmostBoolean.MAYBE;
protected final boolean bukkitHasGetHeightAndGetWidth;

/**
* Constructor to let it fail.
Expand All @@ -48,13 +49,16 @@ public MCAccessBukkitBase() {
Material.AIR.isOccluding();
Material.AIR.isTransparent();
// TODO: Deactivate checks that might not work. => MCAccess should have availability method, NCP deactivates check on base of that.
// TODO: Move getHeight and the like to EntityAccessXY.
bukkitHasGetHeightAndGetWidth = ReflectionUtil.getMethodNoArgs(Entity.class, "getHeight", double.class) != null
&& ReflectionUtil.getMethodNoArgs(Entity.class, "getWidth", double.class) != null;
}

@Override
public String getMCVersion() {
// Bukkit API.
// TODO: maybe output something else.
return "1.4.6|1.4.7|1.5.x|1.6.x|1.7.x|1.8.x|1.9.x|?"; // 1.8.x is bold!
return "1.4.6-1.11.2|?"; // uh oh
}

@Override
Expand Down Expand Up @@ -84,8 +88,13 @@ public BlockCache getBlockCache(final World world) {

@Override
public double getHeight(final Entity entity) {
// TODO: Copy defaults like with widths.
final double entityHeight = 1.0;
double entityHeight;
if (bukkitHasGetHeightAndGetWidth) {
entityHeight = entity.getHeight();
}
else {
entityHeight = 1.0;
}
if (entity instanceof LivingEntity) {
return Math.max(((LivingEntity) entity).getEyeHeight(), entityHeight);
} else {
Expand All @@ -105,8 +114,7 @@ public AlmostBoolean isBlockSolid(final int id) {
}
}

@Override
public double getWidth(final Entity entity) {
private final double legacyGetWidth(final Entity entity) {
// TODO: Make readable from file for defaults + register individual getters where appropriate.
// TODO: For height too. [Automatize most by spawning + checking?]
// Values taken from 1.7.10.
Expand Down Expand Up @@ -207,7 +215,16 @@ public double getWidth(final Entity entity) {
} catch (Throwable t) {}
// Default entity width.
return 0.6f;
}

@Override
public double getWidth(final Entity entity) {
if (bukkitHasGetHeightAndGetWidth) {
return entity.getWidth();
}
else {
return legacyGetWidth(entity);
}
}

@Override
Expand Down
Expand Up @@ -70,7 +70,7 @@ else if (GenericVersion.compareVersions(mcVersion, "1.11") > 0) {
@Override
public String getMCVersion() {
// Potentially all :p.
return "1.4.5-1.11|?";
return "1.4.5-1.11.2|?";
}

@Override
Expand Down Expand Up @@ -173,6 +173,9 @@ public AlmostBoolean isBlockLiquid(final int id) {

@Override
public double getHeight(Entity entity) {
if (bukkitHasGetHeightAndGetWidth) {
return super.getHeight(entity);
}
try {
return helper.getHeight(entity);
}
Expand All @@ -183,6 +186,9 @@ public double getHeight(Entity entity) {

@Override
public double getWidth(Entity entity) {
if (bukkitHasGetHeightAndGetWidth) {
return super.getWidth(entity);
}
try {
return helper.getWidth(entity);
}
Expand Down
Expand Up @@ -128,8 +128,9 @@ public BlockCache getBlockCache(final World world) {

@Override
public double getHeight(final Entity entity) {
// (entity.getHeight() returns the length field, but we access nms anyway.)
final net.minecraft.server.v1_11_R1.Entity mcEntity = ((CraftEntity) entity).getHandle();
AxisAlignedBB boundingBox = mcEntity.getBoundingBox();
final AxisAlignedBB boundingBox = mcEntity.getBoundingBox();
final double entityHeight = Math.max(mcEntity.length, Math.max(mcEntity.getHeadHeight(), boundingBox.e - boundingBox.b));
if (entity instanceof LivingEntity) {
return Math.max(((LivingEntity) entity).getEyeHeight(), entityHeight);
Expand Down Expand Up @@ -171,7 +172,7 @@ public AlmostBoolean isBlockLiquid(final int id) {

@Override
public double getWidth(final Entity entity) {
return ((CraftEntity) entity).getHandle().width;
return entity.getWidth();
}

@Override
Expand Down
Expand Up @@ -42,7 +42,11 @@
public interface MCAccess extends IGetBlockCache, IEntityAccessDimensions {

/**
* Simple version identifiers, if several must be separated by '|' like "1.4.2|1.4.4|1.4.5", to indicate multiple sub-versions supported use "1.5.x", use "?" to indicate general future support.
* Simple/rough version information, separate several individual versions by
* '|' ranges with '-', potential future support with '?', e.g.
* "1.x.1|1.x.2|2.3.4-3.4.5|?". For large ranges, don't expect all versions
* between to be supported.
*
* @return
*/
public String getMCVersion();
Expand Down

0 comments on commit 0491fa7

Please sign in to comment.