forked from GregTechCEu/GregTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariantBlock.java
More file actions
142 lines (121 loc) · 5.22 KB
/
VariantBlock.java
File metadata and controls
142 lines (121 loc) · 5.22 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package gregtech.api.block;
import gregtech.api.util.LocalizationUtils;
import gregtech.common.creativetab.GTCreativeTabs;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
public class VariantBlock<T extends Enum<T> & IStringSerializable> extends Block {
protected PropertyEnum<T> VARIANT;
protected T[] VALUES;
public VariantBlock(@NotNull Material materialIn) {
super(materialIn);
if (VALUES.length > 0 && VALUES[0] instanceof IStateHarvestLevel) {
for (T t : VALUES) {
IStateHarvestLevel stateHarvestLevel = (IStateHarvestLevel) t;
IBlockState state = getState(t);
setHarvestLevel(stateHarvestLevel.getHarvestTool(state), stateHarvestLevel.getHarvestLevel(state),
state);
}
}
setCreativeTab(GTCreativeTabs.TAB_GREGTECH);
setDefaultState(this.blockState.getBaseState().withProperty(VARIANT, VALUES[0]));
}
@Override
public void getSubBlocks(@NotNull CreativeTabs tab, @NotNull NonNullList<ItemStack> list) {
for (T variant : VALUES) {
list.add(getItemVariant(variant));
}
}
public IBlockState getState(T variant) {
return getDefaultState().withProperty(VARIANT, variant);
}
public T getState(IBlockState blockState) {
return blockState.getValue(VARIANT);
}
public T getState(ItemStack stack) {
return getState(getStateFromMeta(stack.getItemDamage()));
}
public ItemStack getItemVariant(T variant) {
return getItemVariant(variant, 1);
}
public ItemStack getItemVariant(T variant, int amount) {
return new ItemStack(this, amount, variant.ordinal());
}
@NotNull
@Override
protected BlockStateContainer createBlockState() {
Class<T> enumClass = getActualTypeParameter(getClass(), VariantBlock.class);
this.VARIANT = PropertyEnum.create("variant", enumClass);
this.VALUES = enumClass.getEnumConstants();
return new BlockStateContainer(this, VARIANT);
}
@Override
@SideOnly(Side.CLIENT)
public void addInformation(@NotNull ItemStack stack, @Nullable World player, @NotNull List<String> tooltip,
@NotNull ITooltipFlag advanced) {
// tier less tooltip like: tile.turbine_casing.tooltip
String unlocalizedVariantTooltip = getTranslationKey() + ".tooltip";
if (I18n.hasKey(unlocalizedVariantTooltip))
Collections.addAll(tooltip, LocalizationUtils.formatLines(unlocalizedVariantTooltip));
// item specific tooltip: tile.turbine_casing.bronze_gearbox.tooltip
String unlocalizedTooltip = stack.getTranslationKey() + ".tooltip";
if (I18n.hasKey(unlocalizedTooltip))
Collections.addAll(tooltip, LocalizationUtils.formatLines(unlocalizedTooltip));
}
@Override
public int damageDropped(@NotNull IBlockState state) {
return getMetaFromState(state);
}
@NotNull
@Override
@SuppressWarnings("deprecation")
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(VARIANT, VALUES[meta % VALUES.length]);
}
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(VARIANT).ordinal();
}
@NotNull
@Override
public SoundType getSoundType(@NotNull IBlockState state, @NotNull World world, @NotNull BlockPos pos,
@Nullable Entity entity) {
if (getState(state) instanceof IStateSoundType stateSoundType) {
return stateSoundType.getSoundType(state);
}
return super.getSoundType(state, world, pos, entity);
}
// magic is here
@SuppressWarnings("unchecked")
protected static <T, R> Class<T> getActualTypeParameter(Class<? extends R> thisClass, Class<R> declaringClass) {
Type type = thisClass.getGenericSuperclass();
while (!(type instanceof ParameterizedType) || ((ParameterizedType) type).getRawType() != declaringClass) {
if (type instanceof ParameterizedType) {
type = ((Class<?>) ((ParameterizedType) type).getRawType()).getGenericSuperclass();
} else {
type = ((Class<?>) type).getGenericSuperclass();
}
}
return (Class<T>) ((ParameterizedType) type).getActualTypeArguments()[0];
}
}