/
DiskDriveBlockEntity.java
224 lines (179 loc) · 7.53 KB
/
DiskDriveBlockEntity.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
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
/* SPDX-License-Identifier: MIT */
package li.cil.oc2.common.blockentity;
import li.cil.oc2.common.Constants;
import li.cil.oc2.common.block.DiskDriveBlock;
import li.cil.oc2.common.bus.device.vm.block.DiskDriveContainer;
import li.cil.oc2.common.bus.device.vm.block.DiskDriveDevice;
import li.cil.oc2.common.capabilities.Capabilities;
import li.cil.oc2.common.container.TypedItemStackHandler;
import li.cil.oc2.common.network.Network;
import li.cil.oc2.common.network.message.DiskDriveFloppyMessage;
import li.cil.oc2.common.tags.ItemTags;
import li.cil.oc2.common.util.ItemStackUtils;
import li.cil.oc2.common.util.LocationSupplierUtils;
import li.cil.oc2.common.util.SoundEvents;
import li.cil.oc2.common.util.ThrottledSoundEmitter;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
public final class DiskDriveBlockEntity extends ModBlockEntity implements DiskDriveContainer {
private static final String DATA_TAG_NAME = "data";
///////////////////////////////////////////////////////////////////
private final DiskDriveItemStackHandler itemHandler = new DiskDriveItemStackHandler();
private final DiskDriveDevice<DiskDriveBlockEntity> device = new DiskDriveDevice<>(this);
private final ThrottledSoundEmitter accessSoundEmitter;
private final ThrottledSoundEmitter insertSoundEmitter;
private final ThrottledSoundEmitter ejectSoundEmitter;
///////////////////////////////////////////////////////////////////
public DiskDriveBlockEntity(final BlockPos pos, final BlockState state) {
super(BlockEntities.DISK_DRIVE.get(), pos, state);
this.accessSoundEmitter = new ThrottledSoundEmitter(LocationSupplierUtils.of(this),
SoundEvents.FLOPPY_ACCESS.get()).withMinInterval(Duration.ofSeconds(1));
this.insertSoundEmitter = new ThrottledSoundEmitter(LocationSupplierUtils.of(this),
SoundEvents.FLOPPY_INSERT.get()).withMinInterval(Duration.ofMillis(100));
this.ejectSoundEmitter = new ThrottledSoundEmitter(LocationSupplierUtils.of(this),
SoundEvents.FLOPPY_EJECT.get()).withMinInterval(Duration.ofMillis(100));
}
///////////////////////////////////////////////////////////////////
public boolean canInsert(final ItemStack stack) {
return !stack.isEmpty() && stack.is(ItemTags.DEVICES_FLOPPY);
}
public ItemStack insert(final ItemStack stack, @Nullable final Player player) {
if (!canInsert(stack)) {
return stack;
}
eject(player);
insertSoundEmitter.play();
return itemHandler.insertItem(0, stack, false);
}
public boolean canEject() {
return !itemHandler.extractItem(0, 1, true).isEmpty();
}
public void eject(@Nullable final Player player) {
if (level == null) {
return;
}
final ItemStack stack = itemHandler.extractItem(0, 1, false);
if (!stack.isEmpty()) {
final Direction facing = getBlockState().getValue(DiskDriveBlock.FACING);
ejectSoundEmitter.play();
ItemStackUtils.spawnAsEntity(level, getBlockPos().relative(facing), stack, facing).ifPresent(entity -> {
if (player != null) {
entity.setNoPickUpDelay();
entity.setOwner(player.getUUID());
}
});
}
}
public ItemStack getFloppy() {
return itemHandler.getStackInSlot(0);
}
@OnlyIn(Dist.CLIENT)
public void setFloppyClient(final ItemStack stack) {
itemHandler.setStackInSlot(0, stack);
}
@Override
protected void collectCapabilities(final CapabilityCollector collector, @Nullable final Direction direction) {
collector.offer(Capabilities.itemHandler(), itemHandler);
if (direction == getBlockState().getValue(DiskDriveBlock.FACING).getOpposite()) {
collector.offer(Capabilities.device(), device);
}
}
@Override
public CompoundTag getUpdateTag() {
final CompoundTag tag = super.getUpdateTag();
tag.put(Constants.ITEMS_TAG_NAME, itemHandler.serializeNBT());
return tag;
}
@Override
public void handleUpdateTag(final CompoundTag tag) {
super.handleUpdateTag(tag);
itemHandler.deserializeNBT(tag.getCompound(Constants.ITEMS_TAG_NAME));
}
@Override
protected void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
tag.put(Constants.ITEMS_TAG_NAME, itemHandler.serializeNBT());
}
@Override
public void load(final CompoundTag tag) {
super.load(tag);
itemHandler.deserializeNBT(tag.getCompound(Constants.ITEMS_TAG_NAME));
}
@Override
public ItemStack getDiskItemStack() {
return itemHandler.getStackInSlotRaw(0);
}
@Override
public void handleDataAccess() {
accessSoundEmitter.play();
}
///////////////////////////////////////////////////////////////////
private final class DiskDriveItemStackHandler extends TypedItemStackHandler {
public DiskDriveItemStackHandler() {
super(1, ItemTags.DEVICES_FLOPPY);
}
public ItemStack getStackInSlotRaw(final int slot) {
return super.getStackInSlot(slot);
}
@Override
@Nonnull
public ItemStack getStackInSlot(final int slot) {
final ItemStack stack = getStackInSlotRaw(slot);
exportDeviceDataToItemStack(stack);
return stack;
}
@Override
@Nonnull
public ItemStack extractItem(final int slot, final int amount, final boolean simulate) {
if (slot == 0 && !simulate && amount > 0) {
exportDeviceDataToItemStack(getStackInSlotRaw(0));
}
return super.extractItem(slot, amount, simulate);
}
@Override
public int getSlotLimit(final int slot) {
return 1;
}
@Override
public CompoundTag serializeNBT() {
exportDeviceDataToItemStack(getStackInSlotRaw(0));
return super.serializeNBT();
}
@Override
protected void onContentsChanged(final int slot) {
super.onContentsChanged(slot);
if (level == null || level.isClientSide()) {
return;
}
final ItemStack stack = getStackInSlotRaw(slot);
if (stack.isEmpty()) {
device.removeBlockDevice();
} else {
final CompoundTag tag = ItemStackUtils.getOrCreateModDataTag(stack).getCompound(DATA_TAG_NAME);
device.updateBlockDevice(tag);
}
Network.sendToClientsTrackingBlockEntity(new DiskDriveFloppyMessage(DiskDriveBlockEntity.this), DiskDriveBlockEntity.this);
setChanged();
}
private void exportDeviceDataToItemStack(final ItemStack stack) {
if (stack.isEmpty()) {
return;
}
if (level == null || level.isClientSide()) {
return;
}
final CompoundTag tag = new CompoundTag();
device.exportToItemStack(tag);
ItemStackUtils.getOrCreateModDataTag(stack).put(DATA_TAG_NAME, tag);
}
}
}