Skip to content

Commit

Permalink
The real initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
WesJD committed May 8, 2016
1 parent a4698ef commit 1ee283f
Show file tree
Hide file tree
Showing 16 changed files with 904 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Auto detect text files and perform LF normalization
* text=auto

# Custom for Visual Studio
*.cs diff=csharp

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Idea
.idea
*.iml

# Maven
target
70 changes: 69 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
AnvilGUI
#AnvilGUI
Easily user anvil guis to get a user's input.

This project was made since their is no easy way to do this with the Spigot / Bukkit APIs. It requires interaction
with NMS and that is a pain in a non-private plugin where users will have lots of different versions of the server
running.

##How to use

###As a dependency

```xml
<dependencies>
<dependency>
<groupId>net.buildstatic.util</groupId>
<artifactId>anvilgui</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
...
</dependencies>

<repositories>
<repository>
<id>buildstatic-repo</id>
<name>BuildStatic Repository</name>
<url>http://serv.buildstatic.net/maven-repo</url>
</repository>
...
</repositories>
```
alternatively, you can download a jar [here](http://ci.buildstatic.net/job/AnvilGUI/).

###In your plugin

####Prompting a user for input

```java
new AnvilGUI(myPluginInstance, holder, "What is the meaning of life?", new AnvilGUI.ClickHandler() {
@Override
public String onClick(Player player, String reply) {
if (reply.equalsIgnoreCase("you")) {
player.sendMessage("You have magical powers!");
return null;
}
return "Incorrect.";
}
});
```
The AnvilGUI takes in a parameter of your plugin, the player that the GUI should open for, a prompt, and the
`Click Handler`. The first two parameters are quite obvious, and the third for example would be a question, just like
what is shown above.

###Handling their answer

```java
@Override
public String onClick(Player player, String reply) {
if (reply.equalsIgnoreCase("you")) {
player.sendMessage("You have magical powers!");
return null;
}
return "Incorrect.";
}
```
The above code is what is inside your `ClickHandler`. The parameters of the method are also obvious, the player to answered
and their reply. The method also has a return value of `String`. This string is to be used if the user is wrong, etc,
and it will show in the dialogue box in the GUI what is supplied. If you return `null`, the inventory will close.


144 changes: 144 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>net.buildstatic.util</groupId>
<artifactId>anvilgui</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.8</artifactId>
<version>1.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.8.3</artifactId>
<version>1.8.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.8.8</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.9</artifactId>
<version>1.9-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>1.8-install</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/src/libs/spigot-1.8.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.8</artifactId>
<version>1.8-R0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
<execution>
<id>1.8.3-install</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/src/libs/spigot-1.8.3.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.8.3</artifactId>
<version>1.8.3-R0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
<execution>
<id>1.8.8-install</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/src/libs/spigot-1.8.8.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.8.8</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
<execution>
<id>1.9-install</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/src/libs/spigot-1.9.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.9</artifactId>
<version>1.9-R0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>1.0-beta-6</version>
</extension>
</extensions>
</build>

<distributionManagement>
<repository>
<id>buildstatic-repo</id>
<name>BuildStatic Repository</name>
<url>sftp://serv.buildstatic.net/maven-repo</url>
</repository>
</distributionManagement>

</project>
Binary file added src/libs/spigot-1.8.3.jar
Binary file not shown.
Binary file added src/libs/spigot-1.8.8.jar
Binary file not shown.
Binary file added src/libs/spigot-1.8.jar
Binary file not shown.
Binary file added src/libs/spigot-1.9.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: AnvilGUI

135 changes: 135 additions & 0 deletions src/main/java/net.buildstatic.util.anvilgui/AnvilGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package net.buildstatic.util.anvilgui;

import net.buildstatic.util.anvilgui.version.Version;
import net.buildstatic.util.anvilgui.version.VersionWrapper;
import org.apache.commons.lang3.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;

/*
* The MIT License (MIT)
*
* Copyright (c) 2016 BuildStatic
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
public class AnvilGUI implements Listener {

private final Player holder;
private final ItemStack insert;
private final ClickHandler clickHandler;

private final VersionWrapper wrapper;
private final int containerId;
private final Inventory inventory;

private boolean open = false;

public AnvilGUI(Plugin plugin, Player holder, String insert, ClickHandler clickHandler) {
this.holder = holder;
this.clickHandler = clickHandler;

final ItemStack paper = new ItemStack(Material.PAPER);
final ItemMeta paperMeta = paper.getItemMeta();
paperMeta.setDisplayName(insert);
paper.setItemMeta(paperMeta);
this.insert = paper;

final Version version = Version.of(Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]);
Validate.notNull(version, "Your server version isn't supported in AnvilGUI!");
wrapper = version.getWrapper();

wrapper.handleInventoryCloseEvent(holder);
wrapper.setActiveContainerDefault(holder);

Bukkit.getPluginManager().registerEvents(this, plugin);

final Object container = wrapper.newContainerAnvil(holder);

inventory = wrapper.toBukkitInventory(container);
inventory.setItem(Slot.INPUT_LEFT, this.insert);

containerId = wrapper.getNextContainerId(holder);
wrapper.sendPacketOpenWindow(holder, containerId);
wrapper.setActiveContainer(holder, container);
wrapper.setActiveContainerId(container, containerId);
wrapper.addActiveContainerSlotListener(container, holder);

open = true;
}

public void closeInventory() {
Validate.isTrue(open, "You can't close an inventory that isn't open!");
open = false;

wrapper.handleInventoryCloseEvent(holder);
wrapper.setActiveContainerDefault(holder);
wrapper.sendPacketCloseWindow(holder, containerId);

HandlerList.unregisterAll(this);
}

@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
if(e.getInventory().equals(inventory)) {
e.setCancelled(true);
final Player clicker = (Player) e.getWhoClicked();
if(e.getRawSlot() == Slot.OUTPUT) {
final ItemStack clicked = inventory.getItem(e.getRawSlot());
final String ret = clickHandler.onClick(clicker, clicked.hasItemMeta() ? clicked.getItemMeta().getDisplayName() : clicked.getType().toString());
if(ret != null) {
final ItemMeta meta = clicked.getItemMeta();
meta.setDisplayName(ret);
clicked.setItemMeta(meta);
inventory.setItem(e.getRawSlot(), clicked);
} else closeInventory();
}
}
}

@EventHandler
public void onInventoryClose(InventoryCloseEvent e) {
if(open && e.getInventory().equals(inventory)) closeInventory();
}

public static abstract class ClickHandler {

public abstract String onClick(Player clicker, String input);

}

public static class Slot {

public static final int INPUT_LEFT = 0;
public static final int INPUT_RIGHT = 1;
public static final int OUTPUT = 2;

}

}
Loading

0 comments on commit 1ee283f

Please sign in to comment.