mirror of
https://github.com/essentials/Essentials.git
synced 2025-08-26 16:04:27 +02:00
Add /recipe command
This commit is contained in:
11
Essentials/src/messages.properties
Normal file → Executable file
11
Essentials/src/messages.properties
Normal file → Executable file
@@ -493,3 +493,14 @@ metrics4=This will start 5 minutes after the first admin/op joins.
|
||||
userNotFound=User not found!
|
||||
flyFailed=Can't change fly mode for {0}
|
||||
deniedWorldAccess=You do not have permission to access world {0}
|
||||
recipeNone=No recipes exist for {0}
|
||||
invalidNumber=Invalid Number
|
||||
recipeBadIndex=There is no recipe by that number
|
||||
recipeNothing=nothing
|
||||
recipeShapeless=\u00a76Combine \u00a7c{0}
|
||||
recipe=\u00a76Recipe for \u00a7c{0}\u00a76 ({1} of {2})
|
||||
recipeFurnace=\u00a76Smelt \u00a7c{0}
|
||||
recipeGrid=\u00a7{0}X \u00a76| \u00a7{1}X \u00a76| \u00a7{2}X
|
||||
recipeGridItem=\ \u00a7{0}X \u00a76is \u00a7c{1}
|
||||
recipeMore=\u00a76Type /{0} \u00a7c{1}\u00a76 <number> to see other recipes for \u00a7c{2}
|
||||
recipeWhere=\u00a76Where: {0}
|
4
Essentials/src/net/ess3/api/IUser.java
Normal file → Executable file
4
Essentials/src/net/ess3/api/IUser.java
Normal file → Executable file
@@ -123,6 +123,10 @@ public interface IUser extends OfflinePlayer, CommandSender, IStorageObjectHolde
|
||||
|
||||
boolean checkSignThrottle(int throttle);
|
||||
|
||||
public boolean isRecipeSee();
|
||||
|
||||
public void setRecipeSee(boolean recipeSee);
|
||||
|
||||
/**
|
||||
* Since the Player object should not be stored for a long time, this method should be called again with a null
|
||||
* value.
|
||||
|
159
Essentials/src/net/ess3/commands/Commandrecipe.java
Executable file
159
Essentials/src/net/ess3/commands/Commandrecipe.java
Executable file
@@ -0,0 +1,159 @@
|
||||
package net.ess3.commands;
|
||||
|
||||
import static net.ess3.I18n._;
|
||||
|
||||
import net.ess3.api.IUser;
|
||||
import net.ess3.user.User;
|
||||
import net.ess3.utils.Util;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
public class Commandrecipe extends EssentialsCommand
|
||||
{
|
||||
|
||||
@Override
|
||||
public void run(final CommandSender sender, final String commandLabel, final String[] args) throws Exception
|
||||
{
|
||||
if (args.length < 1)
|
||||
{
|
||||
throw new NotEnoughArgumentsException();
|
||||
}
|
||||
final ItemStack item = ess.getItemDb().get(args[0]);
|
||||
final List<Recipe> recipes = ess.getServer().getRecipesFor(item);
|
||||
if (recipes.size() < 1)
|
||||
{
|
||||
throw new Exception(_("recipeNone", getMaterialName(item)));
|
||||
}
|
||||
int recipeNo = 0;
|
||||
if (args.length > 1)
|
||||
{
|
||||
if (Util.isInt(args[1]))
|
||||
{
|
||||
recipeNo = Integer.parseInt(args[1]) - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception(_("invalidNumber"));
|
||||
}
|
||||
}
|
||||
if (recipeNo < 0 || recipeNo >= recipes.size())
|
||||
{
|
||||
throw new Exception(_("recipeBadIndex"));
|
||||
}
|
||||
final Recipe recipe = recipes.get(recipeNo);
|
||||
sender.sendMessage(_("recipe", getMaterialName(item), recipeNo + 1, recipes.size()));
|
||||
if (recipe instanceof FurnaceRecipe)
|
||||
{
|
||||
furnaceRecipe(sender, (FurnaceRecipe)recipe);
|
||||
}
|
||||
else if (recipe instanceof ShapedRecipe)
|
||||
{
|
||||
shapedRecipe(sender, (ShapedRecipe)recipe);
|
||||
}
|
||||
else if (recipe instanceof ShapelessRecipe)
|
||||
{
|
||||
shapelessRecipe(sender, (ShapelessRecipe)recipe);
|
||||
}
|
||||
if (recipes.size() > 1 && args.length == 1)
|
||||
{
|
||||
sender.sendMessage(_("recipeMore", commandLabel, args[0], getMaterialName(item)));
|
||||
}
|
||||
}
|
||||
|
||||
public void furnaceRecipe(CommandSender sender, FurnaceRecipe recipe)
|
||||
{
|
||||
sender.sendMessage(_("recipeFurnace", getMaterialName(recipe.getInput())));
|
||||
}
|
||||
|
||||
public void shapedRecipe(CommandSender sender, ShapedRecipe recipe)
|
||||
{
|
||||
Map<Character, ItemStack> recipeMap = recipe.getIngredientMap();
|
||||
if (sender instanceof IUser)
|
||||
{
|
||||
IUser user = getUser(sender);
|
||||
user.setRecipeSee(true);
|
||||
InventoryView view = user.getPlayer().openWorkbench(null, true);
|
||||
for (Map.Entry<Character, ItemStack> e : recipe.getIngredientMap().entrySet())
|
||||
{
|
||||
view.setItem(" abcdefghi".indexOf(e.getKey()), e.getValue());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HashMap<ItemStack, String> colorMap = new HashMap<ItemStack, String>();
|
||||
int i = 1;
|
||||
for (Character c : "abcdefghi".toCharArray())
|
||||
{
|
||||
if (!colorMap.containsKey(recipeMap.get(c)))
|
||||
{
|
||||
colorMap.put(recipeMap.get(c), String.valueOf(i++));
|
||||
}
|
||||
}
|
||||
sender.sendMessage(_("recipeGrid", colorMap.get(recipeMap.get('a')), colorMap.get(recipeMap.get('b')), colorMap.get(recipeMap.get('c'))));
|
||||
sender.sendMessage(_("recipeGrid", colorMap.get(recipeMap.get('d')), colorMap.get(recipeMap.get('e')), colorMap.get(recipeMap.get('f'))));
|
||||
sender.sendMessage(_("recipeGrid", colorMap.get(recipeMap.get('g')), colorMap.get(recipeMap.get('h')), colorMap.get(recipeMap.get('i'))));
|
||||
|
||||
StringBuilder s = new StringBuilder();
|
||||
for (ItemStack items : colorMap.keySet().toArray(new ItemStack[colorMap.size()]))
|
||||
{
|
||||
s.append(_("recipeGridItem", colorMap.get(items), getMaterialName(items)));
|
||||
}
|
||||
sender.sendMessage(_("recipeWhere", s.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
public void shapelessRecipe(CommandSender sender, ShapelessRecipe recipe)
|
||||
{
|
||||
List<ItemStack> ingredients = recipe.getIngredientList();
|
||||
if (sender instanceof IUser)
|
||||
{
|
||||
IUser user = getUser(sender);
|
||||
user.setRecipeSee(true);
|
||||
InventoryView view = user.getPlayer().openWorkbench(null, true);
|
||||
for (int i = 0; i < ingredients.size(); i++)
|
||||
{
|
||||
view.setItem(i + 1, ingredients.get(i));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ess.getLogger().info(sender.getClass().getName());
|
||||
StringBuilder s = new StringBuilder();
|
||||
for (int i = 0; i < ingredients.size(); i++)
|
||||
{
|
||||
s.append(getMaterialName(ingredients.get(i)));
|
||||
if (i != ingredients.size() - 1)
|
||||
{
|
||||
s.append(",");
|
||||
}
|
||||
s.append(" ");
|
||||
}
|
||||
sender.sendMessage(_("recipeShapeless", s.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
public String getMaterialName(ItemStack stack)
|
||||
{
|
||||
if (stack == null)
|
||||
{
|
||||
return _("recipeNothing");
|
||||
}
|
||||
return getMaterialName(stack.getType());
|
||||
}
|
||||
|
||||
public String getMaterialName(Material type)
|
||||
{
|
||||
if (type == null)
|
||||
{
|
||||
return _("recipeNothing");
|
||||
}
|
||||
return type.toString().replace("_", " ").toLowerCase(Locale.ENGLISH);
|
||||
}
|
||||
}
|
17
Essentials/src/net/ess3/listener/EssentialsPlayerListener.java
Normal file → Executable file
17
Essentials/src/net/ess3/listener/EssentialsPlayerListener.java
Normal file → Executable file
@@ -495,6 +495,14 @@ public class EssentialsPlayerListener implements Listener
|
||||
}
|
||||
}
|
||||
}
|
||||
if (event.getView().getTopInventory().getType() == InventoryType.WORKBENCH)
|
||||
{
|
||||
final IUser user = ess.getUserMap().getUser((Player)event.getWhoClicked());
|
||||
if(user.isRecipeSee())
|
||||
{
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
@@ -505,6 +513,15 @@ public class EssentialsPlayerListener implements Listener
|
||||
final IUser user = ess.getUserMap().getUser((Player)event.getPlayer());
|
||||
user.setInvSee(false);
|
||||
}
|
||||
else if (event.getView().getTopInventory().getType() == InventoryType.WORKBENCH)
|
||||
{
|
||||
final IUser user = ess.getUserMap().getUser((Player)event.getPlayer());
|
||||
if(user.isRecipeSee())
|
||||
{
|
||||
user.setRecipeSee(false);
|
||||
event.getView().getTopInventory().clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
|
3
Essentials/src/net/ess3/user/User.java
Normal file → Executable file
3
Essentials/src/net/ess3/user/User.java
Normal file → Executable file
@@ -54,6 +54,9 @@ public class User extends UserBase implements IUser
|
||||
private transient Location afkPosition;
|
||||
private AtomicBoolean gotMailInfo = new AtomicBoolean(false);
|
||||
private WeakReference<Player> playerCache;
|
||||
@Getter
|
||||
@Setter
|
||||
private boolean recipeSee = false;
|
||||
|
||||
public User(final OfflinePlayer base, final IEssentials ess) throws InvalidNameException
|
||||
{
|
||||
|
4
Essentials/src/plugin.yml
Normal file → Executable file
4
Essentials/src/plugin.yml
Normal file → Executable file
@@ -278,6 +278,10 @@ commands:
|
||||
description: Displays the username of a user based on nick.
|
||||
usage: /<command> <nickname>
|
||||
aliases: [erealname]
|
||||
recipe:
|
||||
description: Displays how to craft items.
|
||||
usage: /<command> <item> [number]
|
||||
aliases: [erecipe]
|
||||
remove:
|
||||
description: Removes entities in your world
|
||||
usage: /<command> <drops|arrows|boats|minecarts|xp|paintings> [radius]
|
||||
|
Reference in New Issue
Block a user