|
2 | 2 |
|
3 | 3 | import com.minecolonies.api.colony.buildings.IBuilding; |
4 | 4 | import com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding; |
| 5 | +import com.simibubi.create.AllDataComponents; |
| 6 | +import com.simibubi.create.content.equipment.clipboard.ClipboardContent; |
5 | 7 | import com.simibubi.create.content.equipment.clipboard.ClipboardEntry; |
6 | 8 | import com.simibubi.create.content.equipment.clipboard.ClipboardOverrides; |
7 | 9 | import net.minecraft.ChatFormatting; |
|
11 | 13 | import net.minecraft.network.chat.HoverEvent; |
12 | 14 | import net.minecraft.network.chat.MutableComponent; |
13 | 15 | import net.minecraft.network.chat.Style; |
14 | | -import net.minecraft.util.Unit; |
15 | 16 | import net.minecraft.world.InteractionResult; |
16 | 17 | import net.minecraft.world.entity.player.Player; |
17 | 18 | import net.minecraft.world.item.ItemStack; |
18 | 19 | import net.minecraft.world.level.Level; |
19 | 20 | import net.minecraft.world.level.block.entity.BlockEntity; |
20 | 21 | import net.minecraft.world.level.block.state.BlockState; |
21 | | -import net.neoforged.bus.api.SubscribeEvent; |
22 | 22 | import net.neoforged.neoforge.common.util.FakePlayer; |
23 | 23 | import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent; |
| 24 | +import net.neoforged.bus.api.SubscribeEvent; |
24 | 25 | import nl.motionlesstrain.createcolonies.compatibility.Minecolonies; |
25 | | - |
26 | 26 | import java.util.ArrayList; |
27 | 27 | import java.util.List; |
| 28 | +import java.util.Optional; |
28 | 29 | import java.util.TreeMap; |
29 | 30 |
|
30 | 31 | import static com.minecolonies.core.colony.buildings.modules.BuildingModules.BUILDING_RESOURCES; |
31 | | -import static nl.motionlesstrain.createcolonies.resources.CreateResources.DataComponentTypes.clipboardPages; |
32 | | -import static nl.motionlesstrain.createcolonies.resources.CreateResources.DataComponentTypes.clipboardReadOnly; |
33 | 32 | import static nl.motionlesstrain.createcolonies.resources.CreateResources.Items.clipboard; |
34 | 33 | import static nl.motionlesstrain.createcolonies.resources.MinecoloniesResources.Blocks.blockHutBuilder; |
35 | 34 |
|
36 | 35 | public class InteractionHook { |
37 | 36 |
|
38 | | - @SubscribeEvent |
39 | | - public static void onPlayerRightClick(PlayerInteractEvent.RightClickBlock evt) { |
40 | | - final Player player = evt.getEntity(); |
41 | | - if (!player.isShiftKeyDown()) return; |
42 | | - |
43 | | - final Level world = evt.getLevel(); |
44 | | - final BlockPos blockPosClicked = evt.getHitVec().getBlockPos(); |
45 | | - final BlockState blockStateClicked = world.getBlockState(blockPosClicked); |
46 | | - |
47 | | - final ItemStack heldItem = evt.getItemStack(); |
48 | | - |
49 | | - if (blockHutBuilder.isBound() && blockStateClicked.is(blockHutBuilder) && clipboard.isBound() && heldItem.is(clipboard)) { |
50 | | - evt.setCanceled(true); |
51 | | - evt.setCancellationResult(InteractionResult.SUCCESS); |
52 | | - if (evt.getSide().isClient()) return; |
53 | | - |
54 | | - if (player instanceof FakePlayer) return; |
55 | | - |
56 | | - Minecolonies.runIfInstalled(() -> () -> { |
57 | | - final BlockEntity blockEntity = world.getBlockEntity(blockPosClicked); |
58 | | - if (blockEntity instanceof AbstractTileEntityColonyBuilding buildingEntity) { |
59 | | - final IBuilding building = buildingEntity.getBuilding(); |
60 | | - final var resourcesModule = building.getModule(BUILDING_RESOURCES); |
61 | | - if (resourcesModule != null) { |
62 | | - final var allResources = resourcesModule.getNeededResources(); |
63 | | - final TreeMap<String, NeededItem> neededResources = new TreeMap<>(); |
64 | | - allResources.values().forEach(resource -> { |
65 | | - final int entireAmount = resource.getAmount(), |
66 | | - availableAmount = resource.getAvailable(), |
67 | | - deliveringAmount = resource.getAmountInDelivery(), |
68 | | - neededAmount = entireAmount - availableAmount - deliveringAmount; |
69 | | - final ItemStack itemStack = resource.getItemStack(); |
70 | | - if (neededAmount > 0) { |
71 | | - neededResources.put(itemStack.getItem().getName(itemStack).getString(), |
72 | | - new NeededItem(itemStack, neededAmount, entireAmount)); |
73 | | - } |
| 37 | + @SubscribeEvent |
| 38 | + public static void onPlayerRightClick(PlayerInteractEvent.RightClickBlock evt) { |
| 39 | + final Player player = evt.getEntity(); |
| 40 | + if (!player.isShiftKeyDown()) return; |
| 41 | + |
| 42 | + final Level world = evt.getLevel(); |
| 43 | + final BlockPos blockPosClicked = evt.getHitVec().getBlockPos(); |
| 44 | + final BlockState blockStateClicked = world.getBlockState(blockPosClicked); |
| 45 | + final ItemStack heldItem = evt.getItemStack(); |
| 46 | + |
| 47 | + if (blockHutBuilder.isPresent() && blockStateClicked.is(blockHutBuilder.get()) && clipboard != null && heldItem.is(clipboard)) { |
| 48 | + evt.setCanceled(true); |
| 49 | + evt.setCancellationResult(InteractionResult.SUCCESS); |
| 50 | + |
| 51 | + if (evt.getSide().isClient()) return; |
| 52 | + |
| 53 | + if (player instanceof FakePlayer) return; |
| 54 | + |
| 55 | + Minecolonies.runIfInstalled(() -> () -> { |
| 56 | + final BlockEntity blockEntity = world.getBlockEntity(blockPosClicked); |
| 57 | + if (blockEntity instanceof AbstractTileEntityColonyBuilding buildingEntity) { |
| 58 | + final IBuilding building = buildingEntity.getBuilding(); |
| 59 | + final var resourcesModule = building.getModule(BUILDING_RESOURCES); |
| 60 | + if (resourcesModule != null) { |
| 61 | + final var allResources = resourcesModule.getNeededResources(); |
| 62 | + final TreeMap<String, NeededItem> neededResources = new TreeMap<>(); |
| 63 | + |
| 64 | + allResources.values().forEach(resource -> { |
| 65 | + final int entireAmount = resource.getAmount(), |
| 66 | + availableAmount = resource.getAvailable(), |
| 67 | + deliveringAmount = resource.getAmountInDelivery(), |
| 68 | + neededAmount = entireAmount - availableAmount - deliveringAmount; |
| 69 | + final ItemStack itemStack = resource.getItemStack(); |
| 70 | + if (neededAmount > 0) { |
| 71 | + neededResources.put(itemStack.getItem().getName(itemStack).getString(), |
| 72 | + new NeededItem(itemStack, neededAmount, entireAmount)); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + List<List<ClipboardEntry>> pages = new ArrayList<>(); |
| 77 | + List<ClipboardEntry> currentEntries = new ArrayList<>(); |
| 78 | + int i = 0; |
| 79 | + |
| 80 | + for (final var neededItem : neededResources.values()) { |
| 81 | + final MutableComponent text = Component.translatable(neededItem.item.getDescriptionId()).setStyle( |
| 82 | + Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, new HoverEvent.ItemStackInfo(neededItem.item))) |
| 83 | + ).append( |
| 84 | + Component.literal("\n x" + neededItem.count).withStyle(ChatFormatting.BLACK) |
| 85 | + ).append( |
| 86 | + Component.literal(" | " + neededItem.count / 64 + "▤ +" + neededItem.count % 64).withStyle(ChatFormatting.GRAY) |
| 87 | + ); |
| 88 | + |
| 89 | + ClipboardEntry entry = new ClipboardEntry(neededItem.count == 0, text); |
| 90 | + entry.displayItem(neededItem.item, neededItem.count); |
| 91 | + currentEntries.add(entry); |
| 92 | + |
| 93 | + if (++i == 7) { |
| 94 | + currentEntries.add(new ClipboardEntry(false, Component.literal(">>>").withStyle(ChatFormatting.DARK_GRAY))); |
| 95 | + pages.add(new ArrayList<>(currentEntries)); |
| 96 | + currentEntries.clear(); |
| 97 | + i = 0; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (!currentEntries.isEmpty()) { |
| 102 | + pages.add(new ArrayList<>(currentEntries)); |
| 103 | + } |
| 104 | + |
| 105 | + ClipboardContent content = new ClipboardContent( |
| 106 | + ClipboardOverrides.ClipboardType.WRITTEN, |
| 107 | + pages, |
| 108 | + true, |
| 109 | + 0, |
| 110 | + Optional.empty() |
| 111 | + ); |
| 112 | + |
| 113 | + heldItem.set(AllDataComponents.CLIPBOARD_CONTENT, content); |
| 114 | + |
| 115 | + heldItem.set(DataComponents.CUSTOM_NAME, |
| 116 | + Component.translatable("create.materialChecklist").setStyle(Style.EMPTY.withItalic(Boolean.FALSE))); |
| 117 | + |
| 118 | + final Component message = Component.translatable("nl.motionlesstrain.createcolonies.clipboard.registered", |
| 119 | + Component.translatable(building.getBuildingDisplayName())).setStyle(Style.EMPTY.withColor(ChatFormatting.GRAY)); |
| 120 | + player.displayClientMessage(message, false); |
| 121 | + } |
| 122 | + } |
74 | 123 | }); |
75 | | - // Inspired by MaterialChecklist, which is part of the schematicannon code to write resources into a clipboard |
76 | | - final List<List<ClipboardEntry>> pages = new ArrayList<>(); |
77 | | - List<ClipboardEntry> entries = new ArrayList<>(); |
78 | | - int i = 0; |
79 | | - for (final var neededItem : neededResources.values()) { |
80 | | - final MutableComponent text = Component.translatable(neededItem.item.getDescriptionId()).setStyle( |
81 | | - Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, new HoverEvent.ItemStackInfo(neededItem.item))) |
82 | | - ).append( |
83 | | - Component.literal("\n x" + neededItem.count).withStyle(ChatFormatting.BLACK) |
84 | | - ).append( |
85 | | - Component.literal(" | " + neededItem.count / 64 + "▤ +" + neededItem.count % 64).withStyle(ChatFormatting.GRAY) |
86 | | - ); |
87 | | - entries.add( |
88 | | - new ClipboardEntry(neededItem.count == 0, text).displayItem(neededItem.item, neededItem.count)); |
89 | | - |
90 | | - if (++i == 7) { |
91 | | - entries.add( |
92 | | - new ClipboardEntry(false, Component.literal(">>>").withStyle(ChatFormatting.DARK_GRAY))); |
93 | | - pages.add(entries); |
94 | | - entries = new ArrayList<>(); |
95 | | - i = 0; |
96 | | - } |
97 | | - } |
98 | | - if (i > 0) { |
99 | | - pages.add(entries); |
100 | | - } |
101 | | - ClipboardOverrides.switchTo(ClipboardOverrides.ClipboardType.WRITTEN, heldItem); |
102 | | - heldItem.set(clipboardPages, pages); |
103 | | - heldItem.set(DataComponents.CUSTOM_NAME, Component.translatable("create.materialChecklist").setStyle(Style.EMPTY.withItalic(Boolean.FALSE))); |
104 | | - heldItem.set(clipboardReadOnly, Unit.INSTANCE); |
105 | | - |
106 | | - final Component message = Component.translatable("nl.motionlesstrain.createcolonies.clipboard.registered", |
107 | | - Component.translatable(building.getBuildingDisplayName())).setStyle(Style.EMPTY.withColor(ChatFormatting.GRAY)); |
108 | | - player.displayClientMessage(message, false); |
109 | | - } |
110 | 124 | } |
111 | | - }); |
112 | 125 | } |
113 | | - } |
114 | 126 |
|
115 | | - private record NeededItem(ItemStack item, int count, int totalCount) {} |
| 127 | + private record NeededItem(ItemStack item, int count, int totalCount) {} |
116 | 128 | } |
0 commit comments