Skip to content

Commit

Permalink
Não cancelar trade se flag estiver vazia (#2)
Browse files Browse the repository at this point in the history
* Não cancelar trade se estiver vazio

* Adicionado permissão para dar bypass
  • Loading branch information
SrBedrock authored Sep 27, 2024
1 parent a0ebf79 commit 202d02e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -59,6 +60,7 @@ public void init() {
this.tradeLimits.addAll(toFlags(getConfig().getStringList("trade")));
this.griefPrevention = (GriefPrevention) Bukkit.getPluginManager().getPlugin("GriefPrevention");
Log.debug("GPCompat: Started up");

try {
getLogger().info("Registering unsafe event listener...");
Bukkit.getPluginManager().registerEvents(new Listener() {
Expand All @@ -84,7 +86,7 @@ public void onClaimResized(ClaimResizeEvent event) {
}
}

private List<Flag> toFlags(List<String> flags) {
private @NotNull List<Flag> toFlags(@NotNull List<String> flags) {
List<Flag> result = new ArrayList<>(3);
for (String flagStr : flags) {
Flag flag = Flag.getFlag(flagStr);
Expand All @@ -105,7 +107,7 @@ public void onClaimExpired(ClaimExpirationEvent event) {
}

// If it is the main claim, then we will delete all the shops that were inside of it.
private void handleMainClaimUnclaimedOrExpired(Claim claim, String logMessage) {
private void handleMainClaimUnclaimedOrExpired(@NotNull Claim claim, String logMessage) {
for (Chunk chunk : claim.getChunks()) {
Map<Location, Shop> shops = getApi().getShopManager().getShops(chunk);
if (shops != null) {
Expand All @@ -122,7 +124,7 @@ private void handleMainClaimUnclaimedOrExpired(Claim claim, String logMessage) {

// If it is a main claim, then we will remove the shops if the main claim was resized (size was decreased).
// A shop will be removed if the old claim contains it but the new claim doesn't.
private void handleMainClaimResized(Claim oldClaim, Claim newClaim) {
private void handleMainClaimResized(@NotNull Claim oldClaim, Claim newClaim) {
for (Chunk chunk : oldClaim.getChunks()) {
Map<Location, Shop> shops = getApi().getShopManager().getShops(chunk);
if (shops != null) {
Expand All @@ -146,7 +148,7 @@ private void handleSubClaimResized(Claim oldClaim, Claim newClaim) {
handleSubClaimResizedHelper(newClaim, oldClaim);
}

private void handleSubClaimResizedHelper(Claim claimVerifyChunks, Claim claimVerifyShop) {
private void handleSubClaimResizedHelper(@NotNull Claim claimVerifyChunks, Claim claimVerifyShop) {
for (Chunk chunk : claimVerifyChunks.getChunks()) {
Map<Location, Shop> shops = getApi().getShopManager().getShops(chunk);
if (shops != null) {
Expand Down Expand Up @@ -182,7 +184,7 @@ public void onClaimTrustChanged(TrustChangedEvent event) {
}

// Helper to the Claim Trust Changed Event Handler (to avoid duplicate code above)
private void handleClaimTrustChanged(Claim claim, TrustChangedEvent event) {
private void handleClaimTrustChanged(Claim claim, @NotNull TrustChangedEvent event) {
if (event.isGiven()) {
return;
}
Expand Down Expand Up @@ -225,7 +227,7 @@ public void onClaimUnclaimed(ClaimDeletedEvent event) {

// If it is a subclaim, then we will not remove the shops of the main claim owner.
// But we will remove all the others.
private void handleSubClaimUnclaimed(Claim subClaim) {
private void handleSubClaimUnclaimed(@NotNull Claim subClaim) {
for (Chunk chunk : subClaim.getChunks()) {
Map<Location, Shop> shops = getApi().getShopManager().getShops(chunk);
if (shops != null) {
Expand All @@ -241,16 +243,19 @@ private void handleSubClaimUnclaimed(Claim subClaim) {
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onCreation(ShopCreateEvent event) {
event.getCreator().getBukkitPlayer().ifPresent(p -> {
if (checkPermission(p, event.getShop().getLocation(), Collections.singletonList(createLimit))) {
public void onCreation(@NotNull ShopCreateEvent event) {
event.getCreator().getBukkitPlayer().ifPresent(player -> {
if (checkPermission(player, event.getShop().getLocation(), Collections.singletonList(createLimit))) {
return;
}
event.setCancelled(true, getApi().getTextManager().of(event.getCreator(), "addon.griefprevention.creation-denied").forLocale());
});
}

private boolean checkPermission(@NotNull Player player, @NotNull Location location, List<Flag> limits) {
if (player.hasPermission("griefprevention.ignoreclaims")) {
return true;
}
if (!griefPrevention.claimsEnabledForWorld(location.getWorld())) {
return true;
}
Expand All @@ -267,9 +272,9 @@ private boolean checkPermission(@NotNull Player player, @NotNull Location locati
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPreCreation(ShopPreCreateEvent event) {
event.getCreator().getBukkitPlayer().ifPresent(p -> {
if (checkPermission(p, event.getLocation(), Collections.singletonList(createLimit))) {
public void onPreCreation(@NotNull ShopPreCreateEvent event) {
event.getCreator().getBukkitPlayer().ifPresent(player -> {
if (checkPermission(player, event.getLocation(), Collections.singletonList(createLimit))) {
return;
}
event.setCancelled(true, getApi().getTextManager().of(event.getCreator(), "addon.griefprevention.creation-denied").forLocale());
Expand Down Expand Up @@ -303,21 +308,25 @@ public void onSubClaimCreated(ClaimCreatedEvent event) {
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onTrading(ShopPurchaseEvent event) {
event.getPurchaser().getBukkitPlayer().ifPresent(p -> {
if (checkPermission(p, event.getShop().getLocation(), tradeLimits)) {
public void onTrading(@NotNull ShopPurchaseEvent event) {
event.getPurchaser().getBukkitPlayer().ifPresent(player -> {
if (tradeLimits.isEmpty()) {
return;
}
if (checkPermission(player, event.getShop().getLocation(), tradeLimits)) {
return;
}
event.setCancelled(true, getApi().getTextManager().of(event.getPurchaser(), "addon.griefprevention.trade-denied").forLocale());
});
}

@EventHandler(ignoreCancelled = true)
public void permissionOverride(ShopAuthorizeCalculateEvent event) {
public void permissionOverride(@NotNull ShopAuthorizeCalculateEvent event) {
Log.debug("GP-Compat: Starting override permission...");
Location shopLoc = event.getShop().getLocation();
if (!griefPrevention.claimsEnabledForWorld(shopLoc.getWorld())) {
Log.debug("GP-Compat: World " + shopLoc.getWorld().getName() + " not enabled for claims");
String worldName = shopLoc.getWorld() == null ? "Null World" : shopLoc.getWorld().getName();
Log.debug("GP-Compat: World " + worldName + " not enabled for claims");
return;
}
Claim claim = griefPrevention.dataStore.getClaimAt(shopLoc, false, false, null);
Expand Down Expand Up @@ -370,7 +379,7 @@ ClaimPermission toClaimPermission() {
}
};

public static Flag getFlag(String flag) {
public static @Nullable Flag getFlag(String flag) {
for (Flag value : Flag.values()) {
if (value.name().equalsIgnoreCase(flag)) {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -58,16 +59,16 @@ public void onLoad() {
// some other plugin registered a flag by the same name already.
// you can use the existing flag, but this may cause conflicts - be sure to check type
Flag<?> existing = registry.get("quickshophikari-create");
if (existing instanceof StateFlag createFlag) {
this.createFlag = createFlag;
if (existing instanceof StateFlag stateFlagCreate) {
this.createFlag = stateFlagCreate;
} else {
getLogger().log(Level.WARNING, "Could not register flags! CONFLICT!", e);
Bukkit.getPluginManager().disablePlugin(this);
return;
}
existing = registry.get("quickshophikari-trade");
if (existing instanceof StateFlag tradeFlag) {
this.tradeFlag = tradeFlag;
if (existing instanceof StateFlag stateFlagTrade) {
this.tradeFlag = stateFlagTrade;
} else {
getLogger().log(Level.WARNING, "Could not register flags! CONFLICT!", e);
Bukkit.getPluginManager().disablePlugin(this);
Expand All @@ -85,10 +86,14 @@ public void init() {
}

@EventHandler(ignoreCancelled = true)
public void permissionOverride(ShopAuthorizeCalculateEvent event) {
public void permissionOverride(@NotNull ShopAuthorizeCalculateEvent event) {
Location shopLoc = event.getShop().getLocation();
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionManager manager = container.get(BukkitAdapter.adapt(shopLoc.getWorld()));
World world = shopLoc.getWorld();
if (world == null) {
return;
}
RegionManager manager = container.get(BukkitAdapter.adapt(world));
if (manager == null) {
return;
}
Expand All @@ -103,7 +108,7 @@ public void permissionOverride(ShopAuthorizeCalculateEvent event) {
}

@EventHandler(ignoreCancelled = true)
public void preCreation(ShopPreCreateEvent event) {
public void preCreation(@NotNull ShopPreCreateEvent event) {
event.getCreator().getBukkitPlayer().ifPresent(player -> {
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
Expand All @@ -116,7 +121,7 @@ public void preCreation(ShopPreCreateEvent event) {
}

@EventHandler(ignoreCancelled = true)
public void preCreation(ShopCreateEvent event) {
public void preCreation(@NotNull ShopCreateEvent event) {
event.getCreator().getBukkitPlayer().ifPresent(player -> {
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
Expand All @@ -136,13 +141,13 @@ public void preCreation(ShopCreateEvent event) {
});
}

private Map<Location, Shop> getRegionShops(ProtectedRegion region, World world) {
private @NotNull Map<Location, Shop> getRegionShops(@NotNull ProtectedRegion region, World world) {
BlockVector3 minPoint = region.getMinimumPoint();
BlockVector3 maxPoint = region.getMaximumPoint();
Set<Chunk> chuckLocations = new HashSet<>();

for (int x = minPoint.getBlockX(); x <= maxPoint.getBlockX() + 16; x += 16) {
for (int z = minPoint.getBlockZ(); z <= maxPoint.getBlockZ() + 16; z += 16) {
for (int x = minPoint.x(); x <= maxPoint.x() + 16; x += 16) {
for (int z = minPoint.z(); z <= maxPoint.z() + 16; z += 16) {
chuckLocations.add(world.getChunkAt(x >> 4, z >> 4));
}
}
Expand All @@ -159,7 +164,7 @@ private Map<Location, Shop> getRegionShops(ProtectedRegion region, World world)
}

@EventHandler(ignoreCancelled = true)
public void preCreation(ShopPurchaseEvent event) {
public void preCreation(@NotNull ShopPurchaseEvent event) {
event.getPurchaser().getBukkitPlayer().ifPresent(player -> {
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
Expand Down

0 comments on commit 202d02e

Please sign in to comment.