Solved Little Help

Discussion in 'Spigot Plugin Development' started by ZetaGaming, Apr 16, 2019.

Thread Status:
Not open for further replies.
  1. Can you guys teach me or send me a link on how to create a plugin which has a random onblockbreak event.
    For example, for x chance that a player breaks a block a gui will pop up.
    Thanks in advance :3
     
  2. Hey

    There are tutorials on youtube , Not sure if I'm allowed to link them here but ,



    Try to mix those 2 up
     
  3. Oh thanks I'll look onto them thanks :)
     
  4. UPDATE: I figured how to combine this two but I still need to know how to implement this "chance". Please help me again thanks!
     
  5. Can you show me how? Here is my code:


    Code (Text):
        @EventHandler
        public void blockBreak(BlockBreakEvent event){
            Player player = event.getPlayer();
            player.sendMessage(ChatColor.GREEN + "You found a Cave In!");
            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "CaveIn" + Main.getConfig().getInt("cchance" ));
            ItemStack i1 = new ItemStack(Material.STONE);
            ItemStack i2 = new ItemStack(Material.GOLD_NUGGET);
            ItemMeta meta = i2.getItemMeta();
            meta.setDisplayName(ChatColor.RED + "Money");
            i2.setItemMeta(meta);
            ItemStack i3 = new ItemStack(Material.LOG);
            inv.addItem(i1);
            inv.addItem(i2);
            inv.addItem(i3);
            inv.setItem(1, i1);
            inv.setItem(2, i1);
            inv.setItem(3, i1);
            inv.setItem(4, i2);
            inv.setItem(5, i2);
            inv.setItem(6, i2);
            inv.setItem(7, i3);
            inv.setItem(8, i3);
            inv.setItem(9, i3);
            player.openInventory(inv);
        }

    And Main.getConfig().getInt("cchance" ) for some reason is also not working
     
  6. There are a ton of things that could go wrong here. Are you getting an error in console? What's the output? Can we see the rest of the class?
     
  7. So I cleaned some codes and I still need opinion/help;
    Here is my main class:

    Code (Text):
    package me.zacky26.cavein;

    import me.zacky26.cavein.events.caveins;
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import me.zacky26.cavein.commands.cavein;
    import net.milkbowl.vault.economy.Economy;

    public class Main extends JavaPlugin {
        private Economy econ;
        public static Economy economy = null;
        @Override
        public void onEnable(){
            //Fired when the server enables the plugin
            this.getCommand("cavein").setExecutor(new cavein());
            this.saveDefaultConfig();
            this.getConfig().addDefault("blocks", "stone");
            this.getConfig().addDefault("cave-in_chances", "1");
            this.getConfig().addDefault("cchance", "1");
            this.getServer().getPluginManager().registerEvents(new caveins(), this);
            if (!setupEconomy()) {
                this.getLogger().severe("Disabled due to no Vault dependency found!");
                Bukkit.getPluginManager().disablePlugin(this);
                return;
            }
        }
        private boolean setupEconomy()
        {
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
            if (economyProvider != null) {
                economy = economyProvider.getProvider();
            }

            return (economy != null);
        }
        public Economy getEcononomy() {
            return econ;
        }

        @Override
        public void onDisable(){
            //Fired when the server stops and disables all plugins
        }
    }
     

    My caveins class

    Code (Text):
    package me.zacky26.cavein.events;

    import me.zacky26.cavein.Main;
    import org.bukkit.Bukkit;
    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.block.BlockBreakEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import net.milkbowl.vault.economy.*;

    public class caveins implements Listener {

        @EventHandler
        public void blockBreak(BlockBreakEvent event){
            Player player = event.getPlayer();
            player.sendMessage(ChatColor.GREEN + "You found a Cave In!");
            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "CaveIn" + "cave-in_chances" );
            ItemStack i1 = new ItemStack(Material.STONE);
            ItemStack i2 = new ItemStack(Material.GOLD_NUGGET);
            ItemMeta meta = i2.getItemMeta();
            meta.setDisplayName(ChatColor.RED + "Money");
            i2.setItemMeta(meta);
            ItemStack i3 = new ItemStack(Material.LOG);
            inv.addItem(i1);
            inv.addItem(i2);
            inv.addItem(i3);
            inv.setItem(1, i1);
            inv.setItem(2, i1);
            inv.setItem(3, i1);
            inv.setItem(4, i2);
            inv.setItem(5, i2);
            inv.setItem(6, i2);
            inv.setItem(7, i3);
            inv.setItem(8, i3);
            inv.setItem(9, i3);
            player.openInventory(inv);
        }
    }
     

    Console (Related to my plugin)

    Code (Text):
    > [16:31:40 INFO]: [CaveIn] Loading CaveIn v1.0.0
    > [16:31:51 INFO]: [CaveIn] Enabling CaveIn v1.0.0
    >pl
    > [16:32:19 INFO]: Plugins (10): EpicWorldGenerator, LuckPerms, Vault, Essentials, EssentialsChat, ProtocolLib, CaveIn, LibsDisguises, MythicMobs, MythicMobsExtension
    >cavein
    > [16:32:30 INFO]: CaveIn - Version 1.0 by Zacky26
    > [16:32:30 INFO]: Get CaveIns while breaking blocks!
    >ci
    > [16:32:34 INFO]: CaveIn - Version 1.0 by Zacky26
    > [16:32:34 INFO]: Get CaveIns while breaking blocks!


    My problem now is cave-in_chances is not working properly :3 it is apearing as "cave-in_chances" instead of the value in the config.yml
     
  8. Code (Text):

    Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "CaveIn" + "cave-in_chances" );
     
    Is the problem here? Instead of "cave-in_changes", you want to get the FileConfiguration object and do getString("cave-in_chances") (or .getInt("cave-in_chances") depending on what you're trying to do) and that'll get the value from the config.

    To get the instance of the FileConfiguration on the Listener class, what you want to do is create a constructor in the Listener class that has a parameter of your Main class. Like this:
    Code (Text):

    private FileConfiguration config;

    public caveins(Main plugin) {
    this.config = plugin.getConfig();
    }
     
    Then in your event method, you wanna add
    Code (Text):

    int value = config.getInt("cave-in_changes");
     
    then do whatever you want with it

    Also! You must register your listener object with the new parameter, so when registering, it should look like this:
    Code (Text):

    public void onEnable() {
    getServer().getPluginManager().registerEvents(new caveins(this), this);
    }
     
     
    • Agree Agree x 1
  9. Thank you for your assistance guys :)
     
Thread Status:
Not open for further replies.

Share This Page