Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Add a basic equip command. WARNING: SPONGE HASN'T YET IMPLEMENTED EQU…
Browse files Browse the repository at this point in the history
…IPMENT SOMEHOW
  • Loading branch information
mcmonkey4eva committed Mar 3, 2017
1 parent 45b4898 commit 0802dbd
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 13 deletions.
Expand Up @@ -124,6 +124,7 @@ public void onServerStart(GamePreInitializationEvent event) {
Denizen2Core.getImplementation().getScriptDataFolder().mkdirs();
// Commands: Entity
Denizen2Core.register(new EditEntityCommand());
Denizen2Core.register(new EquipCommand());
Denizen2Core.register(new FlagCommand());
Denizen2Core.register(new MountCommand());
Denizen2Core.register(new SpawnCommand());
Expand Down
@@ -0,0 +1,68 @@
package com.denizenscript.denizen2sponge.commands.entity;

import com.denizenscript.denizen2core.commands.AbstractCommand;
import com.denizenscript.denizen2core.commands.CommandEntry;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.tags.objects.MapTag;
import com.denizenscript.denizen2core.utilities.Tuple;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import com.denizenscript.denizen2sponge.tags.objects.ItemTag;
import com.denizenscript.denizen2sponge.utilities.Equipment;
import org.spongepowered.api.entity.living.Living;

import java.util.Map;

public class EquipCommand extends AbstractCommand {

// <--[command]
// @Name equip
// @Arguments <entity> <equipment map>
// @Short equips an entity according to a given equipment map.
// @Updated 2017/03/03
// @Group Entities
// @Minimum 2
// @Maximum 2
// @Description
// Equips an entity according to a given equipment map.
// Valid equipment keys: HEAD, CHESTPLATE, LEGGINGS, BOOTS, HAND, OFF_HAND
// TODO: Saddle, Horse_Armor
// TODO: Explain more!
// @Example
// # This example equips a player with a full set of iron armor.
// - equip <player> head:iron_helmet|chestplate:iron_chestplate|boots:iron_boots|leggings:iron_leggings
// -->

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

@Override
public String getArguments() {
return "<entity> <equipment map>";
}

@Override
public int getMinimumArguments() {
return 2;
}

@Override
public int getMaximumArguments() {
return 2;
}

@Override
public void execute(CommandQueue queue, CommandEntry entry) {
EntityTag ent = EntityTag.getFor(queue.error, entry.getArgumentObject(queue, 0));
MapTag map = MapTag.getFor(queue.error, entry.getArgumentObject(queue, 1));
for (Map.Entry<String, AbstractTagObject> mapentry : map.getInternal().entrySet()) {
ItemTag itm = ItemTag.getFor(queue.error, mapentry.getValue());
Equipment.equippers.get(mapentry.getKey()).run(new Tuple<>((Living) ent.getInternal(), itm));

This comment has been minimized.

Copy link
@Morphan1

Morphan1 Mar 3, 2017

Contributor

Lowercase the key pls

This comment has been minimized.

Copy link
@mcmonkey4eva

mcmonkey4eva Mar 3, 2017

Author Member

Knew I was forgetting things... (this + below)

}
if (queue.shouldShowGood()) {
queue.outGood("Equipped " + " on the entity!");

This comment has been minimized.

Copy link
@Morphan1

Morphan1 Mar 3, 2017

Contributor

I'm so glad I Equipped on the entity

}
}
}
Expand Up @@ -15,8 +15,8 @@ public class MountCommand extends AbstractCommand {
// @Name mount
// @Arguments <entity> <entity list>
// @Short mounts all the entities in the list onto the given entity.
// @Updated 2018/02/12
// @Group Player
// @Updated 2017/02/12
// @Group Entities
// @Minimum 2
// @Maximum 2
// @Description
Expand Down
Expand Up @@ -14,7 +14,7 @@ public class TeleportCommand extends AbstractCommand {
// @Arguments <entity> <location>
// @Short teleports the entity to a location.
// @Updated 2016/09/05
// @Group Player
// @Group Entities
// @Minimum 2
// @Maximum 2
// @Description
Expand Down
Expand Up @@ -13,9 +13,17 @@
import org.spongepowered.api.data.key.Key;
import org.spongepowered.api.data.property.entity.EyeHeightProperty;
import org.spongepowered.api.data.property.entity.EyeLocationProperty;
import org.spongepowered.api.data.type.HandTypes;
import org.spongepowered.api.entity.ArmorEquipable;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.Equipable;
import org.spongepowered.api.entity.living.Living;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.equipment.EquipmentType;
import org.spongepowered.api.item.inventory.equipment.EquipmentTypes;
import org.spongepowered.api.world.World;

import java.util.HashMap;
Expand Down Expand Up @@ -146,6 +154,54 @@ public String friendlyName() {
return new NumberTag(ent.health().get() / ent.maxHealth().get());
});
// <--[tag]
// @Name EntityTag.helmet
// @Updated 2017/03/03
// @Group Equipment
// @ReturnType ItemTag
// @Returns the helmet currently worn by the entity.
// -->
handlers.put("helmet", (dat, obj) -> new ItemTag(((Equipable) ((EntityTag) obj).internal).getEquipped(EquipmentTypes.HEADWEAR).orElse(ItemStack.of(ItemTypes.NONE, 1))));
// <--[tag]
// @Name EntityTag.chestplate
// @Updated 2017/03/03
// @Group Equipment
// @ReturnType ItemTag
// @Returns the chestplate currently worn by the entity.
// -->
handlers.put("chestplate", (dat, obj) -> new ItemTag(((Equipable) ((EntityTag) obj).internal).getEquipped(EquipmentTypes.CHESTPLATE).orElse(ItemStack.of(ItemTypes.NONE, 1))));
// <--[tag]
// @Name EntityTag.leggings
// @Updated 2017/03/03
// @Group Equipment
// @ReturnType ItemTag
// @Returns the leggings currently worn by the entity.
// -->
handlers.put("leggings", (dat, obj) -> new ItemTag(((Equipable) ((EntityTag) obj).internal).getEquipped(EquipmentTypes.LEGGINGS).orElse(ItemStack.of(ItemTypes.NONE, 1))));
// <--[tag]
// @Name EntityTag.boots
// @Updated 2017/03/03
// @Group Equipment
// @ReturnType ItemTag
// @Returns the boots currently worn by the entity.
// -->
handlers.put("boots", (dat, obj) -> new ItemTag(((Equipable) ((EntityTag) obj).internal).getEquipped(EquipmentTypes.BOOTS).orElse(ItemStack.of(ItemTypes.NONE, 1))));
// <--[tag]
// @Name EntityTag.held_item
// @Updated 2017/03/03
// @Group Equipment
// @ReturnType ItemTag
// @Returns the item currently held the entity in its main hand.
// -->
handlers.put("held_item", (dat, obj) -> new ItemTag(((ArmorEquipable) ((EntityTag) obj).internal).getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.of(ItemTypes.NONE, 1))));
// <--[tag]
// @Name EntityTag.held_item_offhand
// @Updated 2017/03/03
// @Group Equipment
// @ReturnType ItemTag
// @Returns the item currently held the entity in its off hand.
// -->
handlers.put("held_item_offhand", (dat, obj) -> new ItemTag(((ArmorEquipable) ((EntityTag) obj).internal).getItemInHand(HandTypes.OFF_HAND).orElse(ItemStack.of(ItemTypes.NONE, 1))));
// <--[tag]

This comment has been minimized.

Copy link
@Morphan1

Morphan1 Mar 3, 2017

Contributor

Does every single tag have to be one line if it can be? This is getting close to unreadable

This comment has been minimized.

Copy link
@mcmonkey4eva

mcmonkey4eva Mar 3, 2017

Author Member

What's the point of multilining it? It's linear...

// @Name EntityTag.data
// @Updated 2016/08/28
// @Group Current Information
Expand Down
Expand Up @@ -39,16 +39,6 @@ public Player getInternal() {

static {
// <--[tag]
// @Name PlayerTag.held_item
// @Updated 2016/11/24
// @Group Identification
// @ReturnType ItemTag
// @Returns the item held by the player.
// @Example "Bob" .held_item may return "minecraft:iron_axe/1/".
// -->
handlers.put("held_item", (dat, obj) -> new ItemTag(((PlayerTag) obj).internal.getItemInHand(HandTypes.MAIN_HAND)
.orElse(ItemStack.of(ItemTypes.NONE, 1))));
// <--[tag]
// @Name PlayerTag.name
// @Updated 2016/08/26
// @Group Identification
Expand Down
@@ -0,0 +1,29 @@
package com.denizenscript.denizen2sponge.utilities;

import com.denizenscript.denizen2core.utilities.Action;
import com.denizenscript.denizen2core.utilities.Tuple;
import com.denizenscript.denizen2sponge.tags.objects.ItemTag;
import org.spongepowered.api.data.type.HandTypes;
import org.spongepowered.api.entity.ArmorEquipable;
import org.spongepowered.api.entity.Equipable;
import org.spongepowered.api.entity.living.Living;
import org.spongepowered.api.item.inventory.equipment.EquipmentTypes;

import java.util.HashMap;

/**
* Helper class for equipping an entity.
*/
public class Equipment {

public static final HashMap<String, Action<Tuple<Living, ItemTag>>> equippers = new HashMap<>();

static {
equippers.put("head", (tuple) -> ((Equipable) tuple.one).equip(EquipmentTypes.HEADWEAR, tuple.two.getInternal()));
equippers.put("chestplate", (tuple) -> ((Equipable) tuple.one).equip(EquipmentTypes.CHESTPLATE, tuple.two.getInternal()));
equippers.put("leggings", (tuple) -> ((Equipable) tuple.one).equip(EquipmentTypes.LEGGINGS, tuple.two.getInternal()));
equippers.put("boots", (tuple) -> ((Equipable) tuple.one).equip(EquipmentTypes.BOOTS, tuple.two.getInternal()));
equippers.put("hand", (tuple) -> ((ArmorEquipable) tuple.one).setItemInHand(HandTypes.MAIN_HAND, tuple.two.getInternal()));
equippers.put("offhand", (tuple) -> ((ArmorEquipable) tuple.one).setItemInHand(HandTypes.OFF_HAND, tuple.two.getInternal()));
}
}

0 comments on commit 0802dbd

Please sign in to comment.