|
| 1 | +package io.github.bakedlibs.dough.protection.modules; |
| 2 | + |
| 3 | +import au.lupine.quarters.api.manager.QuarterManager; |
| 4 | +import au.lupine.quarters.object.entity.Quarter; |
| 5 | +import com.palmergames.bukkit.towny.TownyAPI; |
| 6 | +import com.palmergames.bukkit.towny.object.Resident; |
| 7 | +import com.palmergames.bukkit.towny.object.TownyPermission.ActionType; |
| 8 | +import com.palmergames.bukkit.towny.utils.PlayerCacheUtil; |
| 9 | +import io.github.bakedlibs.dough.protection.Interaction; |
| 10 | +import io.github.bakedlibs.dough.protection.ProtectionModule; |
| 11 | +import org.bukkit.Location; |
| 12 | +import org.bukkit.OfflinePlayer; |
| 13 | +import org.bukkit.entity.Player; |
| 14 | +import org.bukkit.plugin.Plugin; |
| 15 | + |
| 16 | +import javax.annotation.Nonnull; |
| 17 | + |
| 18 | + |
| 19 | +/** |
| 20 | + * Protection handling module for Quarters, a Towny add-on. |
| 21 | + * If Quarters is installed on a server, this module must be registered |
| 22 | + * instead of the Towny module. |
| 23 | + * |
| 24 | + * @author galacticwarrior9 |
| 25 | + */ |
| 26 | +public class QuartersProtectionModule implements ProtectionModule { |
| 27 | + |
| 28 | + private final Plugin plugin; |
| 29 | + |
| 30 | + public QuartersProtectionModule(@Nonnull Plugin plugin) { |
| 31 | + this.plugin = plugin; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Plugin getPlugin() { |
| 36 | + return plugin; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void load() { |
| 41 | + // We don't need to load any APIs, everything is static |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean hasPermission(OfflinePlayer p, Location l, Interaction action) { |
| 46 | + if (!(p instanceof Player)) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + return isInteractionAllowed((Player) p, convert(action), l); |
| 50 | + } |
| 51 | + |
| 52 | + private boolean isInteractionAllowed(Player player, ActionType type, Location l) { |
| 53 | + boolean allowedInUnderlyingPlot = PlayerCacheUtil.getCachePermission(player, l, l.getBlock().getType(), type); |
| 54 | + Quarter quarter = QuarterManager.getInstance().getQuarter(l); |
| 55 | + if (quarter == null) { |
| 56 | + return allowedInUnderlyingPlot; |
| 57 | + } |
| 58 | + |
| 59 | + Resident resident = TownyAPI.getInstance().getResident(player.getUniqueId()); |
| 60 | + if (resident == null) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + return quarter.testPermission(convertToQuartersAction(type), resident); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns the corresponding Towny {@link ActionType} from the dough {@link Interaction} |
| 69 | + * |
| 70 | + * @param action The dough {@link Interaction} |
| 71 | + * @return The corresponding Towny {@link ActionType} |
| 72 | + */ |
| 73 | + private ActionType convert(Interaction action) { |
| 74 | + switch (action) { |
| 75 | + case INTERACT_BLOCK: |
| 76 | + return ActionType.SWITCH; |
| 77 | + case INTERACT_ENTITY: |
| 78 | + case ATTACK_PLAYER: |
| 79 | + case ATTACK_ENTITY: |
| 80 | + return ActionType.ITEM_USE; |
| 81 | + case BREAK_BLOCK: |
| 82 | + return ActionType.DESTROY; |
| 83 | + case PLACE_BLOCK: |
| 84 | + default: |
| 85 | + return ActionType.BUILD; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns the corresponding Quarters {@link au.lupine.quarters.object.state.ActionType} from the Towny {@link ActionType} |
| 91 | + * |
| 92 | + * @param action The Towny {@link ActionType} |
| 93 | + * @return The corresponding Quarters {@link au.lupine.quarters.object.state.ActionType} |
| 94 | + */ |
| 95 | + private au.lupine.quarters.object.state.ActionType convertToQuartersAction(ActionType action) { |
| 96 | + switch (action) { |
| 97 | + case DESTROY: |
| 98 | + return au.lupine.quarters.object.state.ActionType.DESTROY; |
| 99 | + case ITEM_USE: |
| 100 | + return au.lupine.quarters.object.state.ActionType.ITEM_USE; |
| 101 | + case SWITCH: |
| 102 | + return au.lupine.quarters.object.state.ActionType.SWITCH; |
| 103 | + default: |
| 104 | + return au.lupine.quarters.object.state.ActionType.BUILD; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments