forked from fnuecke/oc2
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathComputerBlock.java
More file actions
276 lines (235 loc) · 11.9 KB
/
ComputerBlock.java
File metadata and controls
276 lines (235 loc) · 11.9 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* SPDX-License-Identifier: MIT */
package li.cil.oc2.common.block;
import com.mojang.serialization.MapCodec;
import li.cil.oc2.api.capabilities.RedstoneEmitter;
import li.cil.oc2.common.components.RestrictedContainer;
import li.cil.oc2.common.config.Config;
import li.cil.oc2.common.blockentity.BlockEntities;
import li.cil.oc2.common.blockentity.ComputerBlockEntity;
import li.cil.oc2.common.blockentity.TickableBlockEntity;
import li.cil.oc2.common.capabilities.Capabilities;
import li.cil.oc2.common.integration.Wrenches;
import li.cil.oc2.common.item.Items;
import li.cil.oc2.common.tags.ItemTags;
import li.cil.oc2.common.util.HorizontalBlockUtils;
import li.cil.oc2.common.util.TooltipUtils;
import li.cil.oc2.common.util.VoxelShapeUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Optional;
import static li.cil.oc2.common.util.TranslationUtils.text;
public final class ComputerBlock extends HorizontalDirectionalBlock implements EntityBlock {
// We bake the "screen" indent on the front into the collision shape, to prevent stuff being
// placeable on that side, such as network connectors, torches, etc.
private static final VoxelShape NEG_Z_SHAPE = Shapes.or(
Block.box(0, 0, 1, 16, 16, 16), // main body
Block.box(0, 15, 0, 16, 16, 1), // across top
Block.box(0, 0, 0, 16, 6, 1), // across bottom
Block.box(0, 0, 0, 1, 16, 1), // up left
Block.box(15, 0, 0, 16, 16, 1) // up right
);
private static final VoxelShape NEG_X_SHAPE = VoxelShapeUtils.rotateHorizontalClockwise(NEG_Z_SHAPE);
private static final VoxelShape POS_Z_SHAPE = VoxelShapeUtils.rotateHorizontalClockwise(NEG_X_SHAPE);
private static final VoxelShape POS_X_SHAPE = VoxelShapeUtils.rotateHorizontalClockwise(POS_Z_SHAPE);
///////////////////////////////////////////////////////////////////
public ComputerBlock() {
super(Properties
.of()
.mapColor(MapColor.METAL)
.sound(SoundType.METAL)
.strength(1.5f, 6.0f));
registerDefaultState(getStateDefinition().any().setValue(FACING, Direction.NORTH));
}
@Override
protected MapCodec<ComputerBlock> codec() {
return BlockCodecs.COMPUTER.get();
}
///////////////////////////////////////////////////////////////////
@OnlyIn(Dist.CLIENT)
@Override
public void appendHoverText(final ItemStack stack, final Item.TooltipContext context, final List<Component> tooltip, final TooltipFlag advanced) {
super.appendHoverText(stack, context, tooltip, advanced);
TooltipUtils.addEnergyConsumption(Config.computerEnergyPerTick, tooltip);
TooltipUtils.addInventoryInformation(stack, tooltip);
}
@SuppressWarnings("deprecation")
@Override
public boolean isSignalSource(final BlockState state) {
return true;
}
@Override
public int getSignal(final BlockState state, final BlockGetter blockGetter, final BlockPos pos, final Direction side) {
final BlockEntity blockEntity = blockGetter.getBlockEntity(pos);
if (blockEntity != null) {
var level = blockEntity.getLevel();
if (level != null) {
// Redstone requests info for faces with external perspective. Capabilities treat
// the Direction from an internal and local perspective, so flip it, and transform it from global to
// local.
var cap = level.getCapability(Capabilities.RedstoneEmitter.BLOCK, blockEntity.getBlockPos(), null, blockEntity, HorizontalBlockUtils.toLocal(state, side.getOpposite()));
return Optional.ofNullable(cap)
.map(RedstoneEmitter::getRedstoneOutput)
.orElse(0);
}
}
return super.getSignal(state, blockGetter, pos, side);
}
@SuppressWarnings("deprecation")
@Override
public int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction side) {
return getSignal(state, level, pos, side);
}
@SuppressWarnings("deprecation")
@Override
public void neighborChanged(final BlockState state, final Level level, final BlockPos pos, final Block changedBlock, final BlockPos changedBlockPos, final boolean isMoving) {
final BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof final ComputerBlockEntity computer) {
computer.handleNeighborChanged();
}
}
@SuppressWarnings("deprecation")
@Override
public VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
return switch (state.getValue(FACING)) {
case NORTH -> NEG_Z_SHAPE;
case SOUTH -> POS_Z_SHAPE;
case WEST -> NEG_X_SHAPE;
default -> POS_X_SHAPE;
};
}
@Override
protected ItemInteractionResult useItemOn(final ItemStack stack, final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final BlockHitResult hitResult) {
final BlockEntity blockEntity = level.getBlockEntity(pos);
if (!(blockEntity instanceof final ComputerBlockEntity computer)) {
return super.useItemOn(stack, state, level, pos, player, hand, hitResult);
}
if (Wrenches.isWrench(stack)) {
if (!player.isShiftKeyDown()) {
if (!level.isClientSide() && player instanceof final ServerPlayer serverPlayer) {
computer.openInventoryScreen(serverPlayer);
}
return ItemInteractionResult.sidedSuccess(level.isClientSide());
}
return ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION;
}
return super.useItemOn(stack, state, level, pos, player, hand, hitResult);
}
@Override
protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
final BlockEntity blockEntity = level.getBlockEntity(pos);
if (!(blockEntity instanceof final ComputerBlockEntity computer)) {
return super.useWithoutItem(state, level, pos, player, hitResult);
}
if (!level.isClientSide()) {
if (player.isShiftKeyDown()) {
computer.start();
} else if (player instanceof final ServerPlayer serverPlayer) {
computer.openTerminalScreen(serverPlayer);
}
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
@Override
public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
final BlockEntity blockEntity = level.getBlockEntity(pos);
if (!level.isClientSide() && blockEntity instanceof final ComputerBlockEntity computer) {
if (!computer.getItemStackHandlers().isEmpty()) {
if (player.isCreative()) {
final ItemStack stack = new ItemStack(Items.COMPUTER.get());
computer.exportToItemStack(stack);
popResource(level, pos, stack);
}
}
}
return super.playerWillDestroy(level, pos, state, player);
}
@Override
public BlockState getStateForPlacement(final BlockPlaceContext context) {
return super.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());
}
///////////////////////////////////////////////////////////////////
// EntityBlock
@Nullable
@Override
public BlockEntity newBlockEntity(final BlockPos pos, final BlockState state) {
return BlockEntities.COMPUTER.get().create(pos, state);
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(final Level level, final BlockState state, final BlockEntityType<T> type) {
return TickableBlockEntity.createTicker(level, type, BlockEntities.COMPUTER.get());
}
///////////////////////////////////////////////////////////////////
@Override
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
super.createBlockStateDefinition(builder);
builder.add(FACING);
}
///////////////////////////////////////////////////////////////////
private static RestrictedContainer emptyRestrictedContainer() {
var container = new RestrictedContainer();
container.items().put(ItemTags.DEVICES_FLASH_MEMORY, NonNullList.withSize(1, ItemStack.EMPTY));
container.items().put(ItemTags.DEVICES_CPU, NonNullList.withSize(1, ItemStack.EMPTY));
container.items().put(ItemTags.DEVICES_MEMORY, NonNullList.withSize(4, ItemStack.EMPTY));
container.items().put(ItemTags.DEVICES_CARD, NonNullList.withSize(4, ItemStack.EMPTY));
container.items().put(ItemTags.DEVICES_HARD_DRIVE, NonNullList.withSize(4, ItemStack.EMPTY));
return container;
}
public static ItemStack getComputerWithFlash() {
final ItemStack computer = new ItemStack(Items.COMPUTER.get());
var container = emptyRestrictedContainer();
container.items().get(ItemTags.DEVICES_FLASH_MEMORY).set(0, new ItemStack(Items.FLASH_MEMORY_CUSTOM.get()));
computer.set(
li.cil.oc2.common.components.DataComponents.RESTRICTED_CONTAINER,
container
);
return computer;
}
public static ItemStack getPreconfiguredComputer() {
final ItemStack computer = new ItemStack(Items.COMPUTER.get());
var container = emptyRestrictedContainer();
container.items().get(ItemTags.DEVICES_FLASH_MEMORY).set(0, new ItemStack(Items.FLASH_MEMORY_CUSTOM.get()));
container.items().get(ItemTags.DEVICES_CPU).set(0, new ItemStack(Items.CPU_TIER_3.get()));
container.items().get(ItemTags.DEVICES_MEMORY).replaceAll(ignored -> new ItemStack(Items.MEMORY_LARGE.get()));
container.items().get(ItemTags.DEVICES_CARD).set(0, new ItemStack(Items.NETWORK_INTERFACE_CARD.get()));
container.items().get(ItemTags.DEVICES_HARD_DRIVE).set(0, new ItemStack(Items.HARD_DRIVE_LARGE.get()));
computer.set(
li.cil.oc2.common.components.DataComponents.RESTRICTED_CONTAINER,
container
);
computer.set(DataComponents.CUSTOM_NAME, text("block.{mod}.computer.preconfigured"));
return computer;
}
}