[Unsolved] Setting Bool to false on tick 0

Discussion in 'Plugin Development' started by Yummy Juice, Sep 13, 2012.

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

    Yummy Juice

    I'm trying to get the boolean to set to false on time 0. I already have a time system implemented that checks the time every tick and I would like it so that on tick 0 the boolean gets set false and the loot system can then re-generate the items in the chest. Here is the loot system.
    Code:
    package me.yummyjuice.daym;
     
    import java.util.Random;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.InventoryHolder;
    import org.bukkit.inventory.ItemStack;
     
     
     
    public class lootSystem implements Listener {
     
    public boolean chestCleared = false;
     
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
    Player player = event.getPlayer();
     
    if(event.getClickedBlock().getType() == Material.CHEST) {
    InventoryHolder chest = (InventoryHolder) event.getClickedBlock().getState();
    Random random = new Random();
     
    // First value is the ID, Second is "common" and the third is the amount.
    Integer[][] mats = {{0,100,64}, {261, 1, 2}, {262, 1, 3}};
    ItemStack newItem = new ItemStack(Material.AIR);
     
    for (int i = 0; i < chest.getInventory().getSize(); i ++) {
    Boolean gotMat = false;
     
    for(int j = random.nextInt(100); gotMat == false; j = random.nextInt(100)) {
     
    // Choose a random material.
    Integer chosenMat = random.nextInt(mats.length);
    // If the random number is less than the weight.
    if (mats[chosenMat][1] > j) {
    // Set the item to be a random amount of that material.
    newItem = new ItemStack(Material.getMaterial(mats[chosenMat][0]), random.nextInt(mats[chosenMat][2]) + 1);
    gotMat = true;
    }
    }
     
    if (chest.getInventory().getItem(i) == null) {
    chest.getInventory().setItem(i, newItem);
    player.sendRawMessage(ChatColor.RED + "Set to true.");
    chestCleared = true;
    }
    }
    }
    }
    }
    
    Here is my main java class.
    ------------------------------------------------------------------------------------------
    Code:
    package me.yummyjuice.daym;
     
    import org.bukkit.event.Listener;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class DayM extends JavaPlugin {
     
    public lootSystem lootSystem = new lootSystem();
    public blockListener blockListener = new blockListener();
    public Permission blockChange = new Permission("daym.block");
    public Permission spawnSet = new Permission("daym.spawn");
    public DayM plugin;
    public boolean chestCleared = false;
     
     
    public void onEnable(){
    System.out.print("[DayM] Enabled.");
     
    getServer().getScheduler().scheduleSyncRepeatingTask(this, new DayMTime(), 0L, 1L);
    getServer().getPluginManager().registerEvents(this.blockListener, this);
    getServer().getPluginManager().registerEvents((Listener) this.lootSystem, this);
    }
     
    public void onDisable(){
    System.out.print("[DayM] Disabled.");
    }
    }
    
     
  2. Offline

    CorrieKay

    create a sync repeating task, make it poll every tick.

    Since ticks fire every fifty milliseconds, you wont get it RIGHT on the dot every time, however this will probably suit you well:

    if(boolean && mapTime >0){
    //do code, flip bool.
    }
     
  3. Offline

    Yummy Juice

    Sorry, I'm confused. Where would I put the if statement?
     
  4. Offline

    CorrieKay

    You would put it in the run method of the runnable you create to check each tick.

    That said, you'd need a runnable to also reflip the boolean back at the end of the day, so lets fix that, try this:

    if(boolean && (mapTime >0 && mapTime<23000)){
    //do code, boolean = !boolean;
    } else if (!boolean && mapTime >23000){
    boolean = !boolean;
    }

    first off, this will fire your code whenever the day has reset, and it wont flip it until the day is almost done. Besides, when it DOES reflip it (with the else if) it will be out of the acceptable range of the first if statement, and only eligible to do the code until the day has returned to 0


    Also, i believe time 0 = 6am, just fyi.
     
  5. Offline

    Yummy Juice

    Thanks. I understand the code but have no clue where to put it. In my DayMTime class, lootSystem class or main class.
     
  6. Offline

    CorrieKay

    Stick it in the constructor for the object that holds the code you'll be executing.

    i suggest passing in the instance of the object too, as it can then use that instance to execute the method, if its located in that class.
     
  7. Offline

    Yummy Juice

    Where do I get the time from then?

    Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  8. Offline

    CorrieKay

    world.getTime(). If you use a multiworld plugin, use whichever world you need to
     
Thread Status:
Not open for further replies.

Share This Page