|
25 | 25 | */
|
26 | 26 | package com.xpdustry.momo;
|
27 | 27 |
|
28 |
| -import arc.util.Log; |
29 |
| -import mindustry.mod.Plugin; |
| 28 | +import com.xpdustry.distributor.api.annotation.PluginAnnotationProcessor; |
| 29 | +import com.xpdustry.distributor.api.plugin.AbstractMindustryPlugin; |
| 30 | +import com.xpdustry.distributor.api.plugin.PluginListener; |
| 31 | +import com.xpdustry.momo.tower.TowerConfig; |
| 32 | +import com.xpdustry.momo.tower.TowerDrop; |
| 33 | +import com.xpdustry.momo.tower.TowerLogic; |
| 34 | +import java.io.IOException; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Objects; |
| 39 | +import java.util.stream.Collectors; |
| 40 | +import mindustry.Vars; |
| 41 | +import mindustry.ctype.ContentType; |
| 42 | +import mindustry.type.Item; |
| 43 | +import mindustry.type.UnitType; |
| 44 | +import org.jspecify.annotations.Nullable; |
| 45 | +import org.spongepowered.configurate.ConfigurationNode; |
| 46 | +import org.spongepowered.configurate.yaml.YamlConfigurationLoader; |
30 | 47 |
|
31 | 48 | @SuppressWarnings("unused")
|
32 |
| -public final class MoMoPlugin extends Plugin { |
| 49 | +public final class MoMoPlugin extends AbstractMindustryPlugin implements MoMoAPI { |
| 50 | + |
| 51 | + private final PluginAnnotationProcessor<List<Object>> processor = PluginAnnotationProcessor.compose( |
| 52 | + PluginAnnotationProcessor.events(this), |
| 53 | + PluginAnnotationProcessor.tasks(this), |
| 54 | + PluginAnnotationProcessor.triggers(this), |
| 55 | + PluginAnnotationProcessor.playerActions(this)); |
| 56 | + |
| 57 | + private @Nullable MoMoConfig config; |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onInit() { |
| 61 | + try { |
| 62 | + this.config = load(); |
| 63 | + } catch (final Exception e) { |
| 64 | + throw new RuntimeException("Failed to load MoMo", e); |
| 65 | + } |
| 66 | + this.addListener(new TowerLogic(this)); |
| 67 | + } |
33 | 68 |
|
34 | 69 | @Override
|
35 |
| - public void init() { |
36 |
| - Log.info("Hello MOMO"); |
| 70 | + public void addListener(final PluginListener listener) { |
| 71 | + super.addListener(listener); |
| 72 | + processor.process(listener); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean isActive(final MoGameMode mode) { |
| 77 | + return getMoConfig().mode() == mode; |
| 78 | + } |
| 79 | + |
| 80 | + public MoMoConfig getMoConfig() { |
| 81 | + return Objects.requireNonNull(config); |
| 82 | + } |
| 83 | + |
| 84 | + private MoMoConfig load() throws IOException { |
| 85 | + final var file = this.getDirectory().resolve("config.yaml"); |
| 86 | + if (Files.notExists(file)) { |
| 87 | + try (final var stream = Objects.requireNonNull( |
| 88 | + this.getClass().getClassLoader().getResourceAsStream("com/xpdustry/momo/config.yaml"))) { |
| 89 | + Files.copy(stream, file); |
| 90 | + } |
| 91 | + } |
| 92 | + final var node = YamlConfigurationLoader.builder().path(file).build().load(); |
| 93 | + |
| 94 | + final TowerConfig tower; |
| 95 | + { |
| 96 | + final var root = node.node("tower-defense"); |
| 97 | + final var drops = root.node("drops").childrenMap().entrySet().stream() |
| 98 | + .collect(Collectors.toUnmodifiableMap( |
| 99 | + entry -> entry.getKey().toString(), entry -> entry.getValue().childrenList().stream() |
| 100 | + .map(this::parseDrop) |
| 101 | + .toList())); |
| 102 | + final var units = root.node("units").childrenMap().entrySet().stream() |
| 103 | + .collect(Collectors.toUnmodifiableMap( |
| 104 | + entry -> { |
| 105 | + final var name = entry.getKey().toString(); |
| 106 | + return Objects.requireNonNull( |
| 107 | + Vars.content.<UnitType>getByName(ContentType.unit, name), |
| 108 | + "Unknown unit " + name); |
| 109 | + }, |
| 110 | + entry -> parseUnit(drops, entry.getValue()))); |
| 111 | + tower = new TowerConfig(drops, units); |
| 112 | + } |
| 113 | + |
| 114 | + MoGameMode mode = null; |
| 115 | + { |
| 116 | + final var root = node.node("game-mode"); |
| 117 | + if (!root.virtual()) { |
| 118 | + final var name = Objects.requireNonNull(root.getString()); |
| 119 | + mode = switch (name) { |
| 120 | + case "tower-defense" -> MoGameMode.TOWER_DEFENSE; |
| 121 | + default -> throw new IllegalArgumentException("Unknown gamemode " + name);}; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return new MoMoConfig(mode, tower); |
| 126 | + } |
| 127 | + |
| 128 | + private TowerDrop parseDrop(final ConfigurationNode node) { |
| 129 | + final var type = node.node("type").getString("simple"); |
| 130 | + switch (type) { |
| 131 | + case "simple" -> { |
| 132 | + final var itemName = |
| 133 | + Objects.requireNonNull(node.node("item").getString(), "item field missing for " + node.path()); |
| 134 | + final var item = Objects.requireNonNull( |
| 135 | + Vars.content.<Item>getByName(ContentType.item, itemName), "Unknown item " + itemName); |
| 136 | + final var amountNode = node.node("amount"); |
| 137 | + if (amountNode.virtual()) throw new RuntimeException("The amount of item is missing"); |
| 138 | + return new TowerDrop.Simple(item, amountNode.getInt()); |
| 139 | + } |
| 140 | + case "random" -> { |
| 141 | + return new TowerDrop.Random(node.node("items").childrenList().stream() |
| 142 | + .map(this::parseDrop) |
| 143 | + .toList()); |
| 144 | + } |
| 145 | + default -> throw new RuntimeException("Unknown bounty type: " + type); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private TowerConfig.UnitData parseUnit(final Map<String, List<TowerDrop>> drops, final ConfigurationNode node) { |
| 150 | + final var dropName = |
| 151 | + Objects.requireNonNull(node.node("drop").getString(), "drop field missing for " + node.path()); |
| 152 | + final var drop = Objects.requireNonNull(drops.get(dropName), "Unknown drop bundle " + dropName); |
| 153 | + return new TowerConfig.UnitData(drop); |
37 | 154 | }
|
38 | 155 | }
|
0 commit comments