Skip to content

Commit

Permalink
Modify give command to support 1.7 features. Fixes BUKKIT-5286
Browse files Browse the repository at this point in the history
Necessary additions include an interface to add internal value conversions
that are inappropriate for proper API design. This acts as a substitute
for properly formed, user-friendly commands in an effort to maintain
relatively vanilla behavior.
  • Loading branch information
Wolvereness committed Jan 15, 2014
1 parent bff28d8 commit ce94a35
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/main/java/org/bukkit/Bukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,12 @@ public static void setIdleTimeout(int threshold) {
public static int getIdleTimeout() {
return server.getIdleTimeout();
}

/**
* @see Server#getUnsafe()
*/
@Deprecated
public static UnsafeValues getUnsafe() {
return server.getUnsafe();
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/bukkit/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -799,4 +799,10 @@ public interface Server extends PluginMessageRecipient {
* @return the idle timeout in minutes
*/
public int getIdleTimeout();

/**
* @see UnsafeValues
*/
@Deprecated
UnsafeValues getUnsafe();
}
27 changes: 27 additions & 0 deletions src/main/java/org/bukkit/UnsafeValues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.bukkit;

import java.util.List;

import org.bukkit.inventory.ItemStack;

/**
* This interface provides value conversions that may be specific to a
* runtime, or have arbitrary meaning (read: magic values).
* <p>
* Their existence and behavior is not guaranteed across future versions. They
* may be poorly named, throw exceptions, have misleading parameters, or any
* other bad programming practice.
* <p>
* This interface is unsupported and only for internal use.
*
* @deprecated Unsupported & internal use only
*/
@Deprecated
public interface UnsafeValues {

Material getMaterialFromInternalName(String name);

List<String> tabCompleteInternalMaterialName(String token, List<String> completions);

ItemStack modifyItemStack(ItemStack stack, String arguments);
}
28 changes: 20 additions & 8 deletions src/main/java/org/bukkit/command/defaults/GiveCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bukkit.command.defaults;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -14,6 +15,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.StringUtil;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;

public class GiveCommand extends VanillaCommand {
Expand Down Expand Up @@ -47,6 +49,10 @@ public boolean execute(CommandSender sender, String currentAlias, String[] args)
if (player != null) {
Material material = Material.matchMaterial(args[1]);

if (material == null) {
material = Bukkit.getUnsafe().getMaterialFromInternalName(args[1]);
}

if (material != null) {
int amount = 1;
short data = 0;
Expand All @@ -61,7 +67,18 @@ public boolean execute(CommandSender sender, String currentAlias, String[] args)
}
}

player.getInventory().addItem(new ItemStack(material, amount, data));
ItemStack stack = new ItemStack(material, amount, data);

if (args.length >= 5) {
try {
stack = Bukkit.getUnsafe().modifyItemStack(stack, Joiner.on(' ').join(Arrays.asList(args).subList(4, args.length)));
} catch (Throwable t) {
player.sendMessage("Not a valid tag");
return true;
}
}

player.getInventory().addItem(stack);

Command.broadcastCommandMessage(sender, "Gave " + player.getName() + " some " + material.getId() + " (" + material + ")");
} else {
Expand All @@ -86,7 +103,7 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
if (args.length == 2) {
final String arg = args[1];
final List<String> materials = GiveCommand.materials;
List<String> completion = null;
List<String> completion = new ArrayList<String>();

final int size = materials.size();
int i = Collections.binarySearch(materials, arg, String.CASE_INSENSITIVE_ORDER);
Expand All @@ -99,18 +116,13 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
for ( ; i < size; i++) {
String material = materials.get(i);
if (StringUtil.startsWithIgnoreCase(material, arg)) {
if (completion == null) {
completion = new ArrayList<String>();
}
completion.add(material);
} else {
break;
}
}

if (completion != null) {
return completion;
}
return Bukkit.getUnsafe().tabCompleteInternalMaterialName(arg, completion);
}
return ImmutableList.of();
}
Expand Down

0 comments on commit ce94a35

Please sign in to comment.