1.12.2 Send message to player using PHP

Discussion in 'Spigot Plugin Development' started by TaskID, Aug 2, 2020 at 12:41 AM.

  1. Hello,

    I want to send messages to players using PHP. It'd be great that I can also let the player perform a command using PHP.

    All on BungeeCord.

    Is there an "Event" that is called when a MySQL Table gets updated, so I would just add the things I want in a database and then check in BungeeCord if something is updated. Or is there an easier way?

    I really need help because I have no idea how to do that, since I'm a beginner in PHP.

    Thanks :)
     
  2. Strahan

    Benefactor

  3. #4 robertlit, Aug 3, 2020 at 1:53 PM
    Last edited: Aug 3, 2020 at 1:59 PM
    • Informative Informative x 1
  4. Strahan

    Benefactor

    Well, it's just a basic socket connection. I suppose you could write a socket server in your plugin, but that seems foolish to reinvent the wheel IMO.

    As to where the player is, that depends on your website. If you wanted to, say, do a lottery type of thing you could do like:
    Code (Text):
    <form name=data method=post action=test.php>
    <?php

      if (empty($_REQUEST["player"])) {
        echo "<p>Please enter your player name:<br/><input type=text name=player size=20></p>";
        echo "<input type=submit>";
        echo "<script>document.data.player.focus();</script>";
        return;
      }
     
      $awards = array();
      $awards[30] = array("iron_ingot", mt_rand(10, 64), "iron ingot!s");
      $awards[15] = array("iron_pickaxe", 1, "iron pickaxe!s");
      $awards[10] = array("diamond", mt_rand(1, 10), "diamond!s");
      $awards[5] = array("diamond_sword", 1, "diamond sword!s");
     
      $rnd = mt_rand(1, 100);
      foreach ($awards AS $chance=>$data) {
        if ($rnd > $chance) {
          echo "<p>Sorry, better luck next time!</p>";
          break;
        }

        if (!sendPrize($_REQUEST["player"], $data[0], $data[1])) {
          echo "<p class=error>Sorry, I cannot connect to the server!</p>";
          break;
        }

        echo pluralize("<p class=win>You've won !# {$data[2]}</p>", $data[1]);
        break;
      }

     
      function sendPrize($player, $item, $qty) {
        require_once("WebsenderAPI.php");
        $server = new WebsenderAPI("<ipaddress>","<password>","<port>");
        if (!$server->connect()) return false;

        $server->sendCommand("give $player $item $qty");
        $server->disconnect();
        return true;
      }
     
      function pluralize($message, $qty) {
        $ret = str_replace("!#", $qty, $message);
        $ret = str_replace("!s", (($qty == 1)?"":"s"), $ret);
        $ret = str_replace("!es", (($qty == 1)?"":"es"), $ret);
        $ret = str_replace("!ies", (($qty == 1)?"y":"ies"), $ret);
        $ret = str_replace("!oo", (($qty == 1)?"oo":"ee"), $ret);
        $ret = str_replace("!an", (($qty == 1)?"an":"en"), $ret);
        $ret = str_replace("!us", (($qty == 1)?"us":"i"), $ret);
        $ret = str_replace("!is", (($qty == 1)?"is":"es"), $ret);
        $ret = str_replace("!o", (($qty == 1)?"o":"oes"), $ret);
        $ret = str_replace("!on", (($qty == 1)?"a":"on"), $ret);
        $ret = str_replace("!lf", (($qty == 1)?"lf":"lves"), $ret);
        $ret = str_replace("!ia", (($qty == 1)?"is":"are"), $ret);
        $ret = str_replace("!ww", (($qty == 1)?"was":"were"), $ret);
        return $ret;
      }
    ?>
     
  5. Create a connection to the Console.

    Or just like you all ready said write into a database and check in your plugin with a Runnable if it is updated!

    And hier is a good page to learn more about PHP and other Website stuff :D
    https://www.w3schools.com/
     
  6. This answer does not actually answer anything.
    You say "create a connection to the console", but the question is, how?
    The other thing you suggest is just wrong, constantly checking for updates in a database is a bad idea, a proper messaging system should be used instead.
     
    • Agree Agree x 1
  7. Strahan

    Benefactor

    Yea, that's like someone asking how to make chicken pot pie so you tell them "step 1. buy ingredients. step 2. cook it" lol
     
  8. It's ok, I can do it async so It won't cause any lags, and it's enough for me to call it every 10 or 15 seconds so ;)
    Thanks guys