#Maow explaining for iDerpy

1 messages · Page 1 of 1 (latest)

wet grove
paper lodge
#

Okay so

ancient topaz
#

HAllo

paper lodge
#

@ancient topaz

#

For each condition, AKA number == 0 you're doing

#

It has to check that value

#

And then check the next

#

And then the next

#

And then the next

#

With mySet.contains, it's just a single operation

#

So not only is it faster, it's easier to read

ancient topaz
#

w a w

paper lodge
#

Creating a single variable is actually not that bad, especially with something like Set since it's not expensive to create.

ancient topaz
#

w a w

paper lodge
#

Consider that if you're checking multiple slots, it has to run that long if statement multiple times

#

Which is much slower than just a Set

ancient topaz
#

i am convinced, what do i do

paper lodge
#

Well

#

Lemme look at your original code to see

#

@ancient topaz you mind sending it in here?

quiet sorrel
paper lodge
#

Ayup.

ancient topaz
paper lodge
#

I enjoy helping beginners, it makes me feel like I matter :)

#

Thank you

#

Okay so in your CraftingEvents class

#

You're going to need to create a static field

#

static is a bit difficult to explain but

#

Essentially, you can access a field/method in two different ways

ancient topaz
#

like-a dis? private static slotChecker2000

paper lodge
#

Let's say class Example has a field called exampleField

#

if exampleField is static, we access it as Example.exampleField

#

otherwise, it isn't, then we need to make a new Example via Example myExample = new Example(), and then we can access it as myExample.exampleField

#

the benefit of static is that we don't need to make a new Example and that static field is only created once

ancient topaz
#

can't i just remove static? it's not like i'm going to use this outside this class (i think)

paper lodge
#

with a non-static, AKA instance field, it's created every time Example is created (like when you do new Example())

#

so creating it once is just a small performance benefit

ancient topaz
#

wait

#

doesn't that mean i can do static to some variables and then i don't have to make them in config files?

#

to make permenant variables

#

instead of local ones

paper lodge
#

yeah, you probably could do that
but if you want people to be able to change the variables easily then you need to use config files so people don't have to make a plugin to change the variables in your plugin

paper lodge
ancient topaz
#

ok

paper lodge
#

but yeah, back on track

#

you need to add a new static field to your listener, CraftingEvents

paper lodge
#

private static final Set<Integer> allowedSlots

#

Now, notice about this

#

this isn't valid yet, since it's final but we haven't given it a value

ancient topaz
#

how we gonna give it values if it's final

paper lodge
#

easy, private static final Set<Integer> allowedSlots = Sets.newHashSet();

#

the = and everything after it is called an initializer

#

(it's only called an initializer when it is given to the variable when the variable is created)

#

For the record: Sets isn't from Java specifically, it's from a Java library that Minecraft uses called Guava.

ancient topaz
#

private static final Set<Integer> slotChecker2000 = Sets.newHashSet();
SLOT CHECKER 2000

paper lodge
#

you can then add your slots to the Set like
Sets.newHashSet(11, 12, 13); and etc.

ancient topaz
#

actually i won't understand it in the future, i'll just call it allowedSlots

paper lodge
#

so just change the field you've made to have your slot numbers in there

ancient topaz
#

private static final Set<Integer> allowedSlots = Sets.newHashSet(11, 12, 13, 20, 21, 22, 29, 30, 31); dis good?

paper lodge
#

yup

ancient topaz
#

ok now how do i put it in the if statement

paper lodge
#

for convenience, create a new method called isAllowed that returns a boolean

#

it should also have an int parameter

ancient topaz
#

how do i make 2 values per variable

paper lodge
#

so, like private boolean isAllowed(int number) (it should be private since it doesn't need to be accessed outside of the class)

paper lodge
ancient topaz
#

oh u said returns a boolean my b

paper lodge
#

ah

ancient topaz
#

i thought u meant it has a boolean value and an int value

paper lodge
#

I also said method lol

ancient topaz
#

ye im just blind

paper lodge
#

but in the body, AKA code of your method, you can then write return allowedSlots.contains(number);

#

contains returns a boolean saying whether or not that Set has that number in it

#

so, you can then replace your if statements with if (isAllowed(e2.getSlot()))

ancient topaz
#

this is good, right? if (allowedSlots.contains(e2.getSlot())) {

paper lodge
#

yeah you can do that

#

the method isn't explicitly required

#

but some people prefer it bc it gives you more information from the name

#

and it means you repeat less code in a way

ancient topaz
#

ok i tested it out and it works like before

paper lodge
#

yeah but now it's easier for people trying to help to read

#

and it's faster

#

can you send the updated code now?

ancient topaz
#

i bit u bubub

paper lodge
#

So what are you trying to accomplish here?

ancient topaz
#

wdym

paper lodge
#

What are you trying to do with your code?

#

And also why is it not working?

ancient topaz
#

oh it's like a normal crafting table but in a gui

#

facanating ik

paper lodge
#

fascinating*

ancient topaz
#

anyways i used too much brain power for now and im tired

paper lodge
#

aight, one last thing tho

ancient topaz
#

ok

paper lodge
#
                // top row
                slots[0] = e2.getClickedInventory().getItem(11);
                slots[1] = e2.getClickedInventory().getItem(12);
                slots[2] = e2.getClickedInventory().getItem(13);
                // middle row
                slots[3] = e2.getClickedInventory().getItem(20);
                slots[4] = e2.getClickedInventory().getItem(21);
                slots[5] = e2.getClickedInventory().getItem(22);
                // bottom row
                slots[6] = e2.getClickedInventory().getItem(29);
                slots[7] = e2.getClickedInventory().getItem(30);
                slots[8] = e2.getClickedInventory().getItem(31);

you can improve this code

#

this change isn't faster, but it's more readable:

#

replace it with a for loop

ancient topaz
#

shouldn't i do 2 for loops?

#

1 adding 1 to a local variable, then another one adding 9

#

1 inside the 9

paper lodge
#

gimme a sec

ancient topaz
#

i really want to go tho

paper lodge
#

yeah go ahead

ancient topaz
#

my brain hurt

paper lodge
#

you can check back on this tomorrow

ancient topaz
#

alright, thanks a lot for all ur help