|
1 | 1 | package com.gregtechceu.gtceu.api.capability; |
2 | 2 |
|
3 | 3 | import com.gregtechceu.gtceu.GTCEu; |
| 4 | +import com.gregtechceu.gtceu.api.blockentity.ICopyable; |
4 | 5 | import com.gregtechceu.gtceu.api.blockentity.ITickSubscription; |
5 | 6 | import com.gregtechceu.gtceu.api.cover.CoverBehavior; |
6 | 7 | import com.gregtechceu.gtceu.api.cover.CoverDefinition; |
| 8 | +import com.gregtechceu.gtceu.api.registry.GTRegistries; |
7 | 9 | import com.gregtechceu.gtceu.api.transfer.fluid.IFluidHandlerModifiable; |
8 | 10 | import com.gregtechceu.gtceu.utils.GTUtil; |
9 | 11 |
|
10 | 12 | import net.minecraft.core.BlockPos; |
11 | 13 | import net.minecraft.core.Direction; |
| 14 | +import net.minecraft.nbt.CompoundTag; |
| 15 | +import net.minecraft.resources.ResourceLocation; |
12 | 16 | import net.minecraft.server.level.ServerPlayer; |
13 | 17 | import net.minecraft.world.entity.player.Player; |
| 18 | +import net.minecraft.world.item.Item; |
14 | 19 | import net.minecraft.world.item.ItemStack; |
15 | 20 | import net.minecraft.world.level.BlockAndTintGetter; |
16 | 21 | import net.minecraft.world.level.Level; |
|
25 | 30 |
|
26 | 31 | import org.jetbrains.annotations.Nullable; |
27 | 32 |
|
28 | | -import java.util.ArrayList; |
29 | | -import java.util.Arrays; |
30 | | -import java.util.List; |
31 | | -import java.util.Objects; |
| 33 | +import java.util.*; |
32 | 34 | import java.util.stream.Collectors; |
33 | 35 |
|
34 | | -public interface ICoverable extends ITickSubscription { |
| 36 | +public interface ICoverable extends ITickSubscription, ICopyable { |
35 | 37 |
|
36 | 38 | Level getLevel(); |
37 | 39 |
|
@@ -219,15 +221,6 @@ default boolean hasDynamicCovers() { |
219 | 221 | return false; |
220 | 222 | } |
221 | 223 |
|
222 | | - class PrimaryBoxData { |
223 | | - |
224 | | - public final boolean usePlacementGrid; |
225 | | - |
226 | | - public PrimaryBoxData(boolean usePlacementGrid) { |
227 | | - this.usePlacementGrid = usePlacementGrid; |
228 | | - } |
229 | | - } |
230 | | - |
231 | 224 | @Nullable |
232 | 225 | static Direction traceCoverSide(@Nullable BlockHitResult result) { |
233 | 226 | return determineGridSideHit(result); |
@@ -273,4 +266,74 @@ default BlockState getBlockAppearance(BlockState state, BlockAndTintGetter level |
273 | 266 | } |
274 | 267 | return null; |
275 | 268 | } |
| 269 | + |
| 270 | + private CompoundTag createCoverConfigTag(@Nullable CoverBehavior cover) { |
| 271 | + if (cover == null) return new CompoundTag(); |
| 272 | + var tag = new CompoundTag(); |
| 273 | + tag.putString("id", GTRegistries.COVERS.getKey(cover.coverDefinition).toString()); |
| 274 | + tag.put("item", cover.getAttachItem().serializeNBT()); |
| 275 | + tag.put("data", cover.copyConfig(new CompoundTag())); |
| 276 | + return tag; |
| 277 | + } |
| 278 | + |
| 279 | + private void applyCoverConfigTag(ServerPlayer player, Direction dir, CompoundTag tag) { |
| 280 | + if (tag.isEmpty()) return; |
| 281 | + var def = GTRegistries.COVERS.get(new ResourceLocation(tag.getString("id"))); |
| 282 | + ItemStack stack = ItemStack.of(tag.getCompound("item")); |
| 283 | + if (def == null) return; |
| 284 | + |
| 285 | + placeCoverOnSide(dir, stack, def, player); |
| 286 | + |
| 287 | + CoverBehavior placedCover = getCoverAtSide(dir); |
| 288 | + if (placedCover != null && tag.contains("data") && !tag.getCompound("data").isEmpty()) |
| 289 | + placedCover.pasteConfig(player, tag.getCompound("data")); |
| 290 | + } |
| 291 | + |
| 292 | + @Override |
| 293 | + default CompoundTag copyConfig(CompoundTag tag) { |
| 294 | + for (Direction dir : GTUtil.DIRECTIONS) { |
| 295 | + tag.put(dir.getName(), hasCover(dir) ? createCoverConfigTag(getCoverAtSide(dir)) : new CompoundTag()); |
| 296 | + } |
| 297 | + |
| 298 | + return tag; |
| 299 | + } |
| 300 | + |
| 301 | + @Override |
| 302 | + default void pasteConfig(ServerPlayer player, CompoundTag tag) { |
| 303 | + for (Direction side : GTUtil.DIRECTIONS) { |
| 304 | + removeCover(side, player); |
| 305 | + } |
| 306 | + |
| 307 | + for (Direction dir : GTUtil.DIRECTIONS) { |
| 308 | + applyCoverConfigTag(player, dir, tag.getCompound(dir.getName())); |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + @Override |
| 313 | + default List<ItemStack> getItemsRequiredToPaste() { |
| 314 | + Map<Item, Integer> allDrops = new HashMap<>(); |
| 315 | + List<ItemStack> rawDrops = new ArrayList<>(); |
| 316 | + |
| 317 | + for (Direction side : GTUtil.DIRECTIONS) { |
| 318 | + var cover = getCoverAtSide(side); |
| 319 | + if (cover != null) rawDrops.add(cover.getAttachItem()); |
| 320 | + } |
| 321 | + |
| 322 | + for (Direction side : GTUtil.DIRECTIONS) { |
| 323 | + var cover = getCoverAtSide(side); |
| 324 | + if (cover != null) rawDrops.addAll(cover.getAdditionalDrops()); |
| 325 | + } |
| 326 | + |
| 327 | + for (var drop : rawDrops) { |
| 328 | + if (allDrops.containsKey(drop.getItem())) { |
| 329 | + allDrops.put(drop.getItem(), allDrops.get(drop.getItem()) + drop.getCount()); |
| 330 | + } else { |
| 331 | + allDrops.put(drop.getItem(), drop.getCount()); |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + List<ItemStack> mergedStacks = new ArrayList<>(); |
| 336 | + allDrops.forEach((k, v) -> mergedStacks.add(new ItemStack(k, v))); |
| 337 | + return mergedStacks; |
| 338 | + } |
276 | 339 | } |
0 commit comments