I am trying to create a job in java play2 using akka.
I always get the same error error: cannot find symbol
And it points to system.actorOf() Intellij and Eclipse don't give me an error msg.
But I can not find this method. I used the following imports
import play.libs.Akka;
import akka.actor.ActorSystem;
import akka.actor.ActorRef;
import akka.actor.UntypedActorFactory;
import akka.actor.UntypedActor;
import akka.actor.Props;
import akka.actor.ActorRefFactory;
Maybe the docs are outdated and they have removed the system.actorOf() ?
public class Global extends GlobalSettings {
ActorRef tickActor = system.actorOf(new Props().withCreator(new UntypedActorFactory() {
public UntypedActor create() {
return new UntypedActor() {
public void onReceive(Object message) {
if (message.equals("Log")) {
controllers.Application.log();
} else {
unhandled(message);
}
}
};
}
}));
#Override
public void onStart(Application app) {
Cancellable cancellable = system.scheduler().schedule(Duration.Zero(), Duration.create(10, TimeUnit.SECONDS),
tickActor, "Log");
}
}
EDIT:
.
oh... google redirected me to the outdated documentation. It's Akka.System() now..
Can anyone give me an example on how to create the tickActor with up2date code?
http://www.playframework.org/documentation/2.0.2/JavaAkka
I strongly suggest that you take a look at the Akka documentation about actors and schedulers.
You can also take a look at this question: Play Framework 2.0 schedules an Akka Actor at server launch
solved it.
Btw there are some typos in the documentation.
import java.util.concurrent.TimeUnit;
import play.*;
import play.mvc.*;
import play.mvc.Http.RequestHeader;
import static play.mvc.Results.*;
import play.libs.Akka;
import akka.actor.ActorSystem;
import akka.actor.ActorRef;
import akka.actor.UntypedActorFactory;
import akka.actor.UntypedActor;
import akka.actor.Props;
import akka.actor.ActorRefFactory;
import akka.util.*;
public class Global extends GlobalSettings {
ActorRef tickActor;
#Override
public void onStart(Application app) {
Logger.info("D");
tickActor = Akka.system().actorOf((new Props().withCreator(new UntypedActorFactory() {
public UntypedActor create() {
return new UntypedActor() {
public void onReceive(Object message) {
if (message.equals("Log")) {
//Do something
// controllers.Application.log();
} else {
unhandled(message);
}
}
};
}
})));
Akka.system().scheduler().schedule(
Duration.create(0, TimeUnit.MILLISECONDS),
Duration.create(30, TimeUnit.MINUTES),
tickActor,
"Log"
);
}
}
Related
I'm trying to see if someone joined a world and then print something to console but the event doesn't seem to fire. Can someone help me? Here is my code:
package pl00py_TR.pl00pyspvpmod;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
#Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, version = Reference.VERSION)
public class Pl00pysPVPMod {
#EventHandler
public void preInit(FMLPreInitializationEvent event) {
}
#EventHandler
public void init(FMLInitializationEvent event) {
}
#EventHandler
public void postInit(FMLPostInitializationEvent event) {
System.out.println("DIRT BLOCK >> " + Blocks.dirt.getUnlocalizedName());
}
public class EventTest {
#SubscribeEvent
public void OnEvent(LivingSpawnEvent event) {
System.out.println("Event Test");
if (event.entity instanceof EntityPlayer) {
System.out.println("Player Joined");
}
}
}
public void RegisterEvents() {
System.out.println("Registering Events...");
MinecraftForge.EVENT_BUS.register(EventTest.class);
}
}
The code is for 1.8.9 and I am trying to find out if someone joins a server/world. I am trying to get the players coords and print them to console but the event doesn't fire or initialize. Could someone explain why this is happening and give me an idea of how to fix this?
Could be wrong but try MinecraftForge.EVENT_BUS.register(new EventTest()); instead of MinecraftForge.EVENT_BUS.register(EventTest.class);
In the documentation for EventBus
void register(java.lang.Object target)
I am pretty new to coding java and I have done a few tutorials, which have been great but i don't know why it isn't working in-game. I have tried everything such as changing it and looking at so many different forums. There are two classes for the events(Join and Leave Event) and the main class. I have made sure to check for importing them and errors none for me to see from where I have looked. If anyone can help it would be a blessing.
Code: - Main class:
package me.JimmyClown.FirstPlugin;
import org.bukkit.plugin.java.JavaPlugin;
public class MainClass extends JavaPlugin {
#Override
public void onEnable() {
System.out.println("It works!");
getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
getServer().getPluginManager().registerEvents(new PlayerLeave(), this);
}
#Override
public void onDisable() {
}
}
**Code for the Join-Class:**
package me.JimmyClown.FirstPlugin;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
public class PlayerJoin implements Listener{
#EventHandler
void onPlayerJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();
e.setJoinMessage(ChatColor.BLUE + "Welcome back to the server!" + player.getDisplayName());
}
}
**Code for Leave Class:**
package me.JimmyClown.FirstPlugin;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
public class PlayerLeave implements Listener {
#EventHandler
void onPlayerLeave(PlayerQuitEvent e) {
Player player = e.getPlayer();
e.setQuitMessage(ChatColor.DARK_BLUE + "Awwww, We hope we see you soon" + player.getDisplayName());
}
}
The plugin.yml:
name: FirstProject
version: 1.0
main: me.JimmyClown.FirstPlugin.MainClass
authors: JimmyTheClown
description: This is my first plugin.
Try to set the access modifier of the event methods to public.
public void onPlayerLeave(PlayerQuitEvent e) { }
public void onPlayerJoin(PlayerJoinEvent e) { }
I am trying to make a basic mod for minecraft and am following a tutorial for the same. When I run runClient, it gives me the following error
Reference to undefined variable MC_VERSION
Here is my main.java for reference:
package rattandeep.basicmod;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import rattandeep.basicmod.proxy.CommonProxy;
import rattandeep.basicmod.util.Reference;
#Mod(modid = Reference.MOD_ID, name = Reference.NAME, version = Reference.VERSION)
public class Main {
#Instance
public static Main instance;
#SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.COMMON_PROXY_CLASS)
public static CommonProxy proxy;
#EventHandler
public static void PreInit(FMLPreInitializationEvent event) {
}
#EventHandler
public static void init(FMLInitializationEvent event) {
}
#EventHandler
public static void Postinit(FMLPostInitializationEvent event) {
}
}
I have searched for a solution but found none. How do I solve this?
I found out how to solve it. I just needed to change the variable value of MC_VERSION in my runClient launch file to my minecraft version.
I am trying to use ListenableFutues for the first time, getting an error while using addCallback method, Please help.
Attached is the snapshot of the code.
Error is:
error: <identifier> expected
Futures.addCallback(futureRequest,new FutureCallback<T>() {
^
error: <identifier> expected
Futures.addCallback(futureRequest,new FutureCallback<T>() {
^
You can implement ListenableFuture Callback like below.
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
public class EventDriver<T> {
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
public void prepare() {
Callable<T> task = null; // Whatever task you need to assign
ExecutorService executorService = Executors.newFixedThreadPool(10);
ListeningExecutorService service = MoreExecutors.listeningDecorator(executorService);
ListenableFuture<T> futureRequest = service.submit(task);
Futures.addCallback(futureRequest, new FutureCallback<T>() {
#Override
public void onSuccess(T result) {
logger.info("Sucess..");
}
#Override
public void onFailure(Throwable t) {
logger.info("Failure..");
}
});
}
}
I am going through the Play Framework tutorial. I am getting this error:
error: cannot find symbol
In /Users/hseritt/devel/todolist/app/controllers/Application.java at line 12.
import views.html.*;
public class Application extends Controller {
static Form<Task> taskForm = Form.form(Task.class); // ERROR IS HIGHLIGHTED AS Form.form
public static Result index() {
return redirect(routes.Application.tasks());
}
My full code for Application.java:
package controllers;
import play.*;
import play.data.*;
import play.mvc.*;
import models.*;
import views.html.*;
public class Application extends Controller {
static Form<Task> taskForm = Form.form(Task.class);
public static Result index() {
return redirect(routes.Application.tasks());
}
public static Result tasks() {
return ok(
views.html.index.render(Task.all(), taskForm)
);
}
public static Result newTask() {
return TODO;
}
public static Result deleteTask(Long id) {
return TODO;
}
}
I am wondering if I missed something in the tutorial or put something in the wrong place.
Thanks!
I think you should import the following:
import static play.data.Form.*;
As per jnoob, just change import to import play.data.Form, then do static Form<Task> taskForm = form(Task.class);, worked for me.