Skip to content

Commit c44103f

Browse files
author
Paul2708
committed
Code cleanup and rewrote
1 parent d6485e5 commit c44103f

18 files changed

Lines changed: 550 additions & 598 deletions

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
###############
2+
## Maven ##
3+
###############
4+
5+
target/
6+
javadoc/
7+
8+
###############
9+
## IntelliJ ##
10+
###############
11+
12+
.idea/
13+
/*.iml
14+
*.iml
15+
*.iws
16+
17+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
18+
hs_err_pid*

src/main/java/de/paul2708/memory/Memory.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package de.paul2708.memory;
22

3+
import de.paul2708.memory.file.MessageFile;
34
import de.paul2708.memory.game.GameManager;
4-
import de.paul2708.memory.game.Queue;
55
import de.paul2708.memory.listener.*;
66
import org.bukkit.Bukkit;
77
import org.bukkit.plugin.PluginManager;
@@ -19,31 +19,39 @@ public void onLoad() {
1919

2020
@Override
2121
public void onEnable() {
22-
registerCommands();
23-
registerListener();
22+
// Config
23+
Memory.messageFile = new MessageFile(getDataFolder());
24+
Memory.messageFile.load();
25+
26+
// Game manager
27+
Memory.gameManager = new GameManager();
2428

25-
Memory.queue = new Queue();
26-
GameManager.getInstance().initializeThemes();
29+
registerListener();
2730
}
2831

2932
@Override
3033
public void onDisable() {
31-
super.onDisable();
34+
3235
}
3336

3437
private static Memory instance;
35-
private static Queue queue;
38+
private static MessageFile messageFile;
39+
private static GameManager gameManager;
3640

3741
public static Memory getInstance() {
3842
return instance;
3943
}
4044

41-
public static Queue getQueue() {
42-
return queue;
45+
public static MessageFile getMessageFile() {
46+
return messageFile;
4347
}
4448

45-
private void registerCommands() {
49+
public static GameManager getGameManager() {
50+
return gameManager;
51+
}
4652

53+
public void log(String message) {
54+
Bukkit.getConsoleSender().sendMessage(message);
4755
}
4856

4957
private void registerListener() {
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package de.paul2708.memory.file;
2+
3+
import de.paul2708.memory.Memory;
4+
import de.paul2708.memory.util.Constants;
5+
import org.bukkit.ChatColor;
6+
import org.bukkit.configuration.file.YamlConfiguration;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
11+
/**
12+
* Created by Paul on 28.07.2017.
13+
*/
14+
public class MessageFile {
15+
16+
private File directory;
17+
private File configFile;
18+
private YamlConfiguration configuration;
19+
20+
private boolean first;
21+
22+
public MessageFile(File directory) {
23+
this.directory = directory;
24+
25+
this.first = false;
26+
}
27+
28+
public void load() {
29+
try {
30+
// Create directory
31+
if (!directory.exists()) {
32+
directory.mkdir();
33+
}
34+
35+
// Create file
36+
this.configFile = new File(directory.getPath(), "messages.yml");
37+
if (!configFile.exists()) {
38+
configFile.createNewFile();
39+
this.first = true;
40+
41+
Memory.getInstance().log(Constants.TAG + "§amessages.yml was created. §cEdit and restart your server.");
42+
}
43+
44+
// Load configuration
45+
this.configuration = YamlConfiguration.loadConfiguration(configFile);
46+
47+
// Create default value
48+
if (first) {
49+
createDefaultValues();
50+
}
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
56+
private void createDefaultValues() {
57+
configuration.set("tag", "&8[&eMemory&8]");
58+
59+
configuration.set("game.against", "%tag% &7You are playing against &e%player%&7.");
60+
configuration.set("game.first_turn", "%tag% &e%player% &7starts. (Theme: %theme%))");
61+
configuration.set("game.turn", "%tag% &6%player% goes on.");
62+
configuration.set("game.not_your_turn", "%tag% &cIt's not your turn.");
63+
configuration.set("game.pair_found", "%tag% &e%player% found a pair..");
64+
configuration.set("game.no_pair_found", "%tag% &7%player% didn't find a pair.");
65+
configuration.set("game.again", "%tag% &6%player% can take another card.");
66+
configuration.set("game.again_player", "%tag% &6You can take another card.");
67+
68+
configuration.set("queue.already_in", "%tag% &cYour are already in the queue.");
69+
configuration.set("queue.added", "%tag% &aYou joined the queue. &7Waiting for player..");
70+
71+
configuration.set("result.draw", "%tag% &eDraw! - Nobody won.");
72+
configuration.set("result.win", "%tag% &e%player% has won the game. (%score% pairs)");
73+
74+
try {
75+
configuration.save(configFile);
76+
} catch (IOException e) {
77+
e.printStackTrace();
78+
}
79+
}
80+
81+
public String getMessage(String path, String... replace) {
82+
String message = configuration.getString(path);
83+
84+
message = message.replaceAll("%tag%", configuration.getString("tag"));
85+
86+
if (replace.length != 0) {
87+
if (message.contains("%player%")) {
88+
message = message.replaceAll("%player%", replace[0]);
89+
}
90+
if (message.contains("%theme%")) {
91+
message = message.replaceAll("%theme%", replace[1]);
92+
}
93+
if (message.contains("%score%")) {
94+
message = message.replaceAll("%score%", replace[1]);
95+
}
96+
}
97+
98+
message = ChatColor.translateAlternateColorCodes('&', message);
99+
100+
return message;
101+
}
102+
}

src/main/java/de/paul2708/memory/game/Card.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package de.paul2708.memory.game;
22

3-
import de.paul2708.memory.util.ItemManager;
3+
import de.paul2708.memory.util.Util;
44
import org.bukkit.inventory.ItemStack;
55

66
/**
@@ -16,8 +16,8 @@ public Card(int slot, ItemStack item) {
1616
this.item = item;
1717
}
1818

19-
public boolean isPair(Card card) {
20-
return ItemManager.isSame(item, card.getItem());
19+
public void setSlot(int slot) {
20+
this.slot = slot;
2121
}
2222

2323
public int getSlot() {
@@ -27,4 +27,15 @@ public int getSlot() {
2727
public ItemStack getItem() {
2828
return item;
2929
}
30+
31+
@Override
32+
public boolean equals(Object obj) {
33+
if (obj instanceof Card) {
34+
Card card = (Card) obj;
35+
36+
return Util.isSame(item, card.getItem());
37+
}
38+
39+
return false;
40+
}
3041
}

0 commit comments

Comments
 (0)