Skip to content

Commit

Permalink
[O] 优化权限检查
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Jun 13, 2020
1 parent 1f5c1e9 commit 8cf1549
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 25 deletions.
40 changes: 22 additions & 18 deletions src/main/java/io/github/starwishsama/extra/ProtectionChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.plot.Plot;
import io.github.thebusybiscuit.slimefun4.api.events.AndroidMineEvent;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
Expand Down Expand Up @@ -35,7 +34,7 @@ public void onAndroidMine(AndroidMineEvent e) {
if (e != null) {
Player p = Bukkit.getPlayer(getOwnerByJson(BlockStorage.getBlockInfoAsJson(e.getAndroid().getBlock())));

if (!check(p, e.getBlock(), true)) {
if (!canInteract(p, e.getBlock(), InteractType.DESTROY)) {
e.setCancelled(true);
SlimefunPlugin.getLocal().sendMessage(p, "android.no-permission");
}
Expand Down Expand Up @@ -64,12 +63,12 @@ public static void checkInstallStatus(SlimefunPlugin plugin) {
/**
* 检查是否可以在领地/地皮内破坏/交互方块
*
* @param p 玩家
* @param block 被破坏的方块
* @param isBreakBlock 是否在破坏方块
* @param p 玩家
* @param block 被破坏的方块
* @param type 交互类型
* @return 是否可以破坏
*/
public static boolean check(Player p, Block block, boolean isBreakBlock) {
public static boolean canInteract(Player p, Block block, InteractType type) {
if (p != null && block != null) {
if (p.isOp()) {
return true;
Expand All @@ -84,23 +83,24 @@ public static boolean check(Player p, Block block, boolean isBreakBlock) {
return true;
}

if (!isBreakBlock) {
if (!perms.playerHas(p, Flags.use, true)) {
SlimefunPlugin.getLocal().sendMessage(p, "inventory.no-access");
return false;
} else {
return true;
}
switch (type) {
case DESTROY:
return perms.playerHas(p, Flags.destroy, true) || perms.playerHas(p, Flags.build, true);
case PLACE:
return perms.playerHas(p, Flags.place, true) || perms.playerHas(p, Flags.build, true);
case INTERACT:
if (!perms.playerHas(p, Flags.use, true)) {
SlimefunPlugin.getLocal().sendMessage(p, "inventory.no-access");
return false;
} else {
return true;
}
}

return perms.playerHas(p, Flags.destroy, true)
|| perms.playerHas(p, Flags.place, true)
|| perms.playerHas(p, Flags.build, true);
}
}

if (plotInstalled) {
Plot plot = Plot.getPlot(new Location(block.getWorld().getName(), block.getX(), block.getY(), block.getZ()));
Plot plot = Plot.getPlot(new com.plotsquared.core.location.Location(block.getWorld().getName(), block.getX(), block.getY(), block.getZ()));

if (plot != null) {
return plot.isOwner(p.getUniqueId()) || plot.isAdded(p.getUniqueId());
Expand All @@ -120,4 +120,8 @@ public static UUID getOwnerByJson(String json) {
}
return null;
}

public enum InteractType {
DESTROY, PLACE, INTERACT
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private List<GithubBean> getReleaseBean() {
try {
URL url = new URL("https://api.github.com/repos/StarWishsama/Slimefun4/releases");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(8_000);
conn.setConnectTimeout(5_000);
conn.addRequestProperty("Accept-Charset", "UTF-8");
conn.addRequestProperty("User-Agent", "Slimefun 4 Update Checker by StarWishsama");
conn.setDoOutput(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ExplosiveShovel(Category category, SlimefunItemStack item, RecipeType rec

@Override
protected void breakBlock(Player p, ItemStack item, Block b, int fortune, List<ItemStack> drops) {
if (!isUnbreakable(b.getType().name()) && ProtectionChecker.check(p, b, true) && MaterialTools.getBreakableByShovel().contains(b.getType()) && SlimefunPlugin.getProtectionManager().hasPermission(p, b.getLocation(), ProtectableAction.BREAK_BLOCK)) {
if (!isUnbreakable(b.getType().name()) && ProtectionChecker.canInteract(p, b, ProtectionChecker.InteractType.DESTROY) && MaterialTools.getBreakableByShovel().contains(b.getType()) && SlimefunPlugin.getProtectionManager().hasPermission(p, b.getLocation(), ProtectableAction.BREAK_BLOCK)) {
SlimefunPlugin.getProtectionManager().logAction(p, b, ProtectableAction.BREAK_BLOCK);

b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public boolean isDamageable() {

protected void breakBlock(Player p, ItemStack item, Block b, int fortune, List<ItemStack> drops) {
if (!isUnbreakable(b.getType().name())
&& ProtectionChecker.check(p, b, true)
&& ProtectionChecker.canInteract(p, b, ProtectionChecker.InteractType.DESTROY)
&& b.getType() != Material.AIR
&& !b.isLiquid()
&& !MaterialCollections.getAllUnbreakableBlocks().contains(b.getType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List
}

for (Block b : logs) {
if (ProtectionChecker.check(e.getPlayer(), b, true) && SlimefunPlugin.getProtectionManager().hasPermission(e.getPlayer(), b, ProtectableAction.BREAK_BLOCK)) {
if (ProtectionChecker.canInteract(e.getPlayer(), b, ProtectionChecker.InteractType.DESTROY) && SlimefunPlugin.getProtectionManager().hasPermission(e.getPlayer(), b, ProtectableAction.BREAK_BLOCK)) {
breakLog(b);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List
private void breakBlocks(Player p, List<Block> blocks, int fortune) {
for (Block b : blocks) {
if (SlimefunPlugin.getProtectionManager().hasPermission(p, b.getLocation(), ProtectableAction.BREAK_BLOCK)
&& ProtectionChecker.check(p, b, true)) {
&& ProtectionChecker.canInteract(p, b, ProtectionChecker.InteractType.DESTROY)) {
b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType());

for (ItemStack drop : b.getDrops(getItem())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ public void onBlockBreak(BlockBreakEvent e) {
ItemStack item = e.getPlayer().getInventory().getItemInMainHand();
Block b = e.getBlock();

if (item.getType() != Material.AIR && item.getAmount() > 0 && SlimefunPlugin.getProtectionManager().hasPermission(e.getPlayer(), b, ProtectableAction.BREAK_BLOCK) && ProtectionChecker.check(e.getPlayer(), b, true)) {
if (item.getType() != Material.AIR && item.getAmount() > 0
&& SlimefunPlugin.getProtectionManager().hasPermission(e.getPlayer(), b, ProtectableAction.BREAK_BLOCK)
&& ProtectionChecker.canInteract(e.getPlayer(), b, ProtectionChecker.InteractType.DESTROY)) {
Collection<ItemStack> drops = b.getDrops(item);
int dropAmount = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public BlockMenuPreset getPreset() {
}

public boolean canOpen(Block b, Player p) {
return preset.canOpen(b, p) && ProtectionChecker.check(p, b, false);
return preset.canOpen(b, p) && ProtectionChecker.canInteract(p, b, ProtectionChecker.InteractType.INTERACT);
}

public void close() {
Expand Down

0 comments on commit 8cf1549

Please sign in to comment.