-
Notifications
You must be signed in to change notification settings - Fork 150
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
base: master
Are you sure you want to change the base?
Changes from all commits
82906f5
f4af91a
55d6ecd
4a0063e
88f6a36
b61d9b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
|
||
private List aliases; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't need to allocate a field you can just do
|
||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -559,5 +560,6 @@ public void serverLoad(FMLServerStartingEvent pEvent) { | |
if (YAMCore.isDebug()) { | ||
pEvent.registerServerCommand(new AllPurposeDebugCommand()); | ||
} | ||
pEvent.registerServerCommand(new WikiCommand()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
} |
There was a problem hiding this comment.
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