commit 01af5555591b4b8cdca104f63384cc36b5c6277b
parent d30595bde47045b5249f19ebdaa99854351ae6f7
Author: Amb0s <ambos@disroot.org>
Date: Fri, 25 Aug 2023 18:58:55 +0200
Added helper methods and removed the old ones
Diffstat:
1 file changed, 23 insertions(+), 6 deletions(-)
diff --git a/src/main/java/turniplabs/simpletech/SimpleTech.java b/src/main/java/turniplabs/simpletech/SimpleTech.java
@@ -102,16 +102,33 @@ public class SimpleTech implements ModInitializer {
LOGGER.info("Simple Tech initialized.");
}
- public static int getRedstoneFromMetadata(int metadata) {
- return metadata >> 4; // or 3?
+ public static int setBit(int number, int position, int bit) {
+ // Sets a bit at position p in n to b.
+ // Returns modified n.
+ // https://www.geeksforgeeks.org/modify-bit-given-position/
+ int mask = 1 << position;
+ return (number & ~mask) | ((bit << position) & mask);
}
- public static int getDirectionFromMetadata(int metadata) {
- return metadata & 7;
+ public static int getBit(int number, int offset) {
+ // https://stackoverflow.com/questions/9354860/how-to-get-the-value-of-a-bit-at-a-certain-position-from-a-byte
+ return (number >> offset) & 1;
}
- public static int createMetadata(int direction, int redstone) {
- return (redstone << 4) | direction;
+ public static int getRedstoneFromMetadata(int metadata, int redstoneOffset) {
+ return getBit(metadata, redstoneOffset);
+ }
+
+ public static int getInvertedFromMetadata(int metadata, int invertedOffset) {
+ return getBit(metadata, invertedOffset);
+ }
+
+ public static int getMetaWithRedstone(int metadata, int redstone, int redstoneOffset) {
+ return setBit(metadata, redstoneOffset, redstone);
+ }
+
+ public static int getMetaWithInverted(int metadata, int inverted, int invertedOffset) {
+ return setBit(metadata, invertedOffset, inverted);
}
public static int getOppositeDirectionById(int i) {