mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Add some testing (#38)
This commit is contained in:
parent
c42140a45a
commit
ed58c9c107
34 changed files with 1911 additions and 150 deletions
|
|
@ -2,6 +2,7 @@ package xyz.alexcrea.cuanvil.dependency
|
|||
|
||||
import io.delilaheve.CustomAnvil
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.entity.HumanEntity
|
||||
import org.bukkit.event.inventory.InventoryClickEvent
|
||||
import org.bukkit.event.inventory.PrepareAnvilEvent
|
||||
import org.bukkit.inventory.AnvilInventory
|
||||
|
|
@ -91,11 +92,11 @@ object DependencyManager {
|
|||
|
||||
}
|
||||
|
||||
fun tryEventPreAnvilBypass(event: PrepareAnvilEvent): Boolean {
|
||||
fun tryEventPreAnvilBypass(event: PrepareAnvilEvent, player: HumanEntity): Boolean {
|
||||
var bypass = false
|
||||
|
||||
// Test if disenchantment used special prepare anvil
|
||||
if(disenchantmentCompatibility?.testPrepareAnvil(event) == true) bypass = true
|
||||
if(disenchantmentCompatibility?.testPrepareAnvil(event, player) == true) bypass = true
|
||||
|
||||
// Test excellent enchantments used special prepare anvil
|
||||
if(!bypass && (excellentEnchantsCompatibility?.testPrepareAnvil(event) == true)) bypass = true
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import cz.kominekjan.disenchantment.events.DisenchantEvent
|
|||
import cz.kominekjan.disenchantment.events.ShatterClickEvent
|
||||
import cz.kominekjan.disenchantment.events.ShatterEvent
|
||||
import io.delilaheve.CustomAnvil
|
||||
import org.bukkit.entity.HumanEntity
|
||||
import org.bukkit.event.inventory.InventoryClickEvent
|
||||
import org.bukkit.event.inventory.PrepareAnvilEvent
|
||||
import org.bukkit.inventory.AnvilInventory
|
||||
|
|
@ -70,7 +71,7 @@ class DisenchantmentDependency {
|
|||
|
||||
}
|
||||
|
||||
fun testPrepareAnvil(event: PrepareAnvilEvent): Boolean {
|
||||
fun testPrepareAnvil(event: PrepareAnvilEvent, player: HumanEntity): Boolean {
|
||||
val previousResult = event.result
|
||||
event.result = null
|
||||
|
||||
|
|
@ -79,14 +80,14 @@ class DisenchantmentDependency {
|
|||
|
||||
if(event.result != null) {
|
||||
CustomAnvil.log("Detected pre anvil item extract bypass.")
|
||||
AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, event.inventory.repairCost)
|
||||
AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, player, event.inventory.repairCost)
|
||||
return true
|
||||
}
|
||||
|
||||
splitEvent.onDisenchantmentEvent(event)
|
||||
if(event.result != null) {
|
||||
CustomAnvil.log("Detected pre anvil split enchant bypass.")
|
||||
AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, event.inventory.repairCost)
|
||||
AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, player, event.inventory.repairCost)
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,19 +41,20 @@ class PrepareAnvilListener : Listener {
|
|||
*/
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
fun anvilCombineCheck(event: PrepareAnvilEvent) {
|
||||
// Should find player
|
||||
val player: HumanEntity = event.viewers.first()
|
||||
|
||||
// Test if the event should bypass custom anvil.
|
||||
if(DependencyManager.tryEventPreAnvilBypass(event)) return
|
||||
if(DependencyManager.tryEventPreAnvilBypass(event, player)) return
|
||||
|
||||
val inventory = event.inventory
|
||||
val first = inventory.getItem(ANVIL_INPUT_LEFT) ?: return
|
||||
val second = inventory.getItem(ANVIL_INPUT_RIGHT)
|
||||
|
||||
// Should find player
|
||||
val player = event.view.player
|
||||
if (!player.hasPermission(CustomAnvil.affectedByPluginPermission)) return
|
||||
|
||||
// Test custom recipe
|
||||
if(testCustomRecipe(event, inventory, first, second)) return
|
||||
if(testCustomRecipe(event, inventory, player, first, second)) return
|
||||
|
||||
// Test rename lonely item
|
||||
if(second == null) {
|
||||
|
|
@ -75,7 +76,9 @@ class PrepareAnvilListener : Listener {
|
|||
|
||||
}
|
||||
|
||||
private fun testCustomRecipe(event: PrepareAnvilEvent, inventory: AnvilInventory, first: ItemStack, second: ItemStack?): Boolean {
|
||||
private fun testCustomRecipe(event: PrepareAnvilEvent, inventory: AnvilInventory,
|
||||
player: HumanEntity,
|
||||
first: ItemStack, second: ItemStack?): Boolean {
|
||||
val recipe = CustomRecipeUtil.getCustomRecipe(first, second)
|
||||
CustomAnvil.verboseLog("custom recipe not null? ${recipe != null}")
|
||||
if(recipe == null) return false
|
||||
|
|
@ -87,7 +90,7 @@ class PrepareAnvilListener : Listener {
|
|||
|
||||
event.result = resultItem
|
||||
DependencyManager.treatAnvilResult(event, resultItem)
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, recipe.xpCostPerCraft * amount, true)
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, recipe.xpCostPerCraft * amount, true)
|
||||
|
||||
return true
|
||||
}
|
||||
|
|
@ -109,7 +112,7 @@ class PrepareAnvilListener : Listener {
|
|||
|
||||
anvilCost += AnvilXpUtil.calculatePenalty(first, null, resultItem)
|
||||
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, anvilCost)
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
|
||||
}
|
||||
|
||||
private fun handleRename(resultItem: ItemStack, inventory: AnvilInventory, player: HumanEntity): Int {
|
||||
|
|
@ -177,7 +180,7 @@ class PrepareAnvilListener : Listener {
|
|||
event.result = resultItem
|
||||
DependencyManager.treatAnvilResult(event, resultItem)
|
||||
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, anvilCost)
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
|
||||
}
|
||||
|
||||
private fun testUnitRepair(event: PrepareAnvilEvent, inventory: AnvilInventory, player: HumanEntity,
|
||||
|
|
@ -203,7 +206,7 @@ class PrepareAnvilListener : Listener {
|
|||
event.result = resultItem
|
||||
DependencyManager.treatAnvilResult(event, resultItem)
|
||||
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, anvilCost)
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,17 +9,17 @@ class CustomAnvilRecipeManager {
|
|||
|
||||
lateinit var recipeList: ArrayList<AnvilCustomRecipe>
|
||||
|
||||
lateinit var recipeByMat: LinkedHashMap<Material, ArrayList<AnvilCustomRecipe>>
|
||||
lateinit var recipeByMat: HashMap<Material, ArrayList<AnvilCustomRecipe>>
|
||||
|
||||
fun prepareRecipes(config: FileConfiguration) {
|
||||
recipeList = ArrayList()
|
||||
recipeByMat = LinkedHashMap()
|
||||
recipeByMat = HashMap()
|
||||
|
||||
// read all configs
|
||||
val keys = config.getKeys(false)
|
||||
for (key in keys) {
|
||||
val recipe = AnvilCustomRecipe.getFromConfig(key)
|
||||
if(recipe == null){
|
||||
if (recipe == null) {
|
||||
CustomAnvil.log("Can't load recipe $key")
|
||||
continue
|
||||
}
|
||||
|
|
@ -30,34 +30,34 @@ class CustomAnvilRecipeManager {
|
|||
}
|
||||
|
||||
|
||||
fun cleanAddNew(recipe: AnvilCustomRecipe){
|
||||
fun cleanAddNew(recipe: AnvilCustomRecipe) {
|
||||
recipeList.add(recipe)
|
||||
val leftItem = recipe.leftItem
|
||||
if(leftItem != null){
|
||||
if (leftItem != null) {
|
||||
addToMatMap(recipe, leftItem)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun cleanSetLeftItem(recipe: AnvilCustomRecipe, leftItem: ItemStack?){
|
||||
fun cleanSetLeftItem(recipe: AnvilCustomRecipe, leftItem: ItemStack?) {
|
||||
// Remove left item mat if exist
|
||||
val oldLeftItem = recipe.leftItem
|
||||
if(oldLeftItem != null){
|
||||
if (oldLeftItem != null) {
|
||||
val oldMat = oldLeftItem.type
|
||||
|
||||
val test = recipeByMat[oldMat]
|
||||
test!!.remove(recipe)
|
||||
}
|
||||
if(leftItem != null){
|
||||
if (leftItem != null) {
|
||||
addToMatMap(recipe, leftItem)
|
||||
}
|
||||
|
||||
recipe.leftItem = leftItem
|
||||
}
|
||||
|
||||
private fun addToMatMap(recipe: AnvilCustomRecipe, leftItem: ItemStack){
|
||||
private fun addToMatMap(recipe: AnvilCustomRecipe, leftItem: ItemStack) {
|
||||
var recipeList = recipeByMat[leftItem.type]
|
||||
if(recipeList == null){
|
||||
if (recipeList == null) {
|
||||
recipeList = ArrayList()
|
||||
recipeByMat[leftItem.type] = recipeList
|
||||
}
|
||||
|
|
@ -65,11 +65,14 @@ class CustomAnvilRecipeManager {
|
|||
|
||||
}
|
||||
|
||||
fun cleanRemove(recipe: AnvilCustomRecipe) {
|
||||
fun cleanRemove(recipe: AnvilCustomRecipe): Boolean {
|
||||
|
||||
recipeList.remove(recipe)
|
||||
cleanSetLeftItem(recipe, null)
|
||||
val exist = recipeList.remove(recipe)
|
||||
if (exist) {
|
||||
cleanSetLeftItem(recipe, null)
|
||||
}
|
||||
|
||||
return exist;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import io.delilaheve.util.EnchantmentUtil.enchantmentName
|
|||
import io.delilaheve.util.ItemUtil.findEnchantments
|
||||
import io.delilaheve.util.ItemUtil.isEnchantedBook
|
||||
import org.bukkit.GameMode
|
||||
import org.bukkit.entity.HumanEntity
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.inventory.AnvilInventory
|
||||
import org.bukkit.inventory.InventoryView
|
||||
|
|
@ -25,6 +26,7 @@ object AnvilXpUtil {
|
|||
fun setAnvilInvXp(
|
||||
inventory: AnvilInventory,
|
||||
view: InventoryView,
|
||||
player: HumanEntity,
|
||||
anvilCost: Int,
|
||||
ignoreRules: Boolean = false
|
||||
) {
|
||||
|
|
@ -38,8 +40,6 @@ object AnvilXpUtil {
|
|||
anvilCost
|
||||
}
|
||||
|
||||
val player = view.player
|
||||
|
||||
/* Because Minecraft likes to have the final say in the repair cost displayed
|
||||
* we need to wait for the event to end before overriding it, this ensures that
|
||||
* we have the final say in the process. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue