As you can see in my, title, I’m trying to use the CraftItemEvent event.
- I want to get the name + amount of the crafting results.
- Now I can’t get the involved player with event.getUser().
How can I perform 1) and 2) ?
Greets
As you can see in my, title, I’m trying to use the CraftItemEvent event.
How can I perform 1) and 2) ?
Greets
In the CraftItemEvent object you have the method getViewer(). It return the Human in the event and you have getResults that return the itemstacks that are the result of the craft.
http://spongepowered.github.io/SpongeAPI/org/spongepowered/api/event/inventory/CraftItemEvent.html
Yeah, thanks, but I want to get the player object, or something to get the UUID of the player / viewer.
Greets
EDIT: Oh, I figured it out, thanks ^^
A Player extends of a Human.
So how can I get the extend object ? (Sorry I’m new to Java )
Player player = (Player) event.getViewer();
Thanks dude!
Sorry, but something is wrong in my code, or I “don’t get it” …
public void onCraftItem(CraftItemEvent event) {
Player player = (Player) event.getViewer();
player.sendMessage(Texts.builder("crafted").style(TextStyles.NONE).color(TextColors.GRAY).build());
}
I think, the player should get “crafted”, when he crafts something, but - nothing…
It throw an error?
No… there is nothing wrong with my console…
Change player.sendMessage(…); for System.out.println(player.getUniqueID());
Nothing (and yes, I was looking at my console)
You can try
if(event.getViewer() instanceof Player)
System.out.println(true);
If it work, the human is a player, if not you can do
System.out.println(event.getViewer());
it will print the object.
@Subscribe
public void onCraftItem(CraftItemEvent event) {
Player player = (Player) event.getViewer();
if(event.getViewer() instanceof Player) {
System.out.println(true);
}else{
System.out.println(event.getViewer());
}
}
I got Nothing…
Have you registered your event?
Main:
@Subscribe
public void onServerInitialize(InitializationEvent event) {
//some code
event.getGame().getEventManager().register(this, new CraftItem(logger, userManager, jobManager));
}
CraftItem:
package me.Hitzk0pf.CaveAccounts.listeners;
import me.Hitzk0pf.CaveAccounts.Main;
import me.Hitzk0pf.CaveAccounts.obj.objects.JobActive;
import me.Hitzk0pf.CaveAccounts.obj.objects.JobBlock;
import me.Hitzk0pf.CaveAccounts.obj.objects.User;
import me.Hitzk0pf.CaveAccounts.obj.tools.JobManager;
import me.Hitzk0pf.CaveAccounts.obj.tools.UserManager;
import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.effect.Viewer;
import org.spongepowered.api.entity.player.Player;
import org.spongepowered.api.event.Subscribe;
import org.spongepowered.api.event.entity.player.PlayerPlaceBlockEvent;
import org.spongepowered.api.event.inventory.CraftItemEvent;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.text.format.TextStyles;
import java.util.ArrayList;
public class CraftItem {
//call classes
private Logger logger;
private JobManager jobManager;
private UserManager userManager;
private JobActive job;
private User user;
private ArrayList<JobBlock> blocks;
private boolean once = false;
private int CashCount = 0;
private float cacheMoney;
private Game game;
private int JobCashCount = Main.getJobCashCount();
private int JobCacheMoney = Main.getJobCacheMoney();
public CraftItem(Logger logger, UserManager userManager, JobManager jobManager) {
this.logger = logger;
this.jobManager = jobManager;
this.userManager = userManager;
}
@Subscribe
public void onCraftItem(CraftItemEvent event) {
Player player = (Player) event.getViewer();
if(event.getViewer() instanceof Player) {
System.out.println(true);
}else{
System.out.println(event.getViewer());
}
}
}
I have classes for block break / place events too -> they are working …
Send something in the console everytime the event is called. You will see if the event works.
Yeah, but why I cant get the message when the event gets called?
PS What do you mean, when you say “send something in the console” ?
Do System.out.println(“Print something”);
No, when I do that, I get nothing (thats because I didn’t understand your “plan”).
Do I something wrong?