Add the ability to shear all entities in a stack at once.

This commit is contained in:
Brianna 2020-02-02 11:11:50 -05:00
parent 92cdeae938
commit 4c70a6e79e
2 changed files with 66 additions and 4 deletions

View File

@ -1,16 +1,22 @@
package com.songoda.ultimatestacker.listeners; package com.songoda.ultimatestacker.listeners;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.entity.EntityStack;
import com.songoda.ultimatestacker.entity.EntityStackManager; import com.songoda.ultimatestacker.entity.EntityStackManager;
import com.songoda.ultimatestacker.entity.Split; import com.songoda.ultimatestacker.entity.Split;
import com.songoda.ultimatestacker.settings.Settings; import com.songoda.ultimatestacker.settings.Settings;
import org.bukkit.entity.Entity; import org.bukkit.Material;
import org.bukkit.entity.EntityType; import org.bukkit.World;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.*;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerShearEntityEvent; import org.bukkit.event.player.PlayerShearEntityEvent;
import org.bukkit.inventory.ItemStack;
import java.util.Random;
public class ShearListeners implements Listener { public class ShearListeners implements Listener {
@ -38,6 +44,59 @@ public class ShearListeners implements Listener {
&& Settings.SPLIT_CHECKS.getStringList().stream().noneMatch(line -> Split.valueOf(line) == Split.SNOWMAN_DERP)) && Settings.SPLIT_CHECKS.getStringList().stream().noneMatch(line -> Split.valueOf(line) == Split.SNOWMAN_DERP))
return; return;
plugin.getEntityUtils().splitFromStack((LivingEntity)entity);
if (Settings.SHEAR_IN_ONE_CLICK.getBoolean()) {
World world = entity.getLocation().getWorld();
EntityStack stack = stackManager.getStack(entity);
int amount = stack.getAmount() - 1;
ItemStack item = getDrop(entity);
if (item == null)
return;
int amountToDrop = 0;
if (entity.getType() == EntityType.SHEEP) {
for (int i = 0; i < amount; i++) {
amountToDrop += new Random().nextInt(2) + 1;
}
} else
amountToDrop = item.getAmount() * amount;
int fullStacks = (int) Math.floor(amountToDrop / 64);
int nonStack = amountToDrop - (fullStacks * 64);
for (int i = 0; i < fullStacks; i++) {
ItemStack clone = item.clone();
clone.setAmount(64);
world.dropItemNaturally(entity.getLocation(), clone);
}
if (nonStack != 0) {
ItemStack clone = item.clone();
clone.setAmount(nonStack);
world.dropItemNaturally(entity.getLocation(), clone);
}
} else
plugin.getEntityUtils().splitFromStack((LivingEntity) entity);
}
private ItemStack getDrop(Entity entity) {
ItemStack itemStack = null;
switch (entity.getType()) {
case SHEEP:
itemStack = new ItemStack(CompatibleMaterial.WHITE_WOOL.getMaterial());
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13))
itemStack.setType(Material.valueOf(((Sheep) entity).getColor() + "_WOOL"));
else
itemStack.setDurability((short) ((Sheep) entity).getColor().getWoolData());
break;
case MUSHROOM_COW:
itemStack = new ItemStack(CompatibleMaterial.RED_MUSHROOM.getMaterial(), 5);
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_14)
&& ((MushroomCow) entity).getVariant() == MushroomCow.Variant.BROWN)
itemStack.setType(CompatibleMaterial.BROWN_MUSHROOM.getMaterial());
break;
}
return itemStack;
} }
} }

View File

@ -171,6 +171,9 @@ public class Settings {
public static final ConfigSetting DISABLE_KNOCKBACK = new ConfigSetting(config, "Entities.Disable Knockback", false, public static final ConfigSetting DISABLE_KNOCKBACK = new ConfigSetting(config, "Entities.Disable Knockback", false,
"Should knockback be disabled on unstacked mobs?"); "Should knockback be disabled on unstacked mobs?");
public static final ConfigSetting SHEAR_IN_ONE_CLICK = new ConfigSetting(config, "Entities.Shear In One Click", false,
"Should entities be sheared in a single click?");
public static final ConfigSetting STACK_ITEMS = new ConfigSetting(config, "Items.Enabled", true, public static final ConfigSetting STACK_ITEMS = new ConfigSetting(config, "Items.Enabled", true,
"Should items be stacked?"); "Should items be stacked?");