Permalink
Browse files

Add pcooldown() and set_pcooldown()

  • Loading branch information...
PseudoKnight committed Nov 5, 2017
1 parent 8833e51 commit fd2e2d94b32d3a1282b1d7d23fbd802538babef8
@@ -1,5 +1,6 @@
package com.laytonsmith.abstraction;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.enums.MCGameMode;
/**
@@ -22,9 +23,11 @@
public MCInventory getEnderChest();
public MCInventoryView openWorkbench(MCLocation loc, boolean force);
//public MCInventoryView openWorkbench(MCLocation location, boolean force);
void setGameMode(MCGameMode mode);
void setItemInHand(MCItemStack item);
void setItemOnCursor(MCItemStack item);
void setGameMode(MCGameMode mode);
void setItemInHand(MCItemStack item);
void setItemOnCursor(MCItemStack item);
int getCooldown(MCMaterial material);
void setCooldown(MCMaterial material, int ticks);
//public boolean setWindowProperty(MCInventoryView.Property prop, int value);
}
@@ -647,9 +647,13 @@ public static MCCommandSender BukkitGetCorrectSender(CommandSender sender) {
@Override
public MCMaterial GetMaterial(String name) {
Material match = Material.matchMaterial(name);
Material match = Material.getMaterial(name);
if (match == null) {
return null;
// Try harder
match = Material.matchMaterial(name);
if(match == null){
return null;
}
}
return new BukkitMCMaterial(match);
}
@@ -41,10 +41,14 @@ public Object getHandle(){
@Override
public MCMaterialData getData(){
if(is == null || is.getData() == null){
if(is == null){
return null;
}
MaterialData md = is.getData();
if(md == null){
return null;
}
return new BukkitMCMaterialData(is.getData());
return new BukkitMCMaterialData(md);
}
@Override
@@ -5,11 +5,13 @@
import com.laytonsmith.abstraction.MCInventoryView;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCLocation;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.bukkit.BukkitMCInventory;
import com.laytonsmith.abstraction.bukkit.BukkitMCInventoryView;
import com.laytonsmith.abstraction.bukkit.BukkitMCItemStack;
import com.laytonsmith.abstraction.enums.MCGameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.Inventory;
@@ -88,6 +90,25 @@ public void setItemOnCursor(MCItemStack item) {
he.setItemOnCursor(((BukkitMCItemStack)item).asItemStack());
}
@Override
public int getCooldown(MCMaterial material) {
try {
return he.getCooldown((Material) material.getHandle());
} catch(NoSuchMethodError ex) {
// Probably prior to 1.11.2
return 0;
}
}
@Override
public void setCooldown(MCMaterial material, int ticks) {
try {
he.setCooldown((Material) material.getHandle(), ticks);
} catch(NoSuchMethodError ex) {
// Probably prior to 1.11.2
}
}
@Override
public MCInventoryView openInventory(MCInventory inventory) {
return new BukkitMCInventoryView(he.openInventory((Inventory)inventory.getHandle()));
@@ -10,6 +10,7 @@
import com.laytonsmith.abstraction.MCEntity;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCLivingEntity;
import com.laytonsmith.abstraction.MCMaterialData;
import com.laytonsmith.abstraction.MCMetadatable;
import com.laytonsmith.abstraction.MCOfflinePlayer;
import com.laytonsmith.abstraction.MCPlayer;
@@ -596,23 +597,20 @@ public static String getBlockPrefix() {
* @return
*/
public static MCItemStack ParseItemNotation(String functionName, String notation, int qty, Target t) {
int type = 0;
int type;
short data = 0;
MCItemStack is;
String[] sData = notation.split(":");
try {
type = Integer.parseInt(sData[0]);
if (sData.length > 1) {
data = (short) Integer.parseInt(sData[1]);
}
} catch (NumberFormatException e) {
try {
int separatorIndex = notation.indexOf(':');
if(separatorIndex != -1) {
type = Integer.parseInt(notation.substring(0, separatorIndex));
data = (short) Integer.parseInt(notation.substring(separatorIndex + 1));
} else {
type = Integer.parseInt(notation);
}
} catch (NumberFormatException e) {
throw new CREFormatException("Item value passed to " + functionName + " is invalid: " + notation, t);
}
is = StaticLayer.GetItemStack(type, qty);
is.setDurability(data);
//is.setData(new MaterialData(type, data));
return is;
return StaticLayer.GetItemStack(type, data, qty);
}
/**
@@ -628,8 +626,11 @@ public static String ParseItemNotation(MCItemStack is) {
String append = null;
if (is.getDurability() != 0) {
append = Short.toString(is.getDurability());
} else if (is.getData() != null) {
append = Integer.toString(is.getData().getData());
} else {
MCMaterialData md = is.getData();
if(md != null) {
append = Integer.toString(md.getData());
}
}
return is.getTypeId() + (append == null ? "" : ":" + append);
}
@@ -638,7 +639,8 @@ public static String ParseItemNotation(MCBlock b) {
if (b == null || b.isNull()) {
return "0";
}
return b.getTypeId() + (b.getData() == 0 ? "" : ":" + Byte.toString(b.getData()));
byte data = b.getData();
return b.getTypeId() + (data == 0 ? "" : ":" + Byte.toString(data));
}
private static Map<String, MCCommandSender> injectedPlayers = new HashMap<>();
@@ -15,6 +15,7 @@
import com.laytonsmith.abstraction.MCWorld;
import com.laytonsmith.abstraction.StaticLayer;
import com.laytonsmith.abstraction.blocks.MCBlock;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.entities.MCCommandMinecart;
import com.laytonsmith.abstraction.enums.MCGameMode;
import com.laytonsmith.abstraction.enums.MCSound;
@@ -5220,4 +5221,139 @@ public CHVersion since() {
}
}
@api
public static class pcooldown extends AbstractFunction {
@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CRELengthException.class, CREFormatException.class, CREPlayerOfflineException.class};
}
@Override
public boolean isRestricted() {
return true;
}
@Override
public Boolean runAsync() {
return false;
}
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
MCPlayer p;
String materialName;
if(args.length == 2) {
p = Static.GetPlayer(args[0], t);
materialName = args[1].val();
} else {
p = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
Static.AssertPlayerNonNull(p, t);
materialName = args[0].val();
}
MCMaterial mat = StaticLayer.GetMaterial(materialName);
if(mat == null){
throw new CREFormatException("Material name is invalid.", t);
}
return new CInt(p.getCooldown(mat), t);
}
@Override
public String getName() {
return "pcooldown";
}
@Override
public Integer[] numArgs() {
return new Integer[]{1, 2};
}
@Override
public String docs() {
return "int {[player], material} Gets the time left on the player's cooldown for the specified material."
+ " The material is the name found in item arrays. This returns an integer representing the"
+ " time in game ticks until items of this material can be used again by this player. (MC 1.11.2)";
}
@Override
public CHVersion since() {
return CHVersion.V3_3_2;
}
}
@api
public static class set_pcooldown extends AbstractFunction {
@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CRELengthException.class, CREFormatException.class, CREPlayerOfflineException.class,
CRERangeException.class};
}
@Override
public boolean isRestricted() {
return true;
}
@Override
public Boolean runAsync() {
return false;
}
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
MCPlayer p;
String materialName;
int cooldown;
if(args.length == 3) {
p = Static.GetPlayer(args[0], t);
materialName = args[1].val();
cooldown = Static.getInt32(args[2], t);
} else {
p = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
Static.AssertPlayerNonNull(p, t);
materialName = args[0].val();
cooldown = Static.getInt32(args[1], t);
}
MCMaterial mat = StaticLayer.GetMaterial(materialName);
if(mat == null){
throw new CREFormatException("Material name is invalid.", t);
}
if(cooldown < 0){
throw new CRERangeException("Cooldowns cannot be negative.", t);
}
p.setCooldown(mat, cooldown);
return CVoid.VOID;
}
@Override
public String getName() {
return "set_pcooldown";
}
@Override
public Integer[] numArgs() {
return new Integer[]{2, 3};
}
@Override
public String docs() {
return "int {[player], material, cooldown} Sets the player's cooldown time for the specified material."
+ " The material is the name found in item arrays. The cooldown must be a positive integer"
+ " representing game ticks. (MC 1.11.2)";
}
@Override
public CHVersion since() {
return CHVersion.V3_3_2;
}
}
}

0 comments on commit fd2e2d9

Please sign in to comment.