Files
ForgeMultipart/codechicken/multipart/ControlKeyModifier.scala
T

58 lines
1.8 KiB
Scala
Raw Normal View History

2013-05-20 17:09:57 +10:00
package codechicken.multipart
import net.minecraft.client.settings.KeyBinding
import org.lwjgl.input.Keyboard
import net.minecraft.client.Minecraft
import codechicken.lib.packet.PacketCustom
2013-05-20 17:09:57 +10:00
import codechicken.multipart.handler.MultipartCPH
import scala.collection.mutable.HashMap
import net.minecraft.entity.player.EntityPlayer
2014-04-07 23:14:31 +10:00
import cpw.mods.fml.common.eventhandler.SubscribeEvent
import cpw.mods.fml.common.gameevent.InputEvent.KeyInputEvent
import cpw.mods.fml.relauncher.{Side, SideOnly}
import cpw.mods.fml.common.gameevent.TickEvent.ClientTickEvent
2013-05-20 17:09:57 +10:00
2013-09-25 12:56:40 +10:00
/**
* A class that maintains a map server<->client of which players are holding the control (or placement modifier key) much like sneaking.
*/
2013-05-20 17:09:57 +10:00
object ControlKeyModifer
{
implicit def playerControlValue(p:EntityPlayer) = new ControlKeyValue(p)
class ControlKeyValue(p:EntityPlayer)
{
2013-12-30 13:52:08 +10:00
def isControlDown = map(p)
2013-05-20 17:09:57 +10:00
}
val map = HashMap[EntityPlayer, Boolean]().withDefaultValue(false)
2013-09-25 12:56:40 +10:00
/**
* Implicit static for Java users.
*/
def isControlDown(p:EntityPlayer) = p.isControlDown
2013-05-20 17:09:57 +10:00
}
2013-09-25 12:56:40 +10:00
/**
* Key Handler implementation
*/
2014-04-07 23:14:31 +10:00
object ControlKeyHandler extends KeyBinding("key.control", Keyboard.KEY_LCONTROL, "key.categories.gameplay")
2013-05-20 17:09:57 +10:00
{
import ControlKeyModifer._
2014-04-07 23:14:31 +10:00
var wasPressed = false
@SubscribeEvent
@SideOnly(Side.CLIENT)
def tick(event:ClientTickEvent) {
val pressed = getIsKeyPressed
if(pressed != wasPressed) {
wasPressed = pressed
if(Minecraft.getMinecraft.getNetHandler != null)
{
map.put(Minecraft.getMinecraft.thePlayer, pressed)
val packet = new PacketCustom(MultipartCPH.channel, 1)
packet.writeBoolean(pressed)
packet.sendToServer()
}
2013-05-20 17:09:57 +10:00
}
}
}