123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- package me.km.plots;
- import com.mojang.authlib.GameProfile;
- import java.sql.Connection;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.stream.Collectors;
- import me.km.KajetansMod;
- import me.km.api.GlobalText;
- import me.km.api.Module;
- import me.km.databank.SimpleDataBank;
- import me.km.dimensions.ModDimensions;
- import me.km.playerbank.PlayerBank;
- import net.minecraft.command.ICommandSender;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.EntityLeashKnot;
- import net.minecraft.entity.item.EntityBoat;
- import net.minecraft.entity.item.EntityItemFrame;
- import net.minecraft.entity.item.EntityMinecart;
- import net.minecraft.entity.item.EntityPainting;
- import net.minecraft.entity.monster.EntitySnowman;
- import net.minecraft.entity.passive.EntityChicken;
- import net.minecraft.entity.passive.EntityCow;
- import net.minecraft.entity.passive.EntityHorse;
- import net.minecraft.entity.passive.EntityMooshroom;
- import net.minecraft.entity.passive.EntityOcelot;
- import net.minecraft.entity.passive.EntityPig;
- import net.minecraft.entity.passive.EntityRabbit;
- import net.minecraft.entity.passive.EntitySheep;
- import net.minecraft.entity.passive.EntitySquid;
- import net.minecraft.entity.passive.EntityVillager;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.World;
- public class ProtectionBank extends SimpleDataBank
- {
- private final HashSet<Class<? extends Entity>> protectetEntities;
-
- public boolean isProtected(Class<? extends Entity> et)
- {
- return protectetEntities.contains(et);
- }
-
- public ProtectionBank(Module m, Connection c)
- {
- super(m, c);
- protectetEntities = new HashSet();
- protectetEntities.add(EntityBoat.class);
- protectetEntities.add(EntityChicken.class);
- protectetEntities.add(EntityCow.class);
- protectetEntities.add(EntityHorse.class);
- protectetEntities.add(EntityItemFrame.class);
- protectetEntities.add(EntityLeashKnot.class);
- protectetEntities.add(EntityMinecart.class);
- protectetEntities.add(EntityMooshroom.class);
- protectetEntities.add(EntityPainting.class);
- protectetEntities.add(EntityPig.class);
- protectetEntities.add(EntityRabbit.class);
- protectetEntities.add(EntitySheep.class);
- protectetEntities.add(EntitySnowman.class);
- protectetEntities.add(EntitySquid.class);
- protectetEntities.add(EntityOcelot.class);
- protectetEntities.add(EntityVillager.class);
- }
-
- @Override
- protected void init()
- {
-
- if(!this.doesTableExist("plots"))
- {
- this.getModule().sendToConsole("Die Plot-Bank wurde nicht gefunden, erstelle ...");
- if(this.update("CREATE TABLE IF NOT EXISTS plots ("
- + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
- + "x1 int(11) NOT NULL, "
- + "y1 int(11) NOT NULL, "
- + "z1 int(11) NOT NULL, "
- + "x2 int(11) NOT NULL, "
- + "y2 int(11) NOT NULL, "
- + "z2 int(11) NOT NULL, "
- + "world_name varchar(20) NOT NULL, "
- + "name varchar(50) NOT NULL, "
- + "INDEX (x1, x2), "
- + "INDEX (y1, y2), "
- + "INDEX (z1, z2), "
- + "INDEX (world_name));", false))
- {
- this.getModule().sendToConsole("Die Plot-Bank wurde erstellt.");
- }
- }
- else
- {
- this.getModule().sendToConsole("Die Plot-Bank wurde gefunden.");
- }
-
-
- if(!this.doesTableExist("plot_grant"))
- {
- this.getModule().sendToConsole("Die Plot-Owner-Bank wurde nicht gefunden, erstelle ...");
- if(this.update("CREATE TABLE plot_grant ("
- + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
- + "plot_id int(11) NOT NULL, "
- + "player_id int(11) NOT NULL, "
- + "INDEX plot_id (plot_id), "
- + "UNIQUE KEY (plot_id, player_id), "
- + "CONSTRAINT plot_grant_ibfk_1 FOREIGN KEY (plot_id) REFERENCES plots (id) ON DELETE CASCADE);", false))
- {
- this.getModule().sendToConsole("Die Plot-Owner-Bank wurde erstellt.");
- }
- }
- else
- {
- this.getModule().sendToConsole("Die Plot-Owner-Bank wurde gefunden.");
- }
-
-
- if(!this.doesTableExist("plot_tags"))
- {
- this.getModule().sendToConsole("Die Plot-Tag-Bank wurde nicht gefunden, erstelle ...");
- if(this.update("CREATE TABLE plot_tags ("
- + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
- + "plot_id int(11) NOT NULL, "
- + "tag VARCHAR(20) NOT NULL, "
- + "INDEX plot_id (plot_id), "
- + "UNIQUE KEY (plot_id, tag), "
- + "CONSTRAINT plot_tags_ibfk_1 FOREIGN KEY (plot_id) REFERENCES plots (id) ON DELETE CASCADE);", false))
- {
- this.getModule().sendToConsole("Die Plot-Tag-Bank wurde erstellt.");
- }
- }
- else
- {
- this.getModule().sendToConsole("Die Plot-Tag-Bank wurde gefunden.");
- }
- }
-
-
-
-
- private void printError(ICommandSender cs)
- {
- this.getModule().sendWarning(cs, "Es ist ein Fehler aufgetreten.");
- }
-
- private void noPlot(ICommandSender cs)
- {
- this.getModule().send(cs, "Du befindest dich auf keinem Plot.");
- }
-
- private void doesNotExist(ICommandSender cs, int id)
- {
- this.getModule().send(cs, "Der Plot mit der ID '" + id + "' ist nicht vorhanden.");
- }
-
-
-
-
-
- public void addPlot(int x1, int y1, int z1, int x2, int y2, int z2, String world, ICommandSender sender, String pname)
- {
- Integer id;
- pname = makeStringSafe(pname);
- if(pname != null)
- {
- id = KajetansMod.playerbank.getDataBank().getIdByName(pname);
- if(id == null)
- {
- this.getModule().send(sender, GlobalText.cantFindPlayer(pname));
- return;
- }
- }
- else
- {
- if(!(sender instanceof EntityPlayer))
- {
- return;
- }
- EntityPlayer p = (EntityPlayer) sender;
- id = KajetansMod.playerbank.getDataBank().getIdByUUID(p.getUniqueID().toString());
- pname = p.getName();
- }
- if(this.update("INSERT INTO plots (x1,y1,z1,x2,y2,z2,world_name,name) "
- + "VALUES (" + x1 + "," + y1 + "," + z1 + "," + x2 + "," + y2 + "," + z2 +
- ",'" + world + "','" + pname + "');", false) &&
- this.update("INSERT INTO plot_grant (plot_id,player_id) VALUES (LAST_INSERT_ID(), " + id + ");", false))
- {
- this.getModule().send(sender, "Der Plot wurde hinzugefügt.");
- return;
- }
- printError(sender);
- }
-
- public void changeName(Integer id, String name, ICommandSender sender)
- {
- if(id == null)
- {
- noPlot(sender);
- return;
- }
- if(!doesPlotExist(id))
- {
- doesNotExist(sender, id);
- return;
- }
- name = makeStringSafe(name);
- if(this.update("UPDATE plots SET name='" + name + "' WHERE id=" + id + ";", false))
- {
- this.getModule().send(sender, "Der Name wurde auf '" + name + "§r' geändert.");
- return;
- }
- printError(sender);
- }
-
- public void addTag(Integer id, String tag, ICommandSender cs)
- {
- if(id == null)
- {
- noPlot(cs);
- return;
- }
- if(!doesPlotExist(id))
- {
- doesNotExist(cs, id);
- return;
- }
- tag = makeStringSafe(tag);
- String tags = this.getFirst("SELECT tag FROM plot_tags WHERE plot_id=" + id + " AND tag='" + tag + "';", String.class);
- if(tags != null)
- {
- this.getModule().send(cs, "Der Tag '" + tag + "' ist bereits vorhanden.");
- return;
- }
- if(this.update("INSERT INTO plot_tags (plot_id,tag) VALUES(" + id + ",'" + tag + "');", false))
- {
- this.getModule().send(cs, "Der Tag '" + tag + "' wurde hinzugefügt.");
- return;
- }
- printError(cs);
- }
-
- public void removeTag(Integer id, String tag, ICommandSender sender)
- {
- if(id == null)
- {
- noPlot(sender);
- return;
- }
- if(!doesPlotExist(id))
- {
- doesNotExist(sender, id);
- return;
- }
- tag = makeStringSafe(tag);
- String tags = this.getFirst("SELECT tag FROM plot_tags WHERE plot_id=" + id + " AND tag='" + tag + "';", String.class);
- if(tags == null)
- {
- this.getModule().send(sender, "Der Tag '" + tag + "' ist nicht vorhanden.");
- return;
- }
- if(this.update("DELETE FROM plot_tags WHERE plot_id=" + id + " AND tag='" + tag + "';", false))
- {
- this.getModule().send(sender, "Der Tag '" + tag + "' wurde entfernt.");
- return;
- }
- printError(sender);
- }
-
- public boolean hasTag(World w, BlockPos pos, String tag)
- {
- return this.getFirst(
- "SELECT tag FROM plot_tags " +
- "LEFT JOIN plots ON plots.id = plot_tags.plot_id " +
- "WHERE x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
- " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
- " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
- " AND world_name='" + ModDimensions.getWorldName(w) + "' AND tag='" + tag + "';", String.class) != null;
- }
-
- public void removePlayer(Integer id, GameProfile p, ICommandSender sender)
- {
- if(id == null)
- {
- noPlot(sender);
- return;
- }
- if(!doesPlotExist(id))
- {
- doesNotExist(sender, id);
- return;
- }
- Integer playerID = this.getFirst("SELECT player_id FROM plot_grant " +
- "LEFT JOIN players ON players.id = plot_grant.player_id " +
- "WHERE plot_grant.plot_id=" + id + " AND players.uuid='" + p.getId().toString() + "';", Integer.class);
- if(playerID == null)
- {
- this.getModule().send(sender, "Der Spieler '" + p.getName() + "' ist nicht vorhanden.");
- return;
- }
- playerID = KajetansMod.playerbank.getDataBank().getIdByUUID(p.getId().toString());
- if(playerID == null)
- {
- this.getModule().send(sender, GlobalText.cantFindPlayer(p.getName()));
- return;
- }
- if(this.update("DELETE FROM plot_grant WHERE plot_id=" + id + " AND player_id=" + playerID + ";", false))
- {
- this.getModule().send(sender, "Der Spieler '" + p.getName() + "' wurde entfernt.");
- return;
- }
- printError(sender);
- }
-
- public void addPlayer(Integer id, GameProfile p, ICommandSender sender)
- {
- if(id == null)
- {
- noPlot(sender);
- return;
- }
- if(!doesPlotExist(id))
- {
- doesNotExist(sender, id);
- return;
- }
- Integer playerID = this.getFirst("SELECT player_id FROM plot_grant " +
- "LEFT JOIN players ON players.id = plot_grant.player_id " +
- "WHERE plot_grant.plot_id=" + id + " AND players.uuid='" + p.getId().toString() + "';", Integer.class);
- if(playerID != null)
- {
- this.getModule().send(sender, "Der Spieler '" + p.getName() + "' ist bereits vorhanden.");
- return;
- }
- playerID = KajetansMod.playerbank.getDataBank().getIdByUUID(p.getId().toString());
- if(playerID == null)
- {
- this.getModule().send(sender, GlobalText.cantFindPlayer(p.getName()));
- return;
- }
- if(this.update("INSERT INTO plot_grant (plot_id,player_id) VALUES(" + id + "," + playerID + ");", false))
- {
- this.getModule().send(sender, "Der Spieler '" + p.getName() + "' wurde hinzugefügt.");
- return;
- }
- printError(sender);
- }
-
- public boolean isPlotOverlapping(int x1, int y1, int z1, int x2, int y2, int z2, World w)
- {
- ArrayList<Object> list = this.getFirstColumn(
- "SELECT id FROM plots WHERE " +
- "(x1<=" + x1 + " AND x2>=" + x1 + " OR " +
- "x1<=" + x2 + " AND x2>=" + x2 + " OR " +
- "x1>=" + x1 + " AND x1<=" + x2 + " OR " +
- "x2>=" + x1 + " AND x2<=" + x2 + ") AND " +
-
- "(y1<=" + y1 + " AND y2>=" + y1 + " OR " +
- "y1<=" + y2 + " AND y2>=" + y2 + " OR " +
- "y1>=" + y1 + " AND y1<=" + y2 + " OR " +
- "y2>=" + y1 + " AND y2<=" + y2 + ") AND " +
-
- "(z1<=" + z1 + " AND z2>=" + z1 + " OR " +
- "z1<=" + z2 + " AND z2>=" + z2 + " OR " +
- "z1>=" + z1 + " AND z1<=" + z2 + " OR " +
- "z2>=" + z1 + " AND z2<=" + z2 + ") AND " +
- "world_name='" + ModDimensions.getWorldName(w) + "';");
- return !list.isEmpty();
- }
-
- private ProtectionStatus getProtectionStatus(World w, BlockPos pos, EntityPlayer p)
- {
- ArrayList<Object> list = this.getFirstColumn(
- "SELECT id FROM plots WHERE " +
- "x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
- " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
- " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
- " AND world_name='" + ModDimensions.getWorldName(w) + "';");
- if(list == null)
- {
- return ProtectionStatus.ERROR;
- }
- if(list.isEmpty())
- {
- return ProtectionStatus.NOTHING;
- }
- if(list.stream().anyMatch(li ->
- {
- return this.getFirst("SELECT plot_grant.id FROM plot_grant " +
- "LEFT JOIN players ON players.id = plot_grant.player_id " +
- "WHERE plot_grant.plot_id=" + li + " AND " +
- "players.uuid='" + p.getUniqueID().toString() + "';", Integer.class) != null;
- }))
- {
- return ProtectionStatus.OWNER;
- }
- return ProtectionStatus.STRANGER;
- }
-
- public boolean hasProtection(World w, BlockPos pos)
- {
- ArrayList<Object> list = this.getFirstColumn(
- "SELECT id FROM plots WHERE " +
- "x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
- " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
- " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
- " AND world_name='" + ModDimensions.getWorldName(w) + "';");
- if(list == null)
- {
- return true;
- }
- return !list.isEmpty();
- }
-
- public boolean canBuild(World w, BlockPos pos, EntityPlayer p)
- {
- ProtectionStatus ps = getProtectionStatus(w, pos, p);
- return !(ps == ProtectionStatus.STRANGER || ps == ProtectionStatus.ERROR);
- }
-
- public void removePlot(Integer id, ICommandSender sender)
- {
- if(id == null)
- {
- noPlot(sender);
- return;
- }
- if(!doesPlotExist(id))
- {
- doesNotExist(sender, id);
- return;
- }
- if(this.update("DELETE FROM plots WHERE id=" + id + ";", false))
- {
- this.getModule().send(sender, "Der Plot mit der ID '" + id + "' wurde gelöscht.");
- return;
- }
- printError(sender);
- }
-
- public void printInfo(EntityPlayer p)
- {
- Module m = this.getModule();
- BlockPos pos = p.getPosition();
- ArrayList<ArrayList<Object>> list = this.get("SELECT id,name FROM plots WHERE " +
- "x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
- " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
- " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
- " AND world_name='" + ModDimensions.getWorldName(p.world) + "';");
- if(list == null)
- {
- printError(p);
- return;
- }
- if(list.isEmpty())
- {
- noPlot(p);
- return;
- }
- m.send(p, "Du befindest dich auf folgenden Plots:");
- PlayerBank pb = KajetansMod.playerbank.getDataBank();
- list.stream().forEach(li ->
- {
- String owner = this.getFirstColumn("SELECT player_id FROM plot_grant WHERE plot_id=" + (int) li.get(0) + ";").stream().map(id -> pb.getNameById((int) id)).collect(Collectors.joining(", "));
- String tags = this.getFirstColumn("SELECT tag FROM plot_tags WHERE plot_id=" + (int) li.get(0) + ";").stream().map(tag -> tag.toString()).collect(Collectors.joining(", "));
- m.sendHelpListElement(p, li.get(1).toString(), "(" + li.get(0).toString() + ") §7" + owner + " §7" + tags);
- });
- }
-
- public Integer getFirstRegionId(World w, BlockPos pos)
- {
- return this.getFirst("SELECT id FROM plots WHERE " +
- "x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
- " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
- " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
- " AND world_name='" + ModDimensions.getWorldName(w) + "';", Integer.class);
- }
-
- public Integer getFirstRegionId(EntityPlayer p)
- {
- return getFirstRegionId(p.world, p.getPosition());
- }
-
- public String getFirstRegionName(World w, BlockPos pos)
- {
- return this.getFirst("SELECT name FROM plots WHERE " +
- "x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
- " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
- " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
- " AND world_name='" + ModDimensions.getWorldName(w) + "';", String.class);
- }
-
- public boolean doesPlotExist(int id)
- {
- return this.getFirst("SELECT id FROM plots WHERE id=" + id + ";", Integer.class) != null;
- }
-
- private String makeStringSafe(String s)
- {
- if(s == null)
- {
- return null;
- }
- return s.replace("'", "").replace("\"", "");
- }
- }
|