-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added reward for damaging entities (#537)
- Loading branch information
1 parent
959ac32
commit bb4c156
Showing
7 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
.../main/java/com/microsoft/Malmo/MissionHandlers/RewardForDamagingEntityImplementation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.microsoft.Malmo.MissionHandlers; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.util.DamageSource; | ||
import net.minecraftforge.common.MinecraftForge; | ||
import net.minecraftforge.event.entity.living.LivingAttackEvent; | ||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | ||
|
||
import com.microsoft.Malmo.MissionHandlerInterfaces.IRewardProducer; | ||
import com.microsoft.Malmo.Schemas.EntityTypes; | ||
import com.microsoft.Malmo.Schemas.MissionInit; | ||
import com.microsoft.Malmo.Schemas.MobWithReward; | ||
import com.microsoft.Malmo.Schemas.RewardForDamagingEntity; | ||
|
||
public class RewardForDamagingEntityImplementation extends RewardBase implements IRewardProducer | ||
{ | ||
RewardForDamagingEntity params; | ||
Map<MobWithReward, Float> damages = new HashMap<MobWithReward, Float>(); | ||
|
||
@Override | ||
public boolean parseParameters(Object params) | ||
{ | ||
super.parseParameters(params); | ||
if (params == null || !(params instanceof RewardForDamagingEntity)) | ||
return false; | ||
|
||
this.params = (RewardForDamagingEntity) params; | ||
return true; | ||
} | ||
|
||
@Override | ||
public void getReward(MissionInit missionInit, MultidimensionalReward reward) | ||
{ | ||
super.getReward(missionInit, reward); | ||
if (missionInit == null || Minecraft.getMinecraft().thePlayer == null) | ||
return; | ||
synchronized (this.damages) | ||
{ | ||
for (MobWithReward mob : this.damages.keySet()) | ||
{ | ||
float damage_amount = this.damages.get(mob); | ||
float damage_reward = damage_amount * mob.getReward().floatValue(); | ||
float adjusted_reward = adjustAndDistributeReward(damage_reward, this.params.getDimension(), mob.getDistribution()); | ||
reward.add(this.params.getDimension(), adjusted_reward); | ||
} | ||
this.damages.clear(); | ||
} | ||
} | ||
|
||
@Override | ||
public void prepare(MissionInit missionInit) | ||
{ | ||
super.prepare(missionInit); | ||
MinecraftForge.EVENT_BUS.register(this); | ||
} | ||
|
||
@Override | ||
public void cleanup() | ||
{ | ||
super.cleanup(); | ||
MinecraftForge.EVENT_BUS.unregister(this); | ||
} | ||
|
||
@SubscribeEvent | ||
public void onLivingAttackEvent(LivingAttackEvent event) | ||
{ | ||
if (event.entity == null || event.source.getEntity() != Minecraft.getMinecraft().thePlayer) | ||
return; | ||
synchronized (this.damages) | ||
{ | ||
for (MobWithReward mob : this.params.getMob()) | ||
{ | ||
// Have we caught one of these mobs? | ||
for (EntityTypes et : mob.getType()) | ||
{ | ||
String mobName = et.value(); | ||
if (event.entity.getName().equals(mobName)) | ||
{ | ||
if (this.damages.containsKey(mob)) | ||
this.damages.put(mob, this.damages.get(mob) + event.ammount); | ||
else | ||
this.damages.put(mob, event.ammount); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters