-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Changes: + Added a simple Reddit Client in the chat (command: /reddit) + Moved some functions to seperate classes + Updated Spigot API to 1.15.2 + Seperate file for player storage
- Loading branch information
1 parent
881139a
commit 8d252d1
Showing
8 changed files
with
328 additions
and
148 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 |
---|---|---|
@@ -1,13 +1,18 @@ | ||
# MCeddit | ||
A Simple way to link Minecraft with Reddit | ||
## Commands | ||
- Reddit: | ||
- Description: Look at Reddit in game | ||
- Usage: /\<command\> \[Subreddit\] \[API Query\] | ||
- Permission: MCeddit.view | ||
- Default: true | ||
- LinkReddit: | ||
- Description: Link your Minecraft account with your Reddit Account | ||
- Usage: /\<command\> \<reddit username\> [player] | ||
- Usage: /\<command\> \<reddit username\> \[player\] | ||
- Permission: MCeddit.link | ||
- Default: true | ||
- GetReddit: | ||
- Description: Look at the Reddit API using the plugin | ||
- Usage: /\<command\> \<API Object\> [player] | ||
- Usage: /\<command\> \<API Object\> \[player\] | ||
- Permission: MCeddit.get | ||
- Default: false | ||
- Default: false |
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
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,94 @@ | ||
package me.steve8playz; | ||
|
||
import org.bukkit.*; | ||
import org.bukkit.entity.Firework; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.FireworkMeta; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
|
||
public class CakeDay implements Listener { | ||
|
||
private final MCeddit plugin; | ||
// Keeps track of the cooldowns for multiple players | ||
private HashMap<String, Long> cooldowns = new HashMap<String, Long>(); | ||
|
||
public CakeDay(MCeddit plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
private static ItemStack bakeCake(String itemName, String itemLore) { | ||
ItemStack item = new ItemStack(Material.CAKE, 1); | ||
ItemMeta meta = item.getItemMeta(); | ||
meta.setDisplayName(itemName); | ||
ArrayList<String> lore = new ArrayList<String>(); | ||
lore.add(itemLore); | ||
meta.setLore(lore); | ||
item.setItemMeta(meta); | ||
return item; | ||
} | ||
|
||
private static void firework(Player player) { | ||
Location loc = player.getLocation(); | ||
Firework fw = loc.getWorld().spawn(loc, Firework.class); | ||
FireworkMeta data = fw.getFireworkMeta(); | ||
Color color1 = Color.RED; | ||
Color color2 = Color.AQUA; | ||
data.addEffects(FireworkEffect.builder().withColor(color1, color2).with(FireworkEffect.Type.STAR).build()); | ||
fw.setFireworkMeta(data); | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerJoin(PlayerJoinEvent event) { | ||
Player player = event.getPlayer(); | ||
|
||
if (player.hasPermission("mceddit.cakeday") && | ||
plugin.getPlayerConfig().contains(player.getUniqueId().toString())) { | ||
long redditCreated = (long) Double.parseDouble(plugin.getReddit(plugin.getPlayerConfig().getString(player.getUniqueId().toString()), "created")); | ||
if (compareUnix(System.currentTimeMillis() / 1000, redditCreated, "MM-dd")) { | ||
|
||
// Cooldown to stop this from executing twice | ||
// TODO: Find a better way of doing this | ||
// TODO: Put cooldownTime into config | ||
int cooldownTime = 86400; | ||
if (cooldowns.containsKey(player.getName())) { | ||
long secondsLeft = ((cooldowns.get(player.getName()) / 1000) + cooldownTime) - (System.currentTimeMillis() / 1000); | ||
if (secondsLeft > 0) { | ||
// player.sendMessage("You cant use that commands for another "+ secondsLeft +" seconds!"); | ||
return; | ||
} | ||
} | ||
cooldowns.put(player.getName(), System.currentTimeMillis()); | ||
// END Cooldown Code | ||
|
||
plugin.getServer().broadcastMessage(ChatColor.GOLD + "It's " + player.getName() + "'s Reddit Cake Day Today!"); | ||
if (!player.hasPermission("mceddit.cakeday.givecake")) | ||
player.sendMessage(ChatColor.GOLD + "Happy Cake Day " + player.getName() + "!"); | ||
else { | ||
player.sendMessage(ChatColor.GOLD + "Happy Cake Day " + player.getName() + "! Here's a Cake to Celebrate!"); | ||
player.getInventory().addItem(bakeCake(player.getName() + "'s Cake", "A Cake for your Reddit Cake Day")); | ||
} | ||
if (player.hasPermission("mceddit.cakeday.firework")) firework(player); | ||
} | ||
} | ||
} | ||
|
||
private boolean compareUnix(long unixSeconds, long compareUnixSeconds, String comparePattern) { | ||
// Convert seconds to milliseconds | ||
Date date = new java.util.Date(unixSeconds * 1000L); | ||
Date dateCompare = new java.util.Date(compareUnixSeconds * 1000L); | ||
// Format the date | ||
SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(comparePattern); | ||
SimpleDateFormat dateFormatCompare = new java.text.SimpleDateFormat(comparePattern); | ||
// Compare | ||
return ((dateFormat.format(date)).equals(dateFormatCompare.format(dateCompare))); | ||
} | ||
} |
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,111 @@ | ||
package me.steve8playz; | ||
|
||
import net.md_5.bungee.api.chat.ClickEvent; | ||
import net.md_5.bungee.api.chat.TextComponent; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandExecutor; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.Collections; | ||
|
||
public class Commands implements CommandExecutor { | ||
private final MCeddit plugin; | ||
|
||
public Commands(MCeddit plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { | ||
Player player = (Player) sender; | ||
|
||
String messagePrefix = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("PluginPrefix") + " "); | ||
|
||
// TODO: Make the linked account only to be changed once without admin permissions | ||
if ((cmd.getName().equalsIgnoreCase("LinkReddit")) && (sender.hasPermission("mceddit.link"))) { | ||
if (args.length == 1) { | ||
if (sender instanceof Player) { | ||
plugin.setPlayerConfig(player.getUniqueId().toString(), args[0].replace("u/", "")); | ||
sender.sendMessage(messagePrefix + ChatColor.GREEN + "User Linked!"); | ||
} else plugin.getLogger().warning("You need to be a player to run this command."); | ||
} else if (args.length == 2 && sender.hasPermission("mceddit.link.players")) { | ||
if (Bukkit.getPlayer(args[0]).isOnline()) { | ||
plugin.setPlayerConfig(args[1], Bukkit.getPlayer(args[0]).getUniqueId()); | ||
sender.sendMessage(messagePrefix + ChatColor.GREEN + "User Linked!"); | ||
} else sender.sendMessage(messagePrefix + ChatColor.RED + "Error: Player not Online."); | ||
} else { | ||
sender.sendMessage(messagePrefix + ChatColor.RED + "Usage: /LinkReddit <reddit username> [player]"); | ||
} | ||
} | ||
if ((cmd.getName().equalsIgnoreCase("GetReddit")) && (sender.hasPermission("mceddit.get"))) { | ||
if (args.length == 1) { | ||
if (sender instanceof Player) { | ||
if (plugin.getPlayerConfig().contains(player.getUniqueId().toString())) { | ||
String message = plugin.getReddit(plugin.getConfig().getString(player.getUniqueId().toString()), args[0]); | ||
if (message != null) sender.sendMessage(message); | ||
else | ||
sender.sendMessage(messagePrefix + ChatColor.DARK_RED + "An Error Occurred. It is likely that you mistyped an Object in the API or there is no internet."); | ||
} else sender.sendMessage(messagePrefix + "Link your Reddit account first"); | ||
} else plugin.getLogger().warning("You need to be a player to run this command."); | ||
} else if (args.length == 2 && sender.hasPermission("mceddit.get.players")) { | ||
if (Bukkit.getPlayer(args[1]).isOnline()) { | ||
String message = plugin.getReddit(plugin.getConfig().getString(Bukkit.getPlayer(args[1]).getUniqueId().toString()), args[0]); | ||
if (message != null) sender.sendMessage(message); | ||
else | ||
sender.sendMessage(messagePrefix + ChatColor.DARK_RED + "An Error Occurred. " + ChatColor.RED + "It is likely that you mistyped an Object in the API or there is no internet."); | ||
} else sender.sendMessage(messagePrefix + ChatColor.RED + "Error: Player not Online."); | ||
} else { | ||
sender.sendMessage(messagePrefix + ChatColor.RED + "Usage: /GetReddit <API Object> [player]"); | ||
} | ||
} | ||
|
||
if ((cmd.getName().equalsIgnoreCase("reddit")) && (sender.hasPermission("mceddit.view"))) { | ||
if (sender instanceof Player) { | ||
String[][] posts; | ||
String subreddit; | ||
if (args.length == 1) { | ||
subreddit = args[0]; | ||
posts = plugin.getSubreddit(subreddit, null); | ||
} else if (args.length == 2) { | ||
subreddit = args[0]; | ||
posts = plugin.getSubreddit(subreddit, args[1]); | ||
} else { | ||
subreddit = plugin.getConfig().getString("DefaultSubreddit"); | ||
posts = plugin.getSubreddit(subreddit, null); | ||
} | ||
|
||
TextComponent border = new TextComponent(new String(new char[50]).replace('\0', '-')); | ||
border.setColor(net.md_5.bungee.api.ChatColor.DARK_GRAY); | ||
|
||
player.spigot().sendMessage(border); | ||
|
||
for (int i = 0; i < plugin.getConfig().getInt("PostLimit"); i++) { | ||
String[] post = posts[i]; | ||
|
||
TextComponent title = new TextComponent(post[1]); | ||
title.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://www.reddit.com" + post[5])); | ||
title.setColor(net.md_5.bungee.api.ChatColor.WHITE); | ||
title.setUnderlined(true); | ||
|
||
TextComponent text = new TextComponent("submitted by " + post[3] + " to r/" + post[0] + "\n" + | ||
post[2] + " upvotes with " + post[4] + " comments"); | ||
text.setColor(net.md_5.bungee.api.ChatColor.GRAY); | ||
|
||
player.spigot().sendMessage(title); | ||
player.spigot().sendMessage(text); | ||
} | ||
|
||
TextComponent nextButton = new TextComponent("[Next]"); | ||
nextButton.setBold(true); | ||
|
||
nextButton.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/reddit " + | ||
subreddit + " after=" + posts[posts.length - 1][0])); | ||
player.spigot().sendMessage(nextButton); | ||
player.spigot().sendMessage(border); | ||
} else plugin.getLogger().warning("You need to be a player to run this command."); | ||
} | ||
return true; | ||
} | ||
} |
Oops, something went wrong.