Files
EndlessIDs/src/main/java/com/falsepattern/endlessids/Hooks.java
T
2025-06-05 11:35:10 +02:00

87 lines
3.5 KiB
Java

/*
* EndlessIDs
*
* Copyright (C) 2022-2025 FalsePattern, The MEGA Team
* All Rights Reserved
*
* The above copyright notice, this permission notice and the words "MEGA"
* shall be included in all copies or substantial portions of the Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, only version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.endlessids;
import com.falsepattern.endlessids.config.GeneralConfig;
import com.falsepattern.endlessids.constants.ExtendedConstants;
import net.minecraft.block.Block;
public class Hooks {
public static int getIdFromBlockWithCheck(final Block block, final Block oldBlock) {
final int id = Block.getIdFromBlock(block);
if (GeneralConfig.catchUnregisteredBlocks && id == -1) {
throw new IllegalArgumentException("Block " + block +
" is not registered. <-- Say about this to the author of this mod, or you can try to enable \"RemoveInvalidBlocks\" option in EID config.");
}
if (id >= 0 && id <= ExtendedConstants.maxBlockID) {
return id;
}
if (id == -1) {
return Block.getIdFromBlock(oldBlock);
}
throw new IllegalArgumentException("id out of range: " + id);
}
public static void shortToByteArray(short[] shortArray, int shortOffset, byte[] byteArray, int byteOffset, int shortCount) {
for (int i = 0; i < shortCount; i++) {
short s = shortArray[shortOffset + i];
byteArray[byteOffset + i * 2] = (byte) (s & 255);
byteArray[byteOffset + i * 2 + 1] = (byte) ((s >>> 8) & 255);
}
}
public static void byteToShortArray(byte[] byteArray, int byteOffset, short[] shortArray, int shortOffset, int byteCount) {
for (int i = 0; i < byteCount; i++) {
byte b = byteArray[byteOffset + i];
shortArray[shortOffset + i / 2] = (short) (i % 2 == 0
? (b & 255)
: (shortArray[shortOffset + i / 2] | ((b & 255) << 8)));
}
}
public static byte[] shortToByteArray(short[] shortArray) {
byte[] byteArray = new byte[shortArray.length * 2];
shortToByteArray(shortArray, 0, byteArray, 0, shortArray.length);
return byteArray;
}
public static short[] byteToShortArray(byte[] byteArray) {
short[] shortArray = new short[(byteArray.length + 1) / 2];
byteToShortArray(byteArray, 0, shortArray, 0, byteArray.length);
return shortArray;
}
public static void byteToShortArrayLegacy(byte[] byteArray, short[] shortArray) {
for (int i = 0; i < 256; ++i) {
shortArray[i] = (short) (((byteArray[i + 256] << 8) & 255) | (byteArray[i] & 255));
}
}
public static void scatter(byte[] byteArray, short[] shortArray) {
for (int i = 0; i < byteArray.length; i++) {
shortArray[i] = (short) (byteArray[i] & 0xff);
}
}
}