Load cube

Discussion in 'Plugin Development' started by TerroDoor, Apr 6, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    TerroDoor

    I have a cube Location that gets saved to a config but when I reload the server and I put the cube back into the list, my cube location is unknown. I know this because i have a Boolean check to see if the player is in or out and it doesn’t work after reload, I have min and max locations how can I fix this


    Sent from my iPhone using Tapatalk
     
  2. Offline

    timtower Administrator Administrator Moderator

    @TerroDoor How are you storing it? Code and config please.
     
  3. Offline

    TerroDoor

    @timtower I came up with this.. Working but any improvement tips would be appreciated

    Command class using my setCube method:
    Code:
    public boolean onCommand(CommandSender s, Command cmd, String commandLabel, String[] args) {
    Player p = (Player)s;
    if (cmd.getName().equalsIgnoreCase("setspawn")) {
    if (args.length > 0) {
    p.sendMessage("unknown command");
    return true;
    }
    pl.cube.setCube(p.getLocation(), 3);
    p.sendMessage("spawn cube set");
    Location spawn = p.getLocation();
    
    pl.getConfig().set("spawn-loc.x", spawn.getBlockX());
    pl.getConfig().set("spawn-loc.y", spawn.getBlockY());
    pl.getConfig().set("spawn-loc.z", spawn.getBlockZ());
    
    pl.getConfig().set("spawn-loc.minx", spawn.getBlockX() - 5);
    pl.getConfig().set("spawn-loc.maxx", spawn.getBlockX() + 5);
    pl.getConfig().set("spawn-loc.miny", spawn.getBlockY() - 1);
    pl.getConfig().set("spawn-loc.maxy", spawn.getBlockY() + 5);
    pl.getConfig().set("spawn-loc.minz", spawn.getBlockZ() - 5);
    pl.getConfig().set("spawn-loc.maxz", spawn.getBlockZ() + 5);
    pl.saveConfig();
    return true;
    }
    
    Here's my Cube class with the Boolean check:

    Code:
    
    public class Cube {
    public Main pl;
    public Cube(Main ins) {
    pl = ins;
    }
    World world;
    double minx;
    double maxx;
    double miny;
    double maxy;
    double minz;
    double maxz;
    
    public void setCube(Location loc, int size) {
    world = loc.getWorld();
    this.minx = loc.getBlockX() - 5;
    this.maxx = loc.getBlockX() + 5;
    this.miny = loc.getBlockY() - 1;
    this.maxy = loc.getBlockY() + 5;
    this.minz = loc.getBlockZ() - 5;
    this.maxz = loc.getBlockZ() + 5;
    for (double x = minx; x <= maxx; x++) {
    for (double y = miny; y <= maxy; y++) {
    for (double z = minz; z <= maxz; z++) {
    Location area = new Location(world, x, y, z);
    
    if (y == miny) {
    area.getBlock().setType(Material.GLASS);
    
    }
    }
    }
    }
    return;
    }
    public boolean isInSpawn(Player p) {
    Location loc = p.getLocation();
    
    this.minx = pl.getConfig().getInt("spawn-loc.minx");
    this.maxx = pl.getConfig().getInt("spawn-loc.maxx");
    this.miny = pl.getConfig().getInt("spawn-loc.miny");
    this.maxy = pl.getConfig().getInt("spawn-loc.maxy");
    this.minz = pl.getConfig().getInt("spawn-loc.minz");
    this.maxz = pl.getConfig().getInt("spawn-loc.maxz");
    
    if (loc.getX() <= minx || loc.getX() >= maxx ||
    loc.getY() <= miny || loc.getY() >= maxy ||
    loc.getZ() <= minz || loc.getZ() >= maxz) {
    
    
    return false;
    
    }
    return true;
    }
    }
    
    and here's my Boolean method being put to use:

    Code:
    public void addProt(Player p) {
    
    if (!pl.util.protect.contains(p.getName())) {
    
    pl.util.protect.add(p.getName());
    
    } else {
    
    return;
    }
    
    return;
    }
    
    public void removeProt(Player p) {
    
    if (pl.util.protect.contains(p.getName())) {
    
    p.sendMessage(ChatColor.GRAY + "you have lost spawn protection.");
    pl.util.protect.remove(p.getName());
    
    } else {
    
    return;
    }
    
    return;
    }
    
    @EventHandler
    public void onPvP(EntityDamageEvent e) {
    
    Player p = (Player)e.getEntity();
    
    if (pl.util.protect.contains(p.getName())) {
    
    e.setCancelled(true);
    
    } else {
    
    e.setCancelled(false);
    
    }
    
    return;
    }
    
    @EventHandler
    public void onProtect(PlayerMoveEvent e) {
    Player p = (Player)e.getPlayer();
    
    if (pl.cube.isInSpawn(p)) {
    
    this.addProt(p);
    
    } else {
    
    this.removeProt(p);
    
    }
    
    return;
    }
    }
    
    Here's the Main:

    Code:
    public class Main extends JavaPlugin {
    public Util util = new Util(this);
    public Cube cube = new Cube(this);
    public void onEnable() {
    util.registerCommands();
    util.registerListeners();
    for (String players : this.getConfig().getStringList("protected.")) {
    
    util.protect.add(players);
    
    }
    
    this.getConfig().options().copyDefaults(true);
    this.saveConfig();
    
    }
    public void onDisable() {
    
    this.getConfig().set("arraylist.protected", util.protect);
    this.saveConfig();
    
    }
    }
     
  4. Offline

    timtower Administrator Administrator Moderator

    @TerroDoor Cube class: Why does it need access to the config? Make a constructor that takes the 2 corners of the cube as well.
    Making a cube: you are doing the same math twice, just let the cube have a method so it can set values.
     
  5. Offline

    TerroDoor

    I need access to the config because that’s where I save the cube after I type /setspawn, I forgot to attach my setspawn class.
    Am I doing this all completely wrong I’m very bad with constructors how do I properly use one to grab my min/max points .

    When I use /setspawn I use the setCube method to create one where the player is, this cube being the spawn point of my server so I do need it stored somewhere for reloads


    Sent from my iPhone using Tapatalk
     
  6. Offline

    timtower Administrator Administrator Moderator

    @TerroDoor New method.
    In your main class.
    A method that takes a Cube and saves it to the config.
    A method that takes values from the config and returns a Cube.
     
  7. Offline

    TerroDoor

    So in my main I need a saveCube method that takes the min/max xyz from my cube and save to config? And same for a loadCube method


    Sent from my iPhone using Tapatalk
     
  8. Offline

    timtower Administrator Administrator Moderator

    @TerroDoor loadCube takes it from the config.
     
  9. Offline

    TerroDoor

    So how do I make a constructor in my cube class for only 2 points? I have 6 currently.. min and max


    Sent from my iPhone using Tapatalk
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    TerroDoor

    Sorry I’m losing you in this, can I be shown exactly where in my code above I should change or where I’m going wrong, this code is working but feels inefficient.

    As I’m saving minx maxx etc, where exactly will “point1” be on my 3D cube, and how will i grab it from the cube


    Sent from my iPhone using Tapatalk
     
  12. Offline

    timtower Administrator Administrator Moderator

    @TerroDoor You do realize that minX, minY, minZ together are a single Location right?
     
  13. Offline

    TerroDoor

    No I didn’t realise that, so I’d need to make a location object for minx miny minz and another for max?


    Sent from my iPhone using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page