Skip to content

Commit

Permalink
rewards on player birthday - v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
YanisBft committed Apr 19, 2020
1 parent eb59370 commit a8b07b7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
birthday_announcement: "§c§lPeace§6§lAnd§a§lCube §ete souhaite un joyeux anniversaire %1s !"

birthday_saved: "Ta date d'anniversaire a été enregistrée."
birthday_already_saved: "Tu as déjà enregistré ta date d'anniversaire."
birthday_invalid: "La date d'anniversaire est invalide."

player_birthday: "L'anniversaire de %1s est le %2s %3s."
Expand All @@ -12,3 +13,8 @@ player_not_set: "Le joueur n'a pas de date d'anniversaire définie."

birthdays_today: "Il y a %1s anniversaire(s) aujourd'hui : %2s"
no_birthday_today: "Il n'y a aucun anniversaire aujourd'hui."

# Rewards
# Use %1$s to use the player's name in a command
rewards:
- minecraft:give %1$s minecraft:player_head{SkullOwner:%1$s,display:{Name:'{"text":"Tête de %1$s","color":"yellow","italic":false}',Lore:['{"text":"Cadeau d\'anniversaire"}']},Enchantments:[{id:"minecraft:protection",lvl:4}],AttributeModifiers:[{AttributeName:"generic.armor",Name:"generic.armor",Amount:2,Operation:0,UUIDLeast:155648,UUIDMost:419097,Slot:"head"}]}
11 changes: 2 additions & 9 deletions src/fr/peaceandcube/pacbirthday/command/BirthdaysCommand.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package fr.peaceandcube.pacbirthday.command;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bukkit.Bukkit;
Expand All @@ -14,6 +12,7 @@
import org.bukkit.configuration.file.FileConfiguration;

import fr.peaceandcube.pacbirthday.data.BirthdayData;
import fr.peaceandcube.pacbirthday.util.Utils;

public class BirthdaysCommand implements CommandExecutor, TabExecutor {
public FileConfiguration config = Bukkit.getPluginManager().getPlugin("PACBirthday").getConfig();
Expand All @@ -23,7 +22,7 @@ public class BirthdaysCommand implements CommandExecutor, TabExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (args.length == 0 && sender.hasPermission("pacbirthday.today")) {
List<String> players = BirthdayData.getBirthdays(this.getCurrentDay());
List<String> players = BirthdayData.getBirthdays(Utils.getCurrentDay());
if (!players.isEmpty()) {
String playerNames = "";
for (String player : players) {
Expand All @@ -39,12 +38,6 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
return false;
}

private String getCurrentDay() {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd-MM");
return format.format(date);
}

@Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
return new ArrayList<>();
Expand Down
16 changes: 11 additions & 5 deletions src/fr/peaceandcube/pacbirthday/command/SetBirthdayCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class SetBirthdayCommand implements CommandExecutor, TabExecutor {
public FileConfiguration config = Bukkit.getPluginManager().getPlugin("PACBirthday").getConfig();
public String birthdaySaved = config.getString("birthday_saved");
public String birthdayAlreadySaved = config.getString("birthday_already_saved");
public String birthdayInvalid = config.getString("birthday_invalid");

@Override
Expand All @@ -26,13 +27,18 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
Player player = (Player) sender;

if (args.length == 2 && player.hasPermission("pacbirthday.set")) {
if (this.isValidBirthday(args[0], args[1])) {
String monthNumber = String.format("%02d", Month.fromString(args[1]).getNumber());
BirthdayData.saveBirthday(player, String.format("%02d", Integer.parseInt(args[0])) + "-" + monthNumber);
player.sendMessage(ChatColor.YELLOW + this.birthdaySaved);
if (BirthdayData.getBirthday(player) == null) {
if (this.isValidBirthday(args[0], args[1])) {
String monthNumber = String.format("%02d", Month.fromString(args[1]).getNumber());
BirthdayData.saveBirthday(player, String.format("%02d", Integer.parseInt(args[0])) + "-" + monthNumber);
player.sendMessage(ChatColor.YELLOW + this.birthdaySaved);
} else {
player.sendMessage(ChatColor.RED + this.birthdayInvalid);
}
} else {
player.sendMessage(ChatColor.RED + this.birthdayInvalid);
player.sendMessage(ChatColor.RED + this.birthdayAlreadySaved);
}

return true;
}
}
Expand Down
22 changes: 17 additions & 5 deletions src/fr/peaceandcube/pacbirthday/event/PlayerJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
Expand All @@ -13,27 +15,37 @@
import org.bukkit.event.player.PlayerJoinEvent;

import fr.peaceandcube.pacbirthday.data.BirthdayData;
import fr.peaceandcube.pacbirthday.util.Utils;

public class PlayerJoin implements Listener {
public FileConfiguration config = Bukkit.getPluginManager().getPlugin("PACBirthday").getConfig();
public String birthdayAnnouncement = config.getString("birthday_announcement");
public List<String> rewards = config.getStringList("rewards");

@EventHandler
public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
Server server = player.getServer();

String currentDay = this.getCurrentDay();
String currentDay = Utils.getCurrentDay();
String birthday = BirthdayData.getBirthday(player);

if (currentDay.equals(birthday)) {
server.broadcastMessage(String.format(ChatColor.LIGHT_PURPLE + this.birthdayAnnouncement, player.getName()));

if (!currentDay.equals(this.getLastPlayed(player))) {

// Execute all commands defined
for (String command : this.rewards) {
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), String.format(command, player.getName()));
}
}
}
}

private String getCurrentDay() {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd-MM");
return format.format(date);
private String getLastPlayed(OfflinePlayer player) {
long timestamp = player.getLastPlayed();
String date = new SimpleDateFormat("dd-MM").format(new Date(timestamp));
return date;
}
}
13 changes: 13 additions & 0 deletions src/fr/peaceandcube/pacbirthday/util/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fr.peaceandcube.pacbirthday.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {

public static String getCurrentDay() {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd-MM");
return format.format(date);
}
}
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main: fr.peaceandcube.pacbirthday.PACBirthday
name: PACBirthday
version: 1.0.1.1
version: 1.1.0
author: Yanis48
api-version: 1.13

Expand Down

0 comments on commit a8b07b7

Please sign in to comment.