Files
src
build.gradle
settings.gradle
Code
19 1,040
src/main/java/com/blockbay/redeem/RedeemPlugin.java
package com.blockbay.redeem;
import com.blockbay.redeem.inventory.gui.GUIListener;
import com.blockbay.redeem.inventory.gui.GUIManager;
import com.blockbay.redeem.inventory.impl.CreateCodeGUI;
import com.blockbay.redeem.inventory.impl.GiftAddCommand;
import com.blockbay.redeem.inventory.impl.RewardsGUI;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class RedeemPlugin extends JavaPlugin implements Listener {
private final Map<UUID, Boolean> pending = new HashMap<>();
private final Map<String, String> codeClaimerIp = new HashMap<>();
private final Set<String> playerClaims = new HashSet<>();
private File dataFile;
private YamlConfiguration data;
private GUIManager guiManager;
// Chat input state for /gift add flow
private final Map<UUID, String> awaitingInput = new HashMap<>();
private final Map<UUID, Object[]> inputContext = new HashMap<>();
@Override
public void onEnable() {
saveDefaultConfig();
dataFile = new File(getDataFolder(), "redeemed.yml");
if (!dataFile.exists()) {
try { dataFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); }
}
data = YamlConfiguration.loadConfiguration(dataFile);
loadClaims();
guiManager = new GUIManager();
GUIListener guiListener = new GUIListener(guiManager);
Bukkit.getPluginManager().registerEvents(guiListener, this);
Bukkit.getPluginManager().registerEvents(this, this);
getCommand("redeem").setExecutor(new RedeemCommand(this));
getCommand("giftcard").setExecutor(new GiftcardCommand(this));
getCommand("gift").setExecutor(new GiftAddCommand(this, guiManager));
getLogger().info("BlockBay GiftCards enabled.");
}
// ── Chat input for code creation ──
public void startCodeInput(Player p, CodeCreator creator, CreateCodeGUI gui, GUIManager gm) {
awaitingInput.put(p.getUniqueId(), "CODE");
inputContext.put(p.getUniqueId(), new Object[]{creator, gui, gm});
p.sendMessage(cc("&8┃ &2✦ &fType the code name in chat. Type &ccancel &fto exit."));
}
public void startMessageInput(Player p, CodeCreator creator, CreateCodeGUI gui, GUIManager gm) {
awaitingInput.put(p.getUniqueId(), "MESSAGE");
inputContext.put(p.getUniqueId(), new Object[]{creator, gui, gm});
p.sendMessage(cc("&8┃ &2✦ &fType the message in chat. Type &ccancel &fto exit."));
}
public void startCommandInput(Player p, CodeCreator creator, RewardsGUI gui, GUIManager gm) {
awaitingInput.put(p.getUniqueId(), "COMMAND");
inputContext.put(p.getUniqueId(), new Object[]{creator, gui, gm});
p.sendMessage(cc("&8┃ &2✦ &fType a command &7(without /)&f. Example: &7give %player% diamond 1"));
p.sendMessage(cc("&8┃ &fType &adone &fwhen finished, or &ccancel &fto exit."));
}
// ── Claim persistence ──
private void loadClaims() {
playerClaims.clear();
codeClaimerIp.clear();
ConfigurationSection codes = data.getConfigurationSection("codes");
if (codes != null) {
for (String code : codes.getKeys(false)) {
ConfigurationSection sec = codes.getConfigurationSection(code);
if (sec != null) {
for (String uuidStr : sec.getStringList("players")) {
playerClaims.add(code + ":" + uuidStr);
}
String ip = sec.getString("claimerIp");
if (ip != null) codeClaimerIp.put(code, ip);
}
}
}
}
private void saveClaims() {
data.set("codes", null);
Map<String, List<String>> byCode = new HashMap<>();
for (String entry : playerClaims) {
int colon = entry.indexOf(':');
byCode.computeIfAbsent(entry.substring(0, colon), k -> new ArrayList<>()).add(entry.substring(colon + 1));
}
for (Map.Entry<String, List<String>> e : byCode.entrySet()) {
data.set("codes." + e.getKey() + ".players", e.getValue());
if (codeClaimerIp.containsKey(e.getKey()))
data.set("codes." + e.getKey() + ".claimerIp", codeClaimerIp.get(e.getKey()));
}
try { data.save(dataFile); } catch (IOException ex) { ex.printStackTrace(); }
}
public boolean hasClaimed(Player p, String code) {
return playerClaims.contains(code + ":" + p.getUniqueId().toString());
}
public boolean sameIpClaimed(Player p, String code) {
String ip = p.getAddress().getAddress().getHostAddress();
String storedIp = codeClaimerIp.get(code);
return storedIp != null && !storedIp.equals(ip);
}
public void markClaimed(Player p, String code) {
playerClaims.add(code + ":" + p.getUniqueId().toString());
codeClaimerIp.put(code, p.getAddress().getAddress().getHostAddress());
saveClaims();
}
public void markPending(UUID uuid) { pending.put(uuid, true); }
public boolean isPending(UUID uuid) { return pending.containsKey(uuid); }
public void removePending(UUID uuid) { pending.remove(uuid); }
public void reload() { reloadConfig(); loadClaims(); }
public void saveCode(CodeCreator creator) {
String code = creator.getCode().toUpperCase();
getConfig().set("codes." + code + ".message", creator.getMessage());
getConfig().set("codes." + code + ".commands", creator.getCommands());
// Also add a dummy item entry if item was set (for display in GUI)
saveConfig();
reloadConfig();
}
@EventHandler
public void onChat(AsyncPlayerChatEvent e) {
Player p = e.getPlayer();
UUID uuid = p.getUniqueId();
// Code creation input flow
if (awaitingInput.containsKey(uuid)) {
e.setCancelled(true);
String msg = e.getMessage().trim();
String type = awaitingInput.get(uuid);
if (msg.equalsIgnoreCase("cancel")) {
awaitingInput.remove(uuid);
inputContext.remove(uuid);
p.sendMessage(cc("&8┃ &c✘ Cancelled."));
return;
}
if (type.equals("COMMAND") && msg.equalsIgnoreCase("done")) {
Object[] ctx = inputContext.get(uuid);
awaitingInput.remove(uuid);
inputContext.remove(uuid);
RewardsGUI rg = (RewardsGUI) ctx[1];
GUIManager gm = (GUIManager) ctx[2];
Bukkit.getScheduler().runTask(this, () -> gm.openGUI(rg, p));
return;
}
Object[] ctx = inputContext.get(uuid);
CodeCreator creator = (CodeCreator) ctx[0];
switch (type) {
case "CODE":
creator.setCode(msg.toUpperCase().replace(" ", ""));
awaitingInput.remove(uuid);
inputContext.remove(uuid);
CreateCodeGUI cg = (CreateCodeGUI) ctx[1];
GUIManager gm1 = (GUIManager) ctx[2];
p.sendMessage(cc("&8┃ &2✔ &fCode set to &a" + creator.getCode()));
Bukkit.getScheduler().runTask(this, () -> gm1.openGUI(cg, p));
break;
case "MESSAGE":
creator.setMessage(msg);
awaitingInput.remove(uuid);
inputContext.remove(uuid);
CreateCodeGUI cg2 = (CreateCodeGUI) ctx[1];
GUIManager gm2 = (GUIManager) ctx[2];
p.sendMessage(cc("&8┃ &2✔ &fMessage set!"));
Bukkit.getScheduler().runTask(this, () -> gm2.openGUI(cg2, p));
break;
case "COMMAND":
creator.addCommand(msg);
p.sendMessage(cc("&8┃ &2✔ &fCommand added: &7" + msg));
// Stay in command input mode - don't close
p.sendMessage(cc("&8┃ &fType &adone &fwhen finished, or another command."));