Use shop owner for LWC protections and not the creator

Also fix a bug where it was possible for shops to be created with shop owners that did not have a valid Account information when other plugins manipulated the name line after the NameChecker checked it. Now it gets checked twice, once at the start (to abort early and to populate the ownerAccount field) and once at the end to check again when the name line changed.
This commit is contained in:
Phoenix616 2019-08-22 14:03:27 +01:00
parent f90d23cfc3
commit b718fcc429
8 changed files with 101 additions and 18 deletions

View File

@ -1,11 +1,14 @@
package com.Acrobot.ChestShop.Events;
import com.Acrobot.ChestShop.Database.Account;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import javax.annotation.Nullable;
/**
* Represents a state before shop is created
*
@ -15,6 +18,7 @@ public class PreShopCreationEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private Player creator;
@Nullable private Account ownerAccount = null;
private CreationOutcome outcome = CreationOutcome.SHOP_CREATED_SUCCESSFULLY;
private Sign sign;
@ -138,6 +142,25 @@ public class PreShopCreationEvent extends Event implements Cancellable {
return signLines;
}
/**
* Get the account of the shop owner
*
* @return the Account of the shop owner; null if not found
*/
@Nullable
public Account getOwnerAccount() {
return ownerAccount;
}
/**
* Set the account of the shop owner
*
* @param ownerAccount the Account of the shop owner
*/
public void setOwnerAccount(@Nullable Account ownerAccount) {
this.ownerAccount = ownerAccount;
}
public HandlerList getHandlers() {
return handlers;
}

View File

@ -5,6 +5,8 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import java.util.UUID;
/**
* @author Acrobot
@ -12,14 +14,20 @@ import org.bukkit.event.HandlerList;
public class ProtectBlockEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private Player player;
private final Player player;
private final UUID protectionOwner;
private Block block;
boolean isProtected = false;
public ProtectBlockEvent(Block block, Player player) {
this(block, player, player.getUniqueId());
}
public ProtectBlockEvent(Block block, Player player, UUID protectionOwner) {
this.block = block;
this.player = player;
this.protectionOwner = protectionOwner;
}
public boolean isProtected() {
@ -38,6 +46,10 @@ public class ProtectBlockEvent extends Event {
return player;
}
public UUID getProtectionOwner() {
return protectionOwner;
}
public HandlerList getHandlers() {
return handlers;
}

View File

@ -1,5 +1,6 @@
package com.Acrobot.ChestShop.Events;
import com.Acrobot.ChestShop.Database.Account;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
@ -21,6 +22,7 @@ public class ShopCreatedEvent extends Event {
private final Sign sign;
private final String[] signLines;
@Nullable private final Account ownerAccount;
@Nullable private final Container container;
@Deprecated
@ -28,11 +30,17 @@ public class ShopCreatedEvent extends Event {
this(creator, sign, (Container) chest, signLines);
}
@Deprecated
public ShopCreatedEvent(Player creator, Sign sign, @Nullable Container container, String[] signLines) {
this(creator, sign, container, signLines, null);
}
public ShopCreatedEvent(Player creator, Sign sign, @Nullable Container container, String[] signLines, @Nullable Account ownerAccount) {
this.creator = creator;
this.sign = sign;
this.container = container;
this.signLines = signLines.clone();
this.ownerAccount = ownerAccount;
}
/**
@ -89,6 +97,25 @@ public class ShopCreatedEvent extends Event {
return container instanceof Chest ? (Chest) container : null;
}
/**
* Get the account of the shop's owner
*
* @return The account of the shop's owner; null if no Account could be found
*/
@Nullable
public Account getOwnerAccount() {
return ownerAccount;
}
/**
* Check whether or not the created shop is owned by the creator
*
* @return <tt>true</tt> if the owner account is the creators one (or null); <tt>false</tt> if it's not
*/
public boolean createdByOwner() {
return ownerAccount == null || ownerAccount.getUuid().equals(creator.getUniqueId());
}
public HandlerList getHandlers() {
return handlers;
}

View File

@ -3,6 +3,7 @@ package com.Acrobot.ChestShop.Listeners.Block;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
@ -55,7 +56,7 @@ public class SignCreate implements Listener {
return;
}
ShopCreatedEvent postEvent = new ShopCreatedEvent(preEvent.getPlayer(), preEvent.getSign(), uBlock.findConnectedContainer(preEvent.getSign()), preEvent.getSignLines());
ShopCreatedEvent postEvent = new ShopCreatedEvent(preEvent.getPlayer(), preEvent.getSign(), uBlock.findConnectedContainer(preEvent.getSign()), preEvent.getSignLines(), preEvent.getOwnerAccount());
ChestShop.callEvent(postEvent);
}
}

View File

