Add create command.

This commit is contained in:
Jesse Prescott 2018-05-31 14:23:22 +01:00
parent 7bbcba8e17
commit c8dca8dd3b
9 changed files with 365 additions and 5 deletions

View File

@ -18,7 +18,7 @@ import net.md_5.bungee.api.ChatColor;
import org.bukkit.entity.Player;
/**
* Utility class used for communicating in different dialects.
* Utility class used for communicating to players in game with a translator.
*
* @author Jesse Prescott (BleedObsidian).
*/
@ -33,7 +33,7 @@ public final class ChatLogger {
/**
* Language translator.
*/
private LanguageTranslator translator;
private final LanguageTranslator translator;
/**
* Constructor.

View File

@ -0,0 +1,95 @@
/*
* ItemCase 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/gpl.html>.
*/
package com.gmail.bleedobsidian.itemcase;
import com.gmail.bleedobsidian.itemcase.commands.CreateCommand;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
/**
* The central command handler for all ItemCase commands.
*
* @author Jesse Prescott (BleedObsidian)
*/
public final class CommandHandler implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command,
String label, String[] args) {
// Check we have atleast 1 argument (sub-command label).
if(args.length <= 0) {
// Show help message.
this.showHelp(sender, label);
// We have handled this error.
return true;
}
// Direct sub-command to corresponding command handler.
switch(args[0]) {
case "create":
new CreateCommand().execute(sender, label, args);
break;
default:
this.showHelp(sender, label);
break;
}
// We have handled this command.
return true;
}
/**
* Show help for main command to given sender.
*
* @param sender CommandSender.
*/
private void showHelp(CommandSender sender, String label) {
// Get translator.
LanguageTranslator translator = ItemCaseCore.instance.getTranslator();
// Set placeholder.
translator.setPlaceholder("%COMMAND%",
"/" + label + " [create/destroy/modify/storage]");
// Check if sender is a player or console.
if(sender instanceof Player) {
// Cast sender to player.
Player player = (Player) sender;
// Get chat logger.
ChatLogger logger = ItemCaseCore.instance.getChatLogger();
// Send message to player.
logger.message(player, "command.itemcase-help");
} else {
// Get console logger.
GenericLogger logger = ItemCaseCore.instance.getGenericLogger();
// Send console message.
logger.message(sender, "command.itemcase-help");
}
}
}

View File

@ -0,0 +1,62 @@
/*
* ItemCase 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/gpl.html>.
*/
package com.gmail.bleedobsidian.itemcase;
import org.bukkit.command.CommandSender;
/**
* Utility class used for communicating to command senders in different
* dialects.
*
* @author Jesse Prescott (BleedObsidian).
*/
public final class GenericLogger {
/**
* Language translator.
*/
private final LanguageTranslator translator;
/**
* Constructor.
*
* @param translator language translator.
*/
public GenericLogger(LanguageTranslator translator) {
// Set translator.
this.translator = translator;
}
/**
* Send given CommandSender a message.
*
* @param sender CommandSender.
* @param string Message key or message.
*/
public void message(CommandSender sender, String string) {
// If string is a message key.
if(this.translator.isKey(string)) {
// Translate.
string = this.translator.getTranslation(string);
}
// Send message.
sender.sendMessage(string);
}
}

View File

@ -55,6 +55,11 @@ public final class ItemCaseCore extends JavaPlugin {
*/
private final ChatLogger chatLogger = new ChatLogger(translator);
/**
* Generic Logger.
*/
private final GenericLogger genericLogger = new GenericLogger(translator);
/**
* ItemcaseManager.
*/
@ -112,6 +117,9 @@ public final class ItemCaseCore extends JavaPlugin {
// Load itemcases for already loaded worlds.
this.itemcaseManager.initialize();
// Set command executor.
this.getCommand("itemcase").setExecutor(new CommandHandler());
// Set version placeholder and log.
this.translator.setPlaceholder("%VERSION%",
this.getDescription().getVersion());
@ -150,6 +158,20 @@ public final class ItemCaseCore extends JavaPlugin {
return this.consoleLogger;
}
/**
* @return Chat logger.
*/
public ChatLogger getChatLogger() {
return this.chatLogger;
}
/**
* @return Generic Logger.
*/
public GenericLogger getGenericLogger() {
return this.genericLogger;
}
/**
* @return Itemcase manager.
*/

View File

@ -0,0 +1,34 @@
/*
* ItemCase 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/gpl.html>.
*/
package com.gmail.bleedobsidian.itemcase.commands;
import org.bukkit.command.CommandSender;
/**
* An interface for all ItemCase commands.
*
* @author Jesse Prescott (BleedObsidian)
*/
public interface Command {
/**
* Execute this command with the given arguments.
*
* @param sender The command sender.
* @param label The label used,
* @param args Any arguments.
*/
public void execute(CommandSender sender, String label, String[] args);
}

View File

@ -0,0 +1,107 @@
/*
* ItemCase 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/gpl.html>.
*/
package com.gmail.bleedobsidian.itemcase.commands;
import com.gmail.bleedobsidian.itemcase.ChatLogger;
import com.gmail.bleedobsidian.itemcase.ItemCaseCore;
import java.util.ArrayList;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/**
* A command handler for the 'create' command.
*
* @author Jesse Prescott (BleedObsidian)
*/
public final class CreateCommand implements Command {
@Override
public void execute(CommandSender sender, String label,
String[] args) {
// If sender is not a player.
if(!(sender instanceof Player)) {
// Send message.
ItemCaseCore.instance.getGenericLogger().message(
sender, "command.not-player");
// Exit.
return;
}
// Get chat logger.
ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();
// Cast sender to player.
Player player = (Player) sender;
// Check if player has permission.
if(!player.hasPermission("itemcase.create")) {
// Send message.
chatLogger.message(player, "command.permission");
// Exit.
return;
}
// List of materials that can be used as itemcases.
ArrayList<Material> materials =
ItemCaseCore.instance.getConfigFile().getMaterials();
// The target location.
Location target = null;
// ItemStack to use.
ItemStack itemStack = null;
// Get the players target block.
target = player.getTargetBlock(null, 3).getLocation();
// If target block is not on the list of accepted materials.
if(!materials.contains(target.getBlock().getType())) {
// Show message.
chatLogger.message(player, "command.create.invalid-type");
// Exit.
return;
}
// Get item in players main hand to use as the Itemcase item.
itemStack = player.getInventory().getItemInMainHand();
// If the player is not holding anything...
if(itemStack == null || itemStack.getType() == Material.AIR) {
// Show message.
chatLogger.message(player, "command.create.main-hand");
// Exit.
return;
}
// Create itemcase.
ItemCaseCore.instance.getItemcaseManager().createItemcase(
itemStack, target, player);
// Show message.
chatLogger.message(player, "command.create.success");
}
}

View File

@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -109,7 +110,7 @@ public final class ItemcaseManager {
* @param owner The owner of this itemcase.
*/
public void createItemcase(ItemStack itemStack, Location location,
Player owner) {
OfflinePlayer owner) {
// Create new itemcase instance.
Itemcase itemcase = new Itemcase(itemStack, location, owner);

View File

@ -12,9 +12,39 @@ console:
#
# %LANGUAGE% = The language as set in the main config.
# %VERSION% = The version of ItemCase in use.
# %WORLD_NAME% = The name of the world.
locale: "Using locale: %LANGUAGE%"
config-loaded: "Successfully loaded main configuration file."
listener-registered: "Main event listener registered."
enabled: "ItemCase v%VERSION% enabled."
unloaded: "Itemcases unloaded successfully."
loaded: "Loaded itemcases for world: %WORLD_NAME%"
loaded: "Loaded itemcases for world: %WORLD_NAME%"
# Messages shown when using commands.
command:
# Shown when a user incorrectly uses the main itemcase command.
# %COMMAND% = The command usage syntax.
itemcase-help: "Usage: %COMMAND%"
# Shown when a user tries to execute a command and they are not an in game
# player.
not-player: "You must be a player to use this command."
# Shown when a user tries to execute a command and they do not have the
# required permission.
permission: "You do not have permission to use this command."
# Messages shown when using the 'create' command.
create:
# Shown to the player when they try to make an itemcase out of an
# invalid block type.
invalid-type: "This block cannot be used for an ItemCase."
# Shown to the player when they use the create command but they are not
# holding any item in their main hand.
main-hand: "You must be holding something in your main hand."
# Shown to the player upon successful creation.
success: "ItemCase created."

View File

@ -7,4 +7,13 @@ website: http://dev.bukkit.org/bukkit-plugins/itemcase/
database: false
prefix: ItemCase
main: com.gmail.bleedobsidian.itemcase.ItemCaseCore
main: com.gmail.bleedobsidian.itemcase.ItemCaseCore
commands:
itemcase:
aliases: [itemc, ic]
description: "Showcase items on slabs, that can also be used as shops."
permissions:
itemcase.create:
description: Create and destroy showcase itemcases.