|
| 1 | +package com.ref.aea.integration.ftbultimine; |
| 2 | + |
| 3 | +import appeng.api.implementations.items.IMemoryCard; |
| 4 | +import appeng.api.implementations.items.MemoryCardMessages; |
| 5 | +import appeng.api.parts.IPart; |
| 6 | +import appeng.api.parts.IPartHost; |
| 7 | +import appeng.blockentity.AEBaseBlockEntity; |
| 8 | +import appeng.core.definitions.AEBlocks; |
| 9 | +import appeng.core.definitions.AEParts; |
| 10 | +import appeng.items.tools.MemoryCardItem; |
| 11 | +import appeng.util.SettingsFrom; |
| 12 | +import dev.ftb.mods.ftbultimine.FTBUltiminePlayerData; |
| 13 | +import java.util.Objects; |
| 14 | +import net.minecraft.core.BlockPos; |
| 15 | +import net.minecraft.core.Direction; |
| 16 | +import net.minecraft.nbt.CompoundTag; |
| 17 | +import net.minecraft.server.level.ServerPlayer; |
| 18 | +import net.minecraft.world.InteractionHand; |
| 19 | +import net.minecraft.world.item.Item; |
| 20 | +import net.minecraft.world.item.ItemStack; |
| 21 | +import net.minecraft.world.level.Level; |
| 22 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 23 | + |
| 24 | +public class AEMemoryCardHandler { |
| 25 | + |
| 26 | + private static final Direction[] ALL_SIDES = { |
| 27 | + Direction.DOWN, |
| 28 | + Direction.UP, |
| 29 | + Direction.NORTH, |
| 30 | + Direction.SOUTH, |
| 31 | + Direction.WEST, |
| 32 | + Direction.EAST, |
| 33 | + null |
| 34 | + }; |
| 35 | + |
| 36 | + public static int applySettings( |
| 37 | + ServerPlayer player, |
| 38 | + InteractionHand hand, |
| 39 | + BlockPos clickPos, |
| 40 | + Direction face, |
| 41 | + FTBUltiminePlayerData data) { |
| 42 | + ItemStack stack = player.getItemInHand(hand); |
| 43 | + if (!(stack.getItem() instanceof IMemoryCard memoryCard)) return 0; |
| 44 | + |
| 45 | + String storedName = memoryCard.getSettingsName(stack); |
| 46 | + CompoundTag settingsData = memoryCard.getData(stack); |
| 47 | + |
| 48 | + if (storedName == null |
| 49 | + || storedName.isEmpty() |
| 50 | + || settingsData == null |
| 51 | + || settingsData.isEmpty()) { |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | + int appliedCount = 0; |
| 56 | + Level level = player.level(); |
| 57 | + |
| 58 | + for (BlockPos pos : data.cachedPositions()) { |
| 59 | + BlockEntity be = level.getBlockEntity(pos); |
| 60 | + if (be == null) continue; |
| 61 | + |
| 62 | + boolean blockSuccess = false; |
| 63 | + |
| 64 | + if (be instanceof IPartHost host) { |
| 65 | + for (Direction side : ALL_SIDES) { |
| 66 | + IPart part = host.getPart(side); |
| 67 | + if (part != null) { |
| 68 | + if (performApply(part, getSettingsId(part), storedName, settingsData, player)) { |
| 69 | + blockSuccess = true; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } else if (be instanceof AEBaseBlockEntity aeBe) { |
| 74 | + if (performApply(aeBe, getSettingsId(aeBe), storedName, settingsData, player)) { |
| 75 | + blockSuccess = true; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (blockSuccess) { |
| 80 | + appliedCount++; |
| 81 | + } |
| 82 | + } |
| 83 | + if (appliedCount > 0) { |
| 84 | + memoryCard.notifyUser(player, MemoryCardMessages.SETTINGS_LOADED); |
| 85 | + } |
| 86 | + |
| 87 | + return appliedCount; |
| 88 | + } |
| 89 | + |
| 90 | + private static String getSettingsId(Object target) { |
| 91 | + if (target instanceof IPart part) { |
| 92 | + Item partItem = part.getPartItem().asItem(); |
| 93 | + if (AEParts.INTERFACE.asItem() == partItem) { |
| 94 | + partItem = AEBlocks.INTERFACE.asItem(); |
| 95 | + } else if (AEParts.PATTERN_PROVIDER.asItem() == partItem) { |
| 96 | + partItem = AEBlocks.PATTERN_PROVIDER.asItem(); |
| 97 | + } |
| 98 | + return partItem.getDescriptionId(); |
| 99 | + } else if (target instanceof AEBaseBlockEntity be) { |
| 100 | + return be.getBlockState().getBlock().getDescriptionId(); |
| 101 | + } |
| 102 | + return ""; |
| 103 | + } |
| 104 | + |
| 105 | + private static boolean performApply( |
| 106 | + Object target, String targetName, String storedName, CompoundTag data, ServerPlayer player) { |
| 107 | + try { |
| 108 | + if (Objects.equals(targetName, storedName)) { |
| 109 | + if (target instanceof IPart part) { |
| 110 | + part.importSettings(SettingsFrom.MEMORY_CARD, data, player); |
| 111 | + } else if (target instanceof AEBaseBlockEntity be) { |
| 112 | + be.importSettings(SettingsFrom.MEMORY_CARD, data, player); |
| 113 | + } |
| 114 | + } else { |
| 115 | + if (target instanceof IPart part) { |
| 116 | + MemoryCardItem.importGenericSettings(part, data, player); |
| 117 | + } else if (target instanceof AEBaseBlockEntity be) { |
| 118 | + MemoryCardItem.importGenericSettings(be, data, player); |
| 119 | + } |
| 120 | + } |
| 121 | + return true; |
| 122 | + } catch (Exception e) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments