Merge branch 'API' into dev

This commit is contained in:
GeorgH93 2020-02-17 00:52:48 +01:00
commit 384393478c
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
6 changed files with 587 additions and 0 deletions

View File

@ -0,0 +1,128 @@
/*
* Copyright (C) 2019 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.API;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
@SuppressWarnings("unused")
public interface Backpack extends InventoryHolder
{
/**
* Gets the owner of the backpack.
*
* @return The owner of the backpack;
*/
@NotNull OfflinePlayer getOwner();
/**
* Let a given player open this backpack.
*
* @param player The player who opens the backpack.
* @param editable Defines if the player who has opened the backpack can change the items inside.
*/
void open(@NotNull Player player, boolean editable);
/**
* Let a given player open this backpack.
*
* @param player The player who opens the backpack.
* @param editable Defines if the player who has opened the backpack can change the items inside.
* @param title Custom title for the backpack (will be shown to the player who opened the backpack.
*/
void open(@NotNull Player player, boolean editable, @Nullable String title);
/**
* Checks if the backpack is currently opened by a player.
*
* @return True if the backpack is open, false if not.
*/
boolean isOpen();
/**
* Checks if a player can change the content of the backpack.
*
* @param player The player to be checked.
* @return True if he can change the content, false if not.
*/
boolean canEdit(@NotNull Player player);
/**
* Gets the size of the backpack.
*
* @return The size of the backpack.
*/
@SuppressWarnings("unused")
int getSize();
/**
* Checks if the backpack has changed since it was last saved.
*
* @return True if it has been changed, false if not.
*/
boolean hasChanged();
/**
* Marks that the content of the backpack a changed. It will be saved when the next player closes the backpack or before it gets removed from the cache.
*/
void setChanged();
/**
* Forces the backpack to be saved
*/
void save();
/**
* Removes all items from the backpack.
*/
void clear();
/**
* Drops the content of the backpack to the ground on a given location.
*
* @param location The location the content of the backpack should be dropped to.
*/
void drop(Location location);
/**
* @param stack The item stack that should be added to the backpack.
* @return null if the entire item stack has been added. An item stack containing the items that did not fit into the backpack.
*/
default @Nullable ItemStack addItem(ItemStack stack)
{
Map<Integer, ItemStack> left = addItems(stack);
if(left.isEmpty()) return null;
return left.get(0);
}
/**
* @param itemStacks The item that should be added to the backpack.
* @return A HashMap containing items that didn't fit. The key is the number of the added item
*/
default @NotNull Map<Integer, ItemStack> addItems(ItemStack... itemStacks)
{
return getInventory().addItem(itemStacks);
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (C) 2019 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.API;
public interface Callback<T>
{
void onResult(T done);
void onFail();
}

View File

@ -0,0 +1,56 @@
/*
* Copyright (C) 2019 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.API;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@SuppressWarnings("unused")
public interface ItemFilter
{
/**
* @param item The item that should be checked.
* @return True if the item is not allowed. False if the item is allowed.
*/
@Contract("null->false")
boolean isItemBlocked(@Nullable ItemStack item);
/**
* @param player The player that should receive the message that the item is not allowed.
* @param itemStack The item that is not allowed. Will be used for the name.
*/
void sendNotAllowedMessage(@NotNull Player player, @NotNull ItemStack itemStack);
/**
* @param player The player that should receive the message if the item is not allowed.
* @param itemStack The item that should be checked.
* @return True if the item is not allowed. False if the item is allowed.
*/
default boolean checkIsBlockedAndShowMessage(@NotNull Player player, @Nullable ItemStack itemStack)
{
if(isItemBlocked(itemStack))
{
sendNotAllowedMessage(player, itemStack);
return true;
}
return false;
}
}

View File

