You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
3.0 KiB
81 lines
3.0 KiB
package dev.garrettmills.csx.matterlinkreboot;
|
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
import net.minecraftforge.event.ServerChatEvent;
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
|
import net.minecraftforge.fml.ModLoadingContext;
|
|
import net.minecraftforge.fml.common.Mod;
|
|
import net.minecraftforge.fml.config.ModConfig;
|
|
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
|
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
|
|
import net.minecraftforge.fml.event.server.FMLServerStoppingEvent;
|
|
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
import java.util.Timer;
|
|
import java.util.TimerTask;
|
|
|
|
// The value here should match an entry in the META-INF/mods.toml file
|
|
@Mod("matterlinkreboot")
|
|
public class MatterlinkReboot
|
|
{
|
|
public static final String MOD_ID = "matterlinkreboot";
|
|
|
|
// Directly reference a log4j logger.
|
|
private static final Logger LOGGER = LogManager.getLogger(MatterlinkReboot.MOD_ID);
|
|
|
|
private final TimerTask task = new MatterlinkRebootTask();
|
|
private final Timer timer = new Timer(true);
|
|
|
|
public MatterlinkReboot() {
|
|
// Register the setup method for modloading
|
|
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
|
|
|
|
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, MatterlinkRebootConfig.spec);
|
|
|
|
// Register ourselves for server and other game events we are interested in
|
|
MinecraftForge.EVENT_BUS.register(this);
|
|
}
|
|
|
|
private void setup(final FMLCommonSetupEvent event) {
|
|
LOGGER.info("Bridge enabled? " + this.isEnabled());
|
|
LOGGER.info("Bridge API: " + MatterlinkRebootConfig.GENERAL.BridgeHost.get() + ":" + MatterlinkRebootConfig.GENERAL.BridgePort.get());
|
|
}
|
|
|
|
@SubscribeEvent
|
|
public void onServerStarting(FMLServerStartingEvent event) {
|
|
if ( this.isEnabled() ) {
|
|
LOGGER.info("Scheduling background task!");
|
|
LOGGER.info("Polling interval: " + MatterlinkRebootConfig.GENERAL.PollingInterval.get() + " seconds");
|
|
this.timer.scheduleAtFixedRate(this.task, 0, MatterlinkRebootConfig.GENERAL.PollingInterval.get() * 1000);
|
|
}
|
|
}
|
|
|
|
@SubscribeEvent
|
|
public void onServerStopping(final FMLServerStoppingEvent event) {
|
|
if ( this.isEnabled() ) {
|
|
LOGGER.info("Stopping background task!");
|
|
this.timer.cancel();
|
|
}
|
|
}
|
|
|
|
@SubscribeEvent
|
|
public void onServerChat(ServerChatEvent event) {
|
|
if ( this.isEnabled() ) {
|
|
OutgoingMessage msg = new OutgoingMessage();
|
|
|
|
msg.setUsername(event.getUsername());
|
|
msg.setText(event.getMessage());
|
|
msg.setAvatar(MatterlinkRebootConfig.GENERAL.DefaultAvatar.get());
|
|
msg.setGateway(MatterlinkRebootConfig.GENERAL.BridgeGateway.get());
|
|
|
|
MatterbridgeAPIClient.sendMessage(msg);
|
|
}
|
|
}
|
|
|
|
public boolean isEnabled() {
|
|
return MatterlinkRebootConfig.GENERAL.BridgeEnabled.get();
|
|
}
|
|
}
|