aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/rosegoldaddons/features/PingWorldChange.java
blob: 0ac8cb68727a465039327f81fcbcc49eaec020ad (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
package rosegoldaddons.features;

import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import rosegoldaddons.Main;
import rosegoldaddons.utils.ChatUtils;
import rosegoldaddons.utils.DiscordWebhook;

import java.io.IOException;

public class PingWorldChange {
    private int countdown = 0;

    @SubscribeEvent
    public void onWorldChange(WorldEvent.Unload event) {
        if(Main.configFile.pingworldchange && countdown == 0) {
            String url = Main.configFile.hookurl;
            String id = Main.configFile.discordid;
            if(url.contains("https://") && (url.contains("discord.com/api/webhooks/") || url.contains("discordapp.com/api/webhooks/"))) {
                if(!id.equals("")) {
                    DiscordWebhook webhook = new DiscordWebhook(url);
                    webhook.setContent("<@" + id + "> Detected World Change.");
                    try {
                        countdown = 100;
                        webhook.execute();
                    } catch (IOException e) {
                        ChatUtils.sendMessage("Ping sender seemed to have crashed: "+e.getMessage());
                        e.printStackTrace();
                    }
                } else {
                    ChatUtils.sendMessage("Invalid Discord ID");
                }
            } else {
                ChatUtils.sendMessage("Invalid Webhook URL");
            }
        }
    }

    @SubscribeEvent
    public void onTick(TickEvent.ClientTickEvent event) {
        if(event.phase == TickEvent.Phase.END) {
            if(countdown > 0) {
                countdown--;
            }
        }
    }
}