Skip to content

Commit 85c70d4

Browse files
committed
Initial release - 1.0.0
0 parents  commit 85c70d4

File tree

10 files changed

+504
-0
lines changed

10 files changed

+504
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bin/
2+
.classpath
3+
.project
4+
.settings

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# PACDonjons
2+
A Minecraft plugin to manage donjons on PeaceAndCube
3+
4+
## Features
5+
- ``/pacdonjon add <name> <author>`` to add a donjon
6+
- ``/pacdonjon remove <donjon>`` to remove a donjon
7+
- ``/pacdonjon addstep <donjon> <name>`` to add a step in a donjon
8+
- ``/pacdonjon removestep <donjon> <step>`` to remove a step in a donjon
9+
- ``/pacdonjon set <target> <donjon> <step>`` to set the current step of a target in a donjon
10+
- ``/pacdonjon set <target> <donjon>`` to remove the currently set step of a target in a donjon
11+
- ``/donjon <donjon>`` to teleport to your current step in a donjon
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package fr.peaceandcube.pacdonjons;
2+
3+
import org.bukkit.plugin.java.JavaPlugin;
4+
5+
import fr.peaceandcube.pacdonjons.command.DonjonCommand;
6+
import fr.peaceandcube.pacdonjons.command.PacDonjonCommand;
7+
import fr.peaceandcube.pacdonjons.file.DonjonsFile;
8+
import fr.peaceandcube.pacdonjons.file.PlayerDataFile;
9+
10+
public class PACDonjons extends JavaPlugin {
11+
12+
@Override
13+
public void onEnable() {
14+
this.getCommand("pacdonjon").setExecutor(new PacDonjonCommand());
15+
this.getCommand("donjon").setExecutor(new DonjonCommand());
16+
17+
DonjonsFile.load(this, "donjons.yml");
18+
PlayerDataFile.load(this, "playerdata.yml");
19+
}
20+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package fr.peaceandcube.pacdonjons.command;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.bukkit.command.Command;
7+
import org.bukkit.command.CommandExecutor;
8+
import org.bukkit.command.CommandSender;
9+
import org.bukkit.command.TabExecutor;
10+
import org.bukkit.entity.Player;
11+
12+
import fr.peaceandcube.pacdonjons.file.PlayerDataFile;
13+
14+
public class DonjonCommand implements CommandExecutor, TabExecutor {
15+
16+
@Override
17+
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
18+
if (sender.hasPermission("pacdonjons.donjon") && sender instanceof Player) {
19+
Player player = (Player) sender;
20+
if (args.length == 1) {
21+
String donjon = args[0];
22+
if (PlayerDataFile.getDonjons(player, "").contains(donjon)) {
23+
PlayerDataFile.teleportToDonjonStep(player, donjon);
24+
} else {
25+
sender.sendMessage(DonjonMessages.DONJON_NOT_FOUND);
26+
}
27+
return true;
28+
}
29+
}
30+
return false;
31+
}
32+
33+
@Override
34+
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
35+
if (sender.hasPermission("pacdonjons.donjon") && sender instanceof Player) {
36+
Player player = (Player) sender;
37+
switch (args.length) {
38+
default:
39+
return new ArrayList<String>();
40+
case 1:
41+
return PlayerDataFile.getDonjons(player, args[0]);
42+
}
43+
}
44+
return new ArrayList<>();
45+
}
46+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fr.peaceandcube.pacdonjons.command;
2+
3+
import org.bukkit.ChatColor;
4+
5+
public class DonjonMessages {
6+
7+
public static final String DONJON_NOT_FOUND = ChatColor.RED + "Le donjon n'a pas été trouvé.";
8+
public static final String STEP_NOT_FOUND = ChatColor.RED + "L'étape du donjon n'a pas été trouvée.";
9+
10+
public static final String DONJON_ADDED = ChatColor.YELLOW + "Le donjon %s a été ajouté.";
11+
public static final String DONJON_REMOVED = ChatColor.YELLOW + "Le donjon %s a été supprimé.";
12+
public static final String STEP_ADDED = ChatColor.YELLOW + "L'étape %s a été ajoutée au donjon %s.";
13+
public static final String STEP_REMOVED = ChatColor.YELLOW + "L'étape %s a été supprimée du donjon %s.";
14+
public static final String PLAYER_STEP_ADDED = ChatColor.YELLOW + "L'étape %s du donjon %s a été enregistrée pour %s.";
15+
public static final String PLAYER_STEP_REMOVED = ChatColor.YELLOW + "Le donjon %s n'est plus enregistré pour %s.";
16+
17+
public static final String TELEPORTED = ChatColor.YELLOW + "Tu as téléporté dans le donjon %s !";
18+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package fr.peaceandcube.pacdonjons.command;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.command.Command;
8+
import org.bukkit.command.CommandExecutor;
9+
import org.bukkit.command.CommandSender;
10+
import org.bukkit.command.TabExecutor;
11+
import org.bukkit.entity.Player;
12+
13+
import com.google.common.collect.ImmutableList;
14+
15+
import fr.peaceandcube.pacdonjons.file.DonjonsFile;
16+
import fr.peaceandcube.pacdonjons.file.PlayerDataFile;
17+
import fr.peaceandcube.pacpi.player.PlayerSuggestionProviders;
18+
19+
public class PacDonjonCommand implements CommandExecutor, TabExecutor {
20+
private static final List<String> OPERATIONS = ImmutableList.of("add", "remove", "addstep", "removestep", "set");
21+
22+
@Override
23+
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
24+
if (sender.hasPermission("pacdonjons.pacdonjon")) {
25+
if (args.length >= 1) {
26+
switch (args[0]) {
27+
case "add":
28+
if (args.length == 3 && sender instanceof Player) {
29+
String name = args[1];
30+
String author = args[2];
31+
DonjonsFile.add(name, author);
32+
sender.sendMessage(String.format(DonjonMessages.DONJON_ADDED, name));
33+
}
34+
return true;
35+
case "remove":
36+
if (args.length == 2) {
37+
String donjon = args[1];
38+
if (DonjonsFile.getDonjons("").contains(donjon)) {
39+
DonjonsFile.remove(donjon);
40+
sender.sendMessage(String.format(DonjonMessages.DONJON_REMOVED, donjon));
41+
} else {
42+
sender.sendMessage(DonjonMessages.DONJON_NOT_FOUND);
43+
}
44+
}
45+
return true;
46+
case "addstep":
47+
if (args.length == 3 && sender instanceof Player) {
48+
Player player = (Player) sender;
49+
String donjon = args[1];
50+
String name = args[2];
51+
if (DonjonsFile.getDonjons("").contains(donjon)) {
52+
DonjonsFile.addStep(donjon, name, player.getWorld().getName(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ());
53+
sender.sendMessage(String.format(DonjonMessages.STEP_ADDED, name, donjon));
54+
} else {
55+
sender.sendMessage(DonjonMessages.DONJON_NOT_FOUND);
56+
}
57+
}
58+
return true;
59+
case "removestep":
60+
if (args.length == 3) {
61+
String donjon = args[1];
62+
String step = args[2];
63+
if (DonjonsFile.getDonjons("").contains(donjon)) {
64+
if (DonjonsFile.getSteps(donjon, "").contains(step)) {
65+
DonjonsFile.removeStep(donjon, step);
66+
sender.sendMessage(String.format(DonjonMessages.STEP_REMOVED, step, donjon));
67+
} else {
68+
sender.sendMessage(DonjonMessages.STEP_NOT_FOUND);
69+
}
70+
} else {
71+
sender.sendMessage(DonjonMessages.DONJON_NOT_FOUND);
72+
}
73+
}
74+
return true;
75+
case "set":
76+
if (args.length == 3 || args.length == 4) {
77+
String target = args[1];
78+
String donjon = args[2];
79+
if (DonjonsFile.getDonjons("").contains(donjon)) {
80+
if (args.length == 4) {
81+
String step = args[3];
82+
if (DonjonsFile.getSteps(donjon, "").contains(step)) {
83+
String uuid = Bukkit.getPlayer(target).getUniqueId().toString();
84+
PlayerDataFile.addDonjonStep(uuid, donjon, step);
85+
sender.sendMessage(String.format(DonjonMessages.PLAYER_STEP_ADDED, step, donjon, target));
86+
} else {
87+
sender.sendMessage(DonjonMessages.STEP_NOT_FOUND);
88+
}
89+
} else {
90+
String uuid = Bukkit.getPlayer(target).getUniqueId().toString();
91+
PlayerDataFile.removeDonjonStep(uuid, donjon);
92+
sender.sendMessage(String.format(DonjonMessages.PLAYER_STEP_REMOVED, donjon, target));
93+
}
94+
} else {
95+
sender.sendMessage(DonjonMessages.DONJON_NOT_FOUND);
96+
}
97+
}
98+
return true;
99+
}
100+
}
101+
}
102+
return false;
103+
}
104+
105+
@Override
106+
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
107+
if (sender.hasPermission("pacdonjons.pacdonjon")) {
108+
switch (args.length) {
109+
case 1:
110+
List<String> ops = new ArrayList<String>();
111+
for (String op : OPERATIONS) {
112+
if (op.toLowerCase().startsWith(args[0].toLowerCase())) {
113+
ops.add(op);
114+
}
115+
}
116+
return ops;
117+
case 2:
118+
switch (args[0]) {
119+
default:
120+
return new ArrayList<String>();
121+
case "remove":
122+
case "addstep":
123+
case "removestep":
124+
return DonjonsFile.getDonjons(args[1]);
125+
case "set":
126+
return PlayerSuggestionProviders.getOnlinePlayers(args[1]);
127+
}
128+
case 3:
129+
switch (args[0]) {
130+
default:
131+
return new ArrayList<String>();
132+
case "add":
133+
return PlayerSuggestionProviders.getOnlinePlayers(args[2]);
134+
case "removestep":
135+
return DonjonsFile.getSteps(args[1], args[2]);
136+
case "set":
137+
return DonjonsFile.getDonjons(args[2]);
138+
}
139+
case 4:
140+
switch (args[0]) {
141+
default:
142+
return new ArrayList<String>();
143+
case "set":
144+
return DonjonsFile.getSteps(args[2], args[3]);
145+
}
146+
}
147+
}
148+
return new ArrayList<String>();
149+
}
150+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package fr.peaceandcube.pacdonjons.file;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Set;
9+
import java.util.logging.Level;
10+
11+
import org.bukkit.Bukkit;
12+
import org.bukkit.Location;
13+
import org.bukkit.World;
14+
import org.bukkit.configuration.ConfigurationSection;
15+
import org.bukkit.configuration.file.FileConfiguration;
16+
import org.bukkit.configuration.file.YamlConfiguration;
17+
import org.bukkit.entity.Player;
18+
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
19+
import org.bukkit.plugin.Plugin;
20+
21+
public class DonjonsFile {
22+
protected static File file;
23+
protected static FileConfiguration config;
24+
25+
public static void load(Plugin plugin, String name) {
26+
file = new File(plugin.getDataFolder(), name);
27+
28+
if (!file.exists()) {
29+
plugin.getDataFolder().mkdirs();
30+
try {
31+
Files.createFile(file.toPath());
32+
} catch (IOException e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
37+
config = YamlConfiguration.loadConfiguration(file);
38+
}
39+
40+
protected static void save() {
41+
try {
42+
saveToDisk();
43+
} catch (IOException ex) {
44+
ex.printStackTrace();
45+
Bukkit.getLogger().log(Level.SEVERE, "Unable to save data file!");
46+
}
47+
}
48+
49+
private static void saveToDisk() throws IOException {
50+
if (config != null && file != null) {
51+
config.save(file);
52+
}
53+
}
54+
55+
public static List<String> getDonjons(String prefix) {
56+
Set<String> donjonKeys = config.getKeys(false);
57+
if (!donjonKeys.isEmpty()) {
58+
List<String> donjons = new ArrayList<String>();
59+
for (String donjon : donjonKeys) {
60+
if (donjon.toLowerCase().startsWith(prefix.toLowerCase())) {
61+
donjons.add(donjon);
62+
}
63+
}
64+
return donjons;
65+
}
66+
return new ArrayList<String>();
67+
}
68+
69+
public static List<String> getSteps(String donjon, String prefix) {
70+
Set<String> stepKeys = config.getConfigurationSection(donjon).getConfigurationSection("steps").getKeys(false);
71+
if (!stepKeys.isEmpty()) {
72+
List<String> steps = new ArrayList<String>();
73+
for (String step : stepKeys) {
74+
if (step.toLowerCase().startsWith(prefix.toLowerCase())) {
75+
steps.add(step);
76+
}
77+
}
78+
return steps;
79+
}
80+
return new ArrayList<String>();
81+
}
82+
83+
public static void add(String name, String author) {
84+
ConfigurationSection section = config.createSection(name);
85+
section.set("author", author);
86+
section.createSection("steps");
87+
save();
88+
}
89+
90+
public static void remove(String donjon) {
91+
config.set(donjon, null);
92+
save();
93+
}
94+
95+
public static void addStep(String donjon, String name, String world, int x, int y, int z) {
96+
ConfigurationSection section = config.getConfigurationSection(donjon).getConfigurationSection("steps");
97+
ConfigurationSection stepSection = section.createSection(name);
98+
stepSection.set("world", world);
99+
stepSection.set("x", x);
100+
stepSection.set("y", y);
101+
stepSection.set("z", z);
102+
save();
103+
}
104+
105+
public static void removeStep(String donjon, String step) {
106+
ConfigurationSection section = config.getConfigurationSection(donjon).getConfigurationSection("steps");
107+
section.set(step, null);
108+
save();
109+
}
110+
111+
public static void teleportToDonjonStep(Player player, String donjon, String step) {
112+
ConfigurationSection section = config.getConfigurationSection(donjon).getConfigurationSection("steps").getConfigurationSection(step);
113+
World world = Bukkit.getWorld(section.getString("world"));
114+
int x = section.getInt("x");
115+
int y = section.getInt("y");
116+
int z = section.getInt("z");
117+
player.teleport(new Location(world, x, y, z), TeleportCause.COMMAND);
118+
}
119+
}

0 commit comments

Comments
 (0)