forked from Sascha-T/create-computing
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtils.java
More file actions
84 lines (77 loc) · 2.78 KB
/
Utils.java
File metadata and controls
84 lines (77 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package de.saschat.createcomputing;
import com.simibubi.create.foundation.utility.RegisteredObjects;
import net.minecraft.core.Direction;
import net.minecraft.nbt.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.registries.ForgeRegistries;
import java.util.HashMap;
import java.util.Map;
public class Utils {
public static Item getByName(ResourceLocation loc) {
var itemRegistryObject = ForgeRegistries.ITEMS.getEntries().stream().filter(a -> {
return RegisteredObjects.getKeyOrThrow(a.getValue()).equals(loc);
}).findFirst().orElseGet(() -> null);
if (itemRegistryObject == null)
return Items.AIR;
return itemRegistryObject.getValue();
}
public interface Receiver<T> {
void receive(T a);
}
public static VoxelShape rotate(Direction from, Direction to, VoxelShape shape) {
VoxelShape[] buffer = new VoxelShape[]{ shape, Shapes.empty() };
int times = (to.get2DDataValue() - from.get2DDataValue() + 4) % 4;
for (int i = 0; i < times; i++) {
buffer[0].forAllBoxes((minX, minY, minZ, maxX, maxY, maxZ) -> buffer[1] = Shapes.or(buffer[1], Shapes.create(1-maxZ, minY, minX, 1-minZ, maxY, maxX)));
buffer[0] = buffer[1];
buffer[1] = Shapes.empty();
}
return buffer[0];
}
public static Object blowNBT(Tag tag) {
Map<Object, Object> ret = new HashMap<>();
if (tag == null)
return ret;
if (tag instanceof CompoundTag com) {
for (String key : com.getAllKeys()) {
ret.put(key, blowNBT(com.get(key)));
}
return ret;
}
if (tag instanceof CollectionTag lis) {
int idx = 1;
for (Object t : lis) {
ret.put(idx, t instanceof Tag ? blowNBT((Tag) t) : t);
idx++;
}
return ret;
}
if (tag instanceof IntTag t) {
return t.getAsNumber();
}
if (tag instanceof ByteTag t) {
return t.getAsNumber();
}
if (tag instanceof ShortTag t) {
return t.getAsNumber();
}
if (tag instanceof LongTag t) {
return t.getAsNumber();
}
if (tag instanceof FloatTag t) {
return t.getAsNumber();
}
if (tag instanceof DoubleTag t) {
return t.getAsNumber();
}
if (tag instanceof StringTag t) {
return t.getAsString();
}
System.err.println("Invalid tag type: " + tag.getClass().getName());
return null;
}
}