Skip to content

Instantly share code, notes, and snippets.

@hamza-cskn
Last active June 17, 2022 09:12
Show Gist options
  • Save hamza-cskn/9f469772392b444ff36b0b1349045485 to your computer and use it in GitHub Desktop.
Save hamza-cskn/9f469772392b444ff36b0b1349045485 to your computer and use it in GitHub Desktop.
Useful invite code that provides players invites others.
package mc.obliviate.test.textapi;
import com.google.common.base.Preconditions;
import mc.obliviate.test.Test;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.UUID;
import java.util.function.Consumer;
public class Invite implements IInvite {
private final UUID sender;
private final UUID receiver;
private final long expireOutTime;
private final Consumer<Invite> response;
private InviteState state = InviteState.PENDING;
protected Invite(UUID sender, UUID receiverUniqueId, long expireTimeOut, Consumer<Invite> response) {
this.sender = sender;
this.receiver = receiverUniqueId;
this.expireOutTime = expireTimeOut;
this.response = response;
InviteRecipient.getInviteRecipient(receiverUniqueId).addInvite(this);
Bukkit.getScheduler().runTaskLater(MasterDuels.getInstance(), () -> {
if (state.equals(InviteState.PENDING)) {
response(InviteState.EXPIRED);
}
}, (expireTimeOut - System.currentTimeMillis()) / 50); //time difference in ms / 50 = time difference in ticks
}
/**
* Purpose of this class is,
* building new invite class.
*/
public static class Builder {
private UUID sender;
private UUID receiver;
private long expireTime;
private Consumer<Invite> response;
protected Builder() {
}
public InviteBuildResult build() {
InviteRecipient inviteRecipient = InviteRecipient.getInviteRecipient(receiver);
for (Invite invite : inviteRecipient.getInvites()) {
if (invite.sender.equals(sender)) {
if (invite.expireOutTime < System.currentTimeMillis()) {
return new InviteBuildResult(null, InviteBuildState.ERROR_ALREADY_INVITED);
} else {
inviteRecipient.removeInvite(invite);
}
}
}
final Invite inviteBuilt = new Invite(sender, receiver, expireTime, response);
return new InviteBuildResult(inviteBuilt, InviteBuildState.SUCCESS);
}
public Consumer<Invite> getResponse() {
return response;
}
public long getExpireTime() {
return expireTime;
}
public UUID getReceiver() {
return receiver;
}
public UUID getSender() {
return sender;
}
public Builder onResponse(Consumer<Invite> action) {
Preconditions.checkArgument(receiver != null, "action cannot be null");
this.response = action;
return this;
}
public Builder setExpireTimeLater(long msLater) {
Preconditions.checkArgument(msLater > 0, "expire time cannot be negative");
return setExpireTime(System.currentTimeMillis() + msLater);
}
public Builder setExpireTime(long expireTime) {
Preconditions.checkArgument(expireTime > System.currentTimeMillis(), "expire time cannot be smaller than now");
this.expireTime = expireTime;
return this;
}
public Builder setReceiver(UUID receiver) {
Preconditions.checkArgument(receiver != null, "receiver cannot be null");
this.receiver = receiver;
return this;
}
public Builder setSender(UUID sender) {
Preconditions.checkArgument(sender != null, "sender cannot be null");
this.sender = sender;
return this;
}
}
public static Builder create() {
return new Builder();
}
public void response(InviteState inviteState) {
state = inviteState;
InviteRecipient.getInviteRecipient(receiver).removeInvite(this);
response.accept(this);
}
public InviteState getState() {
return state;
}
public long getExpireOutTime() {
return expireOutTime;
}
public UUID getRecipientUniqueId() {
return receiver;
}
public UUID getSenderUniqueId() {
return sender;
}
/**
* Purpose of this class is,
* storing invite and invite build state
* when invite builder built.
*/
public static class InviteBuildResult {
final Invite invite;
final InviteBuildState inviteBuildState;
public InviteBuildResult(Invite invite, InviteBuildState inviteBuildState) {
this.invite = invite;
this.inviteBuildState = inviteBuildState;
}
public Invite getInvite() {
return invite;
}
public InviteBuildState getInviteBuildState() {
return inviteBuildState;
}
}
/**
* Purpose of this class is,
* defining build result of invite.
*/
public enum InviteBuildState {
SUCCESS,
ERROR_ALREADY_INVITED,
}
/**
* Purpose of this class is,
* defining current state of invite.
*/
public enum InviteState {
PENDING,
ACCEPTED,
REJECTED,
EXPIRED,
CANCELLED
}
}
package mc.obliviate.test.textapi;
import java.util.*;
/**
* Purpose of this class is,
* storing all invites thats is sent to
* a player.
*/
public class InviteReceiver {
private static final Map<UUID, InviteReceiver> inviteReceivers = new HashMap<>();
private final List<Invite> invites = new ArrayList<>();
private InviteReceiver(UUID receiverUniqueId) {
inviteReceivers.put(receiverUniqueId, this);
}
public static InviteReceiver getInviteReceiver(UUID uuid) {
InviteReceiver receiver = inviteReceivers.get(uuid);
if (receiver != null) return receiver;
return new InviteReceiver(uuid);
}
protected void addInvite(Invite invite) {
invites.add(invite);
}
protected void removeInvite(Invite invite) {
invites.remove(invite);
}
public List<Invite> getInvites() {
return invites;
}
}
@hamza-cskn
Copy link
Author

