Read and write permaremoved to/from database, added PlayerBindAbilityEvent

This commit is contained in:
jayoevans 2019-10-23 19:05:11 +10:00
parent 6a5c963ec2
commit 29f6056b1c
6 changed files with 162 additions and 20 deletions

View file

@ -63,10 +63,18 @@ public class AbilityManager extends DatabaseModule<AbilityRepository>
public boolean bindAbility(Player player, String abilityName, int slot)
{
// TODO Call event, let MultiAbilityManager cancel
if (MultiAbilityManager.playerAbilities.containsKey(player))
PlayerBindAbilityEvent playerBindAbilityEvent = new PlayerBindAbilityEvent(player, abilityName);
getPlugin().getServer().getPluginManager().callEvent(playerBindAbilityEvent);
if (playerBindAbilityEvent.isCancelled())
{
GeneralMethods.sendBrandingMessage(player, ChatColor.RED + "You can't edit your binds right now!");
String cancelMessage = playerBindAbilityEvent.getCancelMessage();
if (cancelMessage != null)
{
GeneralMethods.sendBrandingMessage(player, cancelMessage);
}
return false;
}

View file

@ -0,0 +1,61 @@
package com.projectkorra.projectkorra.ability;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
public class PlayerBindAbilityEvent extends PlayerEvent implements Cancellable
{
private static final HandlerList HANDLER_LIST = new HandlerList();
private final String abilityName;
private boolean cancelled;
private String cancelMessage;
public PlayerBindAbilityEvent(Player player, String abilityName)
{
super(player);
this.abilityName = abilityName;
}
public String getAbilityName()
{
return this.abilityName;
}
@Override
public boolean isCancelled()
{
return cancelled;
}
@Override
public void setCancelled(boolean cancelled)
{
this.cancelled = cancelled;
}
public String getCancelMessage()
{
return this.cancelMessage;
}
public void setCancelMessage(String cancelMessage)
{
this.cancelMessage = cancelMessage;
}
@Override
public HandlerList getHandlers()
{
return HANDLER_LIST;
}
public static HandlerList getHandlerList()
{
return HANDLER_LIST;
}
}

View file

@ -6,9 +6,12 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.projectkorra.projectkorra.ability.PlayerBindAbilityEvent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitRunnable;
import com.projectkorra.projectkorra.BendingPlayer;
@ -17,7 +20,7 @@ import com.projectkorra.projectkorra.GeneralMethods;
import com.projectkorra.projectkorra.ProjectKorra;
import com.projectkorra.projectkorra.event.PlayerBindChangeEvent;
public class MultiAbilityManager {
public class MultiAbilityManager implements Listener {
public static Map<Player, String[]> playerAbilities = new ConcurrentHashMap<>();
public static Map<Player, Integer> playerSlot = new ConcurrentHashMap<>();
@ -34,6 +37,19 @@ public class MultiAbilityManager {
waterArms.add(new MultiAbilityInfoSub("Spear", Element.ICE));
multiAbilityList.add(new MultiAbilityInfo("WaterArms", waterArms));
manage();
// TODO Properly set this up as a Module
ProjectKorra.plugin.getServer().getPluginManager().registerEvents(this, ProjectKorra.plugin);
}
@EventHandler
public void onPlayerBindAbility(PlayerBindAbilityEvent event)
{
if (playerAbilities.containsKey(event.getPlayer()))
{
event.setCancelled(true);
event.setCancelMessage(ChatColor.RED + "You can't edit your binds right now!");
}
}
/**

View file

@ -35,7 +35,7 @@ public class BendingPlayer
private final String[] abilities;
private ChiAbility stance;
private boolean permanentlyRemoved;
private boolean bendingRemoved;
private boolean toggled;
private boolean tremorSense;
private boolean illumination;
@ -248,14 +248,14 @@ public class BendingPlayer
this.stance = stance;
}
public boolean isPermanentlyRemoved()
public boolean isBendingRemoved()
{
return this.permanentlyRemoved;
return this.bendingRemoved;
}
public void setPermanentlyRemoved(boolean permanentlyRemoved)
protected void setBendingRemoved(boolean bendingRemoved)
{
this.permanentlyRemoved = permanentlyRemoved;
this.bendingRemoved = bendingRemoved;
}
public boolean isToggled()

View file

@ -62,11 +62,46 @@ public class BendingPlayerManager extends DatabaseModule<BendingPlayerRepository
Player player = event.getPlayer();
BendingPlayer bendingPlayer = _players.get(player.getUniqueId());
String ability = event.getAbility();
String ability = bendingPlayer.getBoundAbilityName();
// TODO Check bound ability
if (ability != null && ability.equals(event.getAbility()))
{
GeneralMethods.displayMovePreview(player);
}
}
public void removeBending(Player player)
{
BendingPlayer bendingPlayer = _players.get(player.getUniqueId());
bendingPlayer.setBendingRemoved(true);
updateBendingRemoved(bendingPlayer);
}
public void returnBending(Player player)
{
BendingPlayer bendingPlayer = _players.get(player.getUniqueId());
bendingPlayer.setBendingRemoved(false);
updateBendingRemoved(bendingPlayer);
}
private void updateBendingRemoved(BendingPlayer bendingPlayer)
{
runAsync(() ->
{
try
{
getRepository().updateBendingRemoved(bendingPlayer);
}
catch (SQLException e)
{
e.printStackTrace();
}
});
}
private void loadBendingPlayer(Player player)
{

View file

@ -15,22 +15,26 @@ import java.util.UUID;
public class BendingPlayerRepository extends DatabaseRepository
{
private static final DatabaseQuery CREATE_TABLE_BENDING_PLAYERS = DatabaseQuery.newBuilder()
.mysql("CREATE TABLE IF NOT EXISTS pk_bending_players (player_id INTEGER PRIMARY KEY AUTO_INCREMENT, uuid BINARY(16) NOT NULL, player_name VARCHAR(16) NOT NULL, first_login BIGINT NOT NULL, INDEX uuid_index (uuid));")
.sqlite("CREATE TABLE IF NOT EXISTS pk_bending_players (player_id INTEGER PRIMARY KEY AUTOINCREMENT, uuid BINARY(16) NOT NULL, player_name VARCHAR(16) NOT NULL, first_login BIGINT NOT NULL); CREATE INDEX uuid_index ON pk_bending_players (uuid);")
.mysql("CREATE TABLE IF NOT EXISTS pk_bending_players (player_id INTEGER PRIMARY KEY AUTO_INCREMENT, uuid BINARY(16) NOT NULL, player_name VARCHAR(16) NOT NULL, first_login BIGINT NOT NULL, bending_removed BOOLEAN, INDEX uuid_index (uuid));")
.sqlite("CREATE TABLE IF NOT EXISTS pk_bending_players (player_id INTEGER PRIMARY KEY AUTOINCREMENT, uuid BINARY(16) NOT NULL, player_name VARCHAR(16) NOT NULL, first_login BIGINT NOT NULL, bending_removed BOOLEAN); CREATE INDEX uuid_index ON pk_bending_players (uuid);")
.build();
private static final DatabaseQuery SELECT_BENDING_PLAYER = DatabaseQuery.newBuilder()
.query("SELECT player_id, player_name, first_login FROM pk_bending_players WHERE uuid = ?;")
.query("SELECT player_id, player_name, first_login, bending_removed FROM pk_bending_players WHERE uuid = ?;")
.build();
private static final DatabaseQuery INSERT_BENDING_PLAYER = DatabaseQuery.newBuilder()
.query("INSERT INTO pk_bending_players (uuid, player_name, first_login) VALUES (?, ?, ?);")
.build();
private static final DatabaseQuery UPDATE_BENDING_PLAYER = DatabaseQuery.newBuilder()
private static final DatabaseQuery UPDATE_PLAYER_NAME = DatabaseQuery.newBuilder()
.query("UPDATE pk_bending_players SET player_name = ? WHERE player_id = ?;")
.build();
private static final DatabaseQuery UPDATE_BENDING_REMOVED = DatabaseQuery.newBuilder()
.query("UPDATE pk_bending_players SET bending_removed = ? WHERE player_id = ?;")
.build();
protected void createTables() throws SQLException
{
Connection connection = getDatabase().getConnection();
@ -62,13 +66,18 @@ public class BendingPlayerRepository extends DatabaseRepository
int playerId = rs.getInt("player_id");
String playerName = rs.getString("player_name");
long firstLogin = rs.getLong("first_login");
boolean bendingRemoved = rs.getBoolean("bending_removed");
if (!player.getName().equals(playerName))
{
updatePlayer(playerId, player.getName());
updatePlayerName(playerId, player.getName());
}
return new BendingPlayer(playerId, uuid, playerName, firstLogin);
BendingPlayer bendingPlayer = new BendingPlayer(playerId, uuid, playerName, firstLogin);
bendingPlayer.setBendingRemoved(bendingRemoved);
return bendingPlayer;
}
}
}
@ -99,11 +108,11 @@ public class BendingPlayerRepository extends DatabaseRepository
}
}
protected void updatePlayer(int playerId, String playerName) throws SQLException
protected void updatePlayerName(int playerId, String playerName) throws SQLException
{
Connection connection = getDatabase().getConnection();
try (PreparedStatement statement = connection.prepareStatement(UPDATE_BENDING_PLAYER.getQuery()))
try (PreparedStatement statement = connection.prepareStatement(UPDATE_PLAYER_NAME.getQuery()))
{
statement.setInt(1, playerId);
statement.setString(2, playerName);
@ -111,4 +120,17 @@ public class BendingPlayerRepository extends DatabaseRepository
statement.executeUpdate();
}
}
protected void updateBendingRemoved(BendingPlayer bendingPlayer) throws SQLException
{
Connection connection = getDatabase().getConnection();
try (PreparedStatement statement = connection.prepareStatement(UPDATE_BENDING_REMOVED.getQuery()))
{
statement.setInt(1, bendingPlayer.getId());
statement.setBoolean(2, bendingPlayer.isBendingRemoved());
statement.executeUpdate();
}
}
}