123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- package me.km.blockprotections;
- import com.mojang.authlib.GameProfile;
- import me.km.KajetansMod;
- import me.km.api.GlobalText;
- import me.km.api.Module;
- import me.km.playerbank.PlayerBank;
- import me.km.databank.SimpleDataBank;
- import java.sql.Connection;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- import me.km.api.Location;
- import me.km.dimensions.ModDimensions;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.Vec3d;
- import net.minecraft.world.World;
- public class BlockProtectionBank extends SimpleDataBank
- {
- public BlockProtectionBank(Module m, Connection c)
- {
- super(m, c);
- }
-
- @Override
- protected void init()
- {
- // BlockProtectionBank - Coords und ID
- if(!this.doesTableExist("block"))
- {
- this.getModule().sendToConsole("Die Block-Protection-Bank wurde nicht gefunden, erstelle ...");
- if(this.update("CREATE TABLE IF NOT EXISTS block ("
- + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
- + "x int(11) NOT NULL, "
- + "y int(11) NOT NULL, "
- + "z int(11) NOT NULL, "
- + "world_name varchar(20) NOT NULL, "
- + "UNIQUE KEY uniq_block_pos (world_name,x,y,z));", false))
- {
- this.getModule().sendToConsole("Die Block-Protection-Bank wurde erstellt.");
- }
- }
- else
- {
- this.getModule().sendToConsole("Die Block-Protection-Bank wurde gefunden.");
- }
-
-
- // BlockProtectionDataBank - Coords und ID
- if(!this.doesTableExist("block_grant"))
- {
- this.getModule().sendToConsole("Die Block-Protection-Data-Bank wurde nicht gefunden, erstelle ...");
- if(this.update("CREATE TABLE block_grant ("
- + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
- + "block_id int(11) NOT NULL, "
- + "player_id int(11) NOT NULL, "
- + "INDEX block_id (block_id), "
- + "UNIQUE KEY (block_id, player_id), "
- + "CONSTRAINT block_grant_ibfk_1 FOREIGN KEY (block_id) REFERENCES block (id) ON DELETE CASCADE);", false))
- {
- this.getModule().sendToConsole("Die Block-Protection-Data-Bank wurde erstellt.");
- }
- }
- else
- {
- this.getModule().sendToConsole("Die Block-Protection-Data-Bank wurde gefunden.");
- }
- }
-
- //--------------------------------------------------------------------------
- // Allgemeine Texte
- //--------------------------------------------------------------------------
-
- private void printError(EntityPlayer p)
- {
- this.getModule().sendWarning(p, "Es ist ein Fehler aufgetreten.");
- }
- //--------------------------------------------------------------------------
- // Block-Methoden
- //--------------------------------------------------------------------------
-
- public void addBlock(BlockPos pos, World w, EntityPlayer p, String pname)
- {
- if(doesExist(pos, w))
- {
- this.getModule().send(p, "Der Block ist bereits gesichert.");
- return;
- }
- Integer id;
- pname = makeStringSafe(pname);
- if(pname != null)
- {
- id = KajetansMod.playerbank.getDataBank(PlayerBank.class).getIdByName(pname);
- if(id == null)
- {
- this.getModule().send(p, GlobalText.cantFindPlayer(pname));
- return;
- }
- }
- else
- {
- id = KajetansMod.playerbank.getDataBank(PlayerBank.class).getIdByUUID(p.getUniqueID().toString());
- if(id == null)
- {
- this.getModule().send(p, GlobalText.cantFindPlayer(pname));
- return;
- }
- }
- if(this.update("INSERT INTO block (x,y,z,world_name) "
- + "VALUES (" + pos.getX() + "," + pos.getY() + "," + pos.getZ() + ",'" + ModDimensions.getWorldName(w) + "');", false) &&
- this.update("INSERT INTO block_grant (block_id,player_id) VALUES (LAST_INSERT_ID(), " + id + ");", false))
- {
- this.getModule().send(p, "Der Block wurde gesichert.");
- return;
- }
- printError(p);
- }
-
- public void removeBlock(BlockPos pos, World w, EntityPlayer p)
- {
- if(this.update("DELETE FROM block WHERE x=" + pos.getX() +
- " AND y=" + pos.getY() + " AND z=" + pos.getZ() +
- " AND world_name='" + ModDimensions.getWorldName(w) + "';", false))
- {
- this.getModule().send(p, "Die Block-Sicherung wurde gelöscht.");
- return;
- }
- printError(p);
- }
-
- public void addPlayer(BlockPos pos, World w, GameProfile p, EntityPlayer executor)
- {
- if(hasAccess(pos, w, p, false))
- {
- this.getModule().send(executor, "Der Spieler '" + p.getName() + "' ist bereits vorhanden.");
- return;
- }
- Integer id = KajetansMod.playerbank.getDataBank(PlayerBank.class).getIdByUUID(p.getId().toString());
- if(id == null)
- {
- this.getModule().send(executor, GlobalText.cantFindPlayer(p.getName()));
- return;
- }
- if(this.update("INSERT INTO block_grant (block_id,player_id) "
- + "SELECT id, " + id
- + " FROM block WHERE world_name='" + ModDimensions.getWorldName(w) + "' AND x=" + pos.getX() + " AND y=" + pos.getY()
- + " AND z=" + pos.getZ() + ";", false))
- {
- this.getModule().send(executor, "Der Spieler '" + p.getName() + "' wurde hinzugefügt.");
- return;
- }
- printError(executor);
- }
-
- public void removePlayer(BlockPos pos, World w, GameProfile p, EntityPlayer executor)
- {
- if(!hasAccess(pos, w, p, false))
- {
- this.getModule().send(executor, "Der Spieler '" + p.getName() + "' ist nicht vorhanden.");
- return;
- }
- Integer id = KajetansMod.playerbank.getDataBank(PlayerBank.class).getIdByUUID(p.getId().toString());
- if(id == null)
- {
- this.getModule().send(executor, GlobalText.cantFindPlayer(p.getName()));
- return;
- }
- if(this.update(
- "DELETE block_grant FROM block_grant " +
- "LEFT JOIN block ON block.id = block_grant.block_id " +
- "WHERE block_grant.player_id = " + id + " AND " +
- "block.world_name='" + ModDimensions.getWorldName(w) + "' AND block.x=" + pos.getX() +
- " AND block.y=" + pos.getY() + " AND block.z=" + pos.getZ() + ";", false))
- {
- this.getModule().send(executor, "Der Spieler '" + p.getName() + "' wurde entfernt.");
- return;
- }
- printError(executor);
- }
-
- public boolean hasAccess(BlockPos pos, World w, GameProfile p, boolean check)
- {
- if(check && !doesExist(pos, w))
- {
- return true;
- }
- return this.getFirst(
- "Select block_grant.id from block_grant " +
- "LEFT JOIN players ON players.id = block_grant.player_id LEFT JOIN block ON block.id = block_grant.block_id " +
- "WHERE players.uuid = '" + p.getId().toString() + "' AND " +
- "block.world_name='" + ModDimensions.getWorldName(w) + "' AND block.x=" + pos.getX() +
- " AND block.y=" + pos.getY() + " AND block.z=" + pos.getZ() + ";", Integer.class) != null;
- }
-
- public boolean hasAccess(BlockPos pos, World w, EntityPlayer p, boolean check)
- {
- return hasAccess(pos, w, p.getGameProfile(), check);
- }
-
- public boolean doesExist(BlockPos pos, World w)
- {
- return this.getFirst(
- "SELECT id FROM block WHERE world_name='" + ModDimensions.getWorldName(w) +
- "' AND x=" + pos.getX() + " AND y=" + pos.getY() +
- " AND z=" + pos.getZ() + ";", Integer.class) != null;
- }
-
- public void printInfo(BlockPos pos, World w, EntityPlayer p)
- {
- Module m = this.getModule();
- if(!doesExist(pos, w))
- {
- m.send(p, "Dieser Block ist nicht gesichert.");
- return;
- }
- ArrayList<Object> list = this.getFirstColumn(
- "Select block_grant.player_id from block_grant " +
- "LEFT JOIN block ON block.id = block_grant.block_id " +
- "WHERE block.world_name='" + ModDimensions.getWorldName(w) + "' AND block.x=" + pos.getX() +
- " AND block.y=" + pos.getY() + " AND block.z=" + pos.getZ() + ";");
- if(list == null)
- {
- printError(p);
- return;
- }
- if(list.isEmpty())
- {
- m.send(p, "Dieser Block ist gesichert, gehört aber niemanden.");
- return;
- }
- m.send(p, "Dieser Block gehört den folgenden Spielern:");
- PlayerBank pb = KajetansMod.playerbank.getDataBank(PlayerBank.class);
- list.stream().map(id -> pb.getNameById((int) id)).filter(name -> name != null).forEach(name ->
- {
- m.sendListElement(p, name);
- });
- }
-
- public List<BlockPos> getAllFromWorld(World w)
- {
- String name = ModDimensions.getWorldName(w);
- return this.get("Select x,y,z,world_name from block WHERE world_name='" + name + "';").stream().map(li ->
- {
- try
- {
- return new BlockPos((int) li.get(0), (int) li.get(1), (int) li.get(2));
- }
- catch(Exception ex)
- {
- return null;
- }
- }).collect(Collectors.toList());
- }
-
- private String makeStringSafe(String s)
- {
- if(s == null)
- {
- return null;
- }
- return s.replace("'", "").replace("\"", "");
- }
- }
|