hamza-cskn commented Jun 16, 2022

Example Usage & Converter Methods

Invite.create()
	.setReceiver(target.getUniqueId())
	.setSender(player.getUniqueId())
	.setExpireTimeLater(120000) //120 * 1000 = 120s
	.onResponse(invite -> {
		//stuff
	}).build(); //returns build result
		
	@Override
	public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
		if (args.length > 0) {
			if (args[0].equalsIgnoreCase("accept") || args[0].equalsIgnoreCase("decline")) {
				responseInvite(commandSender, args);
			} else {
				if (!(commandSender instanceof Player)) return false;
				Player player = (Player) commandSender;
				Player target = Bukkit.getPlayerExact(args[0]);
				if (target == null) {
					commandSender.sendMessage("target is not online");
					return false;
				}


				target.sendMessage("you invited by " + player.getName());
				InviteBuildResult buildResult = Invite.create()
						.setReceiver(target.getUniqueId())
						.setSender(player.getUniqueId())
						.setExpireTimeLater(10 * 1000)
						.onResponse(invite -> {
							commandSender.sendMessage("Invite " + invite.getState());
						}).build();
				if (buildResult.getInviteBuildState().equals(Invite.InviteBuildState.ERROR_ALREADY_INVITED)) {
					commandSender.sendMessage("you already invited him.");
				}
			}
		} else {
			commandSender.sendMessage("wrong usage");
			commandSender.sendMessage("test <player>");
			commandSender.sendMessage("test accept");
			commandSender.sendMessage("test decline");
		}
		return true;
	}

	private void responseInvite(CommandSender sender, String[] args) {
		if (sender instanceof Player) {
			final Player player = (Player) sender;
			final InviteReceiver receiver = InviteReceiver.getInviteReceiver(player.getUniqueId());

			final Invite.InviteState response = args[0].equalsIgnoreCase("accept") ? Invite.InviteState.ACCEPTED : Invite.InviteState.REJECTED;

			if (receiver.getInvites().size() == 0) {
				player.sendMessage("You don't have any invite.");

			} else if (receiver.getInvites().size() == 1) {
				receiver.getInvites().get(0).response(response);

			} else {
				if (args.length >= 2) {
					int inviteNo;
					try {
						inviteNo = Integer.parseInt(args[1]);
					} catch (NumberFormatException exception) {
						player.sendMessage(args[1] + " is not valid integer.");
						return;
					}
					receiver.getInvites().get(inviteNo).response(response);
				} else {
					int index = 0;
					for (final Invite invite : receiver.getInvites()) {
						player.sendMessage("test accept " + index++ + " -> " + Bukkit.getOfflinePlayer(invite.getSender()).getName());
					}
				}
			}
		}
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment