22
22
23
23
package dev .galacticraft .machinelib .api .block ;
24
24
25
- import com .google .common .base .Suppliers ;
26
25
import com .mojang .authlib .GameProfile ;
27
26
import com .mojang .serialization .MapCodec ;
28
- import com .mojang .serialization .codecs .RecordCodecBuilder ;
29
27
import dev .galacticraft .machinelib .api .block .entity .MachineBlockEntity ;
30
28
import dev .galacticraft .machinelib .api .machine .configuration .AccessLevel ;
31
29
import dev .galacticraft .machinelib .api .machine .configuration .RedstoneMode ;
40
38
import net .minecraft .core .BlockPos ;
41
39
import net .minecraft .core .component .DataComponentPatch ;
42
40
import net .minecraft .core .component .DataComponents ;
43
- import net .minecraft .core .registries .BuiltInRegistries ;
44
41
import net .minecraft .nbt .CompoundTag ;
45
42
import net .minecraft .nbt .NbtOps ;
46
43
import net .minecraft .nbt .Tag ;
47
44
import net .minecraft .network .chat .Component ;
48
45
import net .minecraft .network .chat .MutableComponent ;
49
- import net .minecraft .resources .ResourceLocation ;
50
46
import net .minecraft .server .level .ServerPlayer ;
51
47
import net .minecraft .world .InteractionResult ;
52
48
import net .minecraft .world .entity .LivingEntity ;
81
77
import java .util .Collections ;
82
78
import java .util .List ;
83
79
import java .util .Objects ;
84
- import java .util .function .Supplier ;
85
80
86
81
/**
87
82
* The base block for all machines.
88
- *
89
- * @param <Machine> The machine block entity attached to this block.
90
83
*/
91
- public class MachineBlock <Machine extends MachineBlockEntity > extends BaseEntityBlock {
92
- public static final MapCodec <MachineBlock <? extends MachineBlockEntity >> CODEC = RecordCodecBuilder .mapCodec (instance -> instance .group (
93
- propertiesCodec (),
94
- ResourceLocation .CODEC .fieldOf ("factory" ).forGetter (machineBlock -> BuiltInRegistries .BLOCK_ENTITY_TYPE .getKey (machineBlock .factory .get ()))
95
- ).apply (instance , MachineBlock ::new ));
96
-
84
+ public abstract class MachineBlock extends BaseBlock {
97
85
/**
98
86
* Represents a boolean property for specifying the active state of the machine.
87
+ *
99
88
* @see MachineBlockEntity#isActive() for the definition of 'active'
100
89
*/
101
90
public static final BooleanProperty ACTIVE = BooleanProperty .create ("active" );
102
91
103
92
/**
104
93
* Tooltip prompt text. Shown instead of the long-form description when shift is not pressed.
105
- *
106
- * @see #shiftDescription(ItemStack, TooltipContext, TooltipFlag)
107
94
*/
108
95
private static final Component PRESS_SHIFT = Component .translatable (Constant .TranslationKey .PRESS_SHIFT ).setStyle (Constant .Text .DARK_GRAY_STYLE );
109
96
110
- /**
111
- * Factory that constructs the relevant machine block entity for this block.
112
- */
113
- private final Supplier <BlockEntityType <Machine >> factory ;
114
-
115
- /**
116
- * The line-wrapped long description of this machine.
117
- *
118
- * @see #shiftDescription(ItemStack, TooltipContext, TooltipFlag)
119
- */
120
- private List <Component > description = null ;
121
-
122
97
/**
123
98
* Creates a new machine block.
124
99
*
125
100
* @param settings The settings for the block.
126
- * @param factoryId the machine block entity factory
127
101
*/
128
- public MachineBlock (Properties settings , ResourceLocation factoryId ) {
102
+ public MachineBlock (Properties settings ) {
129
103
super (settings );
130
- this .factory = Suppliers .memoize (() -> (BlockEntityType <Machine >) BuiltInRegistries .BLOCK_ENTITY_TYPE .get (factoryId ));
131
104
this .registerDefaultState (this .getStateDefinition ().any ().setValue (ACTIVE , false ));
132
105
}
133
106
@@ -153,17 +126,49 @@ public static boolean isActive(@NotNull BlockState state) {
153
126
return state .getValue (ACTIVE );
154
127
}
155
128
129
+ protected static void appendBlockEntityTooltip (ItemStack stack , List <Component > tooltip ) {
130
+ if (stack != null ) {
131
+ CustomData data = stack .getOrDefault (DataComponents .BLOCK_ENTITY_DATA , CustomData .EMPTY );
132
+ if (!data .isEmpty ()) {
133
+ CompoundTag nbt = data .getUnsafe ();
134
+ tooltip .add (Component .empty ());
135
+ if (nbt .contains (Constant .Nbt .ENERGY , Tag .TAG_INT ))
136
+ tooltip .add (Component .translatable (Constant .TranslationKey .CURRENT_ENERGY , Component .literal (String .valueOf (nbt .getInt (Constant .Nbt .ENERGY ))).setStyle (Constant .Text .BLUE_STYLE )).setStyle (Constant .Text .GOLD_STYLE ));
137
+ if (nbt .contains (Constant .Nbt .SECURITY , Tag .TAG_COMPOUND )) {
138
+ CompoundTag security = nbt .getCompound (Constant .Nbt .SECURITY );
139
+ if (security .contains (Constant .Nbt .OWNER , Tag .TAG_COMPOUND )) {
140
+ GameProfile profile = ResolvableProfile .CODEC .parse (NbtOps .INSTANCE , security .getCompound (Constant .Nbt .OWNER )).getOrThrow ().gameProfile ();
141
+ if (profile != null ) {
142
+ MutableComponent owner = Component .translatable (Constant .TranslationKey .OWNER , Component .literal (profile .getName ()).setStyle (Constant .Text .LIGHT_PURPLE_STYLE )).setStyle (Constant .Text .GRAY_STYLE );
143
+ if (Screen .hasControlDown ()) {
144
+ owner .append (Component .literal (" (" + profile .getId ().toString () + ")" ).setStyle (Constant .Text .AQUA_STYLE ));
145
+ }
146
+ tooltip .add (owner );
147
+ } else {
148
+ tooltip .add (Component .translatable (Constant .TranslationKey .OWNER , Component .translatable (Constant .TranslationKey .UNKNOWN ).setStyle (Constant .Text .LIGHT_PURPLE_STYLE )).setStyle (Constant .Text .GRAY_STYLE ));
149
+ }
150
+ tooltip .add (Component .translatable (Constant .TranslationKey .ACCESS_LEVEL , AccessLevel .fromString (security .getString (Constant .Nbt .ACCESS_LEVEL )).getName ()).setStyle (Constant .Text .GREEN_STYLE ));
151
+ }
152
+ }
153
+
154
+ if (nbt .contains (Constant .Nbt .REDSTONE_MODE , Tag .TAG_BYTE )) {
155
+ tooltip .add (Component .translatable (Constant .TranslationKey .REDSTONE_MODE , RedstoneMode .readTag (Objects .requireNonNull (nbt .get (Constant .Nbt .REDSTONE_MODE ))).getName ()).setStyle (Constant .Text .DARK_RED_STYLE ));
156
+ }
157
+ }
158
+ }
159
+ }
160
+
161
+ protected abstract @ NotNull MapCodec <? extends BaseEntityBlock > codec ();
162
+
163
+ @ Override
164
+ public abstract @ Nullable MachineBlockEntity newBlockEntity (BlockPos pos , BlockState state );
165
+
156
166
@ Override
157
167
protected void createBlockStateDefinition (StateDefinition .Builder <Block , BlockState > builder ) {
158
168
super .createBlockStateDefinition (builder );
159
169
builder .add (BlockStateProperties .HORIZONTAL_FACING , ACTIVE );
160
170
}
161
171
162
- @ Override
163
- public Machine newBlockEntity (BlockPos pos , BlockState state ) {
164
- return this .factory .get ().create (pos , state );
165
- }
166
-
167
172
@ Override
168
173
public BlockState getStateForPlacement (@ NotNull BlockPlaceContext context ) {
169
174
return this .defaultBlockState ().setValue (BlockStateProperties .HORIZONTAL_FACING , context .getHorizontalDirection ().getOpposite ());
@@ -199,66 +204,24 @@ public void onPlace(BlockState state, Level level, BlockPos pos, BlockState bloc
199
204
}
200
205
}
201
206
202
- @ Override
203
- protected MapCodec <? extends BaseEntityBlock > codec () {
204
- return CODEC ;
205
- }
206
-
207
207
@ Override
208
208
public @ NotNull RenderShape getRenderShape (BlockState state ) {
209
209
return RenderShape .MODEL ;
210
210
}
211
211
212
- /**
213
- * @see #shiftDescription
214
- */
215
212
@ Override
216
213
public void appendHoverText (ItemStack stack , TooltipContext context , List <Component > tooltip , @ NotNull TooltipFlag flag ) {
217
- Component text = this .shiftDescription (stack , context , flag );
218
- if (text != null ) {
219
- if (Screen .hasShiftDown ()) {
220
- if (this .description == null ) {
221
- this .description = DisplayUtil .wrapText (text , 128 );
222
- }
223
- tooltip .addAll (this .description );
224
- } else {
225
- tooltip .add (PRESS_SHIFT );
226
- }
214
+ if (Screen .hasShiftDown ()) {
215
+ tooltip .addAll (DisplayUtil .wrapText (Component .translatable (this .getDescriptionId () + ".description" ), 128 ));
216
+ } else {
217
+ tooltip .add (PRESS_SHIFT );
227
218
}
228
219
229
- if (stack != null ) {
230
- CustomData data = stack .getOrDefault (DataComponents .BLOCK_ENTITY_DATA , CustomData .EMPTY );
231
- if (!data .isEmpty ()) {
232
- CompoundTag nbt = data .getUnsafe ();
233
- tooltip .add (Component .empty ());
234
- if (nbt .contains (Constant .Nbt .ENERGY , Tag .TAG_INT ))
235
- tooltip .add (Component .translatable (Constant .TranslationKey .CURRENT_ENERGY , Component .literal (String .valueOf (nbt .getInt (Constant .Nbt .ENERGY ))).setStyle (Constant .Text .BLUE_STYLE )).setStyle (Constant .Text .GOLD_STYLE ));
236
- if (nbt .contains (Constant .Nbt .SECURITY , Tag .TAG_COMPOUND )) {
237
- CompoundTag security = nbt .getCompound (Constant .Nbt .SECURITY );
238
- if (security .contains (Constant .Nbt .OWNER , Tag .TAG_COMPOUND )) {
239
- GameProfile profile = ResolvableProfile .CODEC .parse (NbtOps .INSTANCE , security .getCompound (Constant .Nbt .OWNER )).getOrThrow ().gameProfile ();
240
- if (profile != null ) {
241
- MutableComponent owner = Component .translatable (Constant .TranslationKey .OWNER , Component .literal (profile .getName ()).setStyle (Constant .Text .LIGHT_PURPLE_STYLE )).setStyle (Constant .Text .GRAY_STYLE );
242
- if (Screen .hasControlDown ()) {
243
- owner .append (Component .literal (" (" + profile .getId ().toString () + ")" ).setStyle (Constant .Text .AQUA_STYLE ));
244
- }
245
- tooltip .add (owner );
246
- } else {
247
- tooltip .add (Component .translatable (Constant .TranslationKey .OWNER , Component .translatable (Constant .TranslationKey .UNKNOWN ).setStyle (Constant .Text .LIGHT_PURPLE_STYLE )).setStyle (Constant .Text .GRAY_STYLE ));
248
- }
249
- tooltip .add (Component .translatable (Constant .TranslationKey .ACCESS_LEVEL , AccessLevel .fromString (security .getString (Constant .Nbt .ACCESS_LEVEL )).getName ()).setStyle (Constant .Text .GREEN_STYLE ));
250
- }
251
- }
252
-
253
- if (nbt .contains (Constant .Nbt .REDSTONE_MODE , Tag .TAG_BYTE )) {
254
- tooltip .add (Component .translatable (Constant .TranslationKey .REDSTONE_MODE , RedstoneMode .readTag (Objects .requireNonNull (nbt .get (Constant .Nbt .REDSTONE_MODE ))).getName ()).setStyle (Constant .Text .DARK_RED_STYLE ));
255
- }
256
- }
257
- }
220
+ appendBlockEntityTooltip (stack , tooltip );
258
221
}
259
222
260
223
@ Override
261
- public final @ NotNull InteractionResult useWithoutItem (BlockState state , @ NotNull Level level , BlockPos pos , Player player , BlockHitResult hit ) {
224
+ public @ NotNull InteractionResult useWithoutItem (BlockState state , Level level , BlockPos pos , Player player , BlockHitResult hit ) {
262
225
if (!level .isClientSide ) {
263
226
BlockEntity entity = level .getBlockEntity (pos );
264
227
if (entity instanceof MachineBlockEntity machine ) {
@@ -326,15 +289,4 @@ public void appendHoverText(ItemStack stack, TooltipContext context, List<Compon
326
289
public <B extends BlockEntity > BlockEntityTicker <B > getTicker (Level level , BlockState state , BlockEntityType <B > type ) {
327
290
return !level .isClientSide ? MachineBlockEntityTicker .getInstance () : null ;
328
291
}
329
-
330
- /**
331
- * {@return this machine's detailed tooltip description} Shown when left shift is pressed.
332
- *
333
- * @param stack The item stack (the contained item is this block).
334
- * @param context The context of the tooltip.
335
- * @param flag Flags to determine if extra information should be added
336
- */
337
- public @ Nullable Component shiftDescription (ItemStack stack , TooltipContext context , TooltipFlag flag ) {
338
- return Component .translatable (this .getDescriptionId () + ".description" );
339
- }
340
292
}
0 commit comments