@ -0,0 +1,201 @@
/*
* Copyright (C) 2019 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.API;
import at.pcgamingfreaks.Bukkit.Command.SubCommand;
import at.pcgamingfreaks.Bukkit.Message.Message;
import at.pcgamingfreaks.Command.HelpData;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
/**
* Only available if the plugin is not running in standalone mode!
*/
public abstract class MinepacksCommand extends SubCommand
{
private static MinepacksPlugin minepacksPlugin = null;
private static Method showHelp = null;
private static Message messageNoPermission = new Message(ChatColor.RED + "You don't have the permission to do that.");
private static Message messageNotFromConsole = new Message(ChatColor.RED + "This command can't be used from console!");
protected final JavaPlugin plugin;
private final boolean playerOnly;
//region Constructors
/**
* Creates a new command instance.
*
* @param plugin The plugin owning the command.
* @param name The command used.
* @param description The description of the command.
* @param aliases List of aliases for that command.
*/
public MinepacksCommand(@NotNull JavaPlugin plugin, @NotNull String name, @NotNull String description, @Nullable String... aliases)
{
this(plugin, name, description, null, aliases);
}
/**
* Creates a new command instance.
*
* @param plugin The plugin owning the command.
* @param name The command used.
* @param description The description of the command.
* @param permission The permission to be checked for this command. Players without the permission neither can use the command nor will they see it in help.
* @param aliases List of aliases for that command.
*/
public MinepacksCommand(@NotNull JavaPlugin plugin, @NotNull String name, @NotNull String description, @Nullable String permission, @Nullable String... aliases)
{
this(plugin, name, description, permission, false, aliases);
}
/**
* Creates a new command instance.
*
* @param plugin The plugin owning the command.
* @param name The command used.
* @param description The description of the command.
* @param permission The permission to be checked for this command. Players without the permission neither can use the command nor will they see it in help.
* @param playerOnly Limits the command to players, console can't use and can't see the command.
* @param aliases List of aliases for that command.
*/
public MinepacksCommand(@NotNull JavaPlugin plugin, @NotNull String name, @NotNull String description, @Nullable String permission, boolean playerOnly, @Nullable String... aliases)
{
super(name, description, permission, aliases);
this.plugin = plugin;
this.playerOnly = playerOnly;
}
//endregion
/**
* Gets the instance of the marriage master plugin.
*
* @return The instance of the marriage master plugin.
*/
protected @NotNull MinepacksPlugin getMinepacksPlugin()
{
return minepacksPlugin;
}
//region Command Stuff
/**
* Executes some basic checks and runs the command afterwards.
*
* @param sender Source of the command.
* @param mainCommandAlias Alias of the plugins main command which was used.
* @param alias Alias of the command which has been used.
* @param args Passed command arguments.
*/
@Override
public void doExecute(@NotNull CommandSender sender, @NotNull String mainCommandAlias, @NotNull String alias, @NotNull String... args)
{
if(playerOnly && !(sender instanceof Player))
{
messageNotFromConsole.send(sender);
}
else if(getPermission() != null && !sender.hasPermission(getPermission()))
{
messageNoPermission.send(sender);
}
else
{
execute(sender, mainCommandAlias, alias, args);
}
}
/**
* Executes some basic checks and generates list for tab completion.
*
* @param sender Source of the command.
* @param mainCommandAlias Alias of the plugins main command which has been used.
* @param alias The alias used.
* @param args The arguments passed to the command, including final partial argument to be completed and command label.
* @return A List of possible completions for the final argument, or null to default to the command executor.
*/
@Override
public List<String> doTabComplete(@NotNull CommandSender sender, @NotNull String mainCommandAlias, @NotNull String alias, @NotNull String... args)
{
if(playerOnly && !(sender instanceof Player))
{
messageNotFromConsole.send(sender);
}
else if(getPermission() != null && !sender.hasPermission(getPermission()))
{
messageNoPermission.send(sender);
}
else
{
return tabComplete(sender, mainCommandAlias, alias, args);
}
return null;
}
/**
* Gets the help for a given {@link CommandSender}.
*
* @param requester The {@link CommandSender} that requested help.
* @return All the help data for this command.
*/
@Override
public @Nullable List<HelpData> getHelp(@NotNull CommandSender requester)
{
List<HelpData> help = new LinkedList<>();
help.add(new HelpData(getTranslatedName(), null, getDescription()));
return help;
}
/**
* Shows the help to a given command sender.
*
* @param sendTo The command sender that requested help.
* @param usedMainCommandAlias The used backpack alias to replace the /backpack with the used alias.
*/
@Override
public void showHelp(@NotNull CommandSender sendTo, @NotNull String usedMainCommandAlias)
{
try
{
showHelp.invoke(getMinepacksPlugin().getCommandManager(), sendTo, usedMainCommandAlias, doGetHelp(sendTo));
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* Checks if a user can use the command. Checks permission, marriage status and player/console.
*
* @param sender The player/console that should be checked.
* @return True if it can use the command, false if not.
*/
@Override
public boolean canUse(@NotNull CommandSender sender)
{
return (!playerOnly || sender instanceof Player) && (getPermission() == null || sender.hasPermission(getPermission()));
}
//endregion
}

View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2019 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.API;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings("unused")
public interface MinepacksCommandManager
{
/**
* Registers a new sub-command for /backpack.
* This function is only available if the plugin is not running in standalone mode!
*
* @param command The command that should be registered.
*/
void registerSubCommand(@NotNull MinepacksCommand command);
/**
* Unregisters a sub-command for /backpack.
* This function is only available if the plugin is not running in standalone mode!
*
* @param command The command that should be unregistered.
*/
void unRegisterSubCommand(@NotNull MinepacksCommand command);
}

View File

@ -0,0 +1,137 @@
/*
* Copyright (C) 2019 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.API;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@SuppressWarnings("unused")
public interface MinepacksPlugin
{
/**
* Gets the instance of the minepacks plugin.
* WARNING use this function at your own risk! If the plugin is not installed the MinepacksPlugin class will be unknown!
*
* @return The instance of the minepacks plugin.
*/
static @Nullable MinepacksPlugin getInstance()
{
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("Minepacks");
return (plugin instanceof MinepacksPlugin && plugin.isEnabled()) ? (MinepacksPlugin) plugin : null;
}
/**
* Checks if the plugin is running in standalone mode. Some features and API functions are not available in standalone mode!
*
* @return True if the plugin is running in standalone mode.
*/
boolean isRunningInStandaloneMode();
/**
* Let a given player open the backpack of an other player.
*
* @param opener The player who opens the backpack.
* @param owner The owner of the backpack that should be opened.
* @param editable Defines if the player who has opened the backpack can change the items inside.
*/
void openBackpack(@NotNull final Player opener, @NotNull final OfflinePlayer owner, final boolean editable);
/**
* Let a given player open a given {@link Backpack}.
*
* @param opener The player who opens the backpack.
* @param backpack The backpack to be opened. null will result in an error message for the player.
* @param editable Defines if the player who has opened the backpack can change the items inside.
*/
void openBackpack(@NotNull final Player opener, @Nullable final Backpack backpack, boolean editable);
/**
* Let a given player open the backpack of an other player.
*
* @param opener The player who opens the backpack.
* @param owner The owner of the backpack that should be opened.
* @param editable Defines if the player who has opened the backpack can change the items inside.
* @param title Custom title for the backpack (will be shown to the player who opened the backpack.
*/
void openBackpack(@NotNull final Player opener, @NotNull final OfflinePlayer owner, final boolean editable, @Nullable String title);
/**
* Let a given player open a given {@link Backpack}.
*
* @param opener The player who opens the backpack.
* @param backpack The backpack to be opened. null will result in an error message for the player.
* @param editable Defines if the player who has opened the backpack can change the items inside.
* @param title Custom title for the backpack (will be shown to the player who opened the backpack.
*/
void openBackpack(@NotNull final Player opener, @Nullable final Backpack backpack, boolean editable, @Nullable String title);
/**
* Retrieves the backpack for a given player.
* This method only returns a backpack if it is in the cache.
*
* @param owner The player who's backpack should be retrieved.
* @return The backpack of the given player. null if the backpack is in the cache.
*/
@Nullable Backpack getBackpackCachedOnly(@NotNull final OfflinePlayer owner);
/**
* Retrieves the backpack for a given player.
* This method runs async! The result will be delivered with a callback.
* If no backpack exists a new one will be created.
*
* @param owner The player who's backpack should be retrieved.
* @param callback The callback delivering the result of the request.
*/
void getBackpack(@NotNull final OfflinePlayer owner, @NotNull final Callback<Backpack> callback);
/**
* Retrieves the backpack for a given player.
* This method runs async! The result will be delivered with a callback.
*
* @param owner The player who's backpack should be retrieved.
* @param callback The callback delivering the result of the request.
* @param createNewIfNotExists If set to true, a new backpack will be created if there currently is no backpack for this player.
*/
void getBackpack(@NotNull final OfflinePlayer owner, @NotNull final Callback<Backpack> callback, boolean createNewIfNotExists);
/**
* Gets the command manager of the Minepacks plugin.
*
* @return The command manager instance. null if the plugin is running in standalone mode
*/
@Nullable MinepacksCommandManager getCommandManager();
/**
* Checks if the player is allowed to open a backpack based on is permissions and current game-mode.
*
* @param player The player to be checked.
* @return True if the player can use a backpack. False if not.
*/
boolean isPlayerGameModeAllowed(final @NotNull Player player);
/**
* Gets the item filter.
*
* @return The item filter. Null if item filter is disabled
*/
@Nullable ItemFilter getItemFilter();
}