Skip to content

Commit

Permalink
Add psend_equipment()
Browse files Browse the repository at this point in the history
  • Loading branch information
PseudoKnight committed May 18, 2024
1 parent b570ce0 commit 3385a24
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/laytonsmith/abstraction/MCPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.blocks.MCSign;
import com.laytonsmith.abstraction.enums.MCEntityType;
import com.laytonsmith.abstraction.enums.MCEquipmentSlot;
import com.laytonsmith.abstraction.enums.MCInstrument;
import com.laytonsmith.abstraction.enums.MCParticle;
import com.laytonsmith.abstraction.enums.MCPlayerStatistic;
Expand Down Expand Up @@ -209,4 +210,6 @@ public interface MCPlayer extends MCCommandSender, MCHumanEntity, MCOfflinePlaye
String getLocale();

void respawn();

void sendEquipmentChange(MCLivingEntity entity, MCEquipmentSlot slot, MCItemStack item);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.laytonsmith.abstraction.MCCommandSender;
import com.laytonsmith.abstraction.MCEntity;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCLivingEntity;
import com.laytonsmith.abstraction.MCLocation;
import com.laytonsmith.abstraction.MCNote;
import com.laytonsmith.abstraction.MCOfflinePlayer;
Expand All @@ -23,6 +24,7 @@
import com.laytonsmith.abstraction.bukkit.BukkitMCServer;
import com.laytonsmith.abstraction.bukkit.BukkitMCWorldBorder;
import com.laytonsmith.abstraction.enums.MCEntityType;
import com.laytonsmith.abstraction.enums.MCEquipmentSlot;
import com.laytonsmith.abstraction.enums.MCInstrument;
import com.laytonsmith.abstraction.enums.MCParticle;
import com.laytonsmith.abstraction.enums.MCPlayerStatistic;
Expand All @@ -49,7 +51,9 @@
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.potion.PotionEffect;
Expand Down Expand Up @@ -816,4 +820,32 @@ public void setScoreboard(MCScoreboard board) {
public void respawn() {
p.spigot().respawn();
}

@Override
public void sendEquipmentChange(MCLivingEntity entity, MCEquipmentSlot slot, MCItemStack item) {
LivingEntity le = (LivingEntity) entity.getHandle();
ItemStack is;
if(item == null) {
if(Static.getServer().getMinecraftVersion().lt(MCVersion.MC1_19_3)) {
// null isn't supported prior to 1.19.3
is = new ItemStack(Material.AIR);
} else {
is = null;
}
} else {
is = (ItemStack) item.getHandle();
}
try {
switch(slot) {
case WEAPON -> p.sendEquipmentChange(le, EquipmentSlot.HAND, is);
case OFF_HAND -> p.sendEquipmentChange(le, EquipmentSlot.OFF_HAND, is);
case BOOTS -> p.sendEquipmentChange(le, EquipmentSlot.FEET, is);
case LEGGINGS -> p.sendEquipmentChange(le, EquipmentSlot.LEGS, is);
case CHESTPLATE -> p.sendEquipmentChange(le, EquipmentSlot.CHEST, is);
case HELMET -> p.sendEquipmentChange(le, EquipmentSlot.HEAD, is);
}
} catch(NoSuchMethodError ex) {
// probably before 1.18, which is unsupported
}
}
}
97 changes: 89 additions & 8 deletions src/main/java/com/laytonsmith/core/functions/PlayerManagement.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.laytonsmith.abstraction.MCEntity;
import com.laytonsmith.abstraction.MCHumanEntity;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCLivingEntity;
import com.laytonsmith.abstraction.MCLocation;
import com.laytonsmith.abstraction.MCNamespacedKey;
import com.laytonsmith.abstraction.MCOfflinePlayer;
Expand All @@ -25,6 +26,7 @@
import com.laytonsmith.abstraction.entities.MCCommandMinecart;
import com.laytonsmith.abstraction.enums.MCDyeColor;
import com.laytonsmith.abstraction.enums.MCEntityType;
import com.laytonsmith.abstraction.enums.MCEquipmentSlot;
import com.laytonsmith.abstraction.enums.MCGameMode;
import com.laytonsmith.abstraction.enums.MCPlayerStatistic;
import com.laytonsmith.abstraction.enums.MCPotionEffectType;
Expand Down Expand Up @@ -2233,7 +2235,7 @@ public Integer[] numArgs() {

@Override
public String docs() {
return "boolean {player, potionEffect, [strength], [seconds], [ambient], [particles]}"
return "boolean {player, potionEffect, [strength], [seconds], [ambient], [particles], [icon]}"
+ " Adds one, or modifies an existing, potion effect on a mob."
+ " The potionEffect can be " + StringUtils.Join(MCPotionEffectType.types(), ", ", ", or ", " or ")
+ ". It also accepts an integer corresponding to the effect id listed on the Minecraft wiki."
Expand All @@ -2243,6 +2245,7 @@ public String docs() {
+ " Negative seconds makes the effect infinite. (or max in versions prior to 1.19.4)"
+ " Ambient takes a boolean of whether the particles should be more transparent."
+ " Particles takes a boolean of whether the particles should be visible at all."
+ " Icon argument takes a boolean of whether the effect icon should be displayed."
+ " The function returns whether or not the effect was modified.";
}

Expand Down Expand Up @@ -6710,7 +6713,7 @@ public Version since() {
}
}

@api
@api(environments = {CommandHelperEnvironment.class})
public static class plocale extends AbstractFunction {

@Override
Expand Down Expand Up @@ -6761,7 +6764,7 @@ public Boolean runAsync() {
}
}

@api
@api(environments = {CommandHelperEnvironment.class})
public static class phas_recipe extends AbstractFunction {

@Override
Expand Down Expand Up @@ -6819,7 +6822,7 @@ public Boolean runAsync() {
}
}

@api
@api(environments = {CommandHelperEnvironment.class})
public static class pgive_recipe extends AbstractFunction {

@Override
Expand Down Expand Up @@ -6892,7 +6895,7 @@ public Boolean runAsync() {
}
}

@api
@api(environments = {CommandHelperEnvironment.class})
public static class pforce_respawn extends AbstractFunction {

@Override
Expand Down Expand Up @@ -6944,7 +6947,7 @@ public Boolean runAsync() {
}
}

@api
@api(environments = {CommandHelperEnvironment.class})
public static class phide_entity extends AbstractFunction {

@Override
Expand Down Expand Up @@ -7001,7 +7004,7 @@ public Boolean runAsync() {
}
}

@api
@api(environments = {CommandHelperEnvironment.class})
public static class pshow_entity extends AbstractFunction {

@Override
Expand Down Expand Up @@ -7056,7 +7059,7 @@ public Boolean runAsync() {
}
}

@api
@api(environments = {CommandHelperEnvironment.class})
public static class pcan_see_entity extends AbstractFunction {

@Override
Expand Down Expand Up @@ -7110,4 +7113,82 @@ public Boolean runAsync() {
return false;
}
}

@api(environments = {CommandHelperEnvironment.class})
public static class psend_equipment extends AbstractFunction {

@Override
public String getName() {
return "psend_equipment";
}

@Override
public String docs() {
return "void {[player], entityUUID, equipmentArray} Changes a living entity's equipment only for the"
+ " specified player. (MC 1.18+) Equipment array can be null to make all equipment not visible."
+ " Otherwise equipment array must be an associative array where the keys are equipment slots and"
+ " the values are item arrays or null. The equipment slots are: "
+ StringUtils.Join(MCEquipmentSlot.values(), ", ", ", or ", " or ");
}

@Override
public Integer[] numArgs() {
return new Integer[]{2, 3};
}

@Override
public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException {
MCPlayer p;
MCLivingEntity le;
Mixed equipment;
if(args.length == 2) {
p = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
Static.AssertPlayerNonNull(p, t);
le = Static.getLivingEntity(args[0], t);
equipment = args[1];
} else {
p = Static.GetPlayer(args[0], t);
le = Static.getLivingEntity(args[1], t);
equipment = args[2];
}
if(equipment instanceof CNull) {
for(MCEquipmentSlot slot : MCEquipmentSlot.values()) {
p.sendEquipmentChange(le, slot, null);
}
} else if(equipment.isInstanceOf(CArray.TYPE)) {
CArray ea = (CArray) equipment;
for(String key : ea.stringKeySet()) {
try {
p.sendEquipmentChange(le, MCEquipmentSlot.valueOf(key.toUpperCase()),
ObjectGenerator.GetGenerator().item(ea.get(key, t), t));
} catch (IllegalArgumentException iae) {
throw new CREFormatException("Not an equipment slot: " + key, t);
}
}
} else {
throw new CREFormatException("Expected last argument to be an array or null", t);
}
return CVoid.VOID;
}

@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CREPlayerOfflineException.class, CRELengthException.class, CREBadEntityException.class};
}

@Override
public Version since() {
return MSVersion.V3_3_5;
}

@Override
public boolean isRestricted() {
return true;
}

@Override
public Boolean runAsync() {
return false;
}
}
}

0 comments on commit 3385a24

Please sign in to comment.