Skip to content

Commit

Permalink
Update to Latest Version of Placeholder API, Added books display
Browse files Browse the repository at this point in the history
  • Loading branch information
LOOHP committed Sep 14, 2020
1 parent 18e1fcd commit acf43a8
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 27 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>InteractiveChat</groupId>
<artifactId>InteractiveChat</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
Expand Down Expand Up @@ -87,7 +87,7 @@
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.6</version>
<version>2.10.9</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
6 changes: 3 additions & 3 deletions src/com/loohp/interactivechat/InteractiveChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ public class InteractiveChat extends JavaPlugin {

public static ConcurrentHashMap<Long, HashSet<String>> cooldownbypass = new ConcurrentHashMap<Long, HashSet<String>>();

public static ConcurrentHashMap<Long, Inventory> itemDisplay = new ConcurrentHashMap<Long, Inventory>();
public static ConcurrentHashMap<Long, Inventory> inventoryDisplay = new ConcurrentHashMap<Long, Inventory>();
public static ConcurrentHashMap<Long, Inventory> enderDisplay = new ConcurrentHashMap<Long, Inventory>();
public static BiMap<Long, Inventory> itemDisplay = Maps.synchronizedBiMap(HashBiMap.create());
public static BiMap<Long, Inventory> inventoryDisplay = Maps.synchronizedBiMap(HashBiMap.create());
public static BiMap<Long, Inventory> enderDisplay = Maps.synchronizedBiMap(HashBiMap.create());

public static ConcurrentHashMap<Player, ConcurrentHashMap<String, Long>> placeholderCooldowns = new ConcurrentHashMap<Player, ConcurrentHashMap<String, Long>>();
public static ConcurrentHashMap<Player, Long> universalCooldowns = new ConcurrentHashMap<Player, Long>();
Expand Down
50 changes: 37 additions & 13 deletions src/com/loohp/interactivechat/Listeners/Events.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.loohp.interactivechat.Listeners;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -17,6 +20,8 @@
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;

import com.loohp.interactivechat.InteractiveChat;
import com.loohp.interactivechat.ObjectHolders.CommandPlaceholderInfo;
Expand All @@ -29,6 +34,7 @@
import com.loohp.interactivechat.Utils.MessageUtils;

import me.clip.placeholderapi.PlaceholderAPI;
import net.md_5.bungee.api.chat.BaseComponent;

public class Events implements Listener {

Expand Down Expand Up @@ -253,7 +259,7 @@ private void translateAltColorCode(PlayerCommandPreprocessEvent event) {
}

@EventHandler(priority=EventPriority.LOWEST)
public void onInventoryClickLowest(InventoryClickEvent event) {
public void onInventoryClick(InventoryClickEvent event) {
if (event.getClickedInventory() == null) {
return;
}
Expand All @@ -263,22 +269,40 @@ public void onInventoryClickLowest(InventoryClickEvent event) {
if (event.getView().getTopInventory() == null) {
return;
}
if (InteractiveChat.inventoryDisplay.containsValue(event.getView().getTopInventory())) {
boolean block = false;
if (!block && InteractiveChat.inventoryDisplay.inverse().containsKey(event.getView().getTopInventory())) {
event.setCancelled(true);
return;
block = true;
}
if (InteractiveChat.enderDisplay.containsValue(event.getView().getTopInventory())) {
if (!block && InteractiveChat.enderDisplay.inverse().containsKey(event.getView().getTopInventory())) {
event.setCancelled(true);
return;
block = true;
}
if (InteractiveChat.itemDisplay.containsValue(event.getView().getTopInventory())) {
if (!block && InteractiveChat.itemDisplay.inverse().containsKey(event.getView().getTopInventory())) {
event.setCancelled(true);
return;
block = true;
}
if (block && event.getRawSlot() < event.getView().getTopInventory().getSize()) {
if (event.getCurrentItem() != null) {
if (event.getCurrentItem().getType().equals(Material.WRITTEN_BOOK)) {
((Player) event.getWhoClicked()).openBook(event.getCurrentItem().clone());
} else if (event.getCurrentItem().getType().equals(Material.WRITABLE_BOOK)) {
ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
BookMeta ori = (BookMeta) event.getCurrentItem().getItemMeta();
BookMeta dis = (BookMeta) book.getItemMeta();
List<BaseComponent[]> pages = new ArrayList<>(ori.spigot().getPages());
dis.spigot().setPages(pages);
dis.setTitle("Temp Book");
dis.setAuthor("InteractiveChat");
book.setItemMeta(dis);
((Player) event.getWhoClicked()).openBook(book);
}
}
}
}

/*
@EventHandler(priority=EventPriority.HIGHEST)
public void onInventoryClick(InventoryClickEvent event) {
public void onInventoryClickHighest(InventoryClickEvent event) {
if (event.getClickedInventory() == null) {
return;
}
Expand All @@ -288,18 +312,18 @@ public void onInventoryClick(InventoryClickEvent event) {
if (event.getView().getTopInventory() == null) {
return;
}
if (InteractiveChat.inventoryDisplay.containsValue(event.getView().getTopInventory())) {
if (InteractiveChat.inventoryDisplay.inverse().containsKey(event.getView().getTopInventory())) {
event.setCancelled(true);
return;
}
if (InteractiveChat.enderDisplay.containsValue(event.getView().getTopInventory())) {
if (InteractiveChat.enderDisplay.inverse().containsKey(event.getView().getTopInventory())) {
event.setCancelled(true);
return;
}
if (InteractiveChat.itemDisplay.containsValue(event.getView().getTopInventory())) {
if (InteractiveChat.itemDisplay.inverse().containsKey(event.getView().getTopInventory())) {
event.setCancelled(true);
return;
}
}
*/
}
21 changes: 21 additions & 0 deletions src/com/loohp/interactivechat/ObjectHolders/ValuePairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.loohp.interactivechat.ObjectHolders;

public class ValuePairs<F, S> {

private F first;
private S second;

public ValuePairs(F first, S second) {
this.first = first;
this.second = second;
}

public F getFirst() {
return first;
}

public S getSecond() {
return second;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ public void onPluginMessageReceived(String channel, Player pluginMessagingPlayer
if (player2 == null) {
break;
}
String placeholder = DataTypeIO.readString(input, StandardCharsets.UTF_8);
String text = DataTypeIO.readString(input, StandardCharsets.UTF_8);
player2.getRemotePlaceholdersMapping().put(placeholder, text);
int size1 = input.readInt();
for (int i = 0; i < size1; i++) {
String placeholder = DataTypeIO.readString(input, StandardCharsets.UTF_8);
String text = DataTypeIO.readString(input, StandardCharsets.UTF_8);
player2.getRemotePlaceholdersMapping().put(placeholder, text);
}
break;
case 0x06:
String message = DataTypeIO.readString(input, StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Random;
import java.util.UUID;

Expand All @@ -13,6 +14,7 @@
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.loohp.interactivechat.InteractiveChat;
import com.loohp.interactivechat.ObjectHolders.ValuePairs;
import com.loohp.interactivechat.Utils.CompressionUtils;
import com.loohp.interactivechat.Utils.CustomArrayUtils;
import com.loohp.interactivechat.Utils.DataTypeIO;
Expand Down Expand Up @@ -82,11 +84,14 @@ public static boolean forwardEnderchest(UUID player, String title, Inventory end
return forwardData(0x04, out.toByteArray());
}

public static boolean forwardPlaceholders(UUID player, String placeholder, String text) throws IOException {
public static boolean forwardPlaceholders(UUID player, List<ValuePairs<String, String>> pairs) throws IOException {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
DataTypeIO.writeUUID(out, player);
DataTypeIO.writeString(out, placeholder, StandardCharsets.UTF_8);
DataTypeIO.writeString(out, text, StandardCharsets.UTF_8);
out.writeInt(pairs.size());
for (ValuePairs<String, String> pair : pairs) {
DataTypeIO.writeString(out, pair.getFirst(), StandardCharsets.UTF_8);
DataTypeIO.writeString(out, pair.getSecond(), StandardCharsets.UTF_8);
}
return forwardData(0x05, out.toByteArray());
}

Expand Down
16 changes: 13 additions & 3 deletions src/com/loohp/interactivechat/Utils/PlaceholderParser.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
package com.loohp.interactivechat.Utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import org.bukkit.entity.Player;

import com.loohp.interactivechat.InteractiveChat;
import com.loohp.interactivechat.ObjectHolders.PlayerWrapper;
import com.loohp.interactivechat.ObjectHolders.ValuePairs;
import com.loohp.interactivechat.PluginMessaging.BungeeMessageSender;

import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;

public class PlaceholderParser {

public static String parse(PlayerWrapper player, String str) {
if (player.isLocal()) {
for (Entry<String, String> entry : getAllPlaceholdersContained(player.getLocalPlayer(), str).entrySet()) {
if (InteractiveChat.bungeecordMode) {
List<ValuePairs<String, String>> pairs = new ArrayList<>();
for (Entry<String, String> entry : getAllPlaceholdersContained(player.getLocalPlayer(), str).entrySet()) {
pairs.add(new ValuePairs<>(entry.getKey(), entry.getValue()));
}
try {
BungeeMessageSender.forwardPlaceholders(player.getUniqueId(), entry.getKey(), entry.getValue());
BungeeMessageSender.forwardPlaceholders(player.getUniqueId(), pairs);
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -36,7 +46,7 @@ public static String parse(PlayerWrapper player, String str) {
}

public static Map<String, String> getAllPlaceholdersContained(Player player, String str) {
Set<PlaceholderExpansion> expansions = PlaceholderAPI.getExpansions();
Collection<PlaceholderExpansion> expansions = PlaceholderAPIPlugin.getInstance().getLocalExpansionManager().getExpansions();
Set<String> identifier = expansions.parallelStream().map(each -> each.getIdentifier()).collect(Collectors.toSet());
Map<String, String> matchingPlaceholders = new HashMap<>();
for (String word : str.split(" ")) {
Expand Down

0 comments on commit acf43a8

Please sign in to comment.