-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/nuclearcrackhead/serverboss/api/gcp/dot/GcpDotAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.nuclearcrackhead.serverboss.api.gcp.dot; | ||
|
||
import com.google.common.util.concurrent.AtomicDouble; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
public class GcpDotAPI { | ||
|
||
protected static final AtomicDouble INDEX = new AtomicDouble(0); | ||
|
||
protected static final URI GCP_DOT_INDEX_URL = URI.create("https://gcpdot.com/gcpindex.php"); | ||
protected static final Pattern INDEX_PATTERN = Pattern.compile("'>(.*?)<"); | ||
|
||
protected static boolean STARTED = false; | ||
|
||
public static void start() { | ||
if (STARTED) return; | ||
|
||
GcpDotFetcher fetcher = new GcpDotFetcher(); | ||
Thread thread = new Thread(fetcher); | ||
thread.setName("gcp_dot_fetcher_service"); | ||
thread.setPriority(Thread.MIN_PRIORITY); | ||
thread.start(); | ||
STARTED = true; | ||
} | ||
|
||
public static Double getIndex() { | ||
return INDEX.get(); | ||
} | ||
|
||
public static class GcpDotFetcher implements Runnable { | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
while (true) { | ||
URL oracle = GCP_DOT_INDEX_URL.toURL(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(oracle.openStream())); | ||
String text = reader.lines().collect(Collectors.joining()); | ||
Matcher matcher = INDEX_PATTERN.matcher(text); | ||
while (matcher.find()) { | ||
String rawIndex = matcher.group().split("'>")[1].split("<")[0]; | ||
double index = Double.parseDouble(rawIndex); | ||
INDEX.set(index); | ||
TimeUnit.SECONDS.sleep(1); | ||
} | ||
} | ||
} catch (InterruptedException | IOException ignored) {} | ||
} | ||
|
||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/nuclearcrackhead/serverboss/content/item/GcpDotTestingItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.nuclearcrackhead.serverboss.content.item; | ||
|
||
import com.nuclearcrackhead.serverboss.api.gcp.dot.GcpDotAPI; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.Hand; | ||
import net.minecraft.world.World; | ||
|
||
public class GcpDotTestingItem extends Item { | ||
|
||
public GcpDotTestingItem(Settings settings) { | ||
super(settings); | ||
} | ||
|
||
@Override | ||
public ActionResult use(World world, PlayerEntity user, Hand hand) { | ||
if (world.isClient) return ActionResult.SUCCESS; | ||
user.sendMessage( | ||
Text.of(GcpDotAPI.getIndex().toString()), | ||
false | ||
); | ||
return ActionResult.SUCCESS; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters