Added support for name coloring by town (using Towny).

Write the town name to autoflairconfig.txt followed by the color codes.

Example:
test §2§5 will give me §2Norbi§r§5Peti§r

Also made some bugfixes.
This commit is contained in:
Norbi Peti 2015-07-20 18:23:02 +02:00
parent 760c238f16
commit a8af0c0ee6
382 changed files with 101496 additions and 27 deletions

View file

@ -42,3 +42,237 @@ user global configuration and to define the default location to store repositori
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!SESSION 2015-07-20 10:22:45.311 -----------------------------------------------
eclipse.buildId=M20130204-1200
java.version=1.8.0_45
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=hu_HU
Framework arguments: -product org.eclipse.epp.package.jee.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product
!ENTRY org.eclipse.egit.ui 2 0 2015-07-20 10:23:24.280
!MESSAGE Warning: EGit couldn't detect the installation path "gitPrefix" of native Git. Hence EGit can't respect system level
Git settings which might be configured in ${gitPrefix}/etc/gitconfig under the native Git installation directory.
The most important of these settings is core.autocrlf. Git for Windows by default sets this parameter to true in
this system level configuration. The Git installation location can be configured on the
Team > Git > Configuration preference page's 'System Settings' tab.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.egit.ui 2 0 2015-07-20 10:23:24.288
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Norbi'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.ui 4 4 2015-07-20 11:50:38.584
!MESSAGE Save Failed
!ENTRY org.eclipse.ui 2 0 2015-07-20 11:50:38.607
!MESSAGE Save Failed
!STACK 0
org.eclipse.core.runtime.AssertionFailedException: assertion failed:
at org.eclipse.core.runtime.Assert.isTrue(Assert.java:110)
at org.eclipse.core.runtime.Assert.isTrue(Assert.java:96)
at org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel.updateMarkers(AbstractMarkerAnnotationModel.java:550)
at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider.commitWorkingCopy(CompilationUnitDocumentProvider.java:1401)
at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider$4.execute(CompilationUnitDocumentProvider.java:1449)
at org.eclipse.ui.editors.text.TextFileDocumentProvider$DocumentProviderOperation.run(TextFileDocumentProvider.java:132)
at org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation.execute(WorkspaceModifyDelegatingOperation.java:69)
at org.eclipse.ui.actions.WorkspaceModifyOperation$1.run(WorkspaceModifyOperation.java:106)
at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2344)
at org.eclipse.ui.actions.WorkspaceModifyOperation.run(WorkspaceModifyOperation.java:118)
at org.eclipse.ui.internal.editors.text.WorkspaceOperationRunner.run(WorkspaceOperationRunner.java:75)
at org.eclipse.ui.internal.editors.text.WorkspaceOperationRunner.run(WorkspaceOperationRunner.java:65)
at org.eclipse.ui.editors.text.TextFileDocumentProvider.executeOperation(TextFileDocumentProvider.java:456)
at org.eclipse.ui.editors.text.TextFileDocumentProvider.saveDocument(TextFileDocumentProvider.java:772)
at org.eclipse.ui.texteditor.AbstractTextEditor.performSave(AbstractTextEditor.java:5066)
at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor.performSave(CompilationUnitEditor.java:1247)
at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor.doSave(CompilationUnitEditor.java:1301)
at org.eclipse.ui.texteditor.AbstractTextEditor$TextEditorSavable.doSave(AbstractTextEditor.java:7198)
at org.eclipse.ui.Saveable.doSave(Saveable.java:214)
at org.eclipse.ui.internal.SaveableHelper.doSaveModel(SaveableHelper.java:346)
at org.eclipse.ui.internal.SaveableHelper$3.run(SaveableHelper.java:193)
at org.eclipse.ui.internal.SaveableHelper$5.run(SaveableHelper.java:274)
at org.eclipse.jface.operation.ModalContext.runInCurrentThread(ModalContext.java:464)
at org.eclipse.jface.operation.ModalContext.run(ModalContext.java:372)
at org.eclipse.ui.internal.WorkbenchWindow$13.run(WorkbenchWindow.java:1679)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.internal.WorkbenchWindow.run(WorkbenchWindow.java:1676)
at org.eclipse.ui.internal.SaveableHelper.runProgressMonitorOperation(SaveableHelper.java:282)
at org.eclipse.ui.internal.SaveableHelper.runProgressMonitorOperation(SaveableHelper.java:261)
at org.eclipse.ui.internal.SaveableHelper.saveModels(SaveableHelper.java:204)
at org.eclipse.ui.internal.SaveableHelper.savePart(SaveableHelper.java:144)
at org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.doSave(CompatibilityPart.java:417)
at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:231)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:237)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:201)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:89)
at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.savePart(PartServiceImpl.java:1223)
at org.eclipse.ui.internal.WorkbenchPage.saveSaveable(WorkbenchPage.java:3437)
at org.eclipse.ui.internal.WorkbenchPage.saveEditor(WorkbenchPage.java:3455)
at org.eclipse.ui.internal.SaveAction.run(SaveAction.java:76)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
at org.eclipse.jface.commands.ActionHandler.execute(ActionHandler.java:119)
at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:76)
at sun.reflect.GeneratedMethodAccessor36.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:231)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:212)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:131)
at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:171)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.executeCommand(KeyBindingDispatcher.java:277)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.press(KeyBindingDispatcher.java:496)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.processKeyEvent(KeyBindingDispatcher.java:547)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.filterKeySequenceBindings(KeyBindingDispatcher.java:368)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.access$0(KeyBindingDispatcher.java:314)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher$KeyDownFilter.handleEvent(KeyBindingDispatcher.java:83)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.filterEvent(Display.java:1262)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1052)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1104)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1100)
at org.eclipse.swt.widgets.Widget.wmChar(Widget.java:1521)
at org.eclipse.swt.widgets.Control.WM_CHAR(Control.java:4640)
at org.eclipse.swt.widgets.Canvas.WM_CHAR(Canvas.java:345)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4528)
at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:341)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4976)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2546)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3756)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
!ENTRY org.eclipse.ui 4 4 2015-07-20 18:17:08.584
!MESSAGE Save Failed
!ENTRY org.eclipse.ui 2 0 2015-07-20 18:17:08.600
!MESSAGE Save Failed
!STACK 0
org.eclipse.core.runtime.AssertionFailedException: assertion failed:
at org.eclipse.core.runtime.Assert.isTrue(Assert.java:110)
at org.eclipse.core.runtime.Assert.isTrue(Assert.java:96)
at org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel.updateMarkers(AbstractMarkerAnnotationModel.java:550)
at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider.commitWorkingCopy(CompilationUnitDocumentProvider.java:1401)
at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider$4.execute(CompilationUnitDocumentProvider.java:1449)
at org.eclipse.ui.editors.text.TextFileDocumentProvider$DocumentProviderOperation.run(TextFileDocumentProvider.java:132)
at org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation.execute(WorkspaceModifyDelegatingOperation.java:69)
at org.eclipse.ui.actions.WorkspaceModifyOperation$1.run(WorkspaceModifyOperation.java:106)
at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2344)
at org.eclipse.ui.actions.WorkspaceModifyOperation.run(WorkspaceModifyOperation.java:118)
at org.eclipse.ui.internal.editors.text.WorkspaceOperationRunner.run(WorkspaceOperationRunner.java:75)
at org.eclipse.ui.internal.editors.text.WorkspaceOperationRunner.run(WorkspaceOperationRunner.java:65)
at org.eclipse.ui.editors.text.TextFileDocumentProvider.executeOperation(TextFileDocumentProvider.java:456)
at org.eclipse.ui.editors.text.TextFileDocumentProvider.saveDocument(TextFileDocumentProvider.java:772)
at org.eclipse.ui.texteditor.AbstractTextEditor.performSave(AbstractTextEditor.java:5066)
at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor.performSave(CompilationUnitEditor.java:1247)
at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor.doSave(CompilationUnitEditor.java:1301)
at org.eclipse.ui.texteditor.AbstractTextEditor$TextEditorSavable.doSave(AbstractTextEditor.java:7198)
at org.eclipse.ui.Saveable.doSave(Saveable.java:214)
at org.eclipse.ui.internal.SaveableHelper.doSaveModel(SaveableHelper.java:346)
at org.eclipse.ui.internal.SaveableHelper$3.run(SaveableHelper.java:193)
at org.eclipse.ui.internal.SaveableHelper$5.run(SaveableHelper.java:274)
at org.eclipse.jface.operation.ModalContext.runInCurrentThread(ModalContext.java:464)
at org.eclipse.jface.operation.ModalContext.run(ModalContext.java:372)
at org.eclipse.ui.internal.WorkbenchWindow$13.run(WorkbenchWindow.java:1679)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.internal.WorkbenchWindow.run(WorkbenchWindow.java:1676)
at org.eclipse.ui.internal.SaveableHelper.runProgressMonitorOperation(SaveableHelper.java:282)
at org.eclipse.ui.internal.SaveableHelper.runProgressMonitorOperation(SaveableHelper.java:261)
at org.eclipse.ui.internal.SaveableHelper.saveModels(SaveableHelper.java:204)
at org.eclipse.ui.internal.SaveableHelper.savePart(SaveableHelper.java:144)
at org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.doSave(CompatibilityPart.java:417)
at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:231)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:237)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:201)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:89)
at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.savePart(PartServiceImpl.java:1223)
at org.eclipse.ui.internal.WorkbenchPage.saveSaveable(WorkbenchPage.java:3437)
at org.eclipse.ui.internal.WorkbenchPage.saveEditor(WorkbenchPage.java:3455)
at org.eclipse.ui.internal.SaveAction.run(SaveAction.java:76)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
at org.eclipse.jface.commands.ActionHandler.execute(ActionHandler.java:119)
at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:76)
at sun.reflect.GeneratedMethodAccessor36.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:231)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:212)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:131)
at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:171)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.executeCommand(KeyBindingDispatcher.java:277)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.press(KeyBindingDispatcher.java:496)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.processKeyEvent(KeyBindingDispatcher.java:547)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.filterKeySequenceBindings(KeyBindingDispatcher.java:368)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.access$0(KeyBindingDispatcher.java:314)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher$KeyDownFilter.handleEvent(KeyBindingDispatcher.java:83)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.filterEvent(Display.java:1262)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1052)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1104)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1100)
at org.eclipse.swt.widgets.Widget.wmChar(Widget.java:1521)
at org.eclipse.swt.widgets.Control.WM_CHAR(Control.java:4640)
at org.eclipse.swt.widgets.Canvas.WM_CHAR(Canvas.java:345)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4528)
at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:341)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4976)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2546)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3756)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,343 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i));
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,307 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String[] line=br.readLine().split(" ");
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,383 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length()-1;
System.out.println("pos: "+pos+" nextpos: "+nextpos);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos=i+unit;
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,312 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PlayerTowns.put(s[0], s[1])
}
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,371 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+unit);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,382 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length()-1;
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos=i+unit;
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,319 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(String name)
{
}
}

View file

@ -0,0 +1,382 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length()-1;
String substr="§"+colors[x++]+displayname.substring(pos-1, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos=i+unit;
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,92 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload":
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,64 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,337 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,336 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,294 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForClass(flairclass);
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -0,0 +1,292 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -0,0 +1,333 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.towny.object.TownyWorld;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (NotRegisteredException e) {
return "";
}
}
}

View file

@ -0,0 +1,343 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
return i;
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,354 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr=displayname.substring(pos, nextpos-pos);
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,382 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length()-1;
String substr="§"+colors[x++]+displayname.substring(pos-1, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos=i+unit;
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,307 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String line=br.readLine();
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,292 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//player.setDisplayName(player.getDisplayName()+flair);
}
}

View file

@ -0,0 +1,384 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length()-1;
System.out.println("pos: "+pos+" nextpos: "+nextpos);
System.out.println("nextpos-pos: "+nextpos-pos);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
//int nextpos=i+unit;
String substr="§"+colors[x++]+displayname.substring(pos, unit)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,361 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,341 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
int x=0>
while((x=displayname.indexOf("§", x)))
Positions.add(x)
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,343 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=displayname.length()-1; i>=0; i--) {
if(Character.isUpperCase(str.charAt(i))) {
return i;
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,85 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload":
if(player.isOp())
{
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
}
}
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,307 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,291 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
player.setDisplayName(player.getDisplayName()+flair);
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{
}
}

