Minecraft Modding, hitEntity method not running - java

I am fairly new to modding.
I want to create a sword that sets mobs or players on fire. I am using the hitEntity method and, I noticed the function is not even running. I tested it using the console. When I use the #Override notation before the hitEntity method it gives me an error:
The method hitEntity(ItemStack, EntityLiving, EntityLiving) of type BlazeSword must override or implement a supertype method
Here is my code:
package com.example.firstMod.tools;
import com.example.firstMod.FirstMod;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
public class BlazeSword extends ItemSword {
public BlazeSword(int id, ToolMaterial blaze) {
super(blaze);
this.setCreativeTab(FirstMod.blazingMod);
this.setUnlocalizedName("blazeSword");
this.setTextureName(FirstMod.MODID+":"+"blaze_sword");
}
public boolean hitEntity(ItemStack par1itemstack, EntityLiving par2entityliving, EntityLiving par3entityliving){
par2entityliving.setFire(20);
return true;
}
}

If you get the error you said with #Override, so there is no such method in ItemSword. Look ItemSword class for the right hit method.

The hitEntity method isn't working because you never call it. You declare that it exists but you never run it. What you need to do is call your hitEntity method in your BlazeSword class using your requested variables.
In your BlazeSword class:
hitEntity(ItemStack par1itemstack, EntityLiving par2entityliving, EntityLiving par3entityliving)
Also, take out #Override since you aren't trying to find the hierarchy of the blaze sword.

Related

How to get entity that killed player bukkit?

I am trying to make custom death messages when you get killed in certain ways. I want separate messages for when you get blown up by TNT or get blown up by a creeper. I tried debugging it and spawning a creeper and TNT and in the console it outputs the e.getEntity().getKiller() as null in the console. If I do e.getEntity().getKiller().getName(), it gives out an error.
here is my code for the debugger:
package me.Pale_Gray.BetterDeathMessages.deathmessages;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.PlayerDeathEvent;
public class DeathByTnt implements Listener{
#EventHandler
public void onPlayerDeath(PlayerDeathEvent e) {
String msg = e.getDeathMessage();
System.out.println((Player) e.getEntity().getKiller());
if (e.getEntity().getLastDamageCause().getCause().equals(DamageCause.ENTITY_EXPLOSION)) {
msg = ChatColor.translateAlternateColorCodes('&', "&cIt went off with a BANG!");
e.setDeathMessage(msg);
}
}
}
I am only asking to focus in on the System.out.println((Player) e.getEntity().getKiller()); because I know the if statement isn't selective on entities.
There are #Nullable annotations on methods and javadocs so you know what method can be null.
PlayerDeathEvent#getEntity#getKiller method returns Player so you don't need to cast Player object to it.
Otherwise, this method only returns Player, if it killed by another living Player.
Entity#getLastDamageCause method can return null
getCause() method returns an enum so you don't need to use equals, you only need == for comparing enum values.
You could check some other DamageCause fields, including ENTITY_EXPLOSION. Sometimes when creeper explodes the damage cause can be BLOCK_EXPLOSION, maybe because too far away.

How to create model listener for virtual instance's create action in Liferay 7?

package com.test.model.listener;
import org.osgi.service.component.annotations.Component;
import com.google.gson.InstanceCreator;
import com.liferay.portal.kernel.exception.ModelListenerException;
import com.liferay.portal.kernel.model.BaseModelListener;
import com.liferay.portal.kernel.model.ModelListener;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.model.*;
#Component(immediate = true, service = ModelListener.class)
public class InsertInstanceModelListener extends BaseModelListener<Instance??> {
#Override
public void onAfterCreate(Instance?? model) throws ModelListenerException {
System.out.println("InsertInstanceModelListener.onAfterCreate()");
super.onAfterCreate(model);
}
}
I'm newbie of liferay.I think it's may be something like this,but don't know how to make it right.
You are on your way. This OSGi component needs to be specific,
you are probably looking for com.liferay.portal.kernel.model.VirtualHost
If I understood correctly.
You do not need to call supper though.

Mocking Local Scope Objects using Powermock and mockito within a static method

