-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathTools.java
98 lines (87 loc) · 3.69 KB
/
Tools.java
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package mcjty.lib.varia;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.registries.IForgeRegistryEntry;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Nullable;
import java.util.function.Consumer;
import java.util.function.Function;
public class Tools {
public static String getModid(ItemStack stack) {
if (!stack.isEmpty()) {
return stack.getItem().getRegistryName().getNamespace();
} else {
return "";
}
}
public static String getModName(IForgeRegistryEntry<?> entry) {
ResourceLocation registryName = entry.getRegistryName();
String modId = registryName == null ? "minecraft" : registryName.getNamespace();
return ModList.get().getModContainerById(modId)
.map(mod -> mod.getModInfo().getDisplayName())
.orElse(StringUtils.capitalize(modId));
}
public static <INPUT extends BASE, BASE> void safeConsume(BASE o, Consumer<INPUT> consumer, String error) {
try {
consumer.accept((INPUT) o);
} catch (ClassCastException e) {
throw new IllegalArgumentException(error, e);
}
}
public static <INPUT extends BASE, BASE> void safeConsume(BASE o, Consumer<INPUT> consumer) {
try {
consumer.accept((INPUT) o);
} catch (ClassCastException ignore) {
}
}
public static <INPUT extends BASE, BASE, RET> RET safeMap(BASE o, Function<INPUT, RET> consumer, String error) {
try {
return consumer.apply((INPUT) o);
} catch (ClassCastException e) {
throw new IllegalArgumentException(error, e);
}
}
public static String getReadableName(World world, BlockPos pos) {
BlockState state = world.getBlockState(pos);
return getReadableName(state.getBlock().getCloneItemStack(world, pos, state));
}
public static String getReadableName(ItemStack stack) {
return stack.getHoverName().getString() /* was getFormattedText() */;
}
@Nullable
public static BlockState placeStackAt(PlayerEntity player, ItemStack blockStack, World world, BlockPos pos, @Nullable BlockState origState) {
BlockRayTraceResult trace = new BlockRayTraceResult(new Vector3d(0, 0, 0), Direction.UP, pos, false);
BlockItemUseContext context = new BlockItemUseContext(new ItemUseContext(player, Hand.MAIN_HAND, trace));
if (blockStack.getItem() instanceof BlockItem) {
BlockItem itemBlock = (BlockItem) blockStack.getItem();
if (origState == null) {
origState = itemBlock.getBlock().getStateForPlacement(context);
if (origState == null) {
// Cannot place!
return null;
}
}
if (itemBlock.place(context).consumesAction()) {
blockStack.shrink(1);
}
return origState;
} else {
player.setItemInHand(Hand.MAIN_HAND, blockStack);
player.setPos(pos.getX()+.5, pos.getY()+1.5, pos.getZ()+.5);
blockStack.getItem().useOn(context);
return world.getBlockState(pos);
}
}
}