View file

@ -0,0 +1,94 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
player.sendMessage(")
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,391 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(!(Positions.size()>=colors.length)) //TODO
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,92 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload":
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,307 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.ini");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
br.readLine();
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,67 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload":
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,397 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,312 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PlayerTowns.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
return "";
}
}

View file

@ -0,0 +1,335 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color), dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,389 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(!(Positions.size()>=colors.length)) //TODO
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,292 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -0,0 +1,355 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr=displayname.substring(pos, nextpos-pos);
finalname+=substr;
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,370 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+unit);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,322 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.object.TownyWorld;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(String playername)
{
Towny.getTownyInstance()
}
}

View file

@ -0,0 +1,294 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForClass(flairclass);
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -0,0 +1,93 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,99 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
//String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
for()
player.sendMessage("§6Reloaded config file.§r");
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,294 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -0,0 +1,321 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.Towny;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(String playername)
{
}
}

View file

@ -0,0 +1,335 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,337 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,332 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.towny.object.TownyWorld;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
} catch (NotRegisteredException e) {
return "";
}
}
}

View file

@ -0,0 +1,311 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
}
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,378 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C");
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size()-1; i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+unit);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,108 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
//String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
for(Player p : PluginMain.Players)
{
if(PluginMain.PlayerFlairs.containsKey(p.getName()))
{
String flair=PluginMain.PlayerFlairs.get(p.getName());
PluginMain.RemovePlayerDisplayFlairFinal(p, flair);
PluginMain.AppendPlayerDisplayFlairFinal(p, flair);
}
}
player.sendMessage("§6Reloaded config file.§r");
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
private static void DoReload()
}

