#How to prevent closing of a Inventory?

1 messages · Page 1 of 1 (latest)

wise stump
#

Hmm

#

@onyx citrus

crystal flame
#

Please name your thread more usefully next time

wise stump
#

Now check my item file

#

Oh oki

crystal flame
#

?codeblock

#

Oh bot commands don’t work here

wise stump
#

@onyx citrus This is the code from which the error is originating

#

Hmm as Vivian is very busy and his brain.exe going haywire Imma try it myself

#

and I think it could be because I set the name of the item to ""
I will try " " or "."

onyx citrus
#

where is it throwing that code

#

your plugin is null in the scheduler

wise stump
#

Please wait a second I am re adding the instance in another way

onyx citrus
#

k

wise stump
#

Yes I am checking it

#

Here take the code

#

should I post it as messag here?

#

or use hastebin or paste bin?

#

@onyx citrus

#

Also I used this way of adding instances

#

I used the third one in it

#

which was best rated

#

This is the code

#
package srv.yassev.customitem.gui;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;

import srv.yassev.customitem.setup.setup;

public class ResourcepackSelection implements InventoryHolder {

    public static Inventory inv;
    public static Boolean trigger;
    
    private setup mainClass;

    public ResourcepackSelection(setup main) {
        inv = Bukkit.createInventory(this, 9, "Downloading Resourcepack...");
        load();
        ItemPane();
        this.mainClass = main;
    }
    
    @SuppressWarnings("unused")
    private void load() {
        
        ItemStack item;        
        List<String> lore = new ArrayList<>();
        // lore.add("§7Example Lore");
        new BukkitRunnable() {
            int slot = 0;
            @Override
            public void run() {                
                if (slot<8){
                    ItemStack item = ItemPane();
                    inv.setItem(slot, item);
                    slot++;
                } else {
                    trigger = true;
                    this.cancel();
                }
                
            }
        }.runTaskTimer(mainClass,0,10);
    }
    
    private ItemStack ItemPane() {
        ItemStack item = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(".");
        item.setItemMeta(meta);
        return item;
    }

    @Override
    public Inventory getInventory() {
        return inv;
    }
}
onyx citrus
#

looks the same, whats different

wise stump
#

Also Vivian , if you have opened the thread in the parallel view then change it to full view by clicking on the three dots on the top right in the thread and select full view

wise stump
#

what is it related to

#

why does it say Plugin cant be null

#

how do I fix it?

onyx citrus
#

what class does it say the error is in

#

looking for "Caused by:"

wise stump
#

check the three lines infront of the white mark

#

@onyx citrus

onyx citrus
#

send me those lines

wise stump
#

Basically its referring to The resourcepackSelection class

#

while I think its also referring to setup class

#

Just wait a moment I will re name all of the classes to match first letter upper case

#

Or wait more to let me create a completely new plugin'

onyx citrus
#

just send me those lines

crystal flame
# wise stump https://www.spigotmc.org/threads/best-method-for-getting-instance-of-main-class....

I just want to emphasize on this, the third one is best because it allows mock ability, it is less coupled because everything that depends on your plugin will become less coupled to your plugin instance thus adding reusability, flexibility and agility for the future. With that being said you should rarely pass your plugin instance to one of your other classes as it’s the highest level component/class/module of your application.

onyx citrus
#

"Method 3" is what one I was telling you to use earlier.

wise stump
#

So much cleaner

#

time set start this plugin and get the error again

#

@onyx citrus

#

Thats the error

#

so what does the plugin cannot be null mean?

#

These lines in the error

#
        at data.ui.LoadingResourcepack.load(LoadingResourcepack.java:50) ~[?:?]
        at data.ui.LoadingResourcepack.<init>(LoadingResourcepack.java:25) ~[?:?]
        at data.main.Core.onEnable(Core.java:26) ~[?:?]
onyx citrus
#

what is on line 50 of that

wise stump
#

clearly state that its related to the loadingresourcepack class which is the gui class/trigger and the stup class

onyx citrus
#

LoadingResourcepack.java:50

#

send me this

wise stump
#

which?

#

Line 50 of which class

#

@onyx citrus

onyx citrus
#

LoadingResourcepack.java:50

#

LoadingResourcepack.java:50

#

LoadingResourcepack.java:50

