CryptoMorin / XSeries

Library for cross-version Minecraft Bukkit support and various efficient API methods.
https://www.spigotmc.org/threads/378136/
MIT License
403 stars 126 forks source link

[XBlock] - Comparing data on <1.13 #172

Closed Wertik closed 2 years ago

Wertik commented 2 years ago

Description

I've noticed that methods like XBlock#isSimilar(Block, XMaterial) only compare the block type and ignore raw byte data on versions <1.13. I think it would be useful to either have a method that does this, or modify #isSimilar to do so.

Example: (Version 1.8)

// Load a material from config or somewhere to match, let's say LIME_WOOL
XMaterial xMaterial = XMaterial.matchXMaterial("LIME_WOOL");
// If the player mines LIME_WOOL (WOOL, data: 5), something happens..
if (XBlock.isSimilar(event.getBlock(), xMaterial) {
  // Something happens...
}

With the current #isSimilar, this would match any type (color in this case) of wool. This seems like an unexpected behaviour given there's no alternative.

XBlock#getType(Block) seemed like a way to go, but it has been deprecated. I realize there's probably a good reason behind that. However, is there anything wrong about the following solution?

(XBlock#compareType for ex.)

@SuppressWarnings("deprecation")
public static boolean compareType(Block block, XMaterial material) {
    XMaterial blockMaterial = XMaterial.matchXMaterial(block.getType());

    if (blockMaterial != material) {
        return false;
    }

    return ISFLAT || block.getData() == material.getData();
}

This would match flattened types on 1.13, as well as raw byte data on <1.13.

I tried to do my research, if I missed anything I'm sorry.

CryptoMorin commented 2 years ago

Block's raw data is affected by several factors that are not considered as a Material distinguishing factor. For example the direction that stairs are facing is encoded in the data value. Or they're mixed, you can put down wood blocks differently so you have wood type and its direction mixed as a bit flag pattern.

Wertik commented 2 years ago

I tried to dig deeper and played around with logs. Now I get it. It's a mess.

Thanks for the reply.