summaryrefslogtreecommitdiff
path: root/src/main/java/co/fantabos/bscv/BoSCoVicino.java
blob: 7e6527e6f7e8a68a486e80fe93e66148a1c69eb8 (plain) (blame)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package co.fantabos.bscv;

import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.client.event.ClientChatEvent;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig.Type;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import co.fantabos.bscv.Module;
import co.fantabos.bscv.modules.*;

// The value here should match an entry in the META-INF/mods.toml file
@Mod("bscv")
public class BoSCoVicino {
   // Directly reference a log4j logger.
   private static final Logger LOGGER = LogManager.getLogger();

   public static Minecraft minecraft;

   public static List<Module> mods;

   private final CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();

   public BoSCoVicino() {
      // Register the setup method for modloading
      FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
      // Register the enqueueIMC method for modloading
      FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
      // Register the processIMC method for modloading
      FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);
      // Register the doClientStuff method for modloading
      FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);


      // Store minecraft instance
      BoSCoVicino.minecraft = Minecraft.getInstance();

      ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();

      // load and register mods
      BoSCoVicino.mods = new ArrayList<Module>();

      BoSCoVicino.mods.add(new Fullbright(builder, this.dispatcher));
      BoSCoVicino.mods.add(new VanillaFlight(builder, this.dispatcher));

      ForgeConfigSpec spec = builder.build();

      // register config handler
      ModLoadingContext.get().registerConfig(Type.CLIENT, spec, "bscv.toml");

      // TEMPORARY! add command to regenerate suggestions
      dispatcher.register(
         Commands.literal("rebuild_hints")
            .executes(ctx -> {
               ClientPlayerEntity player = BoSCoVicino.minecraft.player;
               if (player != null) {
                  try {
                     Field commands = player.connection.getClass().getDeclaredField("field_195517_n"); // "commands", it's obfuscated
                     commands.setAccessible(true);
                     commands.set(player.connection, this.dispatcher);
                     LOGGER.info("Rebuild HINTS");
                     log("> rebuilt hints");
                     return 1;
                  } catch (NoSuchFieldException e) {
                     log("! no such field error");
                     LOGGER.error("No such field Exception while rebuilding hints");
                     return 0;
                  } catch (IllegalAccessException e) {
                     log("! illegal access error");
                     LOGGER.error("Illegal Access Exception while rebuilding hints");
                     return 0;
                  }
               } else {
                  log("! local player is NULL");
                  LOGGER.error("Local player is NULL");
                  return 0;
               }
            })
      );

      // Register ourselves for server and other game events we are interested in
      MinecraftForge.EVENT_BUS.register(this);
   }

   public static void log(String message) {
      BoSCoVicino.minecraft.gui.getChat().addMessage(new StringTextComponent(message));
   }

   private void setup(final FMLCommonSetupEvent event) {
      // some preinit code
      LOGGER.info("HELLO FROM PREINIT");
      LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
   }

   private void doClientStuff(final FMLClientSetupEvent event) {
      // do something that can only be done on the client
      LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().options);
   }

   private void enqueueIMC(final InterModEnqueueEvent event) {
      // some example code to dispatch IMC to another mod
      InterModComms.sendTo("bscv", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world";});
   }

   private void processIMC(final InterModProcessEvent event) {
      // some example code to receive and process InterModComms from other mods
      LOGGER.info("Got IMC {}", event.getIMCStream().
            map(m->m.getMessageSupplier().get()).
            collect(Collectors.toList()));
   }
   
   // @SubscribeEvent
   // public void onRegisterCommand(RegisterCommandsEvent event) {
   //    this.dispatcher.
   // }

   @SubscribeEvent
   public void onClientChatEvent(ClientChatEvent event) {
      if (event.getMessage().startsWith("/")) {
         CommandSource source = BoSCoVicino.minecraft.player.createCommandSourceStack(); // TODO player could be NULL
         try {
            LOGGER.info(String.format("Running command %s", event.getMessage()));
            this.dispatcher.execute(event.getMessage().substring(1), source);
            BoSCoVicino.minecraft.gui.getChat().addRecentChat(event.getMessage());
            event.setCanceled(true);
         } catch (CommandSyntaxException e) {
            LOGGER.error(String.format("Syntax error in command : %s", e.toString()));
         }
      }
   }

   // You can use SubscribeEvent and let the Event Bus discover methods to call
   @SubscribeEvent
   public void onServerStarting(FMLServerStartingEvent event) {
      // do something when the server starts
      LOGGER.info("HELLO from server starting");
   }

   // You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
   // Event bus for receiving Registry Events)
   @Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
   public static class RegistryEvents {
      @SubscribeEvent
      public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
         // register a new block here
         LOGGER.info("HELLO from Register Block");
      }
   }
}