#

LoadingResourcepack

#

its right there

wise stump
#
package data.ui;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;

import data.main.Core;

public class LoadingResourcepack implements InventoryHolder {

    public static Inventory inv;
    public static Boolean trigger;
    
    private Core mainClass;

    public LoadingResourcepack(Core main) {
        inv = Bukkit.createInventory(this, 9, "Downloading Resourcepack...");
        load();
        ItemPane();
        this.mainClass = main;
    }
    
    @SuppressWarnings("unused")
    private void load() {
        
        ItemStack item;        
        List<String> lore = new ArrayList<>();
        // lore.add("§7Example Lore");
        new BukkitRunnable() {
            int slot = 0;
            @Override
            public void run() {                
                if (slot<8){
                    ItemStack item = ItemPane();
                    inv.setItem(slot, item);
                    slot++;
                } else {
                    trigger = true;
                    this.cancel();
                }
                
            }
        }.runTaskTimer(mainClass,0,10);
    }
    
    private ItemStack ItemPane() {
        ItemStack item = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(".");
        item.setItemMeta(meta);
        return item;
    }

    @Override
    public Inventory getInventory() {
        return inv;
    }
}

#

Thats the full code

onyx citrus
#

which one is line 50

wise stump
#

Line 50: runTaskTimer(mainClass,0,10);

onyx citrus
#

thanks

#

ok so

#

your "plugin" paramter is set to mainClass, so your reference to mainClass is null

#

which..it actually shouldnt be.

#

send your main class.

#

ah

#

ok

#

yep, i see the issue.

#

you're calling load() before assigning the mainClass variable.

wise stump
#

so what should I do

#

public static Inventory inv;
public static Boolean trigger;

private Core core;

public LoadingResourcepack(Core core) {
    inv = Bukkit.createInventory(this, 9, "Downloading Resourcepack...");
    load();
    ItemPane();
    this.core = core;
}

@SuppressWarnings("unused")
private void load() {
    
    ItemStack item;        
    List<String> lore = new ArrayList<>();
    // lore.add("§7Example Lore");
    new BukkitRunnable() {
        int slot = 0;
        @Override
        public void run() {                
            if (slot<8){
                ItemStack item = ItemPane();
                inv.setItem(slot, item);
                slot++;
            } else {
                trigger = true;
                this.cancel();
            }
            
        }
    }.runTaskTimer(core,0,10);
}
#

Thats the code atm

onyx citrus
#

you're calling load() before assigning the mainClass variable.

wise stump
#

OKAY

onyx citrus
#

this is all you need to know to fix it

#

so before u get pissy

#

look where ur calling load()

#

and where ur assigning the mainClass variable.

#

im not gonna spoonfeed or ur not gonna learn.

wise stump
#

load() thats the main method...

#

Assigning the mainclass variable..

#

should do I need to shift the methods up / down

#

When I am assigining it hmm

#

wait pls

onyx citrus
#

load isn't the main method

#

ok here

wise stump
#

oh okay

#

Hmm

#

I am calling the load Hmm a sec wait again

onyx citrus
#
    public LoadingResourcepack(Core main) { // This is called before anything else in the class
        inv = Bukkit.createInventory(this, 9, "Downloading Resourcepack...");
        load(); // This is trying to call load() which is referencing...
        ItemPane();                                                  //|
        this.mainClass = main;                                       //<
    }
#

look at the comments

#

the stuff with //

#

THIS

this.mainClass = main; 

is assigned AFTER you're trying to use it with

load();
wise stump
#

So basically I need to remove that load...

onyx citrus
#

no, you need to move it to after the variable it uses is assigned.

wise stump
#

Hmm should I add a scheduler?

onyx citrus
#

no.

wise stump
#

Hmm so do I need to move the code load() after the mainclass variable

onyx citrus
#

YES.

wise stump
#

Okay

#

Thanks the error is gone

#

I had another error before even though stuff worked find with it

#

fine*

onyx citrus
#

ok

#

good now/

wise stump
#

nvm I guess the cleanup fixed it

#

okay new prob

#

aaa wait please again

#

@onyx citrus How to get title of a gui inventory In a inventoryclick event

#

Like in a old tutorial I used before I remembered he adding this to prevent dragging of items out of the gui

onyx citrus
#

