Jump to content

Recommended Posts

Posted

So I've been asked by a friend to make a mod which will get some info about players from public APIs. Specifically for the Hypixel guild system.

 

This is my code:

 

Main Class:

 

package com.reject.main;



import com.reject.commands.GuildInfoCommand;
import com.reject.utils.ChatListener;

import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

@Mod(modid="reject", name= "Guild Info", version="1.0", clientSideOnly = true)

public class RMain
{

//Creating instances.
    @Instance("reject")
    public static RMain instance;
    

    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {

    	
    	
    }

    @EventHandler
    public void init(FMLInitializationEvent event)
    {
    	ClientCommandHandler.instance.registerCommand(new GuildInfoCommand());
    	MinecraftForge.EVENT_BUS.register(new ChatListener());
    	
     }
}

 

 

Command used to get the info:

 

package com.reject.commands;

import java.util.List;

import com.reject.utils.ApiTool;

import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.BlockPos;

public class GuildInfoCommand extends CommandBase {

@Override
public boolean canCommandSenderUse(ICommandSender arg0) {
	// TODO Auto-generated method stub
	return true;
}

@Override
public void execute(ICommandSender arg0, String[] arg1) throws CommandException {
	// TODO Auto-generated method stub
	System.out.println("Executed command, assigning ApiTool.player");
	ApiTool.player = arg1[0];
ApiTool.playerUUID = ApiTool.playerUUID = PlayerUtil.getPlayerUUID(ApiTool.player);
	System.out.println("Assigned ApiTool.player, getting coins");
	ApiTool.getCoins();

}

@Override
public String getCommandUsage(ICommandSender arg0) {
	// TODO Auto-generated method stub
	return "ginfo (playername)";
}

@Override
public String getName() {
	// TODO Auto-generated method stub
	return "ginfo";
}



}

 

 

PlayerUUID Getter;

 

package com.reject.utils;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import net.minecraft.client.Minecraft;

public class PlayerUtil {



public static String getPlayerUUID(String playerName){
	String uuid = parse("https://api.mojang.com/users/profiles/minecraft/" + playerName);
	return uuid;
}
public static String parse(String jsonLine) {
	JsonElement jelement = new JsonParser().parse(jsonLine);
	JsonObject jobject = jelement.getAsJsonObject();
	jobject = jobject.getAsJsonObject("id");
	return jobject.toString();
}

}

 

 

Hypixel API Reader:

 

 

package com.reject.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import net.minecraft.client.Minecraft;

public class ApiTool {

public static String apikey = "APIKEYWOULDBEHEREFROMCHATLISTENER - CHATLISTENER NOT USED DURING TESTING";

private static Scanner scanner;
private static String response;
public static List<String> stats = new ArrayList<String>();
public static String date = getDate();
public static String player;

public static String playerUUID;

public static String getUrl(String guildID) {
	String requestURL = "https://api.hypixel.net/guild?key=" + apikey + "&id=" + guildID;
	return requestURL;

}

private static String getDate() {
	DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
	return dateFormat.toString();
}

public static void getCoins() {
	String dailyCoins = parse(getUrl("52e683a80cf254781aced8fb"));
	Minecraft.getMinecraft().thePlayer.sendChatMessage(player + " has earned " + dailyCoins + " today!");
}


public static String parse(String jsonLine) {
	System.out.println("Started parsing...");
	JsonElement jelement = new JsonParser().parse(jsonLine);
	System.out.println("Created jelement, creating jobject.");
	JsonObject jobject = jelement.getAsJsonObject();
	System.out.println("Created jobject, reassigning to guild");
	jobject = jobject.getAsJsonObject("guild");
	System.out.println("Reassigned, getting members array...");
	JsonArray jarray = jobject.getAsJsonArray("members");
	System.out.println("Got members array, iterating through...");
	for (int i = 0; i < jarray.size(); i++) {
		jobject = jarray.get(i).getAsJsonObject();
		jelement = jobject.get("uuid");
		if (jelement.equals(playerUUID)) {
			jelement = jobject.get("dailyCoins-" + getDate());
			return jelement.toString();

		}

	}
	Minecraft.getMinecraft().thePlayer.sendChatMessage("Player is not in guild or does not exist!");
	return null;

}

}

 

 

When I run it I get this error:

 

[21:26:29] [Client thread/INFO]: [com.reject.commands.GuildInfoCommand:execute:26]: Executed command, assigning ApiTool.player

[21:26:29] [Client thread/INFO]: [CHAT] An unknown error occurred while attempting to perform this command

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 7

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.gson.JsonParser.parse(JsonParser.java:65)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.gson.JsonParser.parse(JsonParser.java:45)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.reject.utils.PlayerUtil.parse(PlayerUtil.java:19)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.reject.utils.PlayerUtil.getPlayerUUID(PlayerUtil.java:15)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.reject.commands.GuildInfoCommand.execute(GuildInfoCommand.java:28)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraftforge.client.ClientCommandHandler.executeCommand(ClientCommandHandler.java:72)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.gui.GuiScreen.sendChatMessage(GuiScreen.java:453)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.gui.GuiScreen.sendChatMessage(GuiScreen.java:443)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.gui.GuiChat.keyTyped(GuiChat.java:111)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.gui.GuiScreen.handleKeyboardInput(GuiScreen.java:567)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.gui.GuiScreen.handleInput(GuiScreen.java:524)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.Minecraft.runTick(Minecraft.java:1662)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1021)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.Minecraft.run(Minecraft.java:345)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.main.Main.main(SourceFile:120)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at java.lang.reflect.Method.invoke(Method.java:498)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at GradleStart.main(Unknown Source)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 7

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1386)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:531)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.gson.stream.JsonReader.peek(JsonReader.java:414)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.gson.JsonParser.parse(JsonParser.java:60)

[21:26:29] [Client thread/INFO]: [java.lang.Throwable$WrappedPrintStream:println:748]: ... 22 more

 

 

 

I know this is a lot to go through and I've probably made many stupid mistakes with the JSON. If so could anybody point out where I've gone wrong?

 

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.