Skip to content

Commit

Permalink
Added ItemFlags to FastItemStack
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Aug 29, 2021
1 parent 4cbb33b commit c851e35
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
36 changes: 34 additions & 2 deletions eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.willfp.eco.core.Eco;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* FastItemStack contains methods to modify and read items faster than in default bukkit.
Expand Down Expand Up @@ -51,17 +53,47 @@ int getLevelOnItem(@NotNull Enchantment enchantment,
/**
* Set the rework penalty.
*
* @param cost The rework penalty to set.
* @param cost The rework penalty to set.
*/
void setRepairCost(int cost);

/**
* Get the rework penalty.
*.
* .
*
* @return The rework penalty found on the item.
*/
int getRepairCost();

/**
* Add ItemFlags.
*
* @param hideFlags The flags.
*/
void addItemFlags(ItemFlag... hideFlags);

/**
* Remove ItemFlags.
*
* @param hideFlags The flags.
*/
void removeItemFlags(ItemFlag... hideFlags);

/**
* Get the ItemFlags.
*
* @return The flags.
*/
Set<ItemFlag> getItemFlags();

/**
* Test the item for a flag.
*
* @param flag The flag.
* @return If the flag is present.
*/
boolean hasItemFlag(ItemFlag flag);

/**
* Get the Bukkit ItemStack again.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.willfp.eco.internal.fast

import com.willfp.eco.core.fast.FastItemStack
import org.bukkit.inventory.ItemFlag
import org.bukkit.inventory.ItemStack

abstract class EcoFastItemStack<T: Any>(
Expand All @@ -10,4 +11,8 @@ abstract class EcoFastItemStack<T: Any>(
override fun unwrap(): ItemStack {
return bukkit
}

fun getBitModifier(hideFlag: ItemFlag): Int {
return 1 shl hideFlag.ordinal
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack
import org.bukkit.craftbukkit.v1_16_R3.util.CraftMagicNumbers
import org.bukkit.craftbukkit.v1_16_R3.util.CraftNamespacedKey
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemFlag
import java.lang.reflect.Field
import kotlin.experimental.and

Expand Down Expand Up @@ -96,6 +97,49 @@ class NMSFastItemStack(itemStack: org.bukkit.inventory.ItemStack) : EcoFastItemS
}
}

override fun addItemFlags(vararg hideFlags: ItemFlag) {
for (flag in hideFlags) {
this.flagBits = this.flagBits or getBitModifier(flag)
}
}

override fun removeItemFlags(vararg hideFlags: ItemFlag) {
for (flag in hideFlags) {
this.flagBits = this.flagBits and getBitModifier(flag)
}
}

override fun getItemFlags(): MutableSet<ItemFlag> {
val flags = mutableSetOf<ItemFlag>()

var flagArr: Array<ItemFlag>
val size = ItemFlag.values().also { flagArr = it }.size

for (i in 0 until size) {
val flag = flagArr[i]
if (this.hasItemFlag(flag)) {
flags.add(flag)
}
}

return flags
}

override fun hasItemFlag(flag: ItemFlag): Boolean {
val bitModifier = getBitModifier(flag)
return this.flagBits and bitModifier == bitModifier
}

private var flagBits: Int
get() =
if (handle.hasTag() && handle.tag!!.hasKeyOfType(
"HideFlags",
99
)
) handle.tag!!.getInt("HideFlags") else 0
set(value) =
handle.orCreateTag.setInt("HideFlags", value)

override fun getRepairCost(): Int {
return handle.repairCost;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack
import org.bukkit.craftbukkit.v1_17_R1.util.CraftMagicNumbers
import org.bukkit.craftbukkit.v1_17_R1.util.CraftNamespacedKey
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemFlag
import kotlin.experimental.and

class NMSFastItemStack(itemStack: org.bukkit.inventory.ItemStack) : EcoFastItemStack<ItemStack>(
Expand Down Expand Up @@ -115,6 +116,49 @@ class NMSFastItemStack(itemStack: org.bukkit.inventory.ItemStack) : EcoFastItemS
}
}

override fun addItemFlags(vararg hideFlags: ItemFlag) {
for (flag in hideFlags) {
this.flagBits = this.flagBits or getBitModifier(flag)
}
}

override fun removeItemFlags(vararg hideFlags: ItemFlag) {
for (flag in hideFlags) {
this.flagBits = this.flagBits and getBitModifier(flag)
}
}

override fun getItemFlags(): MutableSet<ItemFlag> {
val flags = mutableSetOf<ItemFlag>()

var flagArr: Array<ItemFlag>
val size = ItemFlag.values().also { flagArr = it }.size

for (i in 0 until size) {
val flag = flagArr[i]
if (this.hasItemFlag(flag)) {
flags.add(flag)
}
}

return flags
}

override fun hasItemFlag(flag: ItemFlag): Boolean {
val bitModifier = getBitModifier(flag)
return this.flagBits and bitModifier == bitModifier
}

private var flagBits: Int
get() =
if (handle.hasTag() && handle.tag!!.contains(
"HideFlags",
99
)
) handle.tag!!.getInt("HideFlags") else 0
set(value) =
handle.orCreateTag.putInt("HideFlags", value)

override fun getRepairCost(): Int {
return handle.baseRepairCost;
}
Expand Down

0 comments on commit c851e35

Please sign in to comment.