Commit 240d1fe3 authored by Linus K.'s avatar Linus K.
Browse files

Add new plugin and auto farmer

parent 6e9df14e
Pipeline #119 passed with stage
in 36 seconds
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.linuscde.plugins</groupId>
<artifactId>autosyston</artifactId>
<name>AutoSyston</name>
<description>Light Automations</description>
<version>${mcplugins.autosyston.version}</version>
<parent>
<groupId>me.linuscde</groupId>
<artifactId>mcplugins</artifactId>
<version>${revision}</version>
</parent>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.linuscde.plugins</groupId>
<artifactId>kotlin</artifactId>
<version>${mcplugins.kotlin.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Compile Java and Kotlin code -->
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
</plugin>
<!-- Make jar with all dependencies included -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
name: AutoSyston
version: ${project.version}
author: ${mcplugins.author}
api-version: ${mcplugins.api-version}
depend:
- Kotlin
main: me.linuscde.mcplugins.autosyston.Plugin
package me.linuscde.mcplugins.autosyston
import me.linuscde.mcplugins.autosyston.listeners.*
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.util.logging.Level
class Plugin: JavaPlugin() {
companion object {
lateinit var instance: Plugin
private set
val pluginDir: File
get() {
val dir = File("plugins/${instance.name}")
if(!dir.exists()) dir.mkdirs();
return dir
}
val prefix: String by lazy {
"§a[§2§o${instance.name}§r§a]§f"
}
}
override fun onEnable() {
instance = this
registerCommands()
registerTabCompleters()
registerListeners()
registerSchedulers()
//Bukkit.getScheduler().scheduleSyncRepeatingTask(this, { saveConfig() }, 20*60*5, 20*60*5)
logger.log(Level.INFO, "Plugin enabled")
}
fun registerCommands() {}
fun registerTabCompleters() {
//for (commandName in arrayOf("a", "b")) {
// getCommand(commandName)?.tabCompleter = getCommand(commandName)?.executor as TabCompleter
//}
}
fun registerListeners() {
val pm = Bukkit.getPluginManager()
pm.registerEvents(AutoFarmerListener(), this)
}
fun registerSchedulers() {
AutoFarmerListener.initScheduler(this)
}
override fun onDisable() {
//saveConfig()
logger.log(Level.INFO, "Plugin disabled")
}
}
\ No newline at end of file
package me.linuscde.mcplugins.autosyston.listeners
import me.linuscde.mcplugins.autosyston.Plugin
import org.bukkit.*
import org.bukkit.block.Block
import org.bukkit.block.BlockFace
import org.bukkit.entity.Item
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.block.Action
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.inventory.ItemStack
class AutoFarmerListener: Listener {
companion object {
val IGNORE_INTERACTS = HashMap<Location, Long>()
const val IGNORE_DURATION_MILLIS = 5*1000
val CROPS = hashMapOf(
Material.WHEAT to Material.WHEAT_SEEDS,
Material.BEETROOTS to Material.BEETROOT_SEEDS,
Material.CARROTS to Material.CARROT
)
const val BLOCK_DELAY_TICKS = 5L
const val BLOCK_FARM_PLANT_DELAY_TICKS = 5L
fun isIgnoreInteract(location: Location) = IGNORE_INTERACTS.any { it.key == location && it.value >= System.currentTimeMillis() }
fun initScheduler(plugin: Plugin) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, {
for((player, expiresAt) in IGNORE_INTERACTS.entries.toTypedArray()) {
if(expiresAt <= System.currentTimeMillis())
IGNORE_INTERACTS.remove(player)
}
}, 20, 20)
}
fun isCrop(block: Block) = block.type in CROPS
fun isRipe(block: Block): Boolean {
if(!isCrop(block)) return false
val cropState = CropState.getByData(block.data)
return cropState == CropState.RIPE
}
fun getNearbyCrops(originBlock: Block, maxRecursion: Int = 50): ArrayList<ArrayList<Block>> {
if(!isCrop(originBlock))
return arrayListOf()
val list = arrayListOf<ArrayList<Block>>()
val ignoreLocations = mutableSetOf<Location>() // WHY DOESN'T THIS SEEM TO WORK?!?!?!?!
var queuedBlocks = arrayListOf(originBlock)
var recursion = 0;
while (queuedBlocks.isNotEmpty()) {
for (block in queuedBlocks) {
list.add(queuedBlocks)
}
recursion++
if(recursion > maxRecursion) break
val newQueuedBlocks = arrayListOf<Block>()
for(block in queuedBlocks) {
for(rel in arrayOf(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST)) {
val nextBlock = block.getRelative(rel) ?: continue
if(isCrop(nextBlock) && nextBlock.location !in ignoreLocations) {
newQueuedBlocks.add(nextBlock)
ignoreLocations.add(nextBlock.location)
}
}
}
queuedBlocks = newQueuedBlocks
}
return list
}
fun findNearestItem(location: Location, material: Material, radius: Double = 5.0): Item? {
var nearest: Item? = null
var nearestDist: Double? = null
for(item in location.world!!.entities) {
if(item !is Item) continue
if(item.itemStack.type != material) continue
val dist = item.location.distance(location)
if(dist < radius && (nearestDist == null || dist < nearestDist)) {
nearest = item
nearestDist = dist
}
}
return nearest
}
fun farmFrom(originBlock: Block) {
val blockList = getNearbyCrops(originBlock)
val alreadyQueued = mutableSetOf<Location>()
var i = -1;
for(blocks in blockList) {
for(block in blocks) {
if(block.location in alreadyQueued) continue
alreadyQueued.add(block.location)
if(!isRipe(block)) continue // Do only ripe ones
i++
// Farm
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, {
if (!isCrop(block)) return@scheduleSyncDelayedTask
val cropType = block.type
block.breakNaturally(ItemStack(Material.DIAMOND_HOE))
block.world.playSound(block.location.add(.5, .0, .5), Sound.BLOCK_CROP_BREAK, 1f, 1f)
// Plant
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, {
if (block.type != Material.AIR) return@scheduleSyncDelayedTask
val seedType = CROPS.getOrDefault(cropType, Material.AIR)
if (seedType == Material.AIR) return@scheduleSyncDelayedTask
if (block.getRelative(BlockFace.DOWN).type != Material.FARMLAND) return@scheduleSyncDelayedTask
val seed = findNearestItem(block.location.add(.5, .0, .5), seedType)
?: return@scheduleSyncDelayedTask
val seedStack = seed.itemStack
if (seedStack.amount == 1) {
seed.remove()
} else {
seedStack.amount--
seed.itemStack = seedStack
}
block.type = cropType
block.state.update(true, true)
block.world.playSound(block.location.add(.5, .0, .5), Sound.ITEM_CROP_PLANT, 1f, 1f)
}, BLOCK_FARM_PLANT_DELAY_TICKS)
}, i * BLOCK_DELAY_TICKS)
}
}
}
}
@EventHandler
fun onClockRipeCrop(e: PlayerInteractEvent) {
if(e.action != Action.RIGHT_CLICK_BLOCK) return
val clickedBlock = e.clickedBlock ?: return
if(isIgnoreInteract(clickedBlock.location)) return
if(isRipe(clickedBlock)) {
for(item in arrayOf(e.player.inventory.itemInMainHand, e.player.inventory.itemInOffHand)) {
if(item.type == Material.BONE_MEAL || item.type in CROPS || item.type in CROPS.values)
return
}
IGNORE_INTERACTS[clickedBlock.location] = System.currentTimeMillis() + IGNORE_DURATION_MILLIS
farmFrom(clickedBlock)
e.isCancelled = true
}
}
}
\ No newline at end of file
......@@ -16,8 +16,9 @@
<maven.compiler.target>1.8</maven.compiler.target>
<revision>0.1.0-SNAPSHOT</revision>
<mcplugins.autosyston.version>0.1.0</mcplugins.autosyston.version>
<mcplugins.kotlin.version>1.0.0</mcplugins.kotlin.version>
<mcplugins.teleportastic.version>0.1.0</mcplugins.teleportastic.version>
<mcplugins.teleportastic.version>0.2.0</mcplugins.teleportastic.version>
<spigot-api.version>1.16.3-R0.1-SNAPSHOT</spigot-api.version>
<mcplugins.author>LinusCDE</mcplugins.author>
......@@ -30,6 +31,7 @@
<modules>
<module>AutoSyston</module>
<module>Kotlin</module>
<module>Teleportastic</module>
</modules>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment