Skip to content

Commit

Permalink
Properly restore ArmorStands' gear (#64)
Browse files Browse the repository at this point in the history
armor: Improve handling of armor stands, modernize
  • Loading branch information
Jikoo authored and nitnelave committed Feb 25, 2019
1 parent 589377e commit ae039cf
Showing 1 changed file with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,60 +1,77 @@
package com.nitnelave.CreeperHeal.block;

import com.nitnelave.CreeperHeal.CreeperHeal;
import com.nitnelave.CreeperHeal.config.CreeperConfig;
import com.nitnelave.CreeperHeal.utils.CreeperLog;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.ArmorStand;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;

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

/**
* Represents an ArmorStand
*/
public class CreeperArmorStand implements Replaceable
{
private final ArmorStand stand;
private ArrayList<ItemStack> contents = new ArrayList<ItemStack>();
private final ItemStack mainHand, offHand;
private final ItemStack[] contents;
private boolean wasRemoved = false;

public CreeperArmorStand(ArmorStand stand)
CreeperArmorStand(ArmorStand stand)
{
this.stand = stand;
Collections.addAll(contents,
stand.getBoots(), stand.getChestplate(), stand.getHelmet(), stand.getItemInHand(),
stand.getLeggings());
CreeperLog.debug("Armor: " + contents);
EntityEquipment equipment = stand.getEquipment();
this.mainHand = equipment.getItemInMainHand();
this.offHand = equipment.getItemInOffHand();

this.contents = new ItemStack[equipment.getArmorContents().length];
System.arraycopy(equipment.getArmorContents(), 0, contents, 0, contents.length);
CreeperLog.debug("Armor: " + Arrays.toString(contents));
remove();
}

@Override
public boolean replace(boolean shouldDrop)
{
ArmorStand s = getWorld()
.spawn(getLocation().getBlock().getRelative(BlockFace.DOWN).getLocation(), ArmorStand.class);
final ArmorStand s = getWorld().spawn(stand.getLocation(), ArmorStand.class);
s.setArms(stand.hasArms());
s.setBasePlate(stand.hasBasePlate());
s.setBodyPose(stand.getBodyPose());
s.setBoots(stand.getBoots());
s.setChestplate(stand.getChestplate());
s.setCustomName(stand.getCustomName());
s.setCustomNameVisible(stand.isCustomNameVisible());
s.setGlowing(stand.isGlowing());
s.setGravity(stand.hasGravity());
s.setHeadPose(stand.getHeadPose());
s.setHelmet(stand.getHelmet());
s.setItemInHand(stand.getItemInHand());
s.setLeftArmPose(stand.getLeftArmPose());
s.setRightArmPose(stand.getRightArmPose());
s.setLeftLegPose(stand.getLeftLegPose());
s.setRightLegPose(stand.getRightLegPose());
s.setLeggings(stand.getLeggings());
s.setMarker(stand.isMarker());
s.setSmall(stand.isSmall());
s.setVisible(stand.isVisible());

s.teleport(stand.getLocation());
EntityEquipment equipment = s.getEquipment();
equipment.setArmorContents(contents);
equipment.setItemInMainHand(this.mainHand);
equipment.setItemInOffHand(this.offHand);

Bukkit.getScheduler().runTask(CreeperHeal.getInstance(), new Runnable()
{
@Override
public void run()
{
s.teleport(stand.getLocation());
}
});

return true;
}

Expand Down Expand Up @@ -100,9 +117,9 @@ public boolean drop(boolean forced)
if (forced || CreeperConfig.shouldDrop())
{
getWorld().dropItemNaturally(getLocation(), new ItemStack(Material.ARMOR_STAND, 1));
for (ItemStack s : contents)
if (s.getType() != Material.AIR)
getWorld().dropItemNaturally(getLocation(), s);
for (ItemStack itemStack : contents)
if (itemStack != null && itemStack.getType() != Material.AIR)
getWorld().dropItemNaturally(getLocation(), itemStack);
return true;
}
return false;
Expand All @@ -114,13 +131,7 @@ public void remove()
if (!wasRemoved)
{
wasRemoved = true;
ItemStack air = new ItemStack(Material.AIR);
stand.setChestplate(air);
stand.setHelmet(air);
stand.setLeggings(air);
stand.setBoots(air);
stand.setItemInHand(air);

stand.getEquipment().clear();
CreeperLog.debug("Removing armor, chestplate = " + stand.getChestplate().getType());
stand.remove();
}
Expand Down

0 comments on commit ae039cf

Please sign in to comment.