This question has probably been asked a lot of times before. However, I couldn't find answers anywhere. I'm dealing with legacy code here.
Please Note : I'm simplifying my question to get a very specific answer. The code snippets only represent the problem I'm facing. Not the actual code I'm trying to test. The code snippet to be tested here represents part of the overall code I need to test.
Problem : myObj.loadContent(null,null) is actually being called instead of doing nothing as specified with PowerMockito.doNothing().when(mockObj).loadContent(null, null);
Code I wish to unit test :
class ClassInstantiatingObject {
.
.
public static void doSomething(Arg1 arg1, Arg2 arg2) throws Exception{
MyClass myObj = new MyClass(arg1, arg2);
myObj.loadContent(null, null);
}
}
My Unit Test :
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
.
.
.
#Test
public void testDoSomething() throws Exception {
MyClass mockObj = PowerMockito.mock(MyClass.class);
PowerMockito.whenNew(MyClass.class).withAnyArguments().thenReturn(mockObj);
PowerMockito.doNothing().when(mockObj).loadContent(null, null);
Arg1 mockArg1 = mock(Arg1.class);
Arg2 mockArg2 = mock(Arg2.class);
StaticClass.doSomething(mockArg1, mockArg2);
}
The code to be tested cannot be changed. Hence, I need a way to actually not call loadContent(null,null) using mockito/powermock.
Also, when using : PowerMockito.doNothing().when(MyClass.class,"loadContent",null,null)
OR PowerMockito.doNothing().when(MyClass.class,"loadContent",Mockito.anyString(),Mockito.anyMap())
I get a java.lang.NullPointerException
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.addAnswersForStubbing
CORRECT ANSWER
I've managed to figure out the solution. It's very simple to be honest.
In the example specified above. What I was missing was, in case of using PowerMockito.whenNew() , the class that is calling the constructor you wish to mock must be specified in the annotation #PrepareForTest. The class whose constructor you wish to mock need not be specified at all.
For eg.
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
//Only need to declare the class calling the constructor to use
//PowerMockito.whenNew(). You do not need to declare the class whose mock
//you plan on returning in case of the constructor call.
//In this case, no need to mention MyClass.class in PrepareForTest
#PrepareForTest({ClassInstantiatingObject.class})
public class ClassInstantiatingObjectTest
{
.
.
.
#Test
public void testDoSomething() throws Exception {
MyClass mockObj = PowerMockito.mock(MyClass.class);
PowerMockito.whenNew(MyClass.class).withAnyArguments().thenReturn(mockObj);
//Only way to do nothing via Powermock for a local scope object
//whose method call returns void
//PowerMockito.doNothing().when(mockObj.loadContent(null,null));
//will cause a compile time exception
PowerMockito.doNothing().when(mockObj,"loadContent",null,null);
Arg1 mockArg1 = mock(Arg1.class);
Arg2 mockArg2 = mock(Arg2.class);
StaticClass.doSomething(mockArg1, mockArg2);
}
}
The ^above code will be the solution.

Play Framework 2.X : Dealing w/ onReqest and redirect in Global.java

I'm new to Play Framework. I'm working on a basic project and i'm working now on the authentication function. I want to redirect the unauthorized user to the /login route.
I discover the Global.java class that allows me to control actions accross my project, in particular with onRequest function. I'm planning of using it to do the redirection.
I search several solutions on the web but I couldn't find a working one.
My class:
import play.*;
import play.mvc.Action;
import play.mvc.*;
import play.mvc.Http.*;
import play.mvc.Result.*;
import play.libs.F.*;
import static play.mvc.Results.*;
import play.mvc.Http.Request;
import java.lang.reflect.Method;
public class Global extends GlobalSettings {
#Override
public Action onRequest(Request request, Method actionMethod) {
//Check if the user is connected
if (request.cookie("PLAY_SESSION") == null && !request.path().startsWith("/login")) {
System.out.println("UNAUTHORIZED");
return new Action.Simple() {
#Override
public Result call(Context ctx) throws Throwable {
return redirect(controllers.routes.Application.index());
}
};
}
return super.onRequest(request, actionMethod);
}
}
I found this and i don't understand why Play! doesn't want to compile :
error: <anonymous Global$1> is not abstract and does not override abstract method call(Context) in Action
error: method does not override or implement a method from a supertype
I'm not casual with Play and i don't really understand the problem. Can someone help me please ? Thanks !
I haven't used Play Framework for a while now but I think that the problem is that in 2.2 they made Action to return Promise and not just Result. Hence there is your problem.
Check your version of Action.Simple.call() that it matches
Result call(Context ctx) throws Throwable
See the difference between
https://www.playframework.com/documentation/2.2.x/api/java/index.html
https://www.playframework.com/documentation/2.1.x/api/java/index.html
(look at the return type of the call method)
EDIT
I am not sure whether this is the best approach but it should work.
#Override
public F.Promise<Result> call(Context ctx) throws Throwable {
return F.Promise.pure(redirect(controllers.routes.Application.index()));
}
F.Promise.pure() can be used to convert a Result (or anything that implements it, like Results.Status for example) to a Promise.
Example, where ok() returns play.mvc.Results.Status:
F.Promise.pure(ok("[No Preview Available]"));

Java Bukkit Plugin "Cannot be resolved to a variable"

I've two classes under the same package Class names are "TestPlugin" and "Pokemon". The error I get is in the class TestPlugin at line 7 where there's written "New Pokemon". The error is "Cannot be resolved to a variable". I want the TestPlugin to acces the code in Pokemon so it can be used. What should I do to fix this problem? New to bukkit plugin creation so don't make the answer too advanced please. "I don't own this code/plugin. I've it for educational purposes only!". If you wonder what bukkit libary I'm using, it's the recommended build "craftbukkit-1.6.4-R2.0".
TestPlugin's code:
package com.hotmail.marrunsilkeborg.plugins.testplugin;
import org.bukkit.plugin.java.JavaPlugin;
public class TestPlugin extends JavaPlugin{
public void onEnable(){
getServer().getPluginManager().registerEvents(new Pokemon, this);
}
}
Pokemon's code:
package com.hotmail.marrunsilkeborg.plugins.testplugin;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class Pokemon implements Listener{
#EventHandler
public void onBlockPlace(BlockPlaceEvent event){
Player p = event.getPlayer();
Block bp = event.getBlockPlaced();
p.sendMessage("You've placed a " + bp.getType().toString());
}
}
Change line 7 to
this.getServer().getPluginManager().registerEvents(new Pokemon(this), this);
also think about adding a on disable
You wanted to call Pokemon's constructor, so use
new Pokemon() with the parentheses.
As #Welsar55 mentioned, use new Pokemon(this) if you are referencing your plugin in the Pokemon constructor (common-practice for Java plugins), i.e. where your Pokemon constructor is:
public Pokemon(TestPlugin myPlugin) {
this.plugin = myPlugin;
}

Categories

Resources