event.getClickedInventory().getTitle()

wise stump
#

Not working

#

Oh wait

#

oh i was using wrong

#

Okay

#

Hmmm

#

in 1.17 did it change

#

cuz

#

gives no option

onyx citrus
#

ah

#

its event.getView().getTitle() now

wise stump
#
    public void onClick(InventoryClickEvent e) {
        String title = e.getView().getTitle();
        int size = e.getInventory().getSize();
        if (title == LoadingResourcepack.name || size == 9) {
            Player p = (Player) e.getWhoClicked();
            p.sendMessage("Test");
            e.setCancelled(true);
        }
        
    }
#

@onyx citrus

#

Not workinggg

#

also tell me what does period mean in a runTaskTimer?

onyx citrus
#

I told you

#

period is how often the code inside public void run() runs

#

in ticks

onyx citrus
wise stump
#

Okaayyyy

#

Not okay to your @event handler as I just didnt include it in copy paste i gave to ya

#

Also Okay Now it workssss

#

also How to Make it so that if a specific type of inventory is closed before a certain if

#

Like I dont want the player to be able to close the gui till a certain boolean value is true

#

@onyx citrus

onyx citrus
#

you'd have to cancel InventoryClose event

wise stump
#

Ok so that event exists huzzzah

onyx citrus
#

yeah its not gonna be that easy though.

#

ur about to get into more difficult shit

wise stump
#

@onyx citrus

#

How to prevent it closing the ivnentory

#

It does not have a set cancelled boolean option

#
    @EventHandler
    public void onClose(InventoryCloseEvent e) {
        String title = e.getView().getTitle();
        int size = e.getInventory().getSize();
        if(title == LoadingResourcepack.name || size == 9) {
            if (LoadingResourcepack.trigger == false) {
                
            }
        }
    }
#

Thats the event

#

and I need to put something similar to e.setcancelled(true) in the inner if statement

onyx citrus
#

Ah. Right.

wise stump
#

How to prevent closing of a Inventory?

onyx citrus
#

You need to re-open the inventory if they try to close it.

wise stump
#

...

#

wont work

onyx citrus
#

why not

wise stump
#

the variables will reset :(((

onyx citrus
#

what variables

wise stump
#

wont they???

#

The ones which run with the schduler

#

to make a moving loader

onyx citrus
#

you might just be able to get the inventory that they close, then call open again on it.

wise stump
#

I will try anyway............

onyx citrus
#

just do like

#

player.openInventory(this.getInventory()) or whatever

wise stump
#

@onyx citrus

#

If I set the runtasktimer to this

#

runTaskTimer(core,10L,20L);

#

what will be the psed

#

speed*

#

What does the L add

#

I saw it before in a scheduler tutorial

onyx citrus
#

same thing

#

idk why it exists

wise stump
#

@onyx citrus

#

Heyyy

#

How to get the block player is left clicking on in the player interact event?

#

I was scrolling around in hypix and found that in the lobbies instead of cancelling the block break event they instead made it so that it will cancel the interact event which will stop right when player holds/left clicks on the block

#

Please tell how to get the block player is left clickinggg

onyx citrus
#

I'm not home anymore.

#

Their lobbies are in adventure mode

#

Which stops you breaking blocks

wise stump
#

@onyx citrus Imma just remove the resourcepack stuff..

#

Other than that tmr I need help making a thirst system

#

In which I have to use either variables or scoreboards

onyx citrus
#

Gl

wise stump
#

using scoreboards better?

#

are scoreboards saved always?

#

@onyx citrus

#

Are scoreboards saved after server stopped and restarts?

onyx citrus
#

Scoreboards are not saved.

#

You don't have to ask it 48 times

#

I saw it the first time

wise stump
#

Okay then do you know how to store certain data in numbers and uuid in data files?

#

Like how to make a yml/txt file to save data

#

which will auto load data from it everytime for every player

#

do you know?

#

..

#

why did you leave the thread! @onyx citrus

#

Stay in it dont close this thread or dont press leave button

#

sorry for ping

#

reply tmr or anytime ya want gn btw

#

see ya tmrr

#

I will share the plugin when I have completed it and also made a config

onyx citrus
#

Who do you think you are?

#

I'm done helping you homie

wise stump
#

aww

#

why..