Player turns invisible and immobile when loading custom mod - java

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!!!

Related

TypeTransformer applyAdviceToMethod(isConstructor() doesn't works in opentelemetry extensions

I'm trying to add spans when constructor of some class called. I'm using opentelemetry javaagent and extensions to add tracing to my application.
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.named;
public class ClassConstructorInstrumentation implements TypeInstrumentation {
#Override
public ElementMatcher<TypeDescription> typeMatcher() {
return ElementMatchers
.namedOneOf("org.example.ServiceManagerDummy");
}
#Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isConstructor(),
this.getClass().getName() + "$ConstructorSpanCreateAdvice");
// transformer.applyAdviceToMethod(
// named("dummyMethod"),
// this.getClass().getName() + "$ConstructorSpanCreateAdvice");
}
#SuppressWarnings("unused")
public static class ConstructorSpanCreateAdvice {
#Advice.OnMethodEnter
public static void onEnter() {
System.out.println("START SPAN ");
}
#Advice.OnMethodExit(onThrowable = Throwable.class)
public static void onExit(
#Advice.Thrown Throwable throwable
) {
System.out.println("END SPAN ");
}
}
}
public class ServiceManagerDummy {
public ServiceManagerDummy() {
System.out.println("SERVICE MANAGER CONSTR");
dummyMethod();
}
private void dummyMethod() {
System.out.println("DUMMY METHOD CALLED");
}
}
I'm using a simple configuration as above just to verify that when the constructor was called my advice method log it. But when it configured to add some logs when the constructor was called, I received nothing in the log. But when I add config for method calling (commented code) it works. What's wrong in my configuration?
What Byte Buddy would normally do would be to wrap the constructor in a try-finally-block. For a constructor, that is not possible as the super method call cannot be wrapped in such a block. "onThrowable" is therefore not possible for constructors.

Why does the forge event not fire?

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)

Spigot Unregister event with a command

I made a Plugin that registers an event, I want to make a command that unregisters it, how should I do it, I already searched for 2 h and I found nothing.
I want to make /Pvpeventon to start the event and /Pvpeventoff to turn it off
that is the code I already made:
package me.leopa.R1.FFA;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
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.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class MainFFA extends JavaPlugin implements Listener{
#Override
public void onEnable() {
System.out.println("[INFO Leopa] Start");
super.onEnable();
}
#Override
public void onDisable() {
System.out.println("[INFO Leopa] Stop");
super.onDisable();
}
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(command.getName().equalsIgnoreCase("PVPEVENTon")) {
getServer().getPluginManager().registerEvents(this, this);
}
if(command.getName().equalsIgnoreCase("PVPEVENToff")) {
getServer().getPluginManager().
}
return super.onCommand(sender, command, label, args);
}
#EventHandler
public void onDeathPVPEVENT(PlayerDeathEvent pvpevent) {
Player p = pvpevent.getEntity();
p.sendMessage("HI");
}
}`
Instead of unregister the event you should simplify it and add a boolean as variable which turns into false when the pvp should be disabled and to true if pvp is allowed:
//Some Listener class
...
private YourPlugin plugin; //example
...
#EventHandler
public void playerDeath(PlayerDeathEvent event) {
if(plugin.isEventMode()) { //TODO when event mode is on }
}
Plugin class
...
public class YourPlugin extends JavaPlugin {
...
private boolean eventMode; //false per default
...
public boolean toggleEventMode() {
eventMode = !eventMode; //negation so if it is true it will be turned into false if it is false it will be turned to true
return eventMode;
}
public boolean isEventMode() {
return eventMode;
}
}
Command toggle event mode:
//is declared somewhere
boolean eventMode = plugin.toggleEventMode();
//true if eventMode is on false if not.
Note you can also use a setEventMode method.
You also can use the unregisterAll method to unregister all events in a Listener or a Plugin:
HandlerList.unregisterAll(this); //takes a listener or a plugin. In your case you got all stuff in one class it should still work.
Check these methods:
HandlerList#unregister(Plugin)
HandlerList#unregister(Listener)
HandlerList#unregisterAll(Plugin)
HandlerList#unregisterAll(Listener)

Bukkit PlayerDeathEvent

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);
}

OnTurnBasedMatchUpdateReceivedListener does not trigger

I am trying to get the OnTurnBasedMatchUpdateReceivedListener to trigger but it doesn't seem to work. Below is my code example that I am using. I get a valid GoogleApiClient and am already signed in (I have other Listeners going in other parts of code).
The goal is to have a single class that can handle this event, by passing the GoogleApiClient through and have it callback here (I have no other way of getting code to trigger such as the BaseGameActivity because the code is within another LIB and I am writing additional code).
Any suggestions on how to debug this?
package com.google.example.games.pluginsupport;
import android.util.Log;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.multiplayer.turnbased.OnTurnBasedMatchUpdateReceivedListener;
import com.google.android.gms.games.multiplayer.turnbased.TurnBasedMatch;
public class TurnBaseMatchHelper implements OnTurnBasedMatchUpdateReceivedListener {
public interface TurnBasedMatchListener {
void onTurnBasedMatchReceived(TurnBasedMatch match);
void onTurnBasedMatchRemoved(String matchId);
}
private static TurnBaseMatchHelper turnbaseInterface = null;
private TurnBasedMatchListener sTurnBasedMatchListener = null;
public static void registerTurnBasedCallbacks(GoogleApiClient _googleApiClient, TurnBasedMatchListener sListener) {
if (turnbaseInterface == null) {
turnbaseInterface = new TurnBaseMatchHelper();
}
turnbaseInterface.sTurnBasedMatchListener = sListener;
Log.d("Unity", "registerTurnBasedCallbacks");
Games.TurnBasedMultiplayer.registerMatchUpdateListener(_googleApiClient, turnbaseInterface);
}
#Override
public void onTurnBasedMatchReceived(TurnBasedMatch match) {
Log.d("Unity", "onTurnBasedMatchReceived");
if (turnbaseInterface.sTurnBasedMatchListener != null) {
sTurnBasedMatchListener.onTurnBasedMatchReceived(match);
}
}
#Override
public void onTurnBasedMatchRemoved(String matchId) {
Log.d("Unity", "onTurnBasedMatchRemoved");
if (turnbaseInterface.sTurnBasedMatchListener != null) {
sTurnBasedMatchListener.onTurnBasedMatchRemoved(matchId);
}
}
}
here is my similar problem answer.
From post:
Go to the Settings -> Account and sync-> turn on Auto-sync checkbox

Categories