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)
Related
I'm starting to code my own minecraft 1.14.4 mod. I want to create a very simple mod which will just display a clok showing the in-game time (I couldn't find a similar updated mod, since all other mods show the real-time time, not the in-game time). I took the example mod which comes with Forge, and added some custom code.
When I load the mod, the player turns invisible and immobile. He is there, I can look around and dig and so on, but I can not see him nor move.
If I suppress my custom code, it works... very weird to me.
this is my code so far (it does not show anything since I am stuck in this problem).
package com.alef.simpleclock;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.stream.Collectors;
// The value here should match an entry in the META-INF/mods.toml file
#Mod("simpleclock")
public class SimpleClock
{
// Directly reference a log4j logger.
private static final Logger LOGGER = LogManager.getLogger();
public static boolean startTimer;
public static int tick;
public SimpleClock() {
// Register the setup method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
// Register the enqueueIMC method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
// Register the processIMC method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);
// Register the doClientStuff method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
// Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this);
}
private void setup(final FMLCommonSetupEvent event)
{
// some preinit code
LOGGER.info("HELLO FROM PREINIT");
LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
}
private void doClientStuff(final FMLClientSetupEvent event) {
// do something that can only be done on the client
LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().gameSettings);
}
private void enqueueIMC(final InterModEnqueueEvent event)
{
// some example code to dispatch IMC to another mod
InterModComms.sendTo("examplemod", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world";});
}
private void processIMC(final InterModProcessEvent event)
{
// some example code to receive and process InterModComms from other mods
LOGGER.info("Got IMC {}", event.getIMCStream().
map(m->m.getMessageSupplier().get()).
collect(Collectors.toList()));
}
// You can use SubscribeEvent and let the Event Bus discover methods to call
#SubscribeEvent
public void onServerStarting(FMLServerStartingEvent event) {
// do something when the server starts
LOGGER.info("HELLO from server starting");
}
// You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
// Event bus for receiving Registry Events)
#Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
public static class RegistryEvents {
#SubscribeEvent
public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
// register a new block here
LOGGER.info("HELLO from Register Block");
}
}
#EventBusSubscriber
public static class showClock
{
#SubscribeEvent
public static void onJoin(final EntityJoinWorldEvent event)
{
if (event.getEntity() != null && event.getEntity() instanceof ClientPlayerEntity)
{
LOGGER.info("WELCOME " + event.getEntity().getName() + "!!!");
event.setCanceled(true);
if (!SimpleClock.startTimer)
{
SimpleClock.startTimer = true;
}
}
}
#SubscribeEvent
public static void timer(final TickEvent.WorldTickEvent event)
{
if (SimpleClock.startTimer)
{
if (SimpleClock.tick >= 1000)
{
SimpleClock.tick = 0;
drawClock(event.world);
}
else
{
SimpleClock.tick++;
}
}
}
private static void drawClock(World world)
{
int time = (int) world.getWorldInfo().getDayTime() % 1000;
LOGGER.info(time);
}
}
}
Any help is welcome!!!
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.
why isn't this working? I don't get any response if I've been killed! So as you can see I have tested multiple ways. But no one is working.
package net.gameforce.testing;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
#Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this,this);
getLogger().info("Testing Plugin Started");
}
public void onDisable() {}
#EventHandler
public void onPlayerInteract(PlayerInteractEvent event){
Bukkit.broadcastMessage("test");
event.getPlayer().setExp(100);
}
public void onPlayerDeath (PlayerDeathEvent event){
Bukkit.broadcastMessage("send");
event.getEntity().getPlayer().setExp(1000);
}
public boolean onDeath (PlayerDeathEvent event) {
Player Player = event.getEntity();
Bukkit.broadcastMessage(Player.getKiller().getDisplayName() + ", has killed you!");
if (Player.getKiller() != null) {
Bukkit.broadcastMessage("No Player");
}
else {
Bukkit.broadcastMessage("IDK");
}
return true;
}
}
Have I done something wrong?
Quotation from http://bukkit.gamepedia.com/Event_API_Reference#.40EventHandler
"Before this method can be invoked by Bukkit when the "Event" is fired, we need to annotate it. We do this with EventHandlers."
You've added this annotation to your onPlayerInteract method, but none of your others, such as your onDeath method. If your listener is setup correctly, adding the #EventHandler annotation to these methods will allow bukkit to correctly invoke them, like so:
#EventHandler
public void onPlayerDeath (PlayerDeathEvent event){
Bukkit.broadcastMessage("send");
event.getEntity().getPlayer().setExp(1000);
}
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"
);
}
}