diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/Crate.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/Crate.kt index 87ae87b..aed3aa3 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/Crate.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/Crate.kt @@ -21,6 +21,7 @@ import com.willfp.eco.util.savedDisplayName import com.willfp.ecocrates.crate.placed.HologramFrame import com.willfp.ecocrates.crate.placed.particle.ParticleAnimations import com.willfp.ecocrates.crate.placed.particle.ParticleData +import com.willfp.ecocrates.crate.reroll.ReRollGUI import com.willfp.ecocrates.crate.roll.Roll import com.willfp.ecocrates.crate.roll.RollOptions import com.willfp.ecocrates.crate.roll.Rolls @@ -88,6 +89,8 @@ class Crate( Bukkit.getPluginManager().addPermission(this) } + val canReroll = config.getBool("can-reroll") + private val keysKey: PersistentDataKey = PersistentDataKey( plugin.namespacedKeyFactory.create("${id}_keys"), PersistentDataKeyType.INT, @@ -160,7 +163,7 @@ class Crate( ) { getOpens(it).toString() }.register() } - private fun makeRoll(player: Player, location: Location, reward: Reward): Roll { + private fun makeRoll(player: Player, location: Location, reward: Reward, isReroll: Boolean = false): Roll { val display = mutableListOf() // Add three to the scroll times so that it lines up @@ -174,7 +177,8 @@ class Crate( this, this.plugin, player, - location + location, + isReroll ) ) } @@ -317,7 +321,7 @@ class Crate( } } - fun open(player: Player, location: Location? = null, physicalKey: Boolean = false): Boolean { + fun open(player: Player, location: Location? = null, physicalKey: Boolean = false, isReroll: Boolean = false): Boolean { /* Prevent server crashes */ if (hasRanOutOfRewardsAndNotify(player)) { return false @@ -346,7 +350,7 @@ class Crate( .map { plugin.langYml.prefix + StringUtils.format(it, player) } .forEach { Bukkit.broadcastMessage(it) } - val roll = makeRoll(player, loc, event.reward) + val roll = makeRoll(player, loc, event.reward, isReroll = isReroll) var tick = 0 plugin.runnableFactory.create { @@ -357,7 +361,7 @@ class Crate( it.cancel() roll.onFinish() player.isOpeningCrate = false - this.handleFinish(player, roll, loc) + if (!canReroll || roll.isReroll) handleFinish(roll) else ReRollGUI.open(roll) } }.runTaskTimer(1, 1) @@ -372,7 +376,10 @@ class Crate( previewGUI.open(player) } - fun handleFinish(player: Player, roll: Roll, location: Location) { + fun handleFinish(roll: Roll) { + val player = roll.player + val location = roll.location + val event = CrateRewardEvent(player, this, roll.reward) Bukkit.getPluginManager().callEvent(event) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/reroll/ReRollGUI.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/reroll/ReRollGUI.kt new file mode 100644 index 0000000..e74016e --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/reroll/ReRollGUI.kt @@ -0,0 +1,60 @@ +package com.willfp.ecocrates.crate.reroll + +import com.willfp.eco.core.gui.menu +import com.willfp.eco.core.gui.slot +import com.willfp.eco.core.gui.slot.FillerMask +import com.willfp.eco.core.gui.slot.MaskItems +import com.willfp.eco.core.items.Items +import com.willfp.eco.core.items.builder.ItemStackBuilder +import com.willfp.ecocrates.crate.Crates +import com.willfp.ecocrates.crate.roll.Roll + +object ReRollGUI { + fun open(roll: Roll) { + val plugin = roll.plugin + + val menu = menu(plugin.configYml.getInt("reroll.rows")) { + setMask( + FillerMask( + MaskItems.fromItemNames(plugin.configYml.getStrings("reroll.mask.items")), + *plugin.configYml.getStrings("reroll.mask.pattern").toTypedArray() + ) + ) + + setTitle(plugin.configYml.getFormattedString("reroll.title")) + + for (crate in Crates.values()) { + crate.addToKeyGUI(this) + } + + setSlot( + plugin.configYml.getInt("reroll.accept.row"), + plugin.configYml.getInt("reroll.accept.column"), + slot(roll.reward.getDisplay(roll.player, roll.crate)) { + onLeftClick { _, _, _ -> + roll.player.closeInventory() // Will automatically call finish + } + } + ) + + setSlot( + plugin.configYml.getInt("reroll.reroll.row"), + plugin.configYml.getInt("reroll.reroll.column"), + slot( + ItemStackBuilder(Items.lookup(plugin.configYml.getString("reroll.reroll.item"))) + .addLoreLines(plugin.configYml.getStrings("reroll.reroll.lore")) + .setDisplayName(plugin.configYml.getString("reroll.reroll.name")) + .build() + ) { + onLeftClick { _, _, _ -> + roll.crate.open(roll.player, roll.location, isReroll = true) + } + } + ) + + onClose { _, _ -> roll.crate.handleFinish(roll) } + } + + menu.open(roll.player) + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/Roll.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/Roll.kt index aecaf5d..e2104f2 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/Roll.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/Roll.kt @@ -1,6 +1,10 @@ package com.willfp.ecocrates.crate.roll +import com.willfp.eco.core.EcoPlugin +import com.willfp.ecocrates.crate.Crate import com.willfp.ecocrates.reward.Reward +import org.bukkit.Location +import org.bukkit.entity.Player interface Roll { /** @@ -8,6 +12,31 @@ interface Roll { */ val reward: Reward + /** + * The player. + */ + val player: Player + + /** + * The crate. + */ + val crate: Crate + + /** + * The plugin. + */ + val plugin: EcoPlugin + + /** + * The location. + */ + val location: Location + + /** + * If the roll is a reroll. + */ + val isReroll: Boolean + /** * Called on start - once the player begins opening the crate. */ diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollCSGO.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollCSGO.kt index c65219d..345f112 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollCSGO.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollCSGO.kt @@ -18,10 +18,11 @@ import org.bukkit.inventory.ItemStack class RollCSGO private constructor( override val reward: Reward, - private val crate: Crate, - private val plugin: EcoPlugin, - private val player: Player, - private val location: Location + override val crate: Crate, + override val plugin: EcoPlugin, + override val player: Player, + override val location: Location, + override val isReroll: Boolean ) : Roll { private val scrollTimes = plugin.configYml.getInt("rolls.csgo.scrolls") private val bias = plugin.configYml.getDouble("rolls.csgo.bias") @@ -126,7 +127,8 @@ class RollCSGO private constructor( options.crate, options.plugin, options.player, - options.location + options.location, + options.isReroll ) } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollEncircle.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollEncircle.kt index df78c98..1177899 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollEncircle.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollEncircle.kt @@ -14,10 +14,11 @@ import kotlin.math.PI class RollEncircle private constructor( override val reward: Reward, - private val crate: Crate, - private val plugin: EcoPlugin, - private val player: Player, - private val location: Location + override val crate: Crate, + override val plugin: EcoPlugin, + override val player: Player, + override val location: Location, + override val isReroll: Boolean ) : Roll { private val riseVelocity = plugin.configYml.getDouble("rolls.encircle.rise-velocity") private val spinVelocity = plugin.configYml.getDouble("rolls.encircle.spin-velocity") @@ -172,7 +173,8 @@ class RollEncircle private constructor( options.crate, options.plugin, options.player, - options.location + options.location, + options.isReroll ) } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollFlash.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollFlash.kt index 5a2b316..2b488ec 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollFlash.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollFlash.kt @@ -13,10 +13,11 @@ import org.bukkit.util.Vector class RollFlash private constructor( override val reward: Reward, - private val crate: Crate, - private val plugin: EcoPlugin, - private val player: Player, - private val location: Location + override val crate: Crate, + override val plugin: EcoPlugin, + override val player: Player, + override val location: Location, + override val isReroll: Boolean ) : Roll { private val wait = plugin.configYml.getInt("rolls.flash.wait") private val display = crate.getRandomRewards(player, 100, displayWeight = true) @@ -95,7 +96,8 @@ class RollFlash private constructor( options.crate, options.plugin, options.player, - options.location + options.location, + options.isReroll ) } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollOptions.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollOptions.kt index bb590a6..b4a9787 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollOptions.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecocrates/crate/roll/RollOptions.kt @@ -11,5 +11,6 @@ data class RollOptions( val crate: Crate, val plugin: EcoPlugin, val player: Player, - val location: Location + val location: Location, + val isReroll: Boolean ) \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 28388c8..3a43888 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -5,6 +5,32 @@ no-key-velocity: 1.5 # The speed at which a player should be launched away from a crate if they try to open it without a key. Set to 0 to disable. +reroll: + rows: 3 + mask: + items: + - black_stained_glass_pane + - green_stained_glass_pane + pattern: + - "122211111" + - "120211011" + - "122211111" + title: "Accept your reward?" + accept: + row: 2 + column: 3 + reroll: + row: 2 + column: 7 + item: orange_stained_glass_pane + name: "&6Reroll" + lore: + - "&fNot happy with your item?" + - "&fClick to try again for" + - "&fa chance at something else!" + - "" + - "&cYou can only reroll once!" + keygui: rows: 3 mask: diff --git a/eco-core/core-plugin/src/main/resources/crates.yml b/eco-core/core-plugin/src/main/resources/crates.yml index 29c9bd3..9cd00bf 100644 --- a/eco-core/core-plugin/src/main/resources/crates.yml +++ b/eco-core/core-plugin/src/main/resources/crates.yml @@ -2,6 +2,7 @@ crates: - id: demo name: "Demo Crate" roll: csgo + can-reroll: true preview: title: Demo Crate