Fix serveral bugs

This commit is contained in:
Lennart ten Wolde 2015-07-08 14:17:26 +02:00
parent c1db49eab4
commit 9929579ac0
5 changed files with 50 additions and 25 deletions

View file

@ -17,13 +17,14 @@ public class CommandHelp extends Command {
MarriageCommandExecutor commandExecutor = ((MarriageBase) marriage).getCommandExecutor();
reply("Author: &alenis0012");
reply("Version: &a" + marriage.getPlugin().getDescription().getVersion());
reply("&2&m----------------&2< &a&lMarriage Command Help &2>&2&m----------------"); // Play around with the amount of dashes later
reply("&2&m----------&2< &a&lMarriage Command Help &2>&2&m----------"); // Play around with the amount of dashes later
for(Command command : commandExecutor.getSubCommands()) {
if(command.isHidden()) {
continue;
}
reply("&a/marry " + command.getAliases()[0] + " " + command.getUsage() + " &f- &7" + command.getDescription());
String alias = command instanceof CommandMarry ? "" : command.getAliases()[0] + " ";
reply("&a/marry" + alias + " " + command.getUsage() + " &f- &7" + command.getDescription());
}
reply("&2&m--------------------------------------------"); // Play around with the amount of dashes later

View file

@ -22,23 +22,28 @@ public class CommandMarry extends Command {
public void execute() {
Player target = getArgAsPlayer(-1);
if(target != null) {
MPlayer mPlayer = marriage.getMPlayer(player.getUniqueId());
if(!mPlayer.isMarried()) {
MPlayer mTarget = marriage.getMPlayer(target.getUniqueId());
if(!mTarget.isMarried()) {
if(mPlayer.isMarriageRequested(target.getUniqueId())) {
marriage.marry(mPlayer, mTarget);
broadcast(Message.MARRIED, player.getName(), target.getName());
} else {
mTarget.requestMarriage(player.getUniqueId());
target.sendMessage(ChatColor.translateAlternateColorCodes('&', String.format(Message.MARRIAGE_REQUESTED.toString(), player.getName(), player.getName())));
}
} else {
reply(Message.TARGET_ALREADY_MARRIED, getArg(-1));
}
} else {
reply(Message.ALREADY_MARRIED);
}
if(target.getName().equalsIgnoreCase(player.getName())) {
reply(Message.MARRY_SELF);
} else {
MPlayer mPlayer = marriage.getMPlayer(player.getUniqueId());
if(!mPlayer.isMarried()) {
MPlayer mTarget = marriage.getMPlayer(target.getUniqueId());
if(!mTarget.isMarried()) {
if(mPlayer.isMarriageRequested(target.getUniqueId())) {
marriage.marry(mPlayer, mTarget);
broadcast(Message.MARRIED, player.getName(), target.getName());
} else {
mTarget.requestMarriage(player.getUniqueId());
target.sendMessage(ChatColor.translateAlternateColorCodes('&', String.format(Message.MARRIAGE_REQUESTED.toString(), player.getName(), player.getName())));
reply(Message.REQUEST_SENT, target.getName());
}
} else {
reply(Message.TARGET_ALREADY_MARRIED, getArg(-1));
}
} else {
reply(Message.ALREADY_MARRIED);
}
}
} else {
reply(Message.PLAYER_NOT_FOUND, getArg(-1));
}

View file

@ -9,6 +9,7 @@ public enum Message {
ALREADY_MARRIED("&cYou are already married to someone!"),
MARRIED("&a&lPlayer %s and %s have just married!"),
MARRIAGE_REQUESTED("&aPlayer %s has requested you to marry with them, use &e/marry %s &ato accept it."),
REQUEST_SENT("&aYou have proposed to %s!"),
NOT_MARRIED("&cYou are currently not married with someone!"),
DIVORCED("&aPlayer %s and %s have divorced!"),
HOME_TELEPORT("&aYou have been teleported to your marriage home!"),
@ -22,6 +23,7 @@ public enum Message {
INVALID_FORMAT("&cThe argument could not be parsed to an integer!"),
INVALID_GENDER("&cThe argument could not be parsed to a gender!"),
GENDER_SET("&aYour gender has been set to %s!"),
MARRY_SELF("&cYou cannot marry yourself!"),
NEGATIVE_NUMBER("&cYou must enter a positive number!");
private final String defaultMessage;

View file

@ -4,6 +4,8 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.lenis0012.bukkit.marriage2.MData;
import com.lenis0012.bukkit.marriage2.internal.data.DataConverter;
@ -20,6 +22,7 @@ import com.lenis0012.bukkit.marriage2.misc.ListQuery;
public class MarriageCore extends MarriageBase {
private final Map<UUID, MarriagePlayer> players = Collections.synchronizedMap(new HashMap<UUID, MarriagePlayer>());
private final Lock dbLock = new ReentrantLock();
private DataManager dataManager;
public MarriageCore(MarriagePlugin plugin) {
@ -70,7 +73,9 @@ public class MarriageCore extends MarriageBase {
public MPlayer getMPlayer(UUID uuid) {
MarriagePlayer player = players.get(uuid);
if(player == null) {
dbLock.lock();
player = dataManager.loadPlayer(uuid);
dbLock.unlock();
players.put(uuid, player);
}

View file

@ -13,11 +13,14 @@ import java.util.UUID;
import java.util.logging.Level;
import com.google.common.collect.Lists;
import com.lenis0012.bukkit.marriage2.MPlayer;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import com.lenis0012.bukkit.marriage2.MData;
import com.lenis0012.bukkit.marriage2.internal.MarriageCore;
import com.lenis0012.bukkit.marriage2.misc.ListQuery;
import org.bukkit.entity.Player;
public class DataManager {
private final MarriageCore core;
@ -63,7 +66,8 @@ public class DataManager {
+ "home_z DOUBLE,"
+ "home_yaw FLOAT,"
+ "home_pitch FLOAT,"
+ "pvp_enabled BIT);", prefix));
+ "pvp_enabled BIT,"
+ "PRIMARY KEY(id));", prefix));
} catch (SQLException e) {
core.getLogger().log(Level.WARNING, "Failed to load player data", e);
} finally {
@ -132,7 +136,7 @@ public class DataManager {
if(mdata.getId() >= 0 || mdata.isSaved()) {
// Update existing entry
ps = connection.prepareStatement(String.format(
"UPDATE %sdata SET player1=?,player2=?,home_word=?,home_x=?,home_y=?,home_z=?,home_yaw=?,home_pitch=?,pvp_enabled=? WHERE id=?;", prefix));
"UPDATE %sdata SET player1=?,player2=?,home_world=?,home_x=?,home_y=?,home_z=?,home_yaw=?,home_pitch=?,pvp_enabled=? WHERE id=?;", prefix));
mdata.save(ps);
ps.setInt(10, mdata.getId());
ps.executeUpdate();
@ -163,7 +167,15 @@ public class DataManager {
ps.setString(1, player.getUniqueId().toString());
ResultSet result = ps.executeQuery();
while(result.next()) {
player.addMarriage(new MarriageData(result));
UUID partnerId = UUID.fromString(result.getString(alt ? "player1" : "player2"));
Player partner = Bukkit.getPlayer(partnerId);
if(partner != null && partner.isOnline()) {
// Copy marriage data from partner to ensure a match.
MPlayer mpartner = core.getMPlayer(partnerId);
player.addMarriage((MarriageData) mpartner.getMarriage());
} else {
player.addMarriage(new MarriageData(result));
}
}
if(!alt) {
@ -175,10 +187,10 @@ public class DataManager {
Connection connection = newConnection();
try {
// Count rows to get amount of pages
PreparedStatement ps = connection.prepareStatement("SELECT COUNT(*) AS COUNT FROM " + prefix + "data;");
PreparedStatement ps = connection.prepareStatement("SELECT COUNT(*) FROM " + prefix + "data;");
ResultSet result = ps.executeQuery();
result.next();
int pages = (int) Math.ceil(result.getInt("COUNT") / (double) scale);
int pages = (int) Math.ceil(result.getInt(1) / (double) scale);
// Fetch te right page
ps = connection.prepareStatement(String.format(