Skip to content

Commit

Permalink
1.2.3 - Caching, Maps and Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
LOOHP committed Dec 8, 2020
1 parent c0750d5 commit 66aaec4
Show file tree
Hide file tree
Showing 16 changed files with 546 additions and 44 deletions.
Binary file renamed lib/spigot-1.16.3.jar → lib/spigot-1.16.4.jar
Binary file not shown.
8 changes: 4 additions & 4 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>com.loohp</groupId>
<artifactId>InteractiveChatDiscordSrvAddon</artifactId>
<version>1.2.2.2</version>
<version>1.2.3.0</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
Expand Down Expand Up @@ -126,16 +126,16 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.3-R0.1-SNAPSHOT</version>
<version>1.16.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>Spigot-1.16.3</groupId>
<artifactId>Spigot-1.16.3</artifactId>
<version>1.0</version>
<systemPath>${basedir}/lib/spigot-1.16.3.jar</systemPath>
<systemPath>${basedir}/lib/spigot-1.16.4.jar</systemPath>
<scope>system</scope>
</dependency>
</dependency>
<dependency>
<groupId>com.discordsrv</groupId>
<artifactId>discordsrv</artifactId>
Expand Down
Binary file added src/assets/gui/map_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/gui/map_background_checkerboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/gui/map_icons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class InteractiveChatDiscordSrvAddon extends JavaPlugin {
public Metrics metrics;
public AtomicLong messagesCounter = new AtomicLong(0);
public AtomicLong imageCounter = new AtomicLong(0);
public AtomicLong inventoryImageCounter = new AtomicLong(0);

public boolean itemImage = true;
public boolean invImage = true;
Expand All @@ -65,6 +66,8 @@ public class InteractiveChatDiscordSrvAddon extends JavaPlugin {

public boolean UpdaterEnabled = true;

public int cacheTimeout = 1200;

private ConfigurationSection translations;
private Map<String, String> modernItemTranslations = new HashMap<>();

Expand Down Expand Up @@ -151,6 +154,8 @@ public void reloadConfig() {

UpdaterEnabled = getConfig().getBoolean("Options.UpdaterEnabled");

cacheTimeout = getConfig().getInt("Settings.CacheTimeout") * 20;

itemDisplaySingle = getConfig().getString("InventoryImage.Item.EmbedDisplay.Single");
itemDisplayMultiple = getConfig().getString("InventoryImage.Item.EmbedDisplay.Multiple");
invColor = ColorUtils.hex2Rgb(getConfig().getString("InventoryImage.Inventory.EmbedColor"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.loohp.interactivechatdiscordsrvaddon.InteractiveChatDiscordSrvAddon;
import com.loohp.interactivechatdiscordsrvaddon.Utils.IDProvider;
import com.loohp.interactivechatdiscordsrvaddon.Utils.ImageGeneration;
import com.loohp.interactivechatdiscordsrvaddon.Utils.ItemMapWrapper;
import com.loohp.interactivechatdiscordsrvaddon.Utils.ItemStackUtils;
import com.loohp.interactivechatdiscordsrvaddon.Utils.ItemStackUtils.DiscordDescription;

Expand All @@ -45,6 +46,7 @@
import github.scarsz.discordsrv.dependencies.jda.api.JDA;
import github.scarsz.discordsrv.dependencies.jda.api.entities.Message;
import github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel;
import github.scarsz.discordsrv.dependencies.jda.api.requests.restaction.MessageAction;

public class DiscordSRVEvents {

Expand Down Expand Up @@ -223,11 +225,22 @@ public void discordMessageSent(DiscordGuildMessageSentEvent event) {
}
try {
DiscordDescription description = ItemStackUtils.getDiscordDescription(item);
BufferedImage image = ImageGeneration.getItemStackImage(item);
BufferedImage image = ImageGeneration.getItemStackImage(item);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "png", os);
EmbedBuilder embed = new EmbedBuilder().setDescription(description.getDescription().orElse(null)).setColor(color).setAuthor(description.getName(), null, "attachment://Item.png");
if (iData.isFilledMap()) {
embed.setImage("attachment://Map.png");
}
MessageAction messageToSend = channel.sendMessage(embed.build()).addFile(os.toByteArray(), "Item.png");
if (iData.isFilledMap()) {
BufferedImage map = ImageGeneration.getMapImage(item);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(map, "png", out);
messageToSend.addFile(out.toByteArray(), "Map.png");
}
channel.sendMessage(text).queue();
channel.sendMessage(new EmbedBuilder().setAuthor(description.getName(), null, "attachment://Item.png").setDescription(description.getDescription().orElse(null)).setColor(color).build()).addFile(os.toByteArray(), "Item.png").queue();
messageToSend.queue();
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -282,26 +295,28 @@ public static class ImageDisplayData {
private final Optional<Inventory> inventory;
private final boolean isPlayerInventory;
private final Optional<ItemStack> item;
private final boolean isFilledMap;

public ImageDisplayData(Player player, String title, ImageDisplayType type, Inventory inventory, boolean isPlayerInventory, ItemStack item) {
private ImageDisplayData(Player player, String title, ImageDisplayType type, Inventory inventory, boolean isPlayerInventory, ItemStack item, boolean isFilledMap) {
this.type = type;
this.player = player;
this.title = title;
this.inventory = Optional.ofNullable(inventory);
this.isPlayerInventory = isPlayerInventory;
this.item = Optional.ofNullable(item);
this.isFilledMap = isFilledMap;
}

public ImageDisplayData(Player player, String title, ImageDisplayType type, Inventory inventory) {
this(player, title, type, inventory, false, null);
this(player, title, type, inventory, false, null, false);
}

public ImageDisplayData(Player player, String title, ImageDisplayType type, boolean isPlayerInventory, Inventory inventory) {
this(player, title, type, inventory, isPlayerInventory, null);
this(player, title, type, inventory, isPlayerInventory, null, false);
}

public ImageDisplayData(Player player, String title, ImageDisplayType type, ItemStack itemstack) {
this(player, title, type, null, false, itemstack);
this(player, title, type, null, false, itemstack, ItemMapWrapper.isFilledMap(itemstack));
}

public Player getPlayer() {
Expand All @@ -323,6 +338,10 @@ public boolean isPlayerInventory() {
public Optional<ItemStack> getItemStack() {
return item;
}

public boolean isFilledMap() {
return isFilledMap;
}

public ImageDisplayType getType() {
return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,21 @@ public void onMessageReceived(MessageReceivedEvent event) {
}
try {
DiscordDescription description = ItemStackUtils.getDiscordDescription(item);
BufferedImage image = ImageGeneration.getItemStackImage(item);
BufferedImage image = ImageGeneration.getItemStackImage(item);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "png", os);
messagesToSend.add(new WebhookMessageBuilder().addEmbeds(new WebhookEmbedBuilder().setAuthor(new EmbedAuthor(description.getName(), "attachment://Item.png", null)).setColor(color.getRGB()).setDescription(description.getDescription().orElse(null)).build()).addFile("Item.png", os.toByteArray()));
WebhookEmbedBuilder embed = new WebhookEmbedBuilder().setAuthor(new EmbedAuthor(description.getName(), "attachment://Item.png", null)).setColor(color.getRGB()).setDescription(description.getDescription().orElse(null));
if (iData.isFilledMap()) {
embed.setImageUrl("attachment://Map.png");
}
WebhookMessageBuilder webhookmessage = new WebhookMessageBuilder().addEmbeds(embed.build()).addFile("Item.png", os.toByteArray());
if (iData.isFilledMap()) {
BufferedImage map = ImageGeneration.getMapImage(item);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(map, "png", out);
webhookmessage.addFile("Map.png", out.toByteArray());
}
messagesToSend.add(webhookmessage);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,22 @@ public Integer call() throws Exception {
}
}));

metrics.addCustomChart(new Metrics.SingleLineChart("total_inventory_images_created_per_interval", new Callable<Integer>() {
metrics.addCustomChart(new Metrics.SingleLineChart("total_images_created_per_interval", new Callable<Integer>() {
@Override
public Integer call() throws Exception {
long amount = InteractiveChatDiscordSrvAddon.plugin.imageCounter.getAndSet(0);
return amount > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) amount;
}
}));

metrics.addCustomChart(new Metrics.SingleLineChart("total_inventory_images_created_per_interval", new Callable<Integer>() {
@Override
public Integer call() throws Exception {
long amount = InteractiveChatDiscordSrvAddon.plugin.inventoryImageCounter.getAndSet(0);
return amount > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) amount;
}
}));

}

}
41 changes: 27 additions & 14 deletions src/com/loohp/interactivechatdiscordsrvaddon/Metrics/Metrics.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
package com.loohp.interactivechatdiscordsrvaddon.Metrics;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.zip.GZIPOutputStream;

import javax.net.ssl.HttpsURLConnection;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;

/**
* bStats collects some data for plugin authors.
* <p>
Expand Down
42 changes: 42 additions & 0 deletions src/com/loohp/interactivechatdiscordsrvaddon/Utils/Cache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.loohp.interactivechatdiscordsrvaddon.Utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.Bukkit;

import com.loohp.interactivechatdiscordsrvaddon.InteractiveChatDiscordSrvAddon;

public class Cache<T> {

private static Map<String, Cache<?>> data = new ConcurrentHashMap<>();

private long timeCreated;
private T object;

private Cache(T object) {
this.timeCreated = System.currentTimeMillis();
this.object = object;
}

public long getTimeCreated() {
return timeCreated;
}

public T getObject() {
return object;
}

public static Cache<?> getCache(String key) {
return data.get(key);
}

public static <T> void putCache(String key, T object, long ticks) {
Cache<T> cache = new Cache<>(object);
data.put(key, cache);
Bukkit.getScheduler().runTaskLater(InteractiveChatDiscordSrvAddon.plugin, () -> {
data.remove(key, cache);
}, ticks);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ public static BufferedImage squarify(BufferedImage image) {
return newImage;
}

public static BufferedImage xor(BufferedImage bottom, BufferedImage top, int alpha) {
for (int y = 0; y < bottom.getHeight(); y++) {
for (int x = 0; x < bottom.getWidth(); x++) {
int bottomValue = bottom.getRGB(x, y);
Color bottomColor = new Color(bottomValue, true);

int topValue = top.getRGB(x, y);
Color topColor = new Color(topValue, true);

Color color = new Color(bottomColor.getRed() ^ topColor.getRed(), bottomColor.getGreen() ^ topColor.getGreen(), bottomColor.getBlue() ^ topColor.getBlue(), bottomColor.getAlpha() ^ (topColor.getAlpha() * alpha / 255));
bottom.setRGB(x, y, color.getRGB());
}
}
return bottom;
}

public static BufferedImage copyImage(BufferedImage source){
BufferedImage b = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = b.createGraphics();
Expand All @@ -123,6 +139,7 @@ public static BufferedImage copyAndGetSubImage(BufferedImage source, int x, int
BufferedImage copyOfImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = copyOfImage.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return copyOfImage;
}

Expand Down
Loading

0 comments on commit 66aaec4

Please sign in to comment.