Skip to content

Commit 7cd5c70

Browse files
committed
Add has/give recipe functions for players
1 parent 59d077d commit 7cd5c70

File tree

3 files changed

+149
-3
lines changed

3 files changed

+149
-3
lines changed

src/main/java/com/laytonsmith/abstraction/MCHumanEntity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ public interface MCHumanEntity extends MCInventoryHolder, MCLivingEntity, MCAnim
4444
//boolean setWindowProperty(MCInventoryView.Property prop, int value);
4545

4646
float getAttackCooldown();
47+
48+
boolean discoverRecipe(MCNamespacedKey recipe);
49+
50+
boolean hasDiscoveredRecipe(MCNamespacedKey recipe);
4751
}

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCHumanEntity.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
import com.laytonsmith.abstraction.MCItemStack;
77
import com.laytonsmith.abstraction.MCLocation;
88
import com.laytonsmith.abstraction.MCMerchant;
9+
import com.laytonsmith.abstraction.MCNamespacedKey;
910
import com.laytonsmith.abstraction.blocks.MCMaterial;
1011
import com.laytonsmith.abstraction.bukkit.BukkitMCInventory;
1112
import com.laytonsmith.abstraction.bukkit.BukkitMCInventoryView;
1213
import com.laytonsmith.abstraction.bukkit.BukkitMCItemStack;
1314
import com.laytonsmith.abstraction.enums.MCGameMode;
1415
import org.bukkit.Location;
1516
import org.bukkit.Material;
17+
import org.bukkit.NamespacedKey;
1618
import org.bukkit.entity.Entity;
1719
import org.bukkit.entity.HumanEntity;
1820
import org.bukkit.inventory.Inventory;
19-
import org.bukkit.inventory.ItemStack;
2021
import org.bukkit.inventory.Merchant;
2122