View file

@ -0,0 +1,312 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PlayerTowns.put(s[0], s[1]);
}
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,299 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
return "";
}
}

View file

@ -0,0 +1,323 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.object.TownyWorld;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(String playername)
{
new Class(1, 2);
getPlugin()).getTownyInstance()
}
}

View file

@ -0,0 +1,326 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.towny.object.TownyWorld;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(String playername)
{
List<Town> towns = TownyUniverse.getDataSource().getTowns();
}
}

View file

@ -0,0 +1,312 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PlayerTowns.put(s[0], s[1]);
}
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,327 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.towny.object.TownyWorld;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
}
}

View file

@ -0,0 +1,373 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
{
System.out.println()
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+unit);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,287 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
player.setDisplayName(player.getDisplayName()+flair);
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
}

View file

@ -0,0 +1,98 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.AppendPlayerDisplayFlairFinal(player, flair);
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
player.sendMessage("§6Reloaded config file.§r");
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,343 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,389 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(!(Positions.size()>=colors.length)) //TODO
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,334 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -0,0 +1,360 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,322 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.object.TownyWorld;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(String playername)
{
Towny
}
}

View file

@ -0,0 +1,105 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
//String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
for(Player p : PluginMain.Players)
{
if(PluginMain.PlayerFlairs.containsKey(p.getName()))
{
PluginMain.AppendPlayerDisplayFlairFinal(p, PluginMain.PlayerFlairs.get(p.getName()));
}
}
player.sendMessage("§6Reloaded config file.§r");
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,312 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
File file=new File("autoflairconfig.txt");
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PlayerTowns.put(s[0], s[1]);
}
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
try {
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,294 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForClass(flairclass);
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -0,0 +1,361 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,369 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+unit);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,292 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -0,0 +1,354 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr=displayname.substring(pos, nextpos);
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,382 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C");
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length()-1;
String substr="§"+colors[x++]+displayname.substring(pos-1, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+unit);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,106 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
//String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
for(Player p : PluginMain.Players)
{
if(PluginMain.PlayerFlairs.containsKey(p.getName()))
{
PluginMain.RemovePlayerDisplayFlairFinal(p, flair)
PluginMain.AppendPlayerDisplayFlairFinal(p, PluginMain.PlayerFlairs.get(p.getName()));
}
}
player.sendMessage("§6Reloaded config file.§r");
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,374 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C");
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+unit);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,342 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();for(int i=str.length()-1; i>=0; i--) {
if(Character.isUpperCase(str.charAt(i))) {
return i;
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,94 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
player.sendMessage("")
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,368 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,336 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
color.split("§");
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,323 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.object.TownyWorld;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(String playername)
{
new Class<int, 1>();
getPlugin()).getTownyInstance()
}
}

View file

@ -0,0 +1,385 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
//int nextpos=i+unit;
String substr="§"+colors[x++]+displayname.substring(pos, unit)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,314 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
}

View file

@ -0,0 +1,332 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.towny.object.TownyWorld;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
} catch (NotRegisteredException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,343 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=str.length()-1; i>=0; i--) {
if(Character.isUpperCase(str.charAt(i))) {
return i;
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,353 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,292 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair);
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
}
}

View file

@ -0,0 +1,343 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,101 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
//String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
for(Player p : PluginMain.Players)
if(PluginMain.PlayerFlairs.containsKey(p.getName()))
PluginMain.PlayerFlairs
player.sendMessage("§6Reloaded config file.§r");
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,307 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.ini");
try {
BufferedReader bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,319 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(String playername)
{
}
}

View file

@ -0,0 +1,307 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.ini");
try {
BufferedReader bw=new BufferedReader(new FileReader(file);
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,393 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
System.out.println("Sleeping for 5 seconds...");
Thread.currentThread().sleep(5000);
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,397 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,392 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
Thread.currentThread().sleep(5000);
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,312 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PlayerTowns.put(s[0], s[1]);
}
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.txt");
try {
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,338 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,348 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

Some files were not shown because too many files have changed in this diff Show more