Files
KittyBot/src/commands/general/CommandBeansShow.java
T
Alex Stewart 6ddb0fc532 =Update for Beans
Fixes and updates for beans
2020-09-29 02:42:01 -04:00

139 lines
4.6 KiB
Java

package commands.general;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import core.Command;
import core.Constants;
import core.LocStrings;
import dataStructures.KittyChannel;
import dataStructures.KittyGuild;
import dataStructures.KittyRating;
import dataStructures.KittyRole;
import dataStructures.KittyUser;
import dataStructures.Response;
import dataStructures.UserInput;
public class CommandBeansShow extends Command
{
public CommandBeansShow(KittyRole level, KittyRating rating) { super(level, rating); }
@Override
public String getHelpText() { return LocStrings.stub("BeansShowInfo"); };
@Override
public void onRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res)
{
if(input.mentions == null)
{
//res.send("" + user.getBeans());
try {
res.sendFile(combineImages(user.getBeans()), "png");
} catch (IOException e) {
e.printStackTrace();
}
// res.send(String.format(LocStrings.stub("BeansShowDisplay"), user.getBeans()));
}
else
{
String mentionedBeans = "";
for(KittyUser mentioned:input.mentions)
{
mentionedBeans += mentioned.name + " has " + mentioned.getBeans() + " ";
}
res.send(String.format(LocStrings.stub("BeansShowMentioned"), mentionedBeans));
}
}
private File combineImages(long beans) throws IOException
{
// Acquire images
BufferedImage imageBase = ImageIO.read(new File(Constants.AssetDirectory + "beans/BeansBG.png"));
BufferedImage thouBean = ImageIO.read(new File(Constants.AssetDirectory + "beans/thouBean.png"));
BufferedImage hunBean = ImageIO.read(new File(Constants.AssetDirectory + "beans/hunBean.png"));
BufferedImage tenBean = ImageIO.read(new File(Constants.AssetDirectory + "beans/tenBean.png"));
BufferedImage oneBean = ImageIO.read(new File(Constants.AssetDirectory + "beans/oneBean.png"));
final int baseWidth = imageBase.getWidth();
final int baseHeight = imageBase.getHeight();
long calcBean = beans;
long thousands = beans / 1000;
calcBean = beans % 1000;
long hundreds = (calcBean / 100);
calcBean = calcBean % 100;
long tens = (calcBean / 10);
calcBean = calcBean % 10;
long ones = calcBean;
BufferedImage beanImage = new BufferedImage(baseWidth, baseHeight, BufferedImage.TYPE_INT_ARGB);
Graphics beanGraphics = beanImage.getGraphics();
Rectangle rect = new Rectangle(0, 0, baseWidth, baseHeight);
beanGraphics.drawImage(imageBase,0,0,null);
for(int i = 0; i < ones; i++)
{
int x = (int)(Math.random() * (baseWidth - oneBean.getWidth())) + 1;
int y = (int)(Math.random() * (baseHeight - oneBean.getHeight())) + 1;
beanGraphics.drawImage(oneBean, x, y, null);
}
for(int i = 0; i < tens; i++)
{
int x = (int)(Math.random() * (baseWidth - tenBean.getWidth())) + 1;
int y = (int)(Math.random() * (baseHeight - tenBean.getHeight())) + 1;
beanGraphics.drawImage(tenBean, x, y, null);
}
for(int i = 0; i < hundreds; i++)
{
int x = (int)(Math.random() * (baseWidth - hunBean.getWidth())) + 1;
int y = (int)(Math.random() * (baseHeight - hunBean.getHeight())) + 1;
beanGraphics.drawImage(hunBean, x, y, null);
}
for(int i = 0; i < thousands; i++)
{
int x = (int)(Math.random() * (baseWidth - thouBean.getWidth())) + 1;
int y = (int)(Math.random() * (baseHeight - thouBean.getHeight())) + 1;
beanGraphics.drawImage(thouBean, x, y, null);
}
try {
@SuppressWarnings("unused")
Font font = null;
font = Font.createFont(Font.TRUETYPE_FONT, new File(Constants.AssetDirectory +"fonts/B612Mono-Regular.ttf"));
} catch (Exception e)
{
}
Font beanFont = new Font("font", Font.BOLD, 45);
beanGraphics.setFont(beanFont);
drawCenteredString(beanGraphics, "YOU HAVE", rect, beanFont, .25);
drawCenteredString(beanGraphics, "" + beans, rect, beanFont, .5);
drawCenteredString(beanGraphics, "BEANS!", rect, beanFont, .75);
ImageIO.write(beanImage, "PNG", new File("beanImage.png"));
return new File("beanImage.png");
}
private void drawCenteredString(Graphics beanGraphics, String text, Rectangle rect, Font font, double segment)
{
FontMetrics metrics = beanGraphics.getFontMetrics(font);
int x = rect.x + (rect.width - metrics.stringWidth(text)) / 2;
int y = (int)(rect.y + ((rect.height - metrics.getHeight()) * segment) + metrics.getAscent());
beanGraphics.drawString(text, x, y);
}
}