2223
public class BukkitMCHumanEntity extends BukkitMCLivingEntity implements MCHumanEntity {
@@ -49,8 +50,7 @@ public MCGameMode getGameMode() {
4950

5051
@Override
5152
public MCItemStack getItemInHand() {
52-
ItemStack is = he.getInventory().getItemInMainHand();
53-
return is == null ? null : new BukkitMCItemStack(is);
53+
return new BukkitMCItemStack(he.getInventory().getItemInMainHand());
5454
}
5555

5656
@Override
@@ -132,4 +132,14 @@ public MCInventoryView openMerchant(MCMerchant merchant, boolean force) {
132132
public MCInventoryView openEnchanting(MCLocation loc, boolean force) {
133133
return new BukkitMCInventoryView(he.openEnchanting((Location) loc.getHandle(), force));
134134
}
135+
136+
@Override
137+
public boolean discoverRecipe(MCNamespacedKey recipe) {
138+
return he.discoverRecipe((NamespacedKey) recipe.getHandle());
139+
}
140+
141+
@Override
142+
public boolean hasDiscoveredRecipe(MCNamespacedKey recipe) {
143+
return he.hasDiscoveredRecipe((NamespacedKey) recipe.getHandle());
144+
}
135145
}

src/main/java/com/laytonsmith/core/functions/PlayerManagement.java

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.laytonsmith.abstraction.MCHumanEntity;
1111
import com.laytonsmith.abstraction.MCItemStack;
1212
import com.laytonsmith.abstraction.MCLocation;
13+
import com.laytonsmith.abstraction.MCNamespacedKey;
1314
import com.laytonsmith.abstraction.MCOfflinePlayer;
1415
import com.laytonsmith.abstraction.MCPlayer;
1516
import com.laytonsmith.abstraction.MCServer;
@@ -6703,4 +6704,135 @@ public Boolean runAsync() {
67036704
return false;
67046705
}
67056706
}
6707+
6708+
@api
6709+
public static class phas_recipe extends AbstractFunction {
6710+
6711+
@Override
6712+
public String getName() {
6713+
return "phas_recipe";
6714+
}
6715+
6716+
@Override
6717+
public String docs() {
6718+
return "boolean {[player], recipeKey} Gets whether a player has a recipe in their recipe book.";
6719+
}
6720+
6721+
@Override
6722+
public Integer[] numArgs() {
6723+
return new Integer[]{1, 2};
6724+
}
6725+
6726+
@Override
6727+
public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException {
6728+
MCPlayer p;
6729+
String stringKey;
6730+
if(args.length == 1) {
6731+
p = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
6732+
Static.AssertPlayerNonNull(p, t);
6733+
stringKey = args[0].val();
6734+
} else {
6735+
p = Static.GetPlayer(args[0], t);
6736+
stringKey = args[1].val();
6737+
}
6738+
MCNamespacedKey key = StaticLayer.GetConvertor().GetNamespacedKey(stringKey);
6739+
if(key == null) {
6740+
throw new CREFormatException("Invalid namespaced key format: " + stringKey, t);
6741+
}
6742+
return CBoolean.get(p.hasDiscoveredRecipe(key));
6743+
}
6744+
6745+
@Override
6746+
public Class<? extends CREThrowable>[] thrown() {
6747+
return new Class[]{CREPlayerOfflineException.class, CRELengthException.class, CREFormatException.class};
6748+
}
6749+
6750+
@Override
6751+
public Version since() {
6752+
return MSVersion.V3_3_5;
6753+
}
6754+
6755+
@Override
6756+
public boolean isRestricted() {
6757+
return true;
6758+
}
6759+
6760+
@Override
6761+
public Boolean runAsync() {
6762+
return false;
6763+
}
6764+
}
6765+
6766+
@api
6767+
public static class pgive_recipe extends AbstractFunction {
6768+
6769+
@Override
6770+
public String getName() {
6771+
return "pgive_recipe";
6772+
}
6773+
6774+
@Override
6775+
public String docs() {
6776+
return "int {[player], recipeKey(s)} Adds one or more recipes to a player's recipe book."
6777+
+ " Can take a single recipe key or an array of keys."
6778+
+ " Returns how many recipes were newly discovered.";
6779+
}
6780+
6781+
@Override
6782+
public Integer[] numArgs() {
6783+
return new Integer[]{1, 2};
6784+
}
6785+
6786+
@Override
6787+
public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException {
6788+
MCPlayer p;
6789+
Mixed value;
6790+
if(args.length == 1) {
6791+
p = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
6792+
Static.AssertPlayerNonNull(p, t);
6793+
value = args[0];
6794+
} else {
6795+
p = Static.GetPlayer(args[0], t);
6796+
value = args[1];
6797+
}
6798+
if(value.isInstanceOf(CArray.TYPE)) {
6799+
int result = 0;
6800+
for(Mixed element : ((CArray) value).asList()) {
6801+
MCNamespacedKey key = StaticLayer.GetConvertor().GetNamespacedKey(element.val());
6802+
if(key == null) {
6803+
throw new CREFormatException("Invalid namespaced key format: " + element.val(), t);
6804+
}
6805+
boolean success = p.discoverRecipe(key);
6806+
result += success ? 1 : 0;
6807+
}
6808+
return new CInt(result, t);
6809+
}
6810+
MCNamespacedKey key = StaticLayer.GetConvertor().GetNamespacedKey(value.val());
6811+
if(key == null) {
6812+
throw new CREFormatException("Invalid namespaced key format: " + value.val(), t);
6813+
}
6814+
boolean success = p.discoverRecipe(key);
6815+
return new CInt(success ? 1 : 0, t);
6816+
}
6817+
6818+
@Override
6819+
public Class<? extends CREThrowable>[] thrown() {
6820+
return new Class[]{CREPlayerOfflineException.class, CRELengthException.class, CREFormatException.class};
6821+
}
6822+
6823+
@Override
6824+
public Version since() {
6825+
return MSVersion.V3_3_5;
6826+
}
6827+
6828+
@Override
6829+
public boolean isRestricted() {
6830+
return true;
6831+
}
6832+
6833+
@Override
6834+
public Boolean runAsync() {
6835+
return false;
6836+
}
6837+
}
67066838
}

0 commit comments

Comments
 (0)