Heavy UI and Command progress.

Time: 4hrs
This commit is contained in:
TadahTech 2016-06-05 22:55:06 -05:00
parent a9c086172f
commit c577e5827c
12 changed files with 509 additions and 24 deletions

View File

@ -3,11 +3,18 @@ package mineplex.core.party.command;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.party.Party;
import mineplex.core.party.PartyManager;
import mineplex.core.party.manager.PartyInviteManager;
import mineplex.core.party.ui.menus.PartyMainMenu;
import mineplex.core.party.ui.menus.PartyOwnerMenu;
import mineplex.core.party.ui.menus.PartyViewMenu;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.UUID;
/**
* Command handler for party commands
*/
@ -37,7 +44,18 @@ public class PartyCommand extends CommandBase<PartyManager>
{
if (args.length == 0)
{
new PartyOwnerMenu(new Party(caller.getName()), Plugin).open(caller);
Party party = Plugin.getParty(caller);
if(party == null)
{
new PartyMainMenu(Plugin).open(caller);
return;
}
if(party.getOwner().equalsIgnoreCase(caller.getName()))
{
new PartyOwnerMenu(party, Plugin).open(caller);
return;
}
new PartyViewMenu(party, Plugin).open(caller);
//Show UI
return;
}
@ -89,27 +107,107 @@ public class PartyCommand extends CommandBase<PartyManager>
private void handleInvite(Player caller, String target)
{
Player possible = Bukkit.getPlayer(target);
if(possible != null)
{
UUID uuid = possible.getUniqueId();
caller.sendMessage(F.main("Party", "Sent an invite to " + target + "..."));
Plugin.getInviteManager().inviteTo(uuid, caller.getName(), caller.getName());
Plugin.getInviteManager().sendAcceptOrDeny(possible, caller.getName());
return;
}
Plugin.getRedisManager().findAndInvite(target, caller.getName());
}
private void handleInviteResponse(Player caller, String target, boolean accept)
{
PartyInviteManager inviteManager = Plugin.getInviteManager();
if(!inviteManager.isInvitedTo(caller.getUniqueId(), target))
{
//He isn't invited to this party.
caller.sendMessage(F.main("Party", "You do not have an invitation to " + F.elem(target)));
return;
}
inviteManager.respondToInvite(caller, target, accept);
}
private void handleForceRemove(Player caller, String target)
{
Party party = Plugin.getParty(caller);
if(party == null) {
caller.sendMessage(F.main("Party", "You are not in a party! Do /party help to get started."));
return;
}
if(!party.getOwner().equalsIgnoreCase(caller.getName()))
{
caller.sendMessage(F.main("Party", "You must be the leader of the party to do that!"));
return;
}
Player playerTarget = Bukkit.getPlayer(target);
if(playerTarget == null)
{
caller.sendMessage(F.main("Party", target + " is not online, and is not in your party!"));
return;
}
Party targetParty = Plugin.getParty(playerTarget);
if(targetParty == null || party.getMembers().contains(target))
{
caller.sendMessage(F.main("Party", target + " is not a member of your party."));
return;
}
Plugin.removeFromParty(playerTarget.getUniqueId());
party.getMembers().remove(target);
targetParty.sendMessage(F.main("Party", "You have been removed from " + party.getName() + "."));
party.sendMessage(F.main("Party", caller.getName() + " has removed " + target + " from the party."));
}
private void handleLeave(Player caller)
{
Party party = Plugin.getParty(caller);
if(party == null) {
caller.sendMessage(F.main("Party", "You are not in a party! Do /party help to get started."));
return;
}
party.getMembers().remove(caller.getName());
Plugin.removeFromParty(caller.getUniqueId());
caller.sendMessage(F.main("Party", "You have left " + party.getOwner() + "'s party"));
party.sendMessage(F.main("Party", caller.getName() + " has left the party."));
}
private void handleDisband(Player caller)
{
Party party = Plugin.getParty(caller);
if(party == null) {
caller.sendMessage(F.main("Party", "You are not in a party! Do /party help to get started."));
return;
}
if(!party.getOwner().equalsIgnoreCase(caller.getName()))
{
caller.sendMessage(F.main("Party", "You must be the leader of the party to do that!"));
return;
}
party.sendMessage(F.main("Party", caller.getName() + " has disbanded the party!"));
party.getMembers().stream().map(Bukkit::getPlayer).forEach(player -> Plugin.removeFromParty(player.getUniqueId()));
party.getMembers().clear();
party.getInvites().clear();
Plugin.removeParty(party);
}
}

View File

@ -6,6 +6,7 @@ import mineplex.core.common.util.F;
import mineplex.core.party.InviteData;
import mineplex.core.party.Party;
import mineplex.core.party.PartyManager;
import mineplex.core.party.redis.RedisMessageType;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ClickEvent.Action;
@ -25,7 +26,8 @@ import java.util.UUID;
public class PartyInviteManager
{
public enum InviteResponse {
public enum InviteResponse
{
ACCEPTED("{0} has joined the party!"),
DENIED("{0} has declined joining your party."),
@ -49,6 +51,7 @@ public class PartyInviteManager
private final Map<UUID, List<InviteData>> _activeInvites = Maps.newHashMap();
private final Map<UUID, String> _invitedBy = Maps.newHashMap();
private final Map<UUID, String> _awaitingJoin = Maps.newHashMap();
private final Map<UUID, String> _players = Maps.newHashMap();
private final PartyManager _plugin;
private final PartyRedisManager _partyRedisManager;
@ -59,9 +62,16 @@ public class PartyInviteManager
_partyRedisManager = partyRedisManager;
}
public void invite(String sender, UUID targetUUID)
public void respondToInvite(Player player, String party, boolean accept)
{
addToInvite(targetUUID, sender, sender, System.currentTimeMillis());
remove(party, player.getUniqueId());
_players.remove(player.getUniqueId());
String serverFrom = INVITE_FROM_SERVERS.remove(player.getName());
_partyRedisManager.publish(serverFrom, RedisMessageType.INVITE_PLAYER_RESPONSE, party,
player.getName(), player.getUniqueId().toString(), accept ? InviteResponse.ACCEPTED.name() : InviteResponse.DENIED.name());
}
public void handleInviteRequest(String sender, String target, String serverFrom)
@ -72,7 +82,8 @@ public class PartyInviteManager
//Shouldn't happen, as a "findPLayer" packet will be sent out first.
return;
}
INVITE_FROM_SERVERS.put(sender, serverFrom);
_players.put(player.getUniqueId(), player.getName());
INVITE_FROM_SERVERS.put(target, serverFrom);
inviteTo(player.getUniqueId(), sender, sender);
sendAcceptOrDeny(player, sender);
}
@ -81,7 +92,7 @@ public class PartyInviteManager
{
remove(sender, targetUUID);
Player player = Bukkit.getPlayer(sender);
if(player == null)
if (player == null)
{
return;
}
@ -91,7 +102,7 @@ public class PartyInviteManager
switch (response)
{
case ACCEPTED:
if(party == null)
if (party == null)
{
party = new Party(sender);
_plugin.addParty(party);
@ -103,7 +114,7 @@ public class PartyInviteManager
break;
case EXPIRED:
case DENIED:
if(party == null)
if (party == null)
{
player.sendMessage(message);
player.sendMessage(F.main("Party", "Party not created."));
@ -114,6 +125,17 @@ public class PartyInviteManager
}
}
/**
* Get the name of the server that this request was initiated from.
*
* @param player The player
* @return The server name
*/
public String getServerInvitedFrom(Player player)
{
return INVITE_FROM_SERVERS.get(player.getName());
}
/**
* Get the name of the party who is awaiting this player to join
*
@ -157,6 +179,14 @@ public class PartyInviteManager
return;
}
remove(party, player);
Player possible = Bukkit.getPlayer(player);
String playerName = _players.remove(player);
if (possible != null)
{
playerName = possible.getName();
possible.sendMessage(F.main("Party", "Your invite to " + party + " has expired"));
}
sendExpired(INVITE_FROM_SERVERS.remove(invitedBy), party, playerName, player);
}
}.runTaskLater(_plugin.getPlugin(), 20 * 60);
}
@ -210,6 +240,11 @@ public class PartyInviteManager
_activeInvites.put(invited, data);
}
private void sendExpired(String server, String party, String playerName, UUID player)
{
_plugin.getRedisManager().publish(server, RedisMessageType.INVITE_PLAYER_RESPONSE, party, playerName, player.toString(), InviteResponse.EXPIRED.name());
}
private void addToPendingJoin(UUID player, String party)
{
_awaitingJoin.put(player, party);
@ -229,8 +264,7 @@ public class PartyInviteManager
_activeInvites.put(invited, inviteDatas);
}
private void sendAcceptOrDeny(Player player, String arg)
public void sendAcceptOrDeny(Player player, String arg)
{
TextComponent textComponent = new TextComponent(F.main("Party", "Click one: "));

View File

@ -1,5 +1,7 @@
package mineplex.core.party.manager;
import com.google.common.collect.Maps;
import mineplex.core.common.util.F;
import mineplex.core.party.PartyManager;
import mineplex.core.party.manager.PartyInviteManager.InviteResponse;
import mineplex.core.party.manager.PartyJoinManager.JoinResponseReason;
@ -7,9 +9,12 @@ import mineplex.core.party.redis.PartyRedisListener;
import mineplex.core.party.redis.RedisMessageType;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.Map;
import java.util.UUID;
/**
@ -18,12 +23,14 @@ import java.util.UUID;
public class PartyRedisManager
{
protected static final String CHANNEL_BASE = "party-pubsub";
protected static final String FIND_PLAYERS_CHANNEL = "party-player-finder";
private final Map<String, BukkitTask> TASKS = Maps.newHashMap();
private final PartyManager _plugin;
private final PartyInviteManager _inviteManager;
private JedisPool _writePool;
private String _channel;
private String _serverName;
private final JedisPool _writePool;
private final String _channel;
private final String _serverName;
public PartyRedisManager(PartyManager plugin, String serverName, JedisPool writePool, JedisPool readPool)
{
@ -37,6 +44,7 @@ public class PartyRedisManager
try (Jedis jedis = readPool.getResource())
{
jedis.subscribe(new PartyRedisListener(this), _channel);
jedis.subscribe(new PartyRedisListener(this), FIND_PLAYERS_CHANNEL);
}
});
}
@ -66,33 +74,70 @@ public class PartyRedisManager
case INVITE_PLAYER_REQUEST:
_inviteManager.handleInviteRequest(second, third, first);
break;
case INVITE_PLAYER_RESPONSE:
_inviteManager.handleInviteResponse(first, second, UUID.fromString(third), InviteResponse.valueOf(contents[3].toUpperCase()));
break;
case PLAYER_FIND_REQUEST:
Player player = Bukkit.getPlayer(second);
if(player == null)
{
return;
}
publish(first, RedisMessageType.PLAYER_FIND_RESPONSE,
_serverName, player.getName(), player.getUniqueId().toString(), second);
break;
case PLAYER_FIND_RESPONSE:
UUID uuid = UUID.fromString(third);
_inviteManager.invite(contents[3], uuid);
BukkitTask task = TASKS.remove(second);
if(task != null)
{
task.cancel();
}
_inviteManager.inviteTo(uuid, contents[3], contents[3]);
publish(first, RedisMessageType.INVITE_PLAYER_REQUEST, _serverName, contents[3], second);
break;
case PREJOIN_SERVER_REQUEST:
_plugin.getJoinManager().handleJoinRequest(first, Integer.valueOf(third), second, Boolean.valueOf(contents[3]));
break;
case PREJOIN_SERVER_RESPONSE:
_plugin.getJoinManager().handleJoinResponse(first, second, contents[3], JoinResponseReason.valueOf(third.toUpperCase()));
break;
}
}
public void findAndInvite(String player, String sender)
{
TASKS.put(player, new BukkitRunnable()
{
@Override
public void run()
{
Player senderPlayer = Bukkit.getPlayer(sender);
if(senderPlayer == null)
{
return;
}
senderPlayer.sendMessage(F.main("Party", "Could not locate " + F.elem(player)));
TASKS.remove(player);
}
}.runTaskLater(_plugin.getPlugin(), 20L * 4));
_plugin.runAsync(() -> {
try (Jedis jedis = _writePool.getResource())
{
jedis.publish(FIND_PLAYERS_CHANNEL, RedisMessageType.PLAYER_FIND_REQUEST.format(_serverName, player, sender));
}
});
}
}

View File

@ -2,6 +2,7 @@ package mineplex.core.party.ui;
import mineplex.core.party.PartyManager;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
/**
@ -24,7 +25,7 @@ public abstract class Button
*
* @param player The player who clicked
*/
public abstract void onClick(Player player);
public abstract void onClick(Player player, ClickType clickType);
public ItemStack getItemStack()
{

View File

@ -3,6 +3,7 @@ package mineplex.core.party.ui.button;
import mineplex.core.party.PartyManager;
import mineplex.core.party.ui.Button;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
/**
@ -18,7 +19,7 @@ public class IconButton extends Button
}
@Override
public void onClick(Player player)
public void onClick(Player player, ClickType clickType)
{
}

View File

@ -8,6 +8,7 @@ import mineplex.core.party.ui.Button;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
/**
@ -41,7 +42,7 @@ public class PartyMemberIcon extends Button
}
@Override
public void onClick(Player player)
public void onClick(Player player, ClickType clickType)
{
}

View File

@ -1,8 +1,34 @@
package mineplex.core.party.ui.button.tools;
import mineplex.core.common.util.C;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.party.Party;
import mineplex.core.party.PartyManager;
import mineplex.core.party.ui.menus.InvitePlayerMenu;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
/**
*
*/
public class AddPlayerButton
public class AddPlayerButton extends PartyButton
{
private static final ItemStack ITEM = new ItemBuilder(Material.SIGN)
.setTitle(C.cYellow + "Invite a Player")
.build();
public AddPlayerButton(PartyManager plugin, Party party)
{
super(ITEM, party, plugin);
}
@Override
public void onClick(Player player, ClickType clickType)
{
new InvitePlayerMenu(getPlugin(), player, getParty()).openInventory();
}
}

View File

@ -0,0 +1,34 @@
package mineplex.core.party.ui.button.tools;
import mineplex.core.common.util.C;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.party.PartyManager;
import mineplex.core.party.ui.Button;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
/**
*
*/
public class LeavePartyButton extends Button
{
private static final ItemStack ITEM = new ItemBuilder(Material.STAINED_GLASS_PANE)
.setData((byte) 14)
.setTitle(C.cYellow + "Invite a Player")
.build();
public LeavePartyButton(PartyManager plugin)
{
super(ITEM, plugin);
}
@Override
public void onClick(Player player, ClickType clickType)
{
player.performCommand("/party leave");
}
}

View File

@ -0,0 +1,26 @@
package mineplex.core.party.ui.button.tools;
import mineplex.core.party.Party;
import mineplex.core.party.PartyManager;
import mineplex.core.party.ui.Button;
import org.bukkit.inventory.ItemStack;
/**
*
*/
public abstract class PartyButton extends Button
{
private Party _party;
public PartyButton(ItemStack itemStack, Party party, PartyManager plugin)
{
super(itemStack, plugin);
_party = party;
}
public Party getParty()
{
return _party;
}
}

View File

@ -0,0 +1,178 @@
package mineplex.core.party.ui.menus;
import mineplex.core.common.util.C;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.party.Party;
import mineplex.core.party.PartyManager;
import net.minecraft.server.v1_8_R3.*;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
/**
*
*/
public class InvitePlayerMenu implements Listener
{
private PartyManager _partyManager;
private Player _player;
private Inventory _currentInventory;
private String _itemName = "";
private boolean _searching;
private Party _party;
public InvitePlayerMenu(PartyManager partyManager, Player player, Party party)
{
_player = player;
_partyManager = partyManager;
_party = party;
openInventory();
_partyManager.registerEvents(this);
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event)
{
if (event.getPlayer() == _player)
{
unregisterListener();
}
}
public void unregisterListener()
{
_currentInventory.clear();
HandlerList.unregisterAll(this);
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event)
{
if (event.getPlayer() == _player)
{
unregisterListener();
}
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event)
{
if (event.getRawSlot() < 3)
{
event.setCancelled(true);
if (event.getRawSlot() == 2)
{
if (_itemName.length() > 1 && !_searching)
{
final String name = _itemName;
_partyManager.getClientManager().checkPlayerName(_player, _itemName, result -> {
_searching = false;
if (result != null)
{
_partyManager.getRedisManager().findAndInvite(name, _player.getName());
_player.playSound(_player.getLocation(), Sound.NOTE_PLING, 1, 1.6f);
unregisterListener();
_player.closeInventory();
new PartyOwnerMenu(_party, _partyManager).open(_player);
} else
{
_currentInventory.setItem(2, new ItemBuilder(Material.PAPER)
.setTitle(C.cYellow + "0" + C.cGray + " matches for [" + C.cYellow + name + C.cGray + "]")
.build());
_player.playSound(_player.getLocation(), Sound.ITEM_BREAK, 1, .6f);
}
});
} else
{
_player.playSound(_player.getLocation(), Sound.ITEM_BREAK, 1, .6f);
}
}
} else if (event.isShiftClick())
{
event.setCancelled(true);
}
}
public void openInventory()
{
_player.closeInventory();
EntityPlayer p = ((CraftPlayer) _player).getHandle();
AnvilContainer container = new AnvilContainer(p);
int c = p.nextContainerCounter();
PacketPlayOutOpenWindow packet = new PacketPlayOutOpenWindow(c, "minecraft:anvil", new ChatMessage(Blocks.ANVIL.a() + ".name", new Object[0]));
UtilPlayer.sendPacket(_player, packet);
// Set their active container to the container
p.activeContainer = container;
// Set their active container window id to that counter stuff
p.activeContainer.windowId = c;
// Add the slot listener
p.activeContainer.addSlotListener(p); // Set the items to the items from the inventory given
_currentInventory = container.getBukkitView().getTopInventory();
_currentInventory.setItem(0, new ItemBuilder(Material.PAPER).setRawTitle("Players's Name").build());
_currentInventory.setItem(2, new ItemBuilder(Material.PAPER).setRawTitle("Search").build());
}
private class AnvilContainer extends ContainerAnvil
{
private String n;
public AnvilContainer(EntityHuman entity)
{
super(entity.inventory, entity.world, new BlockPosition(0, 0, 0), entity);
}
@Override
public boolean a(EntityHuman entityhuman)
{
return true;
}
@Override
public void a(String origString)
{
n = origString;
_itemName = origString;
if (getSlot(2).hasItem())
{
net.minecraft.server.v1_8_R3.ItemStack itemstack = getSlot(2).getItem();
if (StringUtils.isBlank(origString))
{
itemstack.r();
} else
{
itemstack.c(this.n);
}
}
e();
}
}
}

View File

@ -5,6 +5,7 @@ import mineplex.core.party.PartyManager;
import mineplex.core.party.ui.Button;
import mineplex.core.party.ui.Menu;
import mineplex.core.party.ui.button.PartyMemberIcon;
import mineplex.core.party.ui.button.tools.AddPlayerButton;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -19,7 +20,7 @@ public class PartyOwnerMenu extends Menu
public PartyOwnerMenu(Party party, PartyManager plugin)
{
super(party.getOwner() + "'s Party", plugin);
super("Manage Party", plugin);
_party = party;
}
@ -29,7 +30,7 @@ public class PartyOwnerMenu extends Menu
Button[] buttons = new Button[54];
//Tools
buttons[1] = create(new ItemStack(Material.SIGN));
buttons[1] = new AddPlayerButton(_plugin, _party);
buttons[4] = create(new ItemStack(Material.IRON_AXE));
buttons[7] = create(new ItemStack(Material.BARRIER));
buttons[46] = create(new ItemStack(Material.COMPASS));
@ -37,9 +38,9 @@ public class PartyOwnerMenu extends Menu
buttons[52] = create(new ItemStack(Material.BOOK_AND_QUILL));
//Players
for(int i = 18; i < 33; i++)
for(int i = 18; i < _party.getMembers().size(); i++)
{
buttons[i] = new PartyMemberIcon(player.getName(), _plugin);
buttons[i] = new PartyMemberIcon(_party.getMembers().get(i), _plugin);
}
return buttons;

View File

@ -0,0 +1,40 @@
package mineplex.core.party.ui.menus;
import mineplex.core.party.Party;
import mineplex.core.party.PartyManager;
import mineplex.core.party.ui.Button;
import mineplex.core.party.ui.Menu;
import mineplex.core.party.ui.button.PartyMemberIcon;
import mineplex.core.party.ui.button.tools.LeavePartyButton;
import org.bukkit.entity.Player;
/**
*
*/
public class PartyViewMenu extends Menu
{
private Party _party;
public PartyViewMenu(Party party, PartyManager plugin)
{
super(party.getName() + "'s Party", plugin);
_party = party;
}
@Override
protected Button[] setUp(Player player)
{
Button[] buttons = new Button[54];
//Tools
buttons[4] = new LeavePartyButton(_plugin);
//Players
for(int i = 18; i < _party.getMembers().size(); i++)
{
buttons[i] = new PartyMemberIcon(_party.getMembers().get(i), _plugin);
}
return buttons;
}
}