mirror of
https://gitlab.com/phoenix-dvpmt/mmocore.git
synced 2024-11-28 00:55:29 +01:00
!improved request code
This commit is contained in:
parent
06f1ab2ab0
commit
da4e3763cc
@ -6,32 +6,23 @@ import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import org.bukkit.Sound;
|
||||
|
||||
public class FriendRequest extends Request {
|
||||
private final PlayerData target;
|
||||
|
||||
public FriendRequest(PlayerData creator, PlayerData target) {
|
||||
super(creator);
|
||||
|
||||
this.target = target;
|
||||
super(creator, target);
|
||||
}
|
||||
|
||||
public PlayerData getTarget() {
|
||||
return target;
|
||||
@Override
|
||||
public void whenDenied() {
|
||||
getTarget().getPlayer().playSound(getTarget().getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1);
|
||||
}
|
||||
|
||||
public void deny() {
|
||||
MMOCore.plugin.requestManager.unregisterRequest(getUniqueId());
|
||||
if(!target.isOnline()) return;
|
||||
target.getPlayer().playSound(target.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1);
|
||||
}
|
||||
|
||||
public void accept() {
|
||||
@Override
|
||||
public void whenAccepted() {
|
||||
getCreator().setLastActivity(PlayerActivity.FRIEND_REQUEST, 0);
|
||||
getCreator().addFriend(target.getUniqueId());
|
||||
target.addFriend(getCreator().getUniqueId());
|
||||
if(target.isOnline() && getCreator().isOnline()) {
|
||||
MMOCore.plugin.configManager.getSimpleMessage("now-friends", "player", target.getPlayer().getName()).send(getCreator().getPlayer());
|
||||
MMOCore.plugin.configManager.getSimpleMessage("now-friends", "player", getCreator().getPlayer().getName()).send(target.getPlayer());
|
||||
}
|
||||
MMOCore.plugin.requestManager.unregisterRequest(getUniqueId());
|
||||
getCreator().addFriend(getTarget().getUniqueId());
|
||||
getTarget().addFriend(getCreator().getUniqueId());
|
||||
if (getCreator().isOnline()) {
|
||||
MMOCore.plugin.configManager.getSimpleMessage("now-friends", "player", getTarget().getPlayer().getName()).send(getCreator().getPlayer());
|
||||
MMOCore.plugin.configManager.getSimpleMessage("now-friends", "player", getCreator().getPlayer().getName()).send(getTarget().getPlayer());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,31 +1,55 @@
|
||||
package net.Indyuce.mmocore.api.player.social;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import net.Indyuce.mmocore.MMOCore;
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import org.apache.commons.lang.Validate;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public abstract class Request {
|
||||
private final UUID uuid = UUID.randomUUID();
|
||||
private final long date = System.currentTimeMillis();
|
||||
private final PlayerData creator;
|
||||
private final PlayerData creator, target;
|
||||
|
||||
public Request(PlayerData creator) {
|
||||
/**
|
||||
* Any request time out is by default 2 minutes.
|
||||
*/
|
||||
private static final long TIME_OUT = 1000 * 60 * 2;
|
||||
|
||||
public Request(PlayerData creator, PlayerData target) {
|
||||
this.creator = creator;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public PlayerData getCreator() {
|
||||
return creator;
|
||||
}
|
||||
|
||||
public PlayerData getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public UUID getUniqueId() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public boolean isTimedOut() {
|
||||
return date + 1000 * 60 * 2 < System.currentTimeMillis();
|
||||
return date + TIME_OUT < System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public abstract void accept();
|
||||
|
||||
public abstract void deny();
|
||||
public void accept() {
|
||||
Validate.isTrue(target.isOnline(), "Target must be online");
|
||||
whenAccepted();
|
||||
MMOCore.plugin.requestManager.unregisterRequest(getUniqueId());
|
||||
}
|
||||
|
||||
public abstract void whenAccepted();
|
||||
|
||||
public void deny() {
|
||||
Validate.isTrue(target.isOnline(), "Target must be online");
|
||||
whenDenied();
|
||||
MMOCore.plugin.requestManager.unregisterRequest(getUniqueId());
|
||||
}
|
||||
|
||||
public abstract void whenDenied();
|
||||
}
|
||||
|
@ -1,50 +1,44 @@
|
||||
package net.Indyuce.mmocore.guild.provided;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import net.Indyuce.mmocore.MMOCore;
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import net.Indyuce.mmocore.api.player.social.Request;
|
||||
import net.Indyuce.mmocore.manager.InventoryManager;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class GuildInvite extends Request {
|
||||
private final PlayerData target;
|
||||
private final Guild guild;
|
||||
|
||||
public GuildInvite(Guild guild, PlayerData creator, PlayerData target) {
|
||||
super(creator);
|
||||
super(creator, target);
|
||||
|
||||
this.guild = guild;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public Guild getGuild() {
|
||||
return guild;
|
||||
}
|
||||
|
||||
public PlayerData getPlayer() {
|
||||
return target;
|
||||
@Override
|
||||
public void whenDenied() {
|
||||
// Nothing
|
||||
}
|
||||
|
||||
public void deny() {
|
||||
MMOCore.plugin.requestManager.unregisterRequest(getUniqueId());
|
||||
}
|
||||
|
||||
public void accept() {
|
||||
@Override
|
||||
public void whenAccepted() {
|
||||
guild.removeLastInvite(getCreator().getPlayer());
|
||||
guild.getMembers().forEach(member -> {
|
||||
if (Bukkit.getPlayer(member) != null) {
|
||||
MMOCore.plugin.configManager.getSimpleMessage("guild-joined-other", "player",
|
||||
target.getPlayer().getName()).send(Bukkit.getPlayer(member));
|
||||
getTarget().getPlayer().getName()).send(Bukkit.getPlayer(member));
|
||||
|
||||
MMOCore.plugin.configManager.getSimpleMessage("guild-joined", "owner",
|
||||
Bukkit.getPlayer(guild.getOwner()).getName()).send(target.getPlayer());
|
||||
}}
|
||||
|
||||
Bukkit.getPlayer(guild.getOwner()).getName()).send(getTarget().getPlayer());
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
guild.addMember(target.getUniqueId());
|
||||
InventoryManager.GUILD_VIEW.newInventory(target).open();
|
||||
MMOCore.plugin.requestManager.unregisterRequest(getUniqueId());
|
||||
guild.addMember(getTarget().getUniqueId());
|
||||
InventoryManager.GUILD_VIEW.newInventory(getTarget()).open();
|
||||
}
|
||||
}
|
@ -1,47 +1,41 @@
|
||||
package net.Indyuce.mmocore.manager.social;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import net.Indyuce.mmocore.MMOCore;
|
||||
import net.Indyuce.mmocore.api.player.social.Request;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class RequestManager {
|
||||
private final Set<Request> requests = new HashSet<>();
|
||||
private final Map<UUID, Request> requests = new HashMap<>();
|
||||
|
||||
/*
|
||||
* flush friend requests every 5 minutes so there is no memory overleak
|
||||
/**
|
||||
* Flushes friend requests every 5 minutes so there is no memory overleak
|
||||
*/
|
||||
public RequestManager() {
|
||||
Bukkit.getScheduler().runTaskTimer(MMOCore.plugin, this::flushRequests, 60 * 20, 60 * 20 * 5);
|
||||
}
|
||||
|
||||
public Request getRequest(UUID uuid) {
|
||||
for (Request request : requests)
|
||||
if (request.getUniqueId().equals(uuid))
|
||||
return request;
|
||||
return null;
|
||||
return requests.get(uuid);
|
||||
}
|
||||
|
||||
public void registerRequest(Request request) {
|
||||
requests.add(request);
|
||||
requests.put(request.getUniqueId(), request);
|
||||
}
|
||||
|
||||
public void unregisterRequest(UUID uuid) {
|
||||
for (Iterator<Request> iterator = requests.iterator(); iterator.hasNext();) {
|
||||
Request next = iterator.next();
|
||||
if (next.getUniqueId().equals(uuid)) {
|
||||
iterator.remove();
|
||||
break;
|
||||
}
|
||||
}
|
||||
requests.remove(uuid);
|
||||
}
|
||||
|
||||
public void flushRequests() {
|
||||
requests.removeIf(Request::isTimedOut);
|
||||
private void flushRequests() {
|
||||
for (Iterator<Request> iterator = requests.values().iterator(); iterator.hasNext(); ) {
|
||||
Request next = iterator.next();
|
||||
if (next.isTimedOut())
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -145,7 +145,6 @@ public class Party implements AbstractParty {
|
||||
public void sendInvite(PlayerData inviter, PlayerData target) {
|
||||
invites.put(target.getUniqueId(), System.currentTimeMillis());
|
||||
Request request = new PartyInvite(this, inviter, target);
|
||||
if (inviter.isOnline() && target.isOnline())
|
||||
new ConfigMessage("party-invite").addPlaceholders("player", inviter.getPlayer().getName(), "uuid", request.getUniqueId().toString())
|
||||
.sendAsJSon(target.getPlayer());
|
||||
MMOCore.plugin.requestManager.registerRequest(request);
|
||||
|
@ -6,43 +6,38 @@ import net.Indyuce.mmocore.api.player.social.Request;
|
||||
import net.Indyuce.mmocore.manager.InventoryManager;
|
||||
|
||||
public class PartyInvite extends Request {
|
||||
private final PlayerData target;
|
||||
private final Party party;
|
||||
|
||||
public PartyInvite(Party party, PlayerData creator, PlayerData target) {
|
||||
super(creator);
|
||||
super(creator, target);
|
||||
|
||||
this.party = party;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public Party getParty() {
|
||||
return party;
|
||||
}
|
||||
|
||||
public PlayerData getPlayer() {
|
||||
return target;
|
||||
@Override
|
||||
public void whenDenied() {
|
||||
// Nothing
|
||||
}
|
||||
|
||||
public void deny() {
|
||||
MMOCore.plugin.requestManager.unregisterRequest(getUniqueId());
|
||||
}
|
||||
|
||||
public void accept() {
|
||||
@Override
|
||||
public void whenAccepted() {
|
||||
if (party.getMembers().size() >= Math.max(2, MMOCore.plugin.getConfig().getInt("party.max-players", 8))) {
|
||||
MMOCore.plugin.configManager.getSimpleMessage("party-is-full").send(target.getPlayer());
|
||||
MMOCore.plugin.configManager.getSimpleMessage("party-is-full").send(getTarget().getPlayer());
|
||||
return;
|
||||
}
|
||||
if (getCreator().isOnline())
|
||||
party.removeLastInvite(getCreator().getPlayer());
|
||||
party.getMembers().forEach(member -> {
|
||||
if(member.isOnline() && target.isOnline())
|
||||
MMOCore.plugin.configManager.getSimpleMessage("party-joined-other", "player", target.getPlayer().getName()).send(member.getPlayer());
|
||||
if (member.isOnline())
|
||||
MMOCore.plugin.configManager.getSimpleMessage("party-joined-other", "player", getTarget().getPlayer().getName()).send(member.getPlayer());
|
||||
});
|
||||
if(party.getOwner().isOnline() && target.isOnline())
|
||||
MMOCore.plugin.configManager.getSimpleMessage("party-joined", "owner", party.getOwner().getPlayer().getName()).send(target.getPlayer());
|
||||
party.addMember(target);
|
||||
InventoryManager.PARTY_VIEW.newInventory(target).open();
|
||||
MMOCore.plugin.requestManager.unregisterRequest(getUniqueId());
|
||||
if (party.getOwner().isOnline())
|
||||
MMOCore.plugin.configManager.getSimpleMessage("party-joined", "owner", party.getOwner().getPlayer().getName()).send(getTarget().getPlayer());
|
||||
party.addMember(getTarget());
|
||||
InventoryManager.PARTY_VIEW.newInventory(getTarget()).open();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user