Skip to content

Commit

Permalink
fix(ArmoredElytra): fixes global sounds for creating/breaking the item
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Jun 29, 2022
1 parent 245e803 commit b187f68
Showing 1 changed file with 64 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import me.machinemaker.vanillatweaks.pdc.PDCKey;
import me.machinemaker.vanillatweaks.tags.Tags;
import me.machinemaker.vanillatweaks.utils.Keys;
Expand All @@ -42,14 +49,6 @@
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static me.machinemaker.vanillatweaks.adventure.Components.join;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.Component.translatable;
Expand All @@ -65,99 +64,63 @@ class ItemDropRunnable extends BukkitRunnable {
private final BiPredicate<Item, Block> itemPredicate;
private int counter = 0;

ItemDropRunnable(Item item, LookingFor lookingFor) {
ItemDropRunnable(final Item item, final LookingFor lookingFor) {
this.item = item;
this.lookingFor = lookingFor;
this.itemPredicate = switch (lookingFor) {
case CHESTPLATE -> constructBiPredicate(Tags.CHESTPLATES::isTagged);
case ARMORED_ELYTRA -> constructBiPredicate(i -> i.getItemStack().getType() == Material.ELYTRA && ItemListener.IS_ARMORED_ELYTRA.has(i.getItemStack()));
case ARMORED_ELYTRA ->
constructBiPredicate(i -> i.getItemStack().getType() == Material.ELYTRA && ItemListener.IS_ARMORED_ELYTRA.has(i.getItemStack()));
};
}

enum LookingFor {
CHESTPLATE,
ARMORED_ELYTRA
}

private static BiPredicate<Item, Block> constructBiPredicate(Predicate<Item> itemPredicate) {
private static BiPredicate<Item, Block> constructBiPredicate(final Predicate<Item> itemPredicate) {
return (i, block) -> itemPredicate.test(i) && i.getLocation().subtract(0, 1, 0).getBlock().equals(block);
}

@Override
public void run() {
if (item.isDead() || counter >= 50) {
this.cancel();
return;
}

final Block block = item.getLocation().subtract(0, 1, 0).getBlock();
if (this.lookingFor == LookingFor.ARMORED_ELYTRA) {
if (block.getType() == Material.GRINDSTONE) {
breakArmoredElytra(block.getWorld(), block.getLocation(), item, true);
}
} else {
if (Tag.ANVIL.isTagged(block.getType())) {
for (Item nearbyItem : VTUtils.getNearbyEntitiesOfType(item, 0.5, 0.1, 0.5, i -> this.itemPredicate.test(i, block) && !i.isDead())) {
if (lookingFor == LookingFor.CHESTPLATE) {
constructArmoredElytra(block, nearbyItem, item);
}
this.cancel();
return;
}
}
}

counter++;
}

static void constructArmoredElytra(Block block, Item chestplate, Item elytra) {
ItemStack chestStack = chestplate.getItemStack();
static void constructArmoredElytra(final Block block, final Item chestplate, final Item elytra) {
final ItemStack chestStack = chestplate.getItemStack();
if (chestStack.getItemMeta() == null) return;
ItemStack elytraStack = elytra.getItemStack();
final ItemStack elytraStack = elytra.getItemStack();
if (elytraStack.getItemMeta() == null) return;
ItemStack armoredElytra = new ItemStack(Material.ELYTRA);
ItemMeta armoredMeta = armoredElytra.getItemMeta();
final ItemStack armoredElytra = new ItemStack(Material.ELYTRA);
final ItemMeta armoredMeta = armoredElytra.getItemMeta();
if (armoredMeta == null) return;

Material chestplateMaterial = chestStack.getType();
final Material chestplateMaterial = chestStack.getType();
armoredMeta.lore(List.of(join(text("+ "), translatable(chestplateMaterial)).color(GOLD).decoration(TextDecoration.ITALIC, false)));

ItemListener.IS_ARMORED_ELYTRA.setTo(armoredMeta, true);
ELYTRA_ITEM.setTo(armoredMeta, elytraStack);
CHESTPLATE_ITEM.setTo(armoredMeta, chestStack);

Map<Enchantment, Integer> enchants = Maps.newHashMap(chestplate.getItemStack().getEnchantments());
final Map<Enchantment, Integer> enchants = Maps.newHashMap(chestplate.getItemStack().getEnchantments());
elytraStack.getEnchantments().forEach((enchantment, level) -> {
Map<Enchantment, Integer> conflicts = enchants.entrySet().stream().filter(entry -> enchantment.conflictsWith(entry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
final Map<Enchantment, Integer> conflicts = enchants.entrySet().stream().filter(entry -> enchantment.conflictsWith(entry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
if (conflicts.isEmpty()) {
enchants.merge(enchantment, level, Math::max);
} else {
conflicts.put(enchantment, level); // add for easier sorting
var max = Collections.max(conflicts.entrySet(), Comparator.comparingInt(Map.Entry::getValue));
final var max = Collections.max(conflicts.entrySet(), Comparator.comparingInt(Map.Entry::getValue));
enchants.merge(max.getKey(), max.getValue(), Math::max);
}
});
enchants.forEach((enchantment, integer) -> {
armoredMeta.addEnchant(enchantment, integer, false);
});

Multimap<Attribute, AttributeModifier> attributeMap = LinkedHashMultimap.create();
Multimap<Attribute, AttributeModifier> chestAttributes = chestStack.getItemMeta().getAttributeModifiers();
final Multimap<Attribute, AttributeModifier> attributeMap = LinkedHashMultimap.create();
final Multimap<Attribute, AttributeModifier> chestAttributes = chestStack.getItemMeta().getAttributeModifiers();
if (chestAttributes != null) {
attributeMap.putAll(chestAttributes);
} else {
// TODO when paper fix is done, can just be a putAll
chestStack.getType().getItemAttributes(EquipmentSlot.CHEST).forEach((attribute, mod) -> {
attributeMap.put(attribute, new AttributeModifier(mod.getUniqueId(), mod.getName(), mod.getAmount(), mod.getOperation(), EquipmentSlot.CHEST));
});
attributeMap.putAll(chestStack.getType().getDefaultAttributeModifiers(EquipmentSlot.CHEST));
}
Multimap<Attribute, AttributeModifier> elytraAttributes = elytraStack.getItemMeta().getAttributeModifiers();
final Multimap<Attribute, AttributeModifier> elytraAttributes = elytraStack.getItemMeta().getAttributeModifiers();
if (elytraAttributes != null) {
attributeMap.putAll(elytraAttributes);
} else {
elytraStack.getType().getItemAttributes(EquipmentSlot.CHEST).forEach((attribute, mod) -> {
attributeMap.put(attribute, new AttributeModifier(mod.getUniqueId(), mod.getName(), mod.getAmount(), mod.getOperation(), EquipmentSlot.CHEST));
});
attributeMap.putAll(elytraStack.getType().getDefaultAttributeModifiers(EquipmentSlot.CHEST));
}
armoredMeta.setAttributeModifiers(attributeMap);

Expand All @@ -167,25 +130,58 @@ static void constructArmoredElytra(Block block, Item chestplate, Item elytra) {
elytraStack.setAmount(0);
elytra.remove();
block.getWorld().dropItem(block.getLocation().add(0, 1.1, 0), armoredElytra);
block.getWorld().playSound(Sound.sound(org.bukkit.Sound.BLOCK_ANVIL_USE.getKey(), Sound.Source.BLOCK, 1, 1));
final Location location = block.getLocation();
block.getWorld().playSound(Sound.sound(org.bukkit.Sound.BLOCK_ANVIL_USE.getKey(), Sound.Source.BLOCK, 1, 1), location.getX(), location.getY(), location.getZ());
}

static void breakArmoredElytra(World world, Location location, Item elytra, boolean sound) {
ItemStack elytraStack = elytra.getItemStack();
static void breakArmoredElytra(final World world, final Location location, final Item elytra, final boolean sound) {
final ItemStack elytraStack = elytra.getItemStack();
if (elytraStack.getItemMeta() == null) return;

ItemStack elytraItem = ELYTRA_ITEM.getFrom(elytraStack);
final ItemStack elytraItem = ELYTRA_ITEM.getFrom(elytraStack);
if (elytraItem != null) {
world.dropItem(location.add(0, 1.1, 0), elytraItem);
}
ItemStack chestItem = CHESTPLATE_ITEM.getFrom(elytraStack);
final ItemStack chestItem = CHESTPLATE_ITEM.getFrom(elytraStack);
if (chestItem != null) {
world.dropItem(location.add(0, 1.1, 0), chestItem);
}
if (sound) {
world.playSound(Sound.sound(org.bukkit.Sound.BLOCK_GRINDSTONE_USE.getKey(), Sound.Source.BLOCK, 1, 1));
world.playSound(Sound.sound(org.bukkit.Sound.BLOCK_GRINDSTONE_USE.getKey(), Sound.Source.BLOCK, 1, 1), location.getX(), location.getY(), location.getZ());
}
elytraStack.setAmount(0);
elytra.remove();
}

@Override
public void run() {
if (this.item.isDead() || this.counter >= 50) {
this.cancel();
return;
}

final Block block = this.item.getLocation().subtract(0, 1, 0).getBlock();
if (this.lookingFor == LookingFor.ARMORED_ELYTRA) {
if (block.getType() == Material.GRINDSTONE) {
breakArmoredElytra(block.getWorld(), block.getLocation(), this.item, true);
}
} else {
if (Tag.ANVIL.isTagged(block.getType())) {
for (final Item nearbyItem : VTUtils.getNearbyEntitiesOfType(this.item, 0.5, 0.1, 0.5, i -> this.itemPredicate.test(i, block) && !i.isDead())) {
if (this.lookingFor == LookingFor.CHESTPLATE) {
constructArmoredElytra(block, nearbyItem, this.item);
}
this.cancel();
return;
}
}
}

this.counter++;
}

enum LookingFor {
CHESTPLATE,
ARMORED_ELYTRA
}
}

0 comments on commit b187f68

Please sign in to comment.