Skip to content

Commit

Permalink
Basic WorldEditCUI hook for claims (TODO: for backup selection too)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgehog1029 committed Jul 25, 2018
1 parent b722b04 commit 45e4d97
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package gq.genprog.autocrat.integration

import gq.genprog.autocrat.modules.claims.PlayerSelection
import io.netty.buffer.Unpooled
import net.minecraft.entity.ai.attributes.RangedAttribute
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.network.NetHandlerPlayServer
import net.minecraft.network.PacketBuffer
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.network.FMLNetworkEvent
import net.minecraftforge.fml.common.network.NetworkRegistry
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket

/**
* Written by @offbeatwitch.
* Licensed under MIT.
*/
class WorldEditCUIHook {
val channelName = "WECUI"
val hasCuiMarker = RangedAttribute(null, "wecui.marker", 0.0, 0.0, 4.0)

val cuiChannel = NetworkRegistry.INSTANCE.newEventDrivenChannel(channelName)

init {
cuiChannel.register(this)
}

fun startCuboidSelection(player: EntityPlayerMP) {
player.sendCuiMsg("s|cuboid")
}

fun clearSelection(player: EntityPlayerMP) {
player.sendCuiMsg("s")
}

fun sendPoint(player: EntityPlayerMP, selection: PlayerSelection) {
if (!isCuiEnabled(player)) return

val area = selection.currentArea()

selection.first?.apply {
player.sendCuiMsg("p|0|$x|$y|$z|$area")
}

selection.second?.apply {
player.sendCuiMsg("p|1|$x|$y|$z|$area")
}

player.sendCuiMsg("grid|4.0")
player.sendCuiMsg("col|#|#00CED1|#FF8C00|#")
}

fun isCuiEnabled(player: EntityPlayerMP): Boolean {
return player.attributeMap.getAttributeInstanceByName(hasCuiMarker.name) != null
}

fun EntityPlayerMP.sendCuiMsg(text: String) {
if (isCuiEnabled(this))
cuiChannel.sendTo(createPacket(text), this)
}

fun createPacket(text: String): FMLProxyPacket {
val byteArray = text.toByteArray(Charsets.UTF_8)
val byteBuf = Unpooled.wrappedBuffer(byteArray)
val packetBuf = PacketBuffer(byteBuf)

return FMLProxyPacket(packetBuf, channelName)
}

@SubscribeEvent fun onCustomPacket(ev: FMLNetworkEvent.ServerCustomPacketEvent) {
if (ev.packet.channel() == channelName) {
val player = (ev.handler as NetHandlerPlayServer).player
val text = ev.packet.payload().toString(Charsets.UTF_8)

if (isCuiEnabled(player)) return

val (type, value) = text.split('|')

println("WECUI version $value")
player.attributeMap.registerAttribute(hasCuiMarker).also { it.baseValue = value.toInt().toDouble() }
}
}
}
12 changes: 10 additions & 2 deletions src/main/kotlin/gq/genprog/autocrat/modules/ClaimsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gq.genprog.autocrat.modules

import gq.genprog.autocrat.InventoryUtils
import gq.genprog.autocrat.config.AutocratConfig
import gq.genprog.autocrat.integration.WorldEditCUIHook
import gq.genprog.autocrat.modules.claims.ClaimWorldStorage
import gq.genprog.autocrat.modules.claims.PlayerSelection
import gq.genprog.autocrat.server.choice
Expand All @@ -25,10 +26,12 @@ import kotlin.collections.HashMap
*/
class ClaimsModule: EventListener {
val selections: HashMap<UUID, PlayerSelection> = HashMap()
val weHook = WorldEditCUIHook()

fun getPlayerSelection(player: EntityPlayer): PlayerSelection {
if (!selections.containsKey(player.uniqueID)) {
selections[player.uniqueID] = PlayerSelection()
weHook.startCuboidSelection(player as EntityPlayerMP)
}

return selections[player.uniqueID]!!
Expand All @@ -48,10 +51,12 @@ class ClaimsModule: EventListener {
}

if (event.itemStack.item == Items.GOLDEN_SHOVEL) {
getPlayerSelection(event.entityPlayer).first = event.pos
val sel = getPlayerSelection(event.entityPlayer)
sel.first = event.pos

event.controller().chat("Set first position to (${event.pos.x}, ${event.pos.y}, ${event.pos.z})")
event.isCanceled = true
weHook.sendPoint(event.entityPlayer as EntityPlayerMP, sel)
}
}

Expand Down Expand Up @@ -81,10 +86,12 @@ class ClaimsModule: EventListener {
}

if (event.itemStack.item == Items.GOLDEN_SHOVEL) {
getPlayerSelection(event.entityPlayer).second = event.pos
val sel = getPlayerSelection(event.entityPlayer)
sel.second = event.pos

event.controller().chat("Set second position to (${event.pos.x}, ${event.pos.y}, ${event.pos.z})")
event.isCanceled = true
weHook.sendPoint(event.entityPlayer as EntityPlayerMP, sel)
}
}

Expand Down Expand Up @@ -139,6 +146,7 @@ class ClaimsModule: EventListener {

ctx.chat("Claimed $numToClaim chunks.", TextFormatting.GREEN)
selections.remove(sender.uniqueID)
weHook.clearSelection(sender)
} else {
ctx.chat("Cancelled claim request.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ class PlayerSelection {
return first != null && second != null
}

fun currentArea(): Int {
if (!isComplete()) return 0

val (x1, y1, z1) = first!!.run { arrayOf(x, y, z) }
val (x2, y2, z2) = second!!.run { arrayOf(x, y, z) }

val w = Math.abs(x1 - x2)
val h = Math.abs(y1 - y2)
val d = Math.abs(z1 - z2)

return w * h * d
}

fun toImmutable(): CompletedSelection? {
if (!isComplete())
return null
Expand Down

0 comments on commit 45e4d97

Please sign in to comment.