Skip to content

Commit ba578f6

Browse files
committed
refactor: drop MachineType system, move some functionality out of MachineBlockEntity
1 parent 07af9e9 commit ba578f6

File tree

105 files changed

+2446
-2184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+2446
-2184
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx1G
33
# Mod Information
44
mod.id=machinelib
55
mod.name=MachineLib
6-
mod.version=0.6.0
6+
mod.version=0.7.0
77

88
# Minecraft and Fabric Loader
99
minecraft.version=1.20.6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2021-2024 Team Galacticraft
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package dev.galacticraft.machinelib.api.block;
24+
25+
import dev.galacticraft.machinelib.api.block.entity.BaseBlockEntity;
26+
import dev.galacticraft.machinelib.impl.block.entity.BaseBlockEntityTicker;
27+
import net.minecraft.core.BlockPos;
28+
import net.minecraft.world.InteractionResult;
29+
import net.minecraft.world.entity.player.Player;
30+
import net.minecraft.world.level.Level;
31+
import net.minecraft.world.level.block.BaseEntityBlock;
32+
import net.minecraft.world.level.block.RenderShape;
33+
import net.minecraft.world.level.block.entity.BlockEntity;
34+
import net.minecraft.world.level.block.entity.BlockEntityTicker;
35+
import net.minecraft.world.level.block.entity.BlockEntityType;
36+
import net.minecraft.world.level.block.state.BlockState;
37+
import net.minecraft.world.phys.BlockHitResult;
38+
import org.jetbrains.annotations.NotNull;
39+
import org.jetbrains.annotations.Nullable;
40+
41+
/**
42+
* The base block for all machines.
43+
*/
44+
public abstract class BaseBlock extends BaseEntityBlock {
45+
/**
46+
* Creates a new machine block.
47+
*
48+
* @param settings The settings for the block.
49+
*/
50+
public BaseBlock(Properties settings) {
51+
super(settings);
52+
}
53+
54+
@Nullable
55+
@Override
56+
public abstract BaseBlockEntity newBlockEntity(BlockPos pos, BlockState state);
57+
58+
@Override
59+
public @NotNull RenderShape getRenderShape(BlockState state) {
60+
return RenderShape.MODEL;
61+
}
62+
63+
@Override
64+
public @NotNull InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
65+
if (!level.isClientSide) {
66+
BlockEntity entity = level.getBlockEntity(pos);
67+
if (entity instanceof BaseBlockEntity be) {
68+
player.openMenu(be);
69+
return InteractionResult.CONSUME;
70+
}
71+
}
72+
73+
return InteractionResult.SUCCESS;
74+
}
75+
76+
@Nullable
77+
@Override
78+
public <B extends BlockEntity> BlockEntityTicker<B> getTicker(Level level, BlockState state, BlockEntityType<B> type) {
79+
return !level.isClientSide ? BaseBlockEntityTicker.getInstance() : null;
80+
}
81+
}

src/main/java/dev/galacticraft/machinelib/api/block/MachineBlock.java

+46-94
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222

2323
package dev.galacticraft.machinelib.api.block;
2424

25-
import com.google.common.base.Suppliers;
2625
import com.mojang.authlib.GameProfile;
2726
import com.mojang.serialization.MapCodec;
28-
import com.mojang.serialization.codecs.RecordCodecBuilder;
2927
import dev.galacticraft.machinelib.api.block.entity.MachineBlockEntity;
3028
import dev.galacticraft.machinelib.api.machine.configuration.AccessLevel;
3129
import dev.galacticraft.machinelib.api.machine.configuration.RedstoneMode;
@@ -40,13 +38,11 @@
4038
import net.minecraft.core.BlockPos;
4139
import net.minecraft.core.component.DataComponentPatch;
4240
import net.minecraft.core.component.DataComponents;
43-
import net.minecraft.core.registries.BuiltInRegistries;
4441
import net.minecraft.nbt.CompoundTag;
4542
import net.minecraft.nbt.NbtOps;
4643
import net.minecraft.nbt.Tag;
4744
import net.minecraft.network.chat.Component;
4845
import net.minecraft.network.chat.MutableComponent;
49-
import net.minecraft.resources.ResourceLocation;
5046
import net.minecraft.server.level.ServerPlayer;
5147
import net.minecraft.world.InteractionResult;
5248
import net.minecraft.world.entity.LivingEntity;
@@ -81,53 +77,30 @@
8177
import java.util.Collections;
8278
import java.util.List;
8379
import java.util.Objects;
84-
import java.util.function.Supplier;
8580

