@Override public Map<Permissible, Boolean> get(Object key) { if (key == null || !(key instanceof String)) { return null; } String permission = ((String) key); Map<Permissible, Boolean> result = super.get(key); if (result == null) { // calculate a new map - always! result = new LPSubscriptionValueMap(permission); super.put(permission, result); } else if (!(result instanceof LPSubscriptionValueMap)) { // ensure return type is a LPSubscriptionMap result = new LPSubscriptionValueMap(permission, result); super.put(permission, result); } return result; }
private void inject() throws Exception { PluginManager pluginManager = this.plugin.getServer().getPluginManager(); if (!(pluginManager instanceof SimplePluginManager)) { this.plugin.getLog().severe("PluginManager instance is not a 'SimplePluginManager', instead: " + pluginManager.getClass()); this.plugin.getLog().severe("Unable to inject LuckPerms Permission Subscription map."); return; } Object map = PERM_SUBS_FIELD.get(pluginManager); if (map instanceof LPSubscriptionMap) { return; // already injected } //noinspection unchecked Map<String, Map<Permissible, Boolean>> castedMap = (Map<String, Map<Permissible, Boolean>>) map; // make a new subscription map LPSubscriptionMap newMap = new LPSubscriptionMap(this.plugin, castedMap); // inject it PERM_SUBS_FIELD.set(pluginManager, newMap); }
public List<String> getUpdateText(Permissible player) { long yesterday = System.currentTimeMillis() - 1000l * 60 * 60 * 24; boolean isEnabled = lastNonNullGetUpdateText > yesterday; try { if (readerProvider == null) return null; if (!player.hasPermission(upgradePermission)) return null; lastNonNullGetUpdateText = System.currentTimeMillis(); isEnabled = true; List<String> text; synchronized (this) { if (lastUpgradeCheck < yesterday) { enabled.set(1); lastUpgradeCheck = System.currentTimeMillis(); asynchronousRunnableExecutor.runTaskAsynchronously(new GetLatestVersion()); } else { enabled.set(0); } text = upgradeText; } return text; } finally { enabled.set(isEnabled ? 1 : 0); } }
@Test public void testRecentlyUpdated() throws Exception { PluginDescriptionFile pluginDescriptionFile = new PluginDescriptionFile(new StringReader("name: craftinomicon\nmain: name.ball.joshua.craftinomicon.Craftinomicon\nversion: 0.2")); when(plugin.getDescription()).thenReturn(pluginDescriptionFile); updateChecker.lastUpgradeCheck = System.currentTimeMillis() - 1000l * 60 * 60 * 12; updateChecker.upgradeText = new ArrayList<String>(); updateChecker.upgradeText.add("hello"); Permissible permissible = mock(Permissible.class); when(permissible.hasPermission(any(Permission.class))).thenReturn(true); List<String> updateText = updateChecker.getUpdateText(permissible); assertEquals(1, updateText.size()); assertEquals("hello", updateText.get(0)); }
@Test public void testUpdateContainsFrenchAndGerman() throws Exception { PluginDescriptionFile pluginDescriptionFile = new PluginDescriptionFile(new StringReader("name: craftinomicon\nmain: name.ball.joshua.craftinomicon.Craftinomicon\nversion: 0.2")); when(plugin.getDescription()).thenReturn(pluginDescriptionFile); Permissible permissible = mock(Permissible.class); when(permissible.hasPermission(any(Permission.class))).thenReturn(true); setApiResponse("0.2.1.25165825"); updateChecker.afterPropertiesSet(); List<String> updateText = updateChecker.getUpdateText(permissible); assertEquals(2, updateText.size()); assertEquals("French language option!", updateText.get(0)); assertEquals("German language option!", updateText.get(1)); }
@Test public void testUpdateContainsOnlyUnpredictedFeatures() throws Exception { PluginDescriptionFile pluginDescriptionFile = new PluginDescriptionFile(new StringReader("name: craftinomicon\nmain: name.ball.joshua.craftinomicon.Craftinomicon\nversion: 0.2")); when(plugin.getDescription()).thenReturn(pluginDescriptionFile); Permissible permissible = mock(Permissible.class); when(permissible.hasPermission(any(Permission.class))).thenReturn(true); setApiResponse("0.2.1.5"); updateChecker.afterPropertiesSet(); List<String> updateText = updateChecker.getUpdateText(permissible); assertEquals(1, updateText.size()); assertEquals("New features!", updateText.get(0)); }
@Test public void testUpdateContainsBugFixesAndUnpredictedFeatures() throws Exception { PluginDescriptionFile pluginDescriptionFile = new PluginDescriptionFile(new StringReader("name: craftinomicon\nmain: name.ball.joshua.craftinomicon.Craftinomicon\nversion: 0.2")); when(plugin.getDescription()).thenReturn(pluginDescriptionFile); Permissible permissible = mock(Permissible.class); when(permissible.hasPermission(any(Permission.class))).thenReturn(true); setApiResponse("0.2.1.7"); updateChecker.afterPropertiesSet(); List<String> updateText = updateChecker.getUpdateText(permissible); assertEquals(2, updateText.size()); assertEquals("Bug fixes!", updateText.get(0)); assertEquals("Additional new features not listed here!", updateText.get(1)); }
@Test public void testUpdateContainsPotionsAndUnpredictedFeatures() throws Exception { PluginDescriptionFile pluginDescriptionFile = new PluginDescriptionFile(new StringReader("name: craftinomicon\nmain: name.ball.joshua.craftinomicon.Craftinomicon\nversion: 0.2")); when(plugin.getDescription()).thenReturn(pluginDescriptionFile); Permissible permissible = mock(Permissible.class); when(permissible.hasPermission(any(Permission.class))).thenReturn(true); setApiResponse("0.2.1.37"); updateChecker.afterPropertiesSet(); List<String> updateText = updateChecker.getUpdateText(permissible); assertEquals(2, updateText.size()); assertEquals("Potions!", updateText.get(0)); assertEquals("Additional new features not listed here!", updateText.get(1)); }
@Test public void testSNAPSHOT() throws Exception { PluginDescriptionFile pluginDescriptionFile = new PluginDescriptionFile(new StringReader("name: craftinomicon\nmain: name.ball.joshua.craftinomicon.Craftinomicon\nversion: 0.2-SNAPSHOT")); when(plugin.getDescription()).thenReturn(pluginDescriptionFile); Permissible permissible = mock(Permissible.class); when(permissible.hasPermission(any(Permission.class))).thenReturn(true); setApiResponse("0.2.1.37"); updateChecker.afterPropertiesSet(); List<String> updateText = updateChecker.getUpdateText(permissible); assertEquals(2, updateText.size()); assertEquals("Potions!", updateText.get(0)); assertEquals("Additional new features not listed here!", updateText.get(1)); }
public Permissible inject(Player player, Permissible permissible) throws NoSuchFieldException, IllegalAccessException { Field permField = null; try { permField = getPermissibleField(player); } catch (Exception e) { } if (permField == null) { return null; } Permissible oldPerm = (Permissible) permField.get(player); if (permissible instanceof PermissibleBase) { PermissibleBase newBase = (PermissibleBase) permissible; permField.set(player, newBase); } // Inject permissible permField.set(player, permissible); return oldPerm; }
/** * Is the recipe allowed? * * @param permissible Permissible the permissible to check against * @param recipe the recipe to check * @return boolean true if allowed, false if not */ private static boolean isRecipeAllowedForPermissible(Permissible permissible, Recipe recipe) { Material result = recipe.getResult().getType(); if(result == Material.SPECKLED_MELON && hasRecipeGotMaterial(recipe, Material.GOLD_NUGGET) && permissible.hasPermission(DISALLOW_OLD_GMELON)) { return false; } if(result == Material.SPECKLED_MELON && hasRecipeGotMaterial(recipe, Material.GOLD_BLOCK) && permissible.hasPermission(ALLOW_NEW_GMELON)) { return false; } if(result == Material.GOLDEN_CARROT && hasRecipeGotMaterial(recipe, Material.GOLD_NUGGET) && permissible.hasPermission(DISALLOW_OLD_GCARROT)) { return false; } if(result == Material.GOLDEN_CARROT && hasRecipeGotMaterial(recipe, Material.GOLD_INGOT) && permissible.hasPermission(ALLOW_NEW_GCARROT)) { return false; } //passed all checks return true; }
@Override public int broadcast(String message, String permission) { int count = 0; Set<Permissible> permissibles = getPluginManager().getPermissionSubscriptions(permission); for (Permissible permissible : permissibles) { if (permissible instanceof CommandSender && permissible.hasPermission(permission)) { CommandSender user = (CommandSender) permissible; user.sendMessage(message); count++; } } return count; }
/** * Inject new permissible base into given commandSender * * @param sender The sender * @param permissible The permissible base * @return The result */ public static boolean inject(CommandSender sender, Permissible permissible) { Field f = getPermissibleField(sender); try { f.set(sender, permissible); return true; } catch(IllegalAccessException e) { return false; } }
/** * Get permissible object from given sender * * @param sender The sender * @return The permissible object */ public static Permissible getPermissible(CommandSender sender) { Field f = getPermissibleField(sender); if(f == null) { return null; } try { return (Permissible) f.get(sender); } catch(IllegalAccessException e) { e.printStackTrace(); return null; } }
/** * When the player joins the server * <p> * SERVER JOIN */ @EventHandler(priority = EventPriority.HIGHEST) public void onJoin(PlayerJoinEvent event) { if(!Moo.getInstance().isConnected()) return; event.setJoinMessage(null); Player player = event.getPlayer(); PlayerData data = new PlayerData(); data.setUuid(player.getUniqueId()); data.setLastName(player.getName()); data.setLastIp(player.getAddress().getHostString()); // changes state Permissible oldPermissible = PermissionInjector.getPermissible(player); CustomPermissible customPermissible = new CustomPermissible(player, data.getUuid(), oldPermissible); PermissionInjector.inject(player, customPermissible); MooQueries.getInstance().changePlayerState(data, PacketPlayerState.State.JOIN_SERVER, response -> { }); // UPDATE SERVER INFO Lightning.getInstance().updateServerInfo(); // SET JOIN MESSAGE String playerName = MooQueries.getInstance().getGroup(player.getUniqueId()).getColor() + player.getName(); Bukkit.getServer().broadcastMessage(LanguageManager.get("join-message-pattern", playerName)); }
private void updateServer(@Nullable Server before, Server after) { boolean dirty = false; dirty |= updatePermission(Permissions.OBSERVER, before == null ? null : before.observer_permissions(), after.observer_permissions()); dirty |= updatePermission(Permissions.PARTICIPANT, before == null ? null : before.participant_permissions(), after.participant_permissions()); dirty |= updatePermission(Permissions.MAPMAKER, before == null ? null : before.mapmaker_permissions(), after.mapmaker_permissions()); if(dirty) { onlinePlayers.all().forEach(Permissible::recalculatePermissions); } }
/** * Check if {@code caller} has permission to execute this {@code SubCommand} * and all super commands. * @param caller the caller executing the sub command * @return true iff the called has permission to execute this and all super commands. */ public boolean canExecute(Permissible caller) { for (String permission : this.permissions) { if (caller.hasPermission(permission) && (this.isBase() || this.superCommand.canExecute(caller))) return true; } return false; }
@Override public Map<Permissible, Boolean> put(String key, Map<Permissible, Boolean> value) { if (value == null) { throw new NullPointerException("Map value cannot be null"); } // ensure values are LP subscription maps if (!(value instanceof LPSubscriptionValueMap)) { value = new LPSubscriptionValueMap(key, value); } return super.put(key, value); }
/** * Converts this map back to a standard HashMap * * @return a standard representation of this map */ public Map<String, Map<Permissible, Boolean>> detach() { Map<String, Map<Permissible, Boolean>> ret = new HashMap<>(); for (Map.Entry<String, Map<Permissible, Boolean>> ent : entrySet()) { if (ent.getValue() instanceof LPSubscriptionValueMap) { ret.put(ent.getKey(), ((LPSubscriptionValueMap) ent.getValue()).backing); } else { ret.put(ent.getKey(), ent.getValue()); } } return ret; }
@Nonnull @Override public Set<Permissible> keySet() { // gather players (LPPermissibles) Set<Permissible> players = LPSubscriptionMap.this.plugin.getServer().getOnlinePlayers().stream() .filter(player -> player.isPermissionSet(this.permission)) .collect(Collectors.toSet()); // then combine the players with the backing map return Sets.union(players, this.backing.keySet()); }
@Nonnull @Override public Set<Entry<Permissible, Boolean>> entrySet() { return keySet().stream() .map(p -> { Boolean ret = get(p); return ret != null ? Maps.immutableEntry(p, ret) : null; }) .filter(Objects::nonNull) .collect(ImmutableCollectors.toSet()); }
private MessageSpecifier getFlagDefsMessage(Permissible player) { StringBuilder flagDefsList = new StringBuilder(); Collection<FlagDefinition> defs = this.flagManager.GetFlagDefinitions(); for(FlagDefinition def : defs) { if(this.playerHasPermissionForFlag(def, player)) { flagDefsList.append(def.getName() + " "); } } return new MessageSpecifier(Messages.InvalidFlagDefName, flagDefsList.toString()); }
private void dirtyPermissibles(boolean op) { Set<Permissible> permissibles = getDefaultPermSubscriptions(op); for (Permissible p : permissibles) { p.recalculatePermissions(); } }
public void subscribeToPermission(String permission, Permissible permissible) { String name = permission.toLowerCase(); Map<Permissible, Boolean> map = permSubs.get(name); if (map == null) { map = new WeakHashMap<Permissible, Boolean>(); permSubs.put(name, map); } map.put(permissible, true); }
public void unsubscribeFromPermission(String permission, Permissible permissible) { String name = permission.toLowerCase(); Map<Permissible, Boolean> map = permSubs.get(name); if (map != null) { map.remove(permissible); if (map.isEmpty()) { permSubs.remove(name); } } }
public Set<Permissible> getPermissionSubscriptions(String permission) { String name = permission.toLowerCase(); Map<Permissible, Boolean> map = permSubs.get(name); if (map == null) { return ImmutableSet.of(); } else { return ImmutableSet.copyOf(map.keySet()); } }
public void subscribeToDefaultPerms(boolean op, Permissible permissible) { Map<Permissible, Boolean> map = defSubs.get(op); if (map == null) { map = new WeakHashMap<Permissible, Boolean>(); defSubs.put(op, map); } map.put(permissible, true); }
public void unsubscribeFromDefaultPerms(boolean op, Permissible permissible) { Map<Permissible, Boolean> map = defSubs.get(op); if (map != null) { map.remove(permissible); if (map.isEmpty()) { defSubs.remove(op); } } }
public Set<Permissible> getDefaultPermSubscriptions(boolean op) { Map<Permissible, Boolean> map = defSubs.get(op); if (map == null) { return ImmutableSet.of(); } else { return ImmutableSet.copyOf(map.keySet()); } }
public static void broadcastCommandMessage(CommandSender source, String message, boolean sendToSource) { String result = source.getName() + ": " + message; if (source instanceof BlockCommandSender) { BlockCommandSender blockCommandSender = (BlockCommandSender) source; if (blockCommandSender.getBlock().getWorld().getGameRuleValue("commandBlockOutput").equalsIgnoreCase("false")) { Bukkit.getConsoleSender().sendMessage(result); return; } } else if (source instanceof CommandMinecart) { CommandMinecart commandMinecart = (CommandMinecart) source; if (commandMinecart.getWorld().getGameRuleValue("commandBlockOutput").equalsIgnoreCase("false")) { Bukkit.getConsoleSender().sendMessage(result); return; } } Set<Permissible> users = Bukkit.getPluginManager().getPermissionSubscriptions(Server.BROADCAST_CHANNEL_ADMINISTRATIVE); String colored = ChatColor.GRAY + "" + ChatColor.ITALIC + "[" + result + ChatColor.GRAY + ChatColor.ITALIC + "]"; if (sendToSource && !(source instanceof ConsoleCommandSender)) { source.sendMessage(message); } for (Permissible user : users) { if (user instanceof CommandSender) { CommandSender target = (CommandSender) user; if (target instanceof ConsoleCommandSender) { target.sendMessage(result); } else if (target != source) { target.sendMessage(colored); } } } }
@Override public void subscribeToPermission(String permission, Permissible permissible) { String name = permission.toLowerCase(java.util.Locale.ENGLISH); Map<Permissible, Boolean> map = permSubs.get(name); if (map == null) { map = new WeakHashMap<Permissible, Boolean>(); permSubs.put(name, map); } map.put(permissible, true); }
@Override public void unsubscribeFromPermission(String permission, Permissible permissible) { String name = permission.toLowerCase(java.util.Locale.ENGLISH); Map<Permissible, Boolean> map = permSubs.get(name); if (map != null) { map.remove(permissible); if (map.isEmpty()) { permSubs.remove(name); } } }
@Override public Set<Permissible> getPermissionSubscriptions(String permission) { String name = permission.toLowerCase(java.util.Locale.ENGLISH); Map<Permissible, Boolean> map = permSubs.get(name); if (map == null) { return ImmutableSet.of(); } else { return ImmutableSet.copyOf(map.keySet()); } }
@Override public void subscribeToDefaultPerms(boolean op, Permissible permissible) { Map<Permissible, Boolean> map = defSubs.get(op); if (map == null) { map = new WeakHashMap<Permissible, Boolean>(); defSubs.put(op, map); } map.put(permissible, true); }
@Override public void unsubscribeFromDefaultPerms(boolean op, Permissible permissible) { Map<Permissible, Boolean> map = defSubs.get(op); if (map != null) { map.remove(permissible); if (map.isEmpty()) { defSubs.remove(op); } } }
@Override public Set<Permissible> getDefaultPermSubscriptions(boolean op) { Map<Permissible, Boolean> map = defSubs.get(op); if (map == null) { return ImmutableSet.of(); } else { return ImmutableSet.copyOf(map.keySet()); } }