|
1 | 1 | package lumien.randomthings.Mixins; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | | -import java.util.Arrays; |
5 | | -import java.util.Collections; |
6 | | -import java.util.List; |
7 | | -import java.util.Set; |
8 | | -import java.util.function.Supplier; |
| 3 | +import javax.annotation.Nonnull; |
9 | 4 |
|
10 | | -import org.apache.logging.log4j.LogManager; |
11 | | -import org.apache.logging.log4j.Logger; |
| 5 | +import com.gtnewhorizon.gtnhmixins.builders.IMixins; |
| 6 | +import com.gtnewhorizon.gtnhmixins.builders.MixinBuilder; |
12 | 7 |
|
13 | | -import cpw.mods.fml.relauncher.FMLLaunchHandler; |
| 8 | +import lumien.randomthings.Configuration.ConfigBlocks; |
14 | 9 | import lumien.randomthings.Configuration.VanillaChanges; |
15 | 10 |
|
16 | | -public enum Mixins { |
17 | | - |
18 | | - BLOCK_MAGICAL_LEAVES(new Builder("Thaumcraft Fast Leaf Decay").setSide(Side.BOTH) |
19 | | - .addTargetedMod(TargetedMod.THAUMCRAFT).setPhase(Phase.LATE) |
20 | | - .setApplyIf(() -> VanillaChanges.FASTER_LEAVEDECAY).addMixinClasses("MixinBlockMagicalLeaves")); |
21 | | - |
22 | | - private final List<String> mixinClasses; |
23 | | - private final List<TargetedMod> targetedMods; |
24 | | - private final List<TargetedMod> excludedMods; |
25 | | - private final Supplier<Boolean> applyIf; |
26 | | - private final Phase phase; |
27 | | - private final Side side; |
28 | | - public static final Logger LOGGER = LogManager.getLogger("RandomThings"); |
29 | | - |
30 | | - Mixins(Builder builder) { |
31 | | - this.mixinClasses = builder.mixinClasses; |
32 | | - this.targetedMods = builder.targetedMods; |
33 | | - this.excludedMods = builder.excludedMods; |
34 | | - this.applyIf = builder.applyIf; |
35 | | - this.phase = builder.phase; |
36 | | - this.side = builder.side; |
37 | | - if (this.mixinClasses.isEmpty()) { |
38 | | - throw new RuntimeException("No mixin class specified for Mixin : " + this.name()); |
39 | | - } |
40 | | - if (this.targetedMods.isEmpty()) { |
41 | | - throw new RuntimeException("No targeted mods specified for Mixin : " + this.name()); |
42 | | - } |
43 | | - if (this.applyIf == null) { |
44 | | - throw new RuntimeException("No ApplyIf function specified for Mixin : " + this.name()); |
45 | | - } |
46 | | - if (this.phase == null) { |
47 | | - throw new RuntimeException("No Phase specified for Mixin : " + this.name()); |
48 | | - } |
49 | | - if (this.side == null) { |
50 | | - throw new RuntimeException("No Side function specified for Mixin : " + this.name()); |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - public static List<String> getEarlyMixins(Set<String> loadedCoreMods) { |
55 | | - final List<String> mixins = new ArrayList<>(); |
56 | | - final List<String> notLoading = new ArrayList<>(); |
57 | | - for (Mixins mixin : Mixins.values()) { |
58 | | - if (mixin.phase == Phase.EARLY) { |
59 | | - if (mixin.shouldLoad(loadedCoreMods, Collections.emptySet())) { |
60 | | - mixins.addAll(mixin.mixinClasses); |
61 | | - } else { |
62 | | - notLoading.addAll(mixin.mixinClasses); |
63 | | - } |
64 | | - } |
65 | | - } |
66 | | - LOGGER.info("Not loading the following EARLY mixins: {}", notLoading.toString()); |
67 | | - return mixins; |
68 | | - } |
69 | | - |
70 | | - public static List<String> getLateMixins(Set<String> loadedMods) { |
71 | | - final List<String> mixins = new ArrayList<>(); |
72 | | - final List<String> notLoading = new ArrayList<>(); |
73 | | - for (Mixins mixin : Mixins.values()) { |
74 | | - if (mixin.phase == Phase.LATE) { |
75 | | - if (mixin.shouldLoad(Collections.emptySet(), loadedMods)) { |
76 | | - mixins.addAll(mixin.mixinClasses); |
77 | | - } else { |
78 | | - notLoading.addAll(mixin.mixinClasses); |
79 | | - } |
80 | | - } |
81 | | - } |
82 | | - LOGGER.info("Not loading the following LATE mixins: {}", notLoading.toString()); |
83 | | - return mixins; |
84 | | - } |
85 | | - |
86 | | - private boolean shouldLoadSide() { |
87 | | - return side == Side.BOTH || (side == Side.SERVER && FMLLaunchHandler.side().isServer()) |
88 | | - || (side == Side.CLIENT && FMLLaunchHandler.side().isClient()); |
89 | | - } |
90 | | - |
91 | | - private boolean allModsLoaded(List<TargetedMod> targetedMods, Set<String> loadedCoreMods, Set<String> loadedMods) { |
92 | | - if (targetedMods.isEmpty()) return false; |
93 | | - |
94 | | - for (TargetedMod target : targetedMods) { |
95 | | - if (target == TargetedMod.VANILLA) continue; |
96 | | - |
97 | | - if (!loadedCoreMods.isEmpty() && target.coreModClass != null |
98 | | - && !loadedCoreMods.contains(target.coreModClass)) |
99 | | - return false; |
100 | | - else if (!loadedMods.isEmpty() && target.modId != null && !loadedMods.contains(target.modId)) return false; |
101 | | - } |
102 | | - |
103 | | - return true; |
104 | | - } |
105 | | - |
106 | | - private boolean noModsLoaded(List<TargetedMod> targetedMods, Set<String> loadedCoreMods, Set<String> loadedMods) { |
107 | | - if (targetedMods.isEmpty()) return true; |
108 | | - |
109 | | - for (TargetedMod target : targetedMods) { |
110 | | - if (target == TargetedMod.VANILLA) continue; |
111 | | - |
112 | | - if (!loadedCoreMods.isEmpty() && target.coreModClass != null |
113 | | - && loadedCoreMods.contains(target.coreModClass)) |
114 | | - return false; |
115 | | - else if (!loadedMods.isEmpty() && target.modId != null && loadedMods.contains(target.modId)) return false; |
116 | | - } |
117 | | - |
118 | | - return true; |
119 | | - } |
120 | | - |
121 | | - private boolean shouldLoad(Set<String> loadedCoreMods, Set<String> loadedMods) { |
122 | | - return (shouldLoadSide() && applyIf.get() |
123 | | - && allModsLoaded(targetedMods, loadedCoreMods, loadedMods) |
124 | | - && noModsLoaded(excludedMods, loadedCoreMods, loadedMods)); |
125 | | - } |
126 | | - |
127 | | - private static class Builder { |
128 | | - |
129 | | - private final String name; |
130 | | - private final List<String> mixinClasses = new ArrayList<>(); |
131 | | - private final List<TargetedMod> targetedMods = new ArrayList<>(); |
132 | | - private final List<TargetedMod> excludedMods = new ArrayList<>(); |
133 | | - private Supplier<Boolean> applyIf = null; |
134 | | - private Phase phase = null; |
135 | | - private Side side = null; |
136 | | - |
137 | | - public Builder(String name) { |
138 | | - this.name = name; |
139 | | - } |
140 | | - |
141 | | - public Builder addMixinClasses(String... mixinClasses) { |
142 | | - this.mixinClasses.addAll(Arrays.asList(mixinClasses)); |
143 | | - return this; |
144 | | - } |
145 | | - |
146 | | - public Builder setPhase(Phase phase) { |
147 | | - if (this.phase != null) { |
148 | | - throw new RuntimeException("Trying to define Phase twice for " + this.name); |
149 | | - } |
150 | | - this.phase = phase; |
151 | | - return this; |
152 | | - } |
153 | | - |
154 | | - public Builder setSide(Side side) { |
155 | | - if (this.side != null) { |
156 | | - throw new RuntimeException("Trying to define Side twice for " + this.name); |
157 | | - } |
158 | | - this.side = side; |
159 | | - return this; |
160 | | - } |
161 | | - |
162 | | - public Builder setApplyIf(Supplier<Boolean> applyIf) { |
163 | | - this.applyIf = applyIf; |
164 | | - return this; |
165 | | - } |
166 | | - |
167 | | - public Builder addTargetedMod(TargetedMod mod) { |
168 | | - this.targetedMods.add(mod); |
169 | | - return this; |
170 | | - } |
171 | | - |
172 | | - public Builder addExcludedMod(TargetedMod mod) { |
173 | | - this.excludedMods.add(mod); |
174 | | - return this; |
175 | | - } |
176 | | - } |
177 | | - |
178 | | - private enum Side { |
179 | | - BOTH, |
180 | | - CLIENT, |
181 | | - SERVER |
182 | | - } |
183 | | - |
184 | | - private enum Phase { |
185 | | - EARLY, |
186 | | - LATE, |
| 11 | +/** |
| 12 | + * IMPORTANT: Do not make any references to any mod from this file. This file is loaded quite early on and if you refer |
| 13 | + * to other mods you load them as well. The consequence is: You can't inject any previously loaded classes! Exception: |
| 14 | + * Reference.java, as long as it is used for Strings only! |
| 15 | + */ |
| 16 | +public enum Mixins implements IMixins { |
| 17 | + |
| 18 | + // spotless:off |
| 19 | + MINECRAFT(new MixinBuilder() |
| 20 | + .addClientMixins( |
| 21 | + "MixinEntityRenderer", |
| 22 | + "GuiAccessor", |
| 23 | + "MixinItem", |
| 24 | + "MixinRenderGlobal") |
| 25 | + .addCommonMixins( |
| 26 | + "EntityAccessor", |
| 27 | + "EntityLivingAccessor", |
| 28 | + "PotionAccessor", |
| 29 | + "MixinSpawnerAnimals", |
| 30 | + "WorldServerAccessor", |
| 31 | + "WorldAccessor") |
| 32 | + .setPhase(Phase.EARLY)), |
| 33 | + BLOCK_LEAVES_BASE(new MixinBuilder() |
| 34 | + .addCommonMixins("MixinBlockLeavesBase") |
| 35 | + .setApplyIf(() -> VanillaChanges.FASTER_LEAVEDECAY) |
| 36 | + .setPhase(Phase.EARLY)), |
| 37 | + THAUMCRAFT_FAST_LEAVE_DECAY(new MixinBuilder() |
| 38 | + .addCommonMixins("MixinBlockMagicalLeaves") |
| 39 | + .addRequiredMod(TargetedMod.THAUMCRAFT) |
| 40 | + .setPhase(Phase.LATE) |
| 41 | + .setApplyIf(() -> VanillaChanges.FASTER_LEAVEDECAY)), |
| 42 | + GUIVIDEOSETTINGS(new MixinBuilder() |
| 43 | + .addClientMixins("MixinGuiVideoSettings") |
| 44 | + .setApplyIf(() -> VanillaChanges.LOCKED_GAMMA) |
| 45 | + .setPhase(Phase.EARLY)), |
| 46 | + WORLD(new MixinBuilder() |
| 47 | + .addCommonMixins("MixinWorld") |
| 48 | + .setApplyIf(() -> ConfigBlocks.wirelessLever) |
| 49 | + .setPhase(Phase.EARLY)); |
| 50 | + // spotless:on |
| 51 | + |
| 52 | + private final MixinBuilder builder; |
| 53 | + |
| 54 | + Mixins(MixinBuilder builder) { |
| 55 | + this.builder = builder; |
| 56 | + } |
| 57 | + |
| 58 | + @Nonnull |
| 59 | + @Override |
| 60 | + public MixinBuilder getBuilder() { |
| 61 | + return this.builder; |
187 | 62 | } |
188 | 63 | } |
0 commit comments