Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added /wiki chat command. #1024

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/main/java/com/dreammaster/command/WikiCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.dreammaster.command;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiConfirmOpenLink;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiYesNoCallback;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;

public class WikiCommand implements ICommand {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can extend CommandBase instead of ICommand and remove some of the methods you implemented that are the same as the vanilla command base


private List aliases;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need to allocate a field you can just do

    @Override
    public String getCommandName() {
        return Collections.singletonList("wiki");
    }


public WikiCommand() {
aliases = new ArrayList();
aliases.add("wiki");
}

@Override
public int compareTo(Object arg0) {
return 0;
}

@Override
public String getCommandName() {
return "wiki";
}

@Override
public String getCommandUsage(ICommandSender p_71518_1_) {
return "/wiki [<topic>|hand|block]";
}

@Override
public List getCommandAliases() {
return aliases;
}

// Main page, if no arguments are provided.
private static final URI URIMainPage;

static {
URI uri = null;
try {
uri = new URI("https://gtnh.miraheze.org/wiki/Main_Page");
} catch (URISyntaxException ignored) {}
URIMainPage = uri;
}

// Substring "@@@" to be replaced by search keyword(s).
private static final String URISearchPattern = "https://gtnh.miraheze.org/wiki/Special:Search?search=@@@&go=Search";

@Override
public void processCommand(ICommandSender pCmdSender, String[] pArgs) {
if (!(pCmdSender instanceof EntityPlayerMP)) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need that line once you register on the client command handler


if (pArgs.length == 0) {
openURIRespectingSettings(URIMainPage);
} else {
URI uri;
try {
uri = new URI(URISearchPattern.replace("@@@", String.join("%20", pArgs)));
} catch (URISyntaxException ignore) {
return;
}
openURIRespectingSettings(uri);
}
}

private void openURIRespectingSettings(URI uri) {
if (uri == null) return;
if (Minecraft.getMinecraft().gameSettings.chatLinksPrompt) {
// Schedule task for the next tick. Otherwise, processing the command closes the window instantly.
Minecraft.getMinecraft().func_152344_a(() -> openURIConfirmationScreen(uri));
} else {
openURI(uri);
}
}

private void openURIConfirmationScreen(URI uri) {
GuiScreen oldScreen = Minecraft.getMinecraft().currentScreen;
Minecraft.getMinecraft().displayGuiScreen(new GuiConfirmOpenLink(new GuiYesNoCallback() {

@Override
public void confirmClicked(boolean result, int id) {
if (result) openURI(uri);
Minecraft.getMinecraft().displayGuiScreen(oldScreen);
}
}, uri.toString(), 0, false));
}

private void openURI(URI uri) {
try {
Class<?> oclass = Class.forName("java.awt.Desktop");
Object object = oclass.getMethod("getDesktop").invoke(null);
oclass.getMethod("browse", URI.class).invoke(object, uri);
} catch (Throwable ignore) {}
}

@Override
public boolean canCommandSenderUseCommand(ICommandSender pCommandSender) {
return true;
}

@Override
public List addTabCompletionOptions(ICommandSender p_71516_1_, String[] p_71516_2_) {
return null;
}

@Override
public boolean isUsernameIndex(String[] p_82358_1_, int p_82358_2_) {
return false;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/dreammaster/main/MainRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.dreammaster.command.CustomToolTipsCommand;
import com.dreammaster.command.HazardousItemsCommand;
import com.dreammaster.command.ItemInHandInfoCommand;
import com.dreammaster.command.WikiCommand;
import com.dreammaster.config.CoreModConfig;
import com.dreammaster.creativetab.ModTabList;
import com.dreammaster.detrav.ScannerTools;
Expand Down Expand Up @@ -559,5 +560,6 @@ public void serverLoad(FMLServerStartingEvent pEvent) {
if (YAMCore.isDebug()) {
pEvent.registerServerCommand(new AllPurposeDebugCommand());
}
pEvent.registerServerCommand(new WikiCommand());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to make it a client command, not a server command otherwise it will crash
register your command in the init block of the client proxy using ClientCommandHandler.instance.registerCommand();

}
}
Loading