Skip to content

Commit

Permalink
ItemTag.placed_material
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 15, 2022
1 parent 24ebe0b commit 48b97a7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.block.data.BlockData;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
Expand Down Expand Up @@ -110,4 +111,8 @@ public boolean renderEntireMap(int mapId, int xMin, int zMin, int xMax, int zMax
public Multimap<Attribute, AttributeModifier> getDefaultAttributes(ItemStack item, org.bukkit.inventory.EquipmentSlot slot) {
throw new UnsupportedOperationException();
}

public BlockData getPlacedBlock(Material material) {
throw new UnsupportedOperationException();
}
}
Expand Up @@ -2068,6 +2068,10 @@ else if (meta.hasDisplayName() && CoreUtilities.toLowerCase(meta.getDisplayName(
}
else if (slots.size() == 1 && !attribute.getParamObject().shouldBeType(ListTag.class)) {
int slot = SlotHelper.nameToIndexFor(attribute.getParam(), object.getInventory().getHolder());
if (slot == -1) {
attribute.echoError("Invalid slot index '" + attribute.getParam() + "'.");
return null;
}
if (slot < 0) {
slot = 0;
}
Expand All @@ -2080,6 +2084,10 @@ else if (slot > object.getInventory().getSize() - 1) {
ListTag result = new ListTag();
for (String slotText : slots) {
int slot = SlotHelper.nameToIndexFor(slotText, object.getInventory().getHolder());
if (slot == -1) {
attribute.echoError("Invalid slot index '" + slotText + "'.");
return null;
}
if (slot < 0) {
slot = 0;
}
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.data.BlockData;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.*;
Expand Down Expand Up @@ -592,6 +593,22 @@ public static void registerTags() {
return object.getMaterial();
});

// <--[tag]
// @attribute <ItemTag.placed_material>
// @returns MaterialTag
// @group conversion
// @description
// Returns the MaterialTag that this item would place as a block, if it is a block-like item.
// For example, the "redstone" item will return a "redstone_wire" block.
// -->
tagProcessor.registerTag(ObjectTag.class, "placed_material", (attribute, object) -> {
BlockData data = NMSHandler.getItemHelper().getPlacedBlock(object.getBukkitMaterial());
if (data == null) {
return null;
}
return new MaterialTag(data);
});

// <--[tag]
// @attribute <ItemTag.json>
// @returns ElementTag
Expand Down
Expand Up @@ -25,10 +25,12 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
Expand All @@ -40,10 +42,12 @@
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_18_R2.CraftServer;
import org.bukkit.craftbukkit.v1_18_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_18_R2.attribute.CraftAttributeInstance;
import org.bukkit.craftbukkit.v1_18_R2.attribute.CraftAttributeMap;
import org.bukkit.craftbukkit.v1_18_R2.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventoryPlayer;
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_18_R2.util.CraftMagicNumbers;
Expand Down Expand Up @@ -531,4 +535,14 @@ public Multimap<org.bukkit.attribute.Attribute, org.bukkit.attribute.AttributeMo
}
return bukkit;
}

@Override
public BlockData getPlacedBlock(Material material) {
Item nmsItem = Registry.ITEM.getOptional(CraftNamespacedKey.toMinecraft(material.getKey())).orElse(null);
if (nmsItem instanceof BlockItem) {
Block block = ((BlockItem) nmsItem).getBlock();
return CraftBlockData.fromData(block.defaultBlockState());
}
return null;
}
}

0 comments on commit 48b97a7

Please sign in to comment.