8681
/**
8782
* The base block for all machines.
88-
*
89-
* @param <Machine> The machine block entity attached to this block.
9083
*/
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 {
9785
/**
9886
* Represents a boolean property for specifying the active state of the machine.
87+
*
9988
* @see MachineBlockEntity#isActive() for the definition of 'active'
10089
*/
10190
public static final BooleanProperty ACTIVE = BooleanProperty.create("active");
10291

10392
/**
10493
* Tooltip prompt text. Shown instead of the long-form description when shift is not pressed.
105-
*
106-
* @see #shiftDescription(ItemStack, TooltipContext, TooltipFlag)
10794
*/
10895
private static final Component PRESS_SHIFT = Component.translatable(Constant.TranslationKey.PRESS_SHIFT).setStyle(Constant.Text.DARK_GRAY_STYLE);
10996

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-
12297
/**
12398
* Creates a new machine block.
12499
*
125100
* @param settings The settings for the block.
126-
* @param factoryId the machine block entity factory
127101
*/
128-
public MachineBlock(Properties settings, ResourceLocation factoryId) {
102+
public MachineBlock(Properties settings) {
129103
super(settings);
130-
this.factory = Suppliers.memoize(() -> (BlockEntityType<Machine>) BuiltInRegistries.BLOCK_ENTITY_TYPE.get(factoryId));
131104
this.registerDefaultState(this.getStateDefinition().any().setValue(ACTIVE, false));
132105
}
133106

@@ -153,17 +126,49 @@ public static boolean isActive(@NotNull BlockState state) {
153126
return state.getValue(ACTIVE);
154127
}
155128

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+
156166
@Override
157167
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
158168
super.createBlockStateDefinition(builder);
159169
builder.add(BlockStateProperties.HORIZONTAL_FACING, ACTIVE);
160170
}
161171

162-
@Override
163-
public Machine newBlockEntity(BlockPos pos, BlockState state) {
164-
return this.factory.get().create(pos, state);
165-
}
166-
167172
@Override
168173
public BlockState getStateForPlacement(@NotNull BlockPlaceContext context) {
169174
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
199204
}
200205
}
201206

202-
@Override
203-
protected MapCodec<? extends BaseEntityBlock> codec() {
204-
return CODEC;
205-
}
206-
207207
@Override
208208
public @NotNull RenderShape getRenderShape(BlockState state) {
209209
return RenderShape.MODEL;
210210
}
211211

212-
/**
213-
* @see #shiftDescription
214-
*/
215212
@Override
216213
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);
227218
}
228219

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);
258221
}
259222

260223
@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) {
262225
if (!level.isClientSide) {
263226
BlockEntity entity = level.getBlockEntity(pos);
264227
if (entity instanceof MachineBlockEntity machine) {
@@ -326,15 +289,4 @@ public void appendHoverText(ItemStack stack, TooltipContext context, List<Compon
326289
public <B extends BlockEntity> BlockEntityTicker<B> getTicker(Level level, BlockState state, BlockEntityType<B> type) {
327290
return !level.isClientSide ? MachineBlockEntityTicker.getInstance() : null;
328291
}
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-
}
340292
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2021-2024 Team Galacticraft
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package dev.galacticraft.machinelib.api.block;
24+
25+
import com.mojang.serialization.MapCodec;
26+
import com.mojang.serialization.codecs.RecordCodecBuilder;
27+
import dev.galacticraft.machinelib.api.block.entity.MachineBlockEntity;
28+
import net.minecraft.core.BlockPos;
29+
import net.minecraft.core.registries.BuiltInRegistries;
30+
import net.minecraft.core.registries.Registries;
31+
import net.minecraft.resources.ResourceKey;
32+
import net.minecraft.resources.ResourceLocation;
33+
import net.minecraft.world.level.block.BaseEntityBlock;
34+
import net.minecraft.world.level.block.entity.BlockEntityType;
35+
import net.minecraft.world.level.block.state.BlockState;
36+
import org.jetbrains.annotations.NotNull;
37+
import org.jetbrains.annotations.Nullable;
38+
39+
/**
40+
* The base block for all machines.
41+
*/
42+
public class SimpleMachineBlock extends MachineBlock {
43+
private static final MapCodec<SimpleMachineBlock> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
44+
propertiesCodec(),
45+
ResourceKey.codec(Registries.BLOCK_ENTITY_TYPE).fieldOf("factory").forGetter(block -> block.key)
46+
).apply(instance, SimpleMachineBlock::new));
47+
private final ResourceKey<BlockEntityType<?>> key;
48+
49+
public SimpleMachineBlock(Properties settings, ResourceLocation id) {
50+
this(settings, ResourceKey.create(Registries.BLOCK_ENTITY_TYPE, id));
51+
}
52+
53+
public SimpleMachineBlock(Properties settings, ResourceKey<BlockEntityType<?>> key) {
54+
super(settings);
55+
this.key = key;
56+
}
57+
58+
@Override
59+
protected @NotNull MapCodec<? extends BaseEntityBlock> codec() {
60+
return CODEC;
61+
}
62+
63+
@Override
64+
public @Nullable MachineBlockEntity newBlockEntity(BlockPos pos, BlockState state) {
65+
return (MachineBlockEntity) BuiltInRegistries.BLOCK_ENTITY_TYPE.getOrThrow(this.key).create(pos, state);
66+
}
67+
}

0 commit comments

Comments
 (0)