forked from 0vergrown/Thaumaturge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVesselBlockEntity.java
More file actions
303 lines (256 loc) · 10.6 KB
/
VesselBlockEntity.java
File metadata and controls
303 lines (256 loc) · 10.6 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package dev.overgrown.thaumaturge.block.entity;
import dev.overgrown.aspectslib.api.AspectsAPI;
import dev.overgrown.aspectslib.data.AspectData;
import dev.overgrown.thaumaturge.block.VesselBlock;
import dev.overgrown.thaumaturge.registry.ModBlocks;
import dev.overgrown.thaumaturge.recipe.VesselRecipe;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class VesselBlockEntity extends BlockEntity implements Inventory {
private final DefaultedList<ItemStack> items = DefaultedList.ofSize(6, ItemStack.EMPTY);
private final Map<String, Integer> aspects = new HashMap<>();
private ItemStack catalyst = ItemStack.EMPTY;
private boolean boiling = false;
private int processTime = 0;
public VesselBlockEntity(BlockPos pos, BlockState state) {
super(ModBlocks.VESSEL_BLOCK_ENTITY, pos, state);
}
public static void serverTick(World world, BlockPos pos, BlockState state, VesselBlockEntity blockEntity) {
if (blockEntity.boiling) {
if (world.getTime() % 10 == 0) {
((ServerWorld) world).spawnParticles(ParticleTypes.BUBBLE_POP,
pos.getX() + 0.5, pos.getY() + 0.3, pos.getZ() + 0.5,
2, 0.2, 0.0, 0.2, 0.05);
}
blockEntity.processTime++;
if (blockEntity.processTime >= 100) {
blockEntity.processTime = 0;
blockEntity.processItemForAspects();
}
}
}
public void processItemForAspects() {
if (items.stream().allMatch(ItemStack::isEmpty)) return;
World world = getWorld();
if (world == null) return;
for (int i = 0; i < items.size(); i++) {
ItemStack stack = items.get(i);
if (stack.isEmpty()) continue;
AspectData aspectData = AspectsAPI.getAspectData(stack);
if (!aspectData.isEmpty()) {
for (var entry : aspectData.getMap().object2IntEntrySet()) {
String aspectName = AspectsAPI.getAspect(entry.getKey())
.map(aspect -> aspect.name())
.orElse(entry.getKey().toString());
int totalAmount = entry.getIntValue() * stack.getCount();
aspects.merge(aspectName, totalAmount, Integer::sum);
}
} else {
ItemEntity itemEntity = new ItemEntity(world, pos.getX() + 0.5, pos.getY() + 1.0, pos.getZ() + 0.5, stack.copy());
world.spawnEntity(itemEntity);
}
items.set(i, ItemStack.EMPTY);
markDirty();
syncToClient();
break;
}
}
public boolean addItem(ItemStack stack) {
World world = getWorld();
if (world == null) return false;
boolean isCatalyst = world.getRecipeManager()
.listAllOfType(VesselRecipe.Type.INSTANCE)
.stream()
.anyMatch(recipe -> ItemStack.areItemsEqual(recipe.getCatalyst(), stack));
if (isCatalyst) {
boolean recipeMatched = tryCraftWithCatalystDropped(stack);
if (recipeMatched) {
return true;
}
}
AspectData aspectData = AspectsAPI.getAspectData(stack);
if (!aspectData.isEmpty()) {
for (var entry : aspectData.getMap().object2IntEntrySet()) {
String aspectName = AspectsAPI.getAspect(entry.getKey())
.map(aspect -> aspect.name())
.orElse(entry.getKey().toString());
int totalAmount = entry.getIntValue() * stack.getCount();
aspects.merge(aspectName, totalAmount, Integer::sum);
}
markDirty();
syncToClient();
return true;
} else {
for (int i = 0; i < items.size(); i++) {
if (items.get(i).isEmpty()) {
items.set(i, stack.copy());
markDirty();
syncToClient();
return true;
}
}
}
return false;
}
public boolean tryCraftWithCatalystDropped(ItemStack catalystStack) {
World world = getWorld();
if (world == null) return false;
boolean shouldConsume = tryCraftWithCatalyst(catalystStack);
if (!shouldConsume && world.getRecipeManager()
.listAllOfType(VesselRecipe.Type.INSTANCE)
.stream()
.anyMatch(recipe -> ItemStack.areItemsEqual(recipe.getCatalyst(), catalystStack) &&
recipe.getAspects().entrySet().stream()
.allMatch(entry -> {
String storedAspectName = entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1);
return aspects.getOrDefault(storedAspectName, 0) >= entry.getValue();
}))) {
ItemEntity catalystEntity = new ItemEntity(world, pos.getX() + 0.5, pos.getY() + 1.0, pos.getZ() + 0.5, catalystStack.copy());
world.spawnEntity(catalystEntity);
return true;
}
return shouldConsume;
}
public boolean tryCraftWithCatalyst(ItemStack catalystStack) {
World world = getWorld();
if (world == null) return false;
Optional<VesselRecipe> match = world.getRecipeManager()
.listAllOfType(VesselRecipe.Type.INSTANCE)
.stream()
.filter(recipe -> ItemStack.areItemsEqual(recipe.getCatalyst(), catalystStack))
.filter(recipe -> recipe.getAspects().entrySet().stream()
.allMatch(entry -> {
String storedAspectName = entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1);
return aspects.getOrDefault(storedAspectName, 0) >= entry.getValue();
}))
.findFirst();
if (match.isPresent()) {
VesselRecipe recipe = match.get();
recipe.getAspects().forEach((aspect, amount) -> {
String storedAspectName = aspect.substring(0, 1).toUpperCase() + aspect.substring(1);
aspects.computeIfPresent(storedAspectName, (k, v) -> v - amount);
});
aspects.entrySet().removeIf(entry -> entry.getValue() <= 0);
ItemStack output = recipe.getOutput(world.getRegistryManager()).copy();
ItemEntity result = new ItemEntity(world, pos.getX() + 0.5, pos.getY() + 1.0, pos.getZ() + 0.5, output);
world.spawnEntity(result);
BlockState state = world.getBlockState(pos);
if (state.getBlock() instanceof VesselBlock && world.random.nextFloat() < 0.3f) {
int waterLevel = state.get(VesselBlock.WATER_LEVEL);
if (waterLevel > 0) {
world.setBlockState(pos, state.with(VesselBlock.WATER_LEVEL, waterLevel - 1));
}
}
markDirty();
syncToClient();
return recipe.consumesCatalyst();
}
return false;
}
public ItemStack getCatalyst() {
return catalyst;
}
public Map<String, Integer> getAspects() {
return aspects;
}
private void syncToClient() {
if (world != null && !world.isClient) {
world.updateListeners(pos, getCachedState(), getCachedState(), Block.NOTIFY_LISTENERS);
}
}
public void setBoiling(boolean boiling) {
this.boiling = boiling;
markDirty();
}
public boolean isBoiling() {
return boiling;
}
@Override
public void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
Inventories.writeNbt(nbt, items);
nbt.put("Catalyst", catalyst.writeNbt(new NbtCompound()));
NbtCompound aspectsNbt = new NbtCompound();
for (Map.Entry<String, Integer> entry : aspects.entrySet()) {
aspectsNbt.putInt(entry.getKey(), entry.getValue());
}
nbt.put("Aspects", aspectsNbt);
nbt.putBoolean("Boiling", boiling);
nbt.putInt("ProcessTime", processTime);
}
@Override
public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
Inventories.readNbt(nbt, items);
catalyst = ItemStack.fromNbt(nbt.getCompound("Catalyst"));
aspects.clear();
NbtCompound aspectsNbt = nbt.getCompound("Aspects");
for (String key : aspectsNbt.getKeys()) {
aspects.put(key, aspectsNbt.getInt(key));
}
boiling = nbt.getBoolean("Boiling");
processTime = nbt.getInt("ProcessTime");
}
@Nullable
@Override
public Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}
@Override
public NbtCompound toInitialChunkDataNbt() {
NbtCompound nbt = new NbtCompound();
this.writeNbt(nbt);
return nbt;
}
@Override
public int size() {
return items.size();
}
@Override
public boolean isEmpty() {
return items.stream().allMatch(ItemStack::isEmpty);
}
@Override
public ItemStack getStack(int slot) {
return items.get(slot);
}
@Override
public ItemStack removeStack(int slot, int amount) {
return Inventories.splitStack(items, slot, amount);
}
@Override
public ItemStack removeStack(int slot) {
return Inventories.removeStack(items, slot);
}
@Override
public void setStack(int slot, ItemStack stack) {
items.set(slot, stack);
}
@Override
public boolean canPlayerUse(PlayerEntity player) {
return true;
}
@Override
public void clear() {
items.clear();
}
}