Browse Source

Improved handling for invalid (used/duped) coupons.

master
Stefatorus 4 years ago
parent
commit
7097df12c1
  1. 4
      ERCoupons/src/com/entryrise/coupons/Data.java
  2. 78
      ERCoupons/src/com/entryrise/coupons/listeners/CouponListener.java

4
ERCoupons/src/com/entryrise/coupons/Data.java

@ -82,7 +82,7 @@ public class Data {
int min = Main.config.getInt("settings.virtual.min"); int min = Main.config.getInt("settings.virtual.min");
int max = Main.config.getInt("settings.virtual.max"); int max = Main.config.getInt("settings.virtual.max");
if (credits > max) { if (credits > max) {
p.sendMessage(Main.PREFIX + "You can make a coupon holding a maximum of " + max + " credits"); p.sendMessage(Main.PREFIX + "You can make a coupon holding a maximum of " + max + " credits");
return; return;
@ -152,7 +152,7 @@ public class Data {
long credits = data.getLong(loc, -1); long credits = data.getLong(loc, -1);
PlayerInventory pinv = p.getInventory(); PlayerInventory pinv = p.getInventory();
if (credits == -1L) {
if (credits == -1) {
for (int i = 0; i < pinv.getSize(); ++i) { for (int i = 0; i < pinv.getSize(); ++i) {
final ItemStack itm = pinv.getItem(i); final ItemStack itm = pinv.getItem(i);
if (itm != null) { if (itm != null) {

78
ERCoupons/src/com/entryrise/coupons/listeners/CouponListener.java

@ -4,43 +4,51 @@
package com.entryrise.coupons.listeners; package com.entryrise.coupons.listeners;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.EventPriority;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.entity.Player;
import com.entryrise.coupons.Data; import com.entryrise.coupons.Data;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.Listener;
public class CouponListener implements Listener
{
@EventHandler(priority = EventPriority.HIGHEST)
public void onCouponInteract(final PlayerInteractEvent e) {
final Player p = e.getPlayer();
final ItemStack itm = e.getItem();
if (e.getAction() != Action.RIGHT_CLICK_AIR) {
return;
}
Data.couponAction(p, itm, true);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onGUIClick(final InventoryClickEvent e) {
final Player p = (Player)e.getWhoClicked();
final ItemStack itm = e.getCurrentItem();
Data.couponAction(p, itm, false);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onGUIDrag(final InventoryDragEvent e) {
final Player p = (Player)e.getWhoClicked();
final ItemStack itm = e.getOldCursor();
if (Data.couponAction(p, itm, false)) {
e.setCancelled(true);
e.setCursor((ItemStack)null);
}
}
public class CouponListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onCouponInteract(PlayerInteractEvent e) {
final Player p = e.getPlayer();
final ItemStack itm = e.getItem();
if (e.getAction().toString().contains("RIGHT_CLICK")) {
e.setCancelled(true);
return;
}
Data.couponAction(p, itm, true);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onGUIClick(InventoryClickEvent e) {
final Player p = (Player) e.getWhoClicked();
final ItemStack itm = e.getCurrentItem();
Data.couponAction(p, itm, false);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onGUIDrag(InventoryDragEvent e) {
final Player p = (Player) e.getWhoClicked();
final ItemStack itm = e.getOldCursor();
if (Data.couponAction(p, itm, false)) {
e.setCancelled(true);
e.setCursor((ItemStack) null);
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onGUIDrag(PlayerDropItemEvent e) {
if (Data.couponAction(e.getPlayer(), e.getItemDrop().getItemStack(), false)) {
e.getItemDrop().remove();
}
}
} }

Loading…
Cancel
Save