aboutsummaryrefslogtreecommitdiff
path: root/src/Java/binnie/core/AbstractMod.java
blob: 74f274cdb74b43202afc0bb057912319f284167a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package binnie.core;

import binnie.Binnie;
import binnie.core.gui.IBinnieGUID;
import binnie.core.mod.config.ManagerConfig;
import binnie.core.mod.parser.FieldParser;
import binnie.core.network.BinniePacketHandler;
import binnie.core.network.IPacketID;
import binnie.core.network.IPacketProvider;
import binnie.core.network.packet.MessageBinnie;
import binnie.core.proxy.IProxyCore;
import cpw.mods.fml.common.eventhandler.EventBus;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import net.minecraftforge.common.MinecraftForge;

public abstract class AbstractMod
  implements IPacketProvider, IInitializable
{
  private SimpleNetworkWrapper wrapper;
  
  public AbstractMod()
  {
    BinnieCore.registerMod(this);
    MinecraftForge.EVENT_BUS.register(this);
  }
  
  public abstract boolean isActive();
  
  public abstract String getChannel();
  
  public IPacketID[] getPacketIDs()
  {
    return new IPacketID[0];
  }
  
  public IBinnieGUID[] getGUIDs()
  {
    return new IBinnieGUID[0];
  }
  
  public Class[] getConfigs()
  {
    return new Class[0];
  }
  
  public abstract IProxyCore getProxy();
  
  public abstract String getModID();
  
  public SimpleNetworkWrapper getNetworkWrapper()
  {
    return this.wrapper;
  }
  
  protected abstract Class<? extends BinniePacketHandler> getPacketHandler();
  
  public void preInit()
  {
    if (!isActive()) {
      return;
    }
    if (getConfigs() != null) {
      for (Class cls : getConfigs()) {
        Binnie.Configuration.registerConfiguration(cls, this);
      }
    }
    getProxy().preInit();
    for (IInitializable module : this.modules) {
      module.preInit();
    }
    for (Field field : getClass().getFields()) {
      this.fields.add(field);
    }
    for (Class cls : getClass().getClasses()) {
      for (Field field : getClass().getFields()) {
        this.fields.add(field);
      }
    }
    for (IInitializable module : this.modules) {
      for (Field field : module.getClass().getFields()) {
        this.fields.add(field);
      }
    }
    for (Field field : this.fields) {
      try
      {
        FieldParser.preInitParse(field, this);
      }
      catch (Exception e)
      {
        throw new RuntimeException(e);
      }
    }
  }
  
  public void init()
  {
    if (!isActive()) {
      return;
    }
    getProxy().init();
    
    this.wrapper = NetworkRegistry.INSTANCE.newSimpleChannel(getChannel());
    
    this.wrapper.registerMessage(getPacketHandler(), MessageBinnie.class, 1, Side.CLIENT);
    this.wrapper.registerMessage(getPacketHandler(), MessageBinnie.class, 1, Side.SERVER);
    for (IInitializable module : this.modules) {
      module.init();
    }
    for (Field field : this.fields) {
      try
      {
        FieldParser.initParse(field, this);
      }
      catch (Exception e)
      {
        throw new RuntimeException(e);
      }
    }
  }
  
  private LinkedHashSet<Field> fields = new LinkedHashSet();
  
  public void postInit()
  {
    if (!isActive()) {
      return;
    }
    getProxy().postInit();
    for (IInitializable module : this.modules) {
      module.postInit();
    }
    for (Field field : this.fields) {
      try
      {
        FieldParser.postInitParse(field, this);
      }
      catch (Exception e)
      {
        throw new RuntimeException(e);
      }
    }
  }
  
  protected final void addModule(IInitializable init)
  {
    this.modules.add(init);
    MinecraftForge.EVENT_BUS.register(init);
  }
  
  protected List<IInitializable> modules = new ArrayList();
}