Skip to content

Commit

Permalink
Hotfix for MySQL error.
Browse files Browse the repository at this point in the history
Fixes an issue where users were faced with a "Data too long for column" error when creating an island.
  • Loading branch information
DuoIncure committed Jul 31, 2023
1 parent c1cba7e commit 7ef0bb5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
4 changes: 2 additions & 2 deletions resources/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-- # { init
CREATE TABLE IF NOT EXISTS skyblockspm_player
(
uuid VARCHAR(32) PRIMARY KEY,
uuid VARCHAR(36) PRIMARY KEY,
name VARCHAR(32),
skyblock TEXT DEFAULT ''
);
Expand Down Expand Up @@ -44,7 +44,7 @@ WHERE uuid=:uuid;
-- # { init
CREATE TABLE IF NOT EXISTS skyblockspm_sb
(
uuid VARCHAR(32) PRIMARY KEY,
uuid VARCHAR(36) PRIMARY KEY,
name VARCHAR(32),
leader VARCHAR(32),
members TEXT,
Expand Down
45 changes: 45 additions & 0 deletions src/Vecnavium/SkyBlocksPM/commands/args/IslandNameArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace Vecnavium\SkyBlocksPM\commands\args;

use CortexPE\Commando\args\BaseArgument;
use pocketmine\command\CommandSender;
use pocketmine\network\mcpe\protocol\AvailableCommandsPacket;
use function preg_match;

class IslandNameArgument extends BaseArgument {

/**
* @return int
*/
public function getNetworkType(): int{
return AvailableCommandsPacket::ARG_TYPE_STRING;
}

/**
* @param string $testString
* @param CommandSender $sender
* @return bool
*/
public function canParse(string $testString, CommandSender $sender): bool {
// PM player username validity regex
return (bool)preg_match('/^\w{3,32}$/i', $testString);
}

/**
* @param string $argument
* @param CommandSender $sender
* @return mixed
*/
public function parse(string $argument, CommandSender $sender): string {
return $argument;
}

/**
* @return string
*/
public function getTypeName(): string{
return 'island';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
use pocketmine\player\Player as P;
use pocketmine\utils\Utils;
use Ramsey\Uuid\Uuid;
use Vecnavium\SkyBlocksPM\commands\args\IslandNameArgument;
use Vecnavium\SkyBlocksPM\player\Player;
use Vecnavium\SkyBlocksPM\SkyBlocksPM;
use function strval;
use function substr;

class CreateSubCommand extends BaseSubCommand {

protected function prepare(): void {
$this->setPermission('skyblockspm.create');
$this->registerArgument(0, new RawStringArgument('name'));
$this->registerArgument(0, new IslandNameArgument('name'));
}

/**
Expand Down Expand Up @@ -47,9 +49,12 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args): vo
return;
}
$sender->sendMessage($plugin->getMessages()->getMessage('skyblock-creating'));
$id = Uuid::uuid4()->toString();

// Hotfix for those with pre-existing SkyBlocksPM databases where the UUID length is specified as 32, and not 36
$id = substr(Uuid::uuid4()->toString(), 0, -4);

$player->setSkyBlock($id);
$plugin->getGenerator()->generateIsland($sender, $id, strval($args['name'])); // Name validation?
$plugin->getGenerator()->generateIsland($sender, $id, strval($args['name']));
}

}
12 changes: 6 additions & 6 deletions src/Vecnavium/SkyBlocksPM/generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ public function setIslandWorld(P $player): void {

/**
* @param P $player
* @param string $folderName
* @param string $uuid
* @param string $name
*
* Thanks SkyWars by GamakCZ
*/
public function generateIsland(P $player, string $folderName, string $name): void{
public function generateIsland(P $player, string $uuid, string $name): void{
$path = $this->plugin->getDataFolder() . 'cache/island';
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator((string)realpath($path)), RecursiveIteratorIterator::LEAVES_ONLY);

$path = $this->plugin->getServer()->getDataPath() . 'worlds' . DIRECTORY_SEPARATOR . $folderName;
$path = $this->plugin->getServer()->getDataPath() . 'worlds' . DIRECTORY_SEPARATOR . $uuid;
@mkdir($path);
@mkdir($path . '/db');

Expand All @@ -83,12 +83,12 @@ public function generateIsland(P $player, string $folderName, string $name): voi
copy($filePath, $path . DIRECTORY_SEPARATOR . $localPath);
}

$this->plugin->getServer()->getWorldManager()->loadWorld($folderName);
$world = $this->plugin->getServer()->getWorldManager()->getWorldByName($folderName);
$this->plugin->getServer()->getWorldManager()->loadWorld($uuid);
$world = $this->plugin->getServer()->getWorldManager()->getWorldByName($uuid);
$skyBlockPlayer = $this->plugin->getPlayerManager()->getPlayer($player->getName());
if($world instanceof World && $skyBlockPlayer instanceof Player) {
$player->teleport(Position::fromObject($world->getSpawnLocation(), $world));
$this->plugin->getSkyBlockManager()->createSkyBlock($world->getFolderName(), $skyBlockPlayer, $name, $world);
$this->plugin->getSkyBlockManager()->createSkyBlock($uuid, $skyBlockPlayer, $name, $world);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/Vecnavium/SkyBlocksPM/player/PlayerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use pocketmine\player\Player as P;
use Vecnavium\SkyBlocksPM\SkyBlocksPM;
use function substr;

class PlayerManager {

Expand Down Expand Up @@ -48,13 +49,15 @@ public function unloadPlayer(P $player): void{
}

public function createPlayer(P $player): void {
// Hotfix for those with pre-existing SkyBlocksPM databases where the UUID length is specified as 32, and not 36
$uuid = substr($player->getUniqueId()->toString(), 0, -4);
$this->plugin->getDataBase()->executeInsert('skyblockspm.player.create',
[
'uuid' => $player->getUniqueId()->toString(),
'uuid' => $uuid,
'name' => $player->getName(),
'skyblock' => ''
]);
$this->players[$player->getName()] = new Player($player->getUniqueId()->toString(), $player->getName(), '');
$this->players[$player->getName()] = new Player($uuid, $player->getName(), '');
}

/**
Expand Down

0 comments on commit 7ef0bb5

Please sign in to comment.