From 18c10ceadd33debec5530028841fe7c03af21c1c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 9 Jan 2019 16:55:33 +1100 Subject: [PATCH] Better /gun that also adjust glass box based on pitch. Also use PlayerClick instead of glass box when possible. --- MCGalaxy/Commands/Fun/CmdGun.cs | 10 ++--- MCGalaxy/Commands/Fun/CmdMissile.cs | 8 +--- MCGalaxy/Commands/Fun/WeaponCmd.cs | 57 ++++++++++++++++++++------ MCGalaxy/Commands/World/CmdFixGrass.cs | 2 +- MCGalaxy/Commands/building/CmdAbort.cs | 2 +- 5 files changed, 52 insertions(+), 27 deletions(-) diff --git a/MCGalaxy/Commands/Fun/CmdGun.cs b/MCGalaxy/Commands/Fun/CmdGun.cs index a18966358..25655c4e3 100644 --- a/MCGalaxy/Commands/Fun/CmdGun.cs +++ b/MCGalaxy/Commands/Fun/CmdGun.cs @@ -27,18 +27,14 @@ namespace MCGalaxy.Commands.Fun { public override string name { get { return "Gun"; } } protected override string Weapon { get { return "Gun"; } } - protected override void PlacedMark(Player p, ushort x, ushort y, ushort z, BlockID block) { - p.RevertBlock(x, y, z); - if (!p.level.Config.Guns) { p.ClearBlockchange(); return; } - if (!CommandParser.IsBlockAllowed(p, "use", block)) return; - + protected override void OnActivated(Player p, byte yaw, byte pitch, BlockID block) { WeaponArgs args = new WeaponArgs(); args.player = p; - args.block = block; + args.block = block; args.weaponType = (WeaponType)p.blockchangeObject; args.start = MakePos(p); - args.dir = DirUtils.GetFlatDirVector(p.Rot.RotY, p.Rot.HeadX); + args.dir = DirUtils.GetDirVector(yaw, pitch); args.pos = args.PosAt(3); args.iterations = 4; diff --git a/MCGalaxy/Commands/Fun/CmdMissile.cs b/MCGalaxy/Commands/Fun/CmdMissile.cs index 950286ef8..bb00deb96 100644 --- a/MCGalaxy/Commands/Fun/CmdMissile.cs +++ b/MCGalaxy/Commands/Fun/CmdMissile.cs @@ -28,15 +28,11 @@ namespace MCGalaxy.Commands.Fun { public override string name { get { return "Missile"; } } protected override string Weapon { get { return "Missile"; } } - protected override void PlacedMark(Player p, ushort x, ushort y, ushort z, BlockID block) { + protected override void OnActivated(Player p, byte yaw, byte pitch, BlockID block) { if (!p.staticCommands) { p.ClearBlockchange(); p.aiming = false; } - p.RevertBlock(x, y, z); - if (!p.level.Config.Guns || !CommandParser.IsBlockAllowed(p, "place", block)) { - p.ClearBlockchange(); return; - } WeaponArgs args = new WeaponArgs(); args.player = p; @@ -78,7 +74,7 @@ namespace MCGalaxy.Commands.Fun { static Vec3U16 MissileTarget(WeaponArgs args) { Player p = args.player; args.start = MakePos(p); - args.dir = DirUtils.GetFlatDirVector(p.Rot.RotY, p.Rot.HeadX); + args.dir = DirUtils.GetDirVector(p.Rot.RotY, p.Rot.HeadX); int i; for (i = 1; ; i++) { diff --git a/MCGalaxy/Commands/Fun/WeaponCmd.cs b/MCGalaxy/Commands/Fun/WeaponCmd.cs index 7765848e9..46e2361d9 100644 --- a/MCGalaxy/Commands/Fun/WeaponCmd.cs +++ b/MCGalaxy/Commands/Fun/WeaponCmd.cs @@ -21,25 +21,29 @@ using System.Threading; using MCGalaxy.Maths; using MCGalaxy.Tasks; using BlockID = System.UInt16; +using MCGalaxy.Events.PlayerEvents; namespace MCGalaxy.Commands.Fun { - public abstract class WeaponCmd : Command2 { + public abstract class WeaponCmd : Command2 { public override string type { get { return CommandTypes.Other; } } public override bool museumUsable { get { return false; } } public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } } public override bool SuperUseable { get { return false; } } protected abstract string Weapon { get; } + protected void Disable(Player p) { + p.aiming = false; + p.ClearBlockchange(); + p.Message(Weapon + " disabled"); + } + static bool hookedPlayerClick; + public override void Use(Player p, string message, CommandData data) { if (!p.level.Config.Guns) { p.Message(Weapon + "s cannot be used on this map!"); return; } - if (p.aiming && message.Length == 0) { - p.aiming = false; - p.ClearBlockchange(); - p.Message(Weapon + " disabled"); - return; + Disable(p); return; } WeaponType weaponType = GetWeaponType(p, message); @@ -47,12 +51,23 @@ namespace MCGalaxy.Commands.Fun { p.blockchangeObject = weaponType; p.ClearBlockchange(); - p.Blockchange += PlacedMark; - - p.Message(Weapon + " engaged, fire at will"); + bool hasPlayerClick = p.Supports(CpeExt.PlayerClick); + + if (hasPlayerClick) { + if (!hookedPlayerClick) { + OnPlayerClickEvent.Register(PlayerClickCallback, Priority.Low); + hookedPlayerClick = true; + } + p.Message(Weapon + " engaged, click to fire at will"); + } else { + p.Blockchange += BlockClickCallback; + p.Message(Weapon + " engaged, fire at will"); + } + if (p.aiming) return; - p.aiming = true; + if (hasPlayerClick) return; + AimState state = new AimState(); state.player = p; SchedulerTask task = new SchedulerTask(AimCallback, state, TimeSpan.Zero, true); @@ -91,7 +106,7 @@ namespace MCGalaxy.Commands.Fun { static void DoAim(AimState state) { Player p = state.player; - Vec3F32 dir = DirUtils.GetFlatDirVector(p.Rot.RotY, p.Rot.HeadX); + Vec3F32 dir = DirUtils.GetDirVector(p.Rot.RotY, p.Rot.HeadX); ushort x = (ushort)Math.Round(p.Pos.BlockX + dir.X * 3); ushort y = (ushort)Math.Round(p.Pos.BlockY + dir.Y * 3); ushort z = (ushort)Math.Round(p.Pos.BlockZ + dir.Z * 3); @@ -129,8 +144,26 @@ namespace MCGalaxy.Commands.Fun { if (lvl.IsAirAt(pos.X, pos.Y, pos.Z)) glassCoords.Add(pos); } - protected abstract void PlacedMark(Player p, ushort x, ushort y, ushort z, BlockID block); + void BlockClickCallback(Player p, ushort x, ushort y, ushort z, BlockID block) { + p.RevertBlock(x, y, z); + if (!p.level.Config.Guns) { Disable(p); return; } + if (!CommandParser.IsBlockAllowed(p, "use", block)) return; + + OnActivated(p, p.Rot.RotY, p.Rot.HeadX, block); + } + void PlayerClickCallback(Player p, MouseButton btn, MouseAction action, + ushort yaw, ushort pitch, byte entity, + ushort x, ushort y, ushort z, TargetBlockFace face) { + if (!p.aiming || btn != MouseButton.Left || action != MouseAction.Pressed) return; + if (!p.level.Config.Guns) { Disable(p); return; } + + BlockID held = p.GetHeldBlock(); + if (!CommandParser.IsBlockAllowed(p, "use", held)) return; + OnActivated(p, (byte)(yaw >> 8), (byte)(pitch >> 8), held); + } + + protected abstract void OnActivated(Player p, byte yaw, byte pitch, BlockID block); protected class WeaponArgs { public Player player; diff --git a/MCGalaxy/Commands/World/CmdFixGrass.cs b/MCGalaxy/Commands/World/CmdFixGrass.cs index b4f8adda0..81f082b45 100644 --- a/MCGalaxy/Commands/World/CmdFixGrass.cs +++ b/MCGalaxy/Commands/World/CmdFixGrass.cs @@ -1,4 +1,4 @@ -/* +/* Copyright 2010 MCLawl Team - Written by Valek (Modified for use with MCGalaxy) diff --git a/MCGalaxy/Commands/building/CmdAbort.cs b/MCGalaxy/Commands/building/CmdAbort.cs index 310c3c70b..f505bc9cd 100644 --- a/MCGalaxy/Commands/building/CmdAbort.cs +++ b/MCGalaxy/Commands/building/CmdAbort.cs @@ -1,4 +1,4 @@ -/* +/* Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) Dual-licensed under the Educational Community License, Version 2.0 and