This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9d03fb8
Showing
11 changed files
with
1,102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<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>ro.nicuch</groupId> | ||
<artifactId>CitizensBooks</artifactId> | ||
<version>2.3-SNAPSHOT</version> | ||
<repositories> | ||
<repository> | ||
<id>vault-repo</id> | ||
<url>http://nexus.hc.to/content/repositories/pub_releases</url> | ||
</repository> | ||
<repository> | ||
<id>placeholderapi</id> | ||
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url> | ||
</repository> | ||
<repository> | ||
<id>citizensapi</id> | ||
<url>http://repo.citizensnpcs.co/</url> | ||
</repository> | ||
</repositories> | ||
<dependencies> | ||
<dependency> | ||
<groupId>net.citizensnpcs</groupId> | ||
<artifactId>citizensapi</artifactId> | ||
<version>2.0.22-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.milkbowl.vault</groupId> | ||
<artifactId>VaultAPI</artifactId> | ||
<version>1.6</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>me.clip</groupId> | ||
<artifactId>placeholderapi</artifactId> | ||
<version>2.8.4</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.spigotmc</groupId> | ||
<artifactId>spigot-api</artifactId> | ||
<version>1.12.2-R0.1-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.bukkit</groupId> | ||
<artifactId>craftbukkit</artifactId> | ||
<version>1.12.2-R0.1-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.7.0</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.1.0</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-resources-plugin</artifactId> | ||
<version>3.0.2</version> | ||
<configuration> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
62 changes: 62 additions & 0 deletions
62
src/main/java/ro/nicuch/citizensbooks/BookNPCRightClickEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package ro.nicuch.citizensbooks; | ||
|
||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.Cancellable; | ||
import org.bukkit.event.HandlerList; | ||
import org.bukkit.event.player.PlayerEvent; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
import net.citizensnpcs.api.npc.NPC; | ||
|
||
public class BookNPCRightClickEvent extends PlayerEvent implements Cancellable { | ||
private final static HandlerList handlers = new HandlerList(); | ||
private final NPC npc; | ||
private ItemStack book; | ||
private boolean usePlaceHolders = true; | ||
private boolean cancel; | ||
|
||
public BookNPCRightClickEvent(Player player, NPC npc, ItemStack book) { | ||
super(player); | ||
this.npc = npc; | ||
this.book = book; | ||
} | ||
|
||
public NPC getNPC() { | ||
return this.npc; | ||
} | ||
|
||
public ItemStack getBook() { | ||
return this.book; | ||
} | ||
|
||
public boolean usePlaceHolders() { | ||
return this.usePlaceHolders; | ||
} | ||
|
||
public void setPlaceHoldersUse(boolean usePlaceHolders) { | ||
this.usePlaceHolders = usePlaceHolders; | ||
} | ||
|
||
public void setBook(ItemStack book) { | ||
this.book = book; | ||
} | ||
|
||
@Override | ||
public boolean isCancelled() { | ||
return cancel; | ||
} | ||
|
||
@Override | ||
public void setCancelled(boolean cancel) { | ||
this.cancel = cancel; | ||
} | ||
|
||
@Override | ||
public HandlerList getHandlers() { | ||
return handlers; | ||
} | ||
|
||
public static HandlerList getHandlerList() { | ||
return handlers; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/ro/nicuch/citizensbooks/CitizensActions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ro.nicuch.citizensbooks; | ||
|
||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
import net.citizensnpcs.api.event.NPCRightClickEvent; | ||
|
||
public class CitizensActions implements Listener { | ||
private final CitizensBooks plugin; | ||
private final CitizensBooksAPI api; | ||
|
||
public CitizensActions(CitizensBooks plugin) { | ||
api = (this.plugin = plugin).getAPI(); | ||
} | ||
|
||
@EventHandler | ||
public void event(NPCRightClickEvent event) { | ||
int npcId = event.getNPC().getId(); | ||
if (!this.plugin.getConfig().isItemStack("save." + npcId)) | ||
return; | ||
ItemStack book = this.plugin.getConfig().getItemStack("save." + npcId); | ||
if (book == null) | ||
return; | ||
BookNPCRightClickEvent e = new BookNPCRightClickEvent(event.getClicker(), event.getNPC(), book); | ||
this.plugin.getServer().getPluginManager().callEvent(e); | ||
if (e.isCancelled()) | ||
return; | ||
book = e.getBook(); | ||
if (book == null) | ||
return; | ||
if (e.usePlaceHolders()) | ||
this.api.openBook(event.getClicker(), this.api.placeholderHook(event.getClicker(), book)); | ||
else | ||
this.api.openBook(event.getClicker(), book); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package ro.nicuch.citizensbooks; | ||
|
||
import java.io.File; | ||
|
||
import org.bukkit.ChatColor; | ||
import org.bukkit.command.TabExecutor; | ||
import org.bukkit.plugin.PluginManager; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import net.milkbowl.vault.permission.Permission; | ||
|
||
public class CitizensBooks extends JavaPlugin { | ||
private Permission PERMISSION; | ||
private boolean placeholder; | ||
private CitizensBooksAPI api; | ||
|
||
@Override | ||
public void onEnable() { | ||
this.reloadSettings(); | ||
PluginManager manager = this.getServer().getPluginManager(); | ||
if (!manager.isPluginEnabled("Vault")) { | ||
this.getLogger().warning(ChatColor.RED + "Vault not enabled, plugin disabled!"); | ||
this.setEnabled(false); | ||
return; | ||
} | ||
this.api = new CitizensBooksAPI(this); | ||
if (!manager.isPluginEnabled("PlaceholderAPI")) { | ||
this.getLogger().info(ChatColor.BLUE + "PlaceholderAPI not found!"); | ||
} else { | ||
this.getLogger().info(ChatColor.GREEN + "PlaceholderAPI found, try hooking!"); | ||
this.placeholder = true; | ||
} | ||
this.PERMISSION = this.getServer().getServicesManager().getRegistration(Permission.class).getProvider(); | ||
TabExecutor te = null; | ||
manager.registerEvents(new PlayerActions(this), this); | ||
if (!manager.isPluginEnabled("Citizens")) { | ||
this.getLogger().info(ChatColor.BLUE + "Citizens not found!"); | ||
te = new PlayerCommands(this); | ||
} else { | ||
this.getLogger().info(ChatColor.GREEN + "Citizens found, try hooking!"); | ||
manager.registerEvents(new CitizensActions(this), this); | ||
te = new CitizensCommands(this); | ||
} | ||
this.getCommand("npcbook").setExecutor(te); | ||
this.getCommand("npcbook").setTabCompleter(te); | ||
} | ||
|
||
public CitizensBooksAPI getAPI() { | ||
return this.api; | ||
} | ||
|
||
@Override | ||
public void onDisable() { | ||
this.saveSettings(); | ||
} | ||
|
||
public void reloadSettings() { | ||
File config = new File(this.getDataFolder() + File.separator + "config.yml"); | ||
if (!config.exists()) { | ||
this.getConfig().options().copyDefaults(true); | ||
this.saveDefaultConfig(); | ||
} | ||
if (this.getConfig().isInt("version") && this.getConfig().getInt("version") != 4) { | ||
File copy = new File( | ||
this.getDataFolder() + File.separator + "config_" + System.currentTimeMillis() + ".yml"); | ||
config.renameTo(copy); | ||
this.getConfig().options().copyDefaults(true); | ||
this.saveDefaultConfig(); | ||
} | ||
} | ||
|
||
public void saveSettings() { | ||
this.saveConfig(); | ||
} | ||
|
||
public Permission getPermission() { | ||
return this.PERMISSION; | ||
} | ||
|
||
public boolean isPlaceHolderEnabled() { | ||
return this.placeholder; | ||
} | ||
|
||
public String getMessage(String path, String def) { | ||
return ChatColor.translateAlternateColorCodes('&', | ||
this.getConfig().getString("lang.header", LangDefaults.header)) | ||
+ ChatColor.translateAlternateColorCodes('&', this.getConfig().getString(path, def)); | ||
} | ||
|
||
public String getMessageNoHeader(String path, String def) { | ||
return ChatColor.translateAlternateColorCodes('&', this.getConfig().getString(path, def)); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/ro/nicuch/citizensbooks/CitizensBooksAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package ro.nicuch.citizensbooks; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
import org.bukkit.ChatColor; | ||
import org.bukkit.Material; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.PlayerInventory; | ||
import org.bukkit.inventory.meta.BookMeta; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import me.clip.placeholderapi.PlaceholderAPI; | ||
|
||
public class CitizensBooksAPI { | ||
private final CitizensBooks plugin; | ||
|
||
public CitizensBooksAPI(CitizensBooks plugin) { | ||
this.plugin = plugin; | ||
String version = null; | ||
try { | ||
version = plugin.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
this.plugin.getLogger().warning(ChatColor.RED + "Your server is running an unknown version!"); | ||
return; | ||
} | ||
this.plugin.getLogger().info(ChatColor.GREEN + "Your server is running version " + version + "!"); | ||
} | ||
|
||
public ItemStack getFilter(String filterName) { | ||
return this.plugin.getConfig().getItemStack("filters." + filterName, new ItemStack(Material.WRITTEN_BOOK)); | ||
} | ||
|
||
public boolean hasFilter(String filterName) { | ||
return this.plugin.getConfig().isItemStack("filters." + filterName); | ||
} | ||
|
||
public void createFilter(String filterName, ItemStack book) { | ||
this.plugin.getConfig().set("filters." + filterName, book); | ||
this.plugin.saveSettings(); | ||
} | ||
|
||
public void removeFilter(String filterName) { | ||
this.plugin.getConfig().set("filters." + filterName, null); | ||
this.plugin.saveSettings(); | ||
} | ||
|
||
// Author: Skionz | ||
private Class<?> getNMSClass(String nmsClassString) throws ClassNotFoundException { | ||
String version = plugin.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + "."; | ||
String name = "net.minecraft.server." + version + nmsClassString; | ||
Class<?> nmsClass = Class.forName(name); | ||
return nmsClass; | ||
} | ||
|
||
// Author: Skionz | ||
private Object getConnection(Player player) throws SecurityException, NoSuchMethodException, NoSuchFieldException, | ||
IllegalArgumentException, IllegalAccessException, InvocationTargetException { | ||
Method getHandle = player.getClass().getMethod("getHandle"); | ||
Object nmsPlayer = getHandle.invoke(player); | ||
Field conField = nmsPlayer.getClass().getField("playerConnection"); | ||
Object con = conField.get(nmsPlayer); | ||
return con; | ||
} | ||
|
||
protected void rightClick(Player player) { | ||
try { | ||
Class<?> pds = this.getNMSClass("PacketDataSerializer"); | ||
Constructor<?> pdsc = pds.getConstructor(ByteBuf.class); | ||
Class<?> ppocp = this.getNMSClass("PacketPlayOutCustomPayload"); | ||
Constructor<?> ppocpc = ppocp.getConstructor(String.class, pds); | ||
ByteBuf buf = Unpooled.buffer(256); | ||
buf.setByte(0, (byte) 0); | ||
buf.writerIndex(1); | ||
Object packet = ppocpc.newInstance("MC|BOpen", pdsc.newInstance(buf)); | ||
Method sendPacket = getNMSClass("PlayerConnection").getMethod("sendPacket", this.getNMSClass("Packet")); | ||
sendPacket.invoke(this.getConnection(player), packet); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void openBook(Player player, ItemStack book) { | ||
int slot = player.getInventory().getHeldItemSlot(); | ||
ItemStack old = player.getInventory().getItem(slot); | ||
PlayerInventory pi = player.getInventory(); | ||
pi.setItem(slot, book); | ||
this.rightClick(player); | ||
pi.setItem(slot, old); | ||
} | ||
|
||
protected ItemStack placeholderHook(Player player, ItemStack item) { | ||
if (!this.plugin.isPlaceHolderEnabled()) | ||
return item; | ||
if (!(item.getItemMeta() instanceof BookMeta)) | ||
return item; | ||
BookMeta meta = (BookMeta) item.getItemMeta(); | ||
meta.setPages(PlaceholderAPI.setPlaceholders(player, meta.getPages())); | ||
item.setItemMeta(meta); | ||
return item; | ||
} | ||
} |
Oops, something went wrong.