Skip to content

Commit

Permalink
Semi-working
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordonbc committed Jun 18, 2023
1 parent be6a915 commit c12e9a0
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/main/java/net/jordon/harmonylink/BatteryInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.jordon.harmonylink;


public class BatteryInfo {
boolean hasBattery;
int batteryPercent;
ChargingStatus chargingStatus;

@Override
public String toString() {
return "BatteryInfo{" +
"hasBattery=" + hasBattery +
", batteryPercent=" + batteryPercent +
", chargingStatus='" + chargingStatus + '\'' +
'}';
}
}
8 changes: 8 additions & 0 deletions src/main/java/net/jordon/harmonylink/ChargingStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.jordon.harmonylink;

public enum ChargingStatus {
BATTERY,
CHARGING,
UNKNOWN
// add any other statuses you might receive
}
16 changes: 16 additions & 0 deletions src/main/java/net/jordon/harmonylink/DockInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.jordon.harmonylink;

public class DockInfo {
String dockModel;
boolean isDocked;
boolean fallbackDetection;

@Override
public String toString() {
return "DockInfo{" +
"dockModel='" + dockModel + '\'' +
", isDocked=" + isDocked +
", fallbackDetection=" + fallbackDetection +
'}';
}
}
90 changes: 83 additions & 7 deletions src/main/java/net/jordon/harmonylink/HarmonyLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,48 @@

import com.mojang.logging.LogUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.util.GsonHelper;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.loading.FMLLoader;
import org.slf4j.Logger;

// The value here should match an entry in the META-INF/mods.toml file
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.GsonBuilder;



@Mod(HarmonyLink.MOD_ID)
public class HarmonyLink
{
// Define mod id in a common place for everything to reference
private SystemInfo systemInfo;


public static final String MOD_ID = "harmonylink";
// Directly reference a slf4j logger
private static final Logger LOGGER = LogUtils.getLogger();

private int tickCount = 0;

public HarmonyLink()
{
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();

// Register the commonSetup method for modloading
modEventBus.addListener(this::commonSetup);

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

Expand All @@ -37,14 +52,75 @@ private void commonSetup(final FMLCommonSetupEvent event)

}

// You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent
@SubscribeEvent
public void onPlayerTick(TickEvent.PlayerTickEvent event)
{
if (event.player.level.isClientSide) // We only want to do this on the client side
{
if (++tickCount >= 20*2) // Increase the tick count and check if we've reached 20 yet
{
tickCount = 0; // Reset the tick count for next time

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://127.0.0.1:9000/all_info"))
.build();

client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(this::handleResponse);
}
}
}


private void handleResponse(String jsonResponse)
{
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();

SystemInfo systemInfo = gson.fromJson(jsonResponse, SystemInfo.class);

this.systemInfo = systemInfo;

LOGGER.info("JSON Response: {}", this.systemInfo);

if (this.systemInfo.battery_info.hasBattery)
{
if (this.systemInfo.battery_info.chargingStatus == ChargingStatus.BATTERY)
{
setRenderDistance(4);
}
else if (this.systemInfo.battery_info.chargingStatus == ChargingStatus.CHARGING)
{
setRenderDistance(12);
}
}
}

private void setRenderDistance(int distance)
{
// Check if the game is running on the client side
if(FMLLoader.getDist() == Dist.CLIENT)
{
//Minecraft.getInstance().;
// Get the current Minecraft instance and adjust the render distance
//Minecraft.getInstance().options.renderDistance = distance;
}
else
{
LOGGER.warn("Attempted to set render distance from server side, this is not supported.");
}
}


@Mod.EventBusSubscriber(modid = MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public static class ClientModEvents
{
@SubscribeEvent
public static void onClientSetup(FMLClientSetupEvent event)
{
// Some client setup code
LOGGER.info("HELLO FROM CLIENT SETUP");
LOGGER.info("MINECRAFT NAME >> {}", Minecraft.getInstance().getUser().getName());
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/jordon/harmonylink/OSInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.jordon.harmonylink;

public class OSInfo {
String osType;
String osVersion;
String osEdition;
String osBits;

@Override
public String toString() {
return "OSInfo{" +
"osType='" + osType + '\'' +
", osVersion='" + osVersion + '\'' +
", osEdition='" + osEdition + '\'' +
", osBits='" + osBits + '\'' +
'}';
}
}
16 changes: 16 additions & 0 deletions src/main/java/net/jordon/harmonylink/SystemInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.jordon.harmonylink;

public class SystemInfo {
OSInfo os_info;
BatteryInfo battery_info;
DockInfo dock_info;

@Override
public String toString() {
return "SystemInfo{" +
"os_info=" + os_info +
", battery_info=" + battery_info +
", dock_info=" + dock_info +
'}';
}
}

0 comments on commit c12e9a0

Please sign in to comment.