Bukkit - Referring to a custom item in an IF statement doesn't work - bukkit

Apologies for terrible use of words but I'm not into the lingo yet.
I've been creating a spell book for a simple bukkit plugin which opens into an inventory on the right click of a certain custom item that is crafted. Here is the code
#EventHandler
public void onBookInteract(PlayerInteractEvent e){
if(e.getAction() == Action.RIGHT_CLICK_AIR){
if(e.getItem() == SpellBook){
openGUI(e.getPlayer());
When I try to do this ingame nothing happens. I've removed if(e.getItem() == SpellBook){ and it works as well as if I change the statement to:
if(e.getMaterial() == Material.BLAZE_POWDER){
It works as well. Probably a simple error but I only started coding a couple of days ago. Thanks for any and all helpful feedback ^_^

There are a couple things that could be wrong.
1) Make sure that the class that this is in implements Listener, and that in your Main class, (the one that extends JavaPlugin) in the onEnable() method, you have:
this.getServer().getPluginManager().registerEvents(new <class that implements Listener>(), this);
so if the class that your code with #EventHandler is in is called Handler, then you would use:
this.getServer().getPluginManager().registerEvents(new Handler(), this);
2) Try using .equals() instead of ==:
if(e.getAction().equals(Action.RIGHT_CLICK_AIR)){
if(e.getItem().equals(SpellBook)){
3) Make sure that SpellBook is actually an ItemStack. If it is, then you may want to try doing this if it has no ItemMeta (display name, lore, etc.)
if(e.getItem().getType().equals(SpellBook.getType())){
Otherwise, if it does have ItemMeta, you could use this:
if(e.getItem().getType().equals(SpellBook.getType()) && e.getItem().hasItemMeta()){
if(e.getItem().getItemMeta().getDisplayName().equals(SpellBook.getItemMeta().getDisplayName(){
if(e.getItem().getItemMeta().getLore().equals(SpellBook.getItemMeta().getLore(){
So, your final code should probably look something like this:
#EventHandler
public void onBookInteract(PlayerInteractEvent e){
if(e.getAction().equals(Action.RIGHT_CLICK_AIR)){
if(e.getItem().getType().equals(SpellBook.getType()) && e.getItem().hasItemMeta()){
if(e.getItem().getItemMeta().getDisplayName().equals(SpellBook.getItemMeta().getDisplayName()){
if(e.getItem().getItemMeta().getLore().equals(SpellBook.getItemMeta().getLore()){
openGUI(e.getPlayer());
}
}
}
}
}

If SpellBook is a type (i.e. a Java class) then that's your problem, e.getItem() returns an instance of a class. Again, if SpellBook is a type (I can't tell with the brief code you gave), then try using e.getItem() instanceof SpellBook instead. Sorry if I'm way off.

Related

Implementing UML Sequence Diagram

My question is relatively simple: how would I go about implementing a UML sequence diagram in C++ code? I was reading up on sequence diagrams the other day, and I found this example for a program for a student enrolling in a seminar.
How would I go about turning this diagram into a program? For the sake of this question, lets focus on one class, say the EnrollInSeminar controller. How would I go about implementing this?
I imagine that it might be something like this:
class EnrollInSeminar
{
public:
void Activate();
};
void EnrollInSeminar::Activate()
{
SecurityLogon logonUI{};
Student theStudent = logonUI.getStudent();
SeminarSelector seminarSelectorUI{};
Seminar seminar = seminarSelectorUI.getSeminar();
if (!seminar.isEligible(theStudent))
return;
theStudent.getSchedule().determineFit(seminar);
Fee fee = StudentFees.calculateFees(seminar, theStudent);
FeeDisplay feeUI{fee};
if (!feeUI.getVerification())
return;
seminar.enrollStudent(theStudent);
}
Is this the correct way to implement the EnrollInSeminar class? If not, how should I do it?
Actually a SD does not tell anything about the methods being used in the messages passed from one object to another except the name, the parameters and - as the name says - the sequence. So the only thing you can draw from "just the SD" are methods and their parameters.
You will need additional information from a use case to know what the methods are all about. Without you simply can not "implement a SD".

How can I check if signal present in object?

I can't find a way to do it with a build-in way like someObject->signalPresent(SomeSignal). Maybe I missed something. I know that I can do this in C++ using SFINAE, but there should be same ability to do that in QT.
There's QMetaObject::indexOfSignal(). Code using it could look like:
if (someQObject->metaObject()->indexOfSignal("someSignal(QString)") != -1) {
// has signal
...
} else {
// doesn't have signal
...
}
Note the requirements regarding signature normalization described in the documentation, e.g. "someSignal(const QString&)" won't work.

Cursor placement when calling a method with void parameters

This is a fairly straight forward question but I am unable to find an answer about this specific type of formatting. I'm looking for a way to modify where eclipse places the cursor after using its content assist to complete a method call based on whether or not the method has any parameters.
To illustrate what exactly I'm talking about lets consider a simple c++ class like so:
class Example
{
public:
int voidParams()
{
//do something
return 42;
};
int nonVoidParams(int a)
{
//do something else
return a*a;
};
};
And at some point I created an instance of the class Example ex;
Now within eclipse if I started typing ex.nonV and I told eclipse to auto complete it would enter in ex.nonVoidParams() and after doing this my cursor would be inside the parenthesis like so ex.nonVoidParams(|) where | is my cursor. This makes sense and is useful since I need to give this particular method an argument.
Hopefully none of what I just said is new to anyone and is all pretty straight forward. This is where my question comes in. Having my cursor be placed within the parenthesis of a method call is only useful if that method takes parameters. If I were to type ex.voi and let eclipse auto complete to ex.voidParams() my cursor would be inside the parenthesis like so ex.voidParams(|) where | is my cursor. This isn't very useful since there is nothing for me to enter there.
I would like to know if there is a way to setup eclipse so, given the above examples, if it auto completes a method with void parameters such as ex.voidParams() it places the cursor after the method call like so ex.voidParams()| again where | is my cursor.
I'm not super familiar with customizing eclipse but I feel like there should be a way to do this since if eclipse is auto completing the method call it should know what its parameters are and be able to adjust its formatting from there.
Oh and this will probably be asked at some point, I'm currently using Eclipse CDT version 4.2.0 (Juno service Release 2).
Go to Window->Preferences->C++->Editor->Content Assist->Advanced, pick the "Parsing-based Proposals" instead of "Parsing-based Proposals (Task-Focused)".

Firebreath how to know if my method is called without debugging

I am using Objective-C++ in my firebreath project. The problem is that I am using Xcode 4 and I can not find the way to debug my project. So I have thought about if my method is been called from the web page.
Here is my source code:
In my OpenOnDesktopPluginAPI.h class:
class OpenOnDesktopPluginAPI : public FB::JSAPIAuto
{
public:
OpenOnDesktopPluginAPI(const OpenOnDesktopPluginPtr& plugin, const FB::BrowserHostPtr& host);
virtual ~OpenOnDesktopPluginAPI();
OpenOnDesktopPluginPtr getPlugin();
...
//This is my method
void runNotification();
...
};
In my OpenOnDesktopPluginAPI.mm class:
OpenOnDesktopPluginAPI::OpenOnDesktopPluginAPI(const OpenOnDesktopPluginPtr& plugin, const FB::BrowserHostPtr& host) : m_plugin(plugin), m_host(host)
{
...
//Register my method
registerMethod("runNotification", make_method(this, &OpenOnDesktopPluginAPI::runNotification));
...
}
//DistributedNotification class is my objective-c class with the implementation for post a distributed notification.
void OpenOnDesktopPluginAPI::runNotification()
{
DistributedNotification * notificationClass = [[DistributedNotification alloc] init];
[notificationClass postNotification];
[notificationClass release];
}
In my FBControl.html:
...
function myFunction()
{
plugin().runNotification();
}
...
My new method
...
I put my DistributedNotification.mm class in the
Build Phases -> "Compile Sources"
for my plugin target.
But I don´t know if my runNotification method is called, because when (In my web page) I click on My new method link, nothing happens.
I'll repeat what I said on the forum when you ask; perhaps you haven't seen that answer yet:
First of all, you can debug with Xcode4, at least on some browsers; the trick is figuring out which process to connect to.
Secondly, you can always use NSLog to log things to the console. Thirdly, you could use log4cplus (see http://www.firebreath.org/display/documentation/Logging).
Finally, you haven't specified what browser you're testing on, nor have you indicated what happens. It looks reasonable, but aparently doesn't work? What doesn't work? What does it do?
It's nearly impossible to give you any useful advice without detailed information about what you are encountering.

Aspect Oriented Programming in Qt

I'm trying to get my head around AOP and some Qt Code would really help.
From wikipedia here is some sample code (easy for a Qt/C++ programmer to read):
void transfer(Account fromAcc, Account toAcc, int amount, User user, Logger logger)
throws Exception {
logger.info("transferring money...");
if (! checkUserPermission(user)){
logger.info("User has no permission.");
throw new UnauthorizedUserException();
}
if (fromAcc.getBalance() < amount) {
logger.info("Insufficient Funds, sorry :( ");
throw new InsufficientFundsException();
}
fromAcc.withdraw(amount);
toAcc.deposit(amount);
//get database connection
//save transactions
logger.info("Successful transaction. :) ");
}
And then "aspectized":
void transfer(Account fromAcc, Account toAcc, int amount) throws Exception {
if (fromAcc.getBalance() < amount) {
throw new InsufficientFundsException();
}
fromAcc.withdraw(amount);
toAcc.deposit(amount);
}
aspect Logger
{
void Bank.transfer(Account fromAcc, Account toAcc, int amount, User user, Logger logger)
{
logger.info("transferring money...");
}
void Bank.getMoneyBack(User user, int transactionId, Logger logger)
{
logger.info("User requested money back");
}
// other crosscutting code...
}
Qt has signals and slots to decouple objects. But I still need to emit signals.
So: Can this be done with Qt or do I need some special framework/preprocessors as referenced in the wikipedia article?
I have a feeling that there must be some trick since Qt uses the Meta Object Compiler and some functionality might be "injected" with dynamic methods.... just spit-balling here ;)
Edit: To give a better context: I really like the dynamic aspects (power) of the Qt meta object with signals and slots and would like to keep a Qt feel to it. Thus, my idea is to make use of slots (or signals) as point cuts. For example:
If I define slot Bank::transfer(...) and then signal Bank::OnBeforeTranfer() and signal Bank::OnAfterTransfer(). If I then connect them to other aspects say Security::transfer() and Logger::transfer() (all QObjects) I can block calls (like fail OnBeforeTransfer).
But, if we then take it to the next evolution to get less and cleaner code I would like to get rid of the OnXXXX signals and connect the Bank::transfer slot to Security::transfer slot and Logger::transfer. Anything dynamic in Qt? : Like order of calling slots and and preventing next call in the "slot chain"?
This whole context can still be considered AOP right? I'm trying to stick to "method level point cuts" or am I totally beside the point here?
In what language are you planning to use Qt? I recently had to build a simple GUI in Qt around a python script and used the AOP python package Aspyct to do some quick before and after stuff. Qt is event-driven programming, I'd say get familiar with the Qt basics, many things are similar to AOP-style operations and then find some AOP libraries for the language you plan to use Qt in.
Another AOP framework you may consider using is AspectC++. I've played with it a bit and it seems to work quite well. They even have a whitepaper on the site that describes how AspectC++ can be used with Qt.
If you want to stay within the Qt framework, you could take a look at the State Machine Framework. (And get rid of the exceptions :)
Then you could just connect the Logger to state change events.