#Maow explaining for iDerpy
1 messages · Page 1 of 1 (latest)
Okay so
HAllo
@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
w a w
Creating a single variable is actually not that bad, especially with something like Set since it's not expensive to create.
w a w
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
i am convinced, what do i do
Well
Lemme look at your original code to see
@ancient topaz you mind sending it in here?
Ah you adopted him. Hf. No Return.
Ayup.
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
like-a dis? private static slotChecker2000
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
can't i just remove static? it's not like i'm going to use this outside this class (i think)
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
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
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
permanent*
ok
but yeah, back on track
you need to add a new static field to your listener, CraftingEvents
?
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
how we gonna give it values if it's final
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.
private static final Set<Integer> slotChecker2000 = Sets.newHashSet();
SLOT CHECKER 2000
you can then add your slots to the Set like
Sets.newHashSet(11, 12, 13); and etc.
actually i won't understand it in the future, i'll just call it allowedSlots
so just change the field you've made to have your slot numbers in there
private static final Set<Integer> allowedSlots = Sets.newHashSet(11, 12, 13, 20, 21, 22, 29, 30, 31); dis good?
yup
ok now how do i put it in the if statement
for convenience, create a new method called isAllowed that returns a boolean
it should also have an int parameter
how do i make 2 values per variable
so, like private boolean isAllowed(int number) (it should be private since it doesn't need to be accessed outside of the class)
What do you mean?
oh u said returns a boolean my b
ah
i thought u meant it has a boolean value and an int value
I also said method lol
ye im just blind
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()))
this is good, right? if (allowedSlots.contains(e2.getSlot())) {
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
ok i tested it out and it works like before
yeah but now it's easier for people trying to help to read
and it's faster
can you send the updated code now?
LMAO look at the name of this link (btw this is the new code)
https://hastebin.com/ibitububub.java
i bit u bubub
So what are you trying to accomplish here?
wdym
fascinating*
anyways i used too much brain power for now and im tired
i think i'll just ask again tmr in #help-development
aight, one last thing tho
ok
// 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
shouldn't i do 2 for loops?
1 adding 1 to a local variable, then another one adding 9
1 inside the 9
gimme a sec
i really want to go tho
yeah go ahead
my brain hurt
you can check back on this tomorrow
alright, thanks a lot for all ur help