@ -22,7 +22,7 @@ public class ShopCreationLogger implements Listener {
@Override public void run() {
String creator = event.getPlayer().getName();
String shopOwner = event.getSignLine(NAME_LINE);
String typeOfShop = ChestShopSign.isAdminShop(shopOwner) ? "an Admin Shop" : "a shop";
String typeOfShop = ChestShopSign.isAdminShop(shopOwner) ? "an Admin Shop" : "a shop" + (event.createdByOwner() ? "" : " for " + event.getOwnerAccount().getName());
String item = event.getSignLine(QUANTITY_LINE) + ' ' + event.getSignLine(ITEM_LINE);
String prices = event.getSignLine(PRICE_LINE);

View File

@ -21,21 +21,34 @@ public class NameChecker implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public static void onPreShopCreation(PreShopCreationEvent event) {
handleEvent(event);
}
@EventHandler(priority = EventPriority.HIGHEST)
public static void onPreShopCreationHighest(PreShopCreationEvent event) {
handleEvent(event);
}
private static void handleEvent(PreShopCreationEvent event) {
String name = event.getSignLine(NAME_LINE);
Player player = event.getPlayer();
Account account = null;
try {
if (name.isEmpty() || !NameManager.canUseName(player, OTHER_NAME_CREATE, name)) {
account = NameManager.getOrCreateAccount(player);
} else {
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(name);
Bukkit.getPluginManager().callEvent(accountQueryEvent);
account = accountQueryEvent.getAccount();
Account account = event.getOwnerAccount();
if (account == null || !account.getShortName().equalsIgnoreCase(name)) {
account = null;
try {
if (name.isEmpty() || !NameManager.canUseName(player, OTHER_NAME_CREATE, name)) {
account = NameManager.getOrCreateAccount(player);
} else {
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(name);
Bukkit.getPluginManager().callEvent(accountQueryEvent);
account = accountQueryEvent.getAccount();
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
event.setOwnerAccount(account);
if (account != null) {
event.setSignLine(NAME_LINE, account.getShortName());
} else {

View File

@ -36,12 +36,13 @@ public class LightweightChestProtection implements Listener {
Container connectedContainer = event.getContainer();
if (Properties.PROTECT_SIGN_WITH_LWC) {
if (!Security.protect(player, sign.getBlock())) {
if (!Security.protect(player, sign.getBlock(), event.getOwnerAccount() != null ? event.getOwnerAccount().getUuid() : player.getUniqueId())) {
player.sendMessage(Messages.prefix(Messages.NOT_ENOUGH_PROTECTIONS));
}
}
if (Properties.PROTECT_CHEST_WITH_LWC && connectedContainer != null && Security.protect(player, connectedContainer.getBlock())) {
if (Properties.PROTECT_CHEST_WITH_LWC && connectedContainer != null
&& Security.protect(player, connectedContainer.getBlock(), event.getOwnerAccount() != null ? event.getOwnerAccount().getUuid() : player.getUniqueId())) {
player.sendMessage(Messages.prefix(Messages.PROTECTED_SHOP));
}
}
@ -101,14 +102,14 @@ public class LightweightChestProtection implements Listener {
Protection protection = null;
try {
protection = lwc.getPhysicalDatabase().registerProtection(block.getType(), Protection.Type.PRIVATE, worldName, player.getUniqueId().toString(), "", x, y, z);
protection = lwc.getPhysicalDatabase().registerProtection(block.getType(), Protection.Type.PRIVATE, worldName, event.getProtectionOwner().toString(), "", x, y, z);
} catch (LinkageError e) {
try {
int blockId = com.griefcraft.cache.BlockCache.getInstance().getBlockId(block);
if (blockId < 0) {
return;
}
protection = lwc.getPhysicalDatabase().registerProtection(blockId, Protection.Type.PRIVATE, worldName, player.getUniqueId().toString(), "", x, y, z);
protection = lwc.getPhysicalDatabase().registerProtection(blockId, Protection.Type.PRIVATE, worldName, event.getProtectionOwner().toString(), "", x, y, z);
} catch (LinkageError e2) {
ChestShop.getBukkitLogger().warning(
"Incompatible LWC version installed! (" + lwc.getPlugin().getName() + " v" + lwc.getVersion() + ") \n" +

View File

@ -15,6 +15,8 @@ import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import java.util.UUID;
/**
* @author Acrobot
*/
@ -23,7 +25,11 @@ public class Security {
private static final BlockFace[] BLOCKS_AROUND = {BlockFace.UP, BlockFace.DOWN, BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH};
public static boolean protect(Player player, Block block) {
ProtectBlockEvent event = new ProtectBlockEvent(block, player);
return protect(player, block, player.getUniqueId());
}
public static boolean protect(Player player, Block block, UUID protectionOwner) {
ProtectBlockEvent event = new ProtectBlockEvent(block, player, protectionOwner);
ChestShop.callEvent(event);
return event.isProtected();