Skip to content

Commit

Permalink
Fix grenades in mechs and complete headshots (#1133)
Browse files Browse the repository at this point in the history
* Fixed Duplicate Death Message

* Cleanup

* Merge

* Initial Grenades Refactor Commit

* NBT Saving for grenades

* Crash fix

* Cleanup

* Review implementation changes

* Display Headshot Kill & Cleanup

* Replace null check with Optional check
  • Loading branch information
Liruxo authored and ChrisLane committed Oct 6, 2019
1 parent 254fa35 commit 7a0b65e
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 179 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/flansmod/client/ClientProxy.java
Expand Up @@ -215,6 +215,7 @@ public void registerModels(ModelRegistryEvent event)
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(FlansMod.paintjobTable), 0, new ModelResourceLocation("flansmod:paintjobTable", "inventory"));
ModelLoader.registerItemVariants(Item.getItemFromBlock(FlansMod.paintjobTable),
new ResourceLocation("flansmod:paintjobTable"));
ModelLoader.setCustomModelResourceLocation(FlansMod.crosshairsymbol, 0, new ModelResourceLocation("flansmod:crosshairsymbol", "inventory"));
}

/**
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/com/flansmod/client/ClientRenderHooks.java
Expand Up @@ -921,8 +921,9 @@ private void renderKillMessages(int i, int j)
{
for(KillMessage killMessage : killMessages)
{
mc.fontRenderer.drawString("\u00a7" + killMessage.killerName + " " + "\u00a7" + killMessage.killedName,
i - mc.fontRenderer.getStringWidth(killMessage.killerName + " " + killMessage.killedName) - 6,
String message = "\u00a7" + killMessage.killerName + (killMessage.headshot ? " ":" ") + "\u00a7" + killMessage.killedName;
mc.fontRenderer.drawString(message,
i - mc.fontRenderer.getStringWidth(message) - 6,
j - 32 - killMessage.line * 16, 0xffffff);
}

Expand All @@ -935,10 +936,14 @@ private void renderKillMessages(int i, int j)
for(KillMessage killMessage : killMessages)
{
drawSlotInventory(mc.fontRenderer, new ItemStack(killMessage.weapon.item, 1, killMessage.paint),
i - mc.fontRenderer.getStringWidth(" " + killMessage.killedName) - 12,
i - mc.fontRenderer.getStringWidth((killMessage.headshot ? " ":" ") + killMessage.killedName),
j - 36 - killMessage.line * 16);
if (killMessage.headshot)
drawSlotInventory(mc.fontRenderer, new ItemStack(FlansMod.crosshairsymbol),
i - mc.fontRenderer.getStringWidth(" " + killMessage.killedName),
j - 36 - killMessage.line * 16);
}
GL11.glDisable(3042 /*GL_BLEND*/);
GL11.glDisable(GL11.GL_BLEND);
RenderHelper.disableStandardItemLighting();
}

Expand Down Expand Up @@ -1022,6 +1027,6 @@ public KillMessage(boolean head, InfoType infoType, String killer, String killed
public int paint = 0;
public int timer = 0;
public int line = 0;
public boolean headshot = false;
public boolean headshot;
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/flansmod/common/FlansMod.java
Expand Up @@ -170,6 +170,7 @@ public class FlansMod
public static ItemBlockManyNames spawnerItem;
public static ItemOpStick opStick;
public static ItemFlagpole flag;
public static Item crosshairsymbol;
public static ArrayList<ItemPart> partItems = new ArrayList<>();
public static ArrayList<ItemMecha> mechaItems = new ArrayList<>();
public static ArrayList<ItemTool> toolItems = new ArrayList<>();
Expand Down Expand Up @@ -240,7 +241,7 @@ public void preInit(FMLPreInitializationEvent event)
paintjobTable = new BlockPaintjobTable();
workbenchItem = new ItemBlockManyNames(workbench);
spawnerItem = new ItemBlockManyNames(spawner);

crosshairsymbol = new Item().setTranslationKey("crosshairsymbol").setRegistryName("crosshairsymbol");

GameRegistry.registerTileEntity(TileEntitySpawner.class, new ResourceLocation("flansmod:teamsSpawner"));
GameRegistry.registerTileEntity(TileEntityPaintjobTable.class, new ResourceLocation("flansmod:paintjobTable"));
Expand Down Expand Up @@ -350,6 +351,7 @@ public void registerItems(RegistryEvent.Register<Item> event)

event.getRegistry().register(workbenchItem);
event.getRegistry().register(spawnerItem);
event.getRegistry().register(crosshairsymbol);
}

@SubscribeEvent
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/com/flansmod/common/FlansModExplosion.java
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;

Expand Down Expand Up @@ -30,7 +31,7 @@
import net.minecraft.world.World;
import net.minecraftforge.event.ForgeEventFactory;

import com.flansmod.common.guns.EntityDamageSourceGun;
import com.flansmod.common.guns.EntityDamageSourceFlan;
import com.flansmod.common.teams.TeamsManager;
import com.flansmod.common.types.InfoType;

Expand All @@ -42,22 +43,22 @@ public class FlansModExplosion extends Explosion
private final Random random;
private final World world;
private final double x, y, z;
private final EntityLivingBase detonator;
private final Optional<? extends EntityPlayer> player;
private final Entity explosive;
private final float size;
private final List<BlockPos> affectedBlockPositions;
private final Map<EntityPlayer, Vec3d> playerKnockbackMap;
private final Vec3d position;
private final InfoType type; // type of Flan's Mod weapon causing explosion

public FlansModExplosion(World world, Entity entity, EntityLivingBase detonator, InfoType type, double x, double y, double z, float size, boolean causesFire, boolean smoking, boolean breaksBlocks)
public FlansModExplosion(World world, Entity entity, Optional<? extends EntityPlayer> player, InfoType type, double x, double y, double z, float size, boolean causesFire, boolean smoking, boolean breaksBlocks)
{
super(world, entity, x, y, z, size, causesFire, smoking);
this.random = new Random();
this.affectedBlockPositions = Lists.newArrayList();
this.playerKnockbackMap = Maps.newHashMap();
this.world = world;
this.detonator = detonator;
this.player = player;
this.size = size;
this.x = x;
this.y = y;
Expand Down Expand Up @@ -168,9 +169,9 @@ public void doExplosionA()
d9 /= d13;
double d14 = (double)this.world.getBlockDensity(vec3d, entity.getEntityBoundingBox());
double d10 = (1.0D - d12) * d14;
if(detonator instanceof EntityPlayer)
if(player.isPresent())
{
entity.attackEntityFrom(new EntityDamageSourceGun(type.shortName, explosive, (EntityPlayer)detonator, type, false),
entity.attackEntityFrom(new EntityDamageSourceFlan(type.shortName, explosive, player.get(), type),
(float)((int)((d10 * d10 + d10) / 2.0D * 7.0D * (double)f3 + 1.0D)));
} else {
entity.attackEntityFrom(DamageSource.causeExplosionDamage(this), (float)((int)((d10 * d10 + d10) / 2.0D * 7.0D * (double)f3 + 1.0D)));
Expand Down Expand Up @@ -275,15 +276,6 @@ public Map<EntityPlayer, Vec3d> getPlayerKnockbackMap()
return this.playerKnockbackMap;
}

/**
* Returns either the entity that placed the explosive block, the entity that caused the explosion or null.
*/
@Override
public EntityLivingBase getExplosivePlacedBy()
{
return detonator;
}

@Override
public void clearAffectedBlockPositions()
{
Expand Down
@@ -1,14 +1,13 @@
package com.flansmod.common.driveables.mechas;

import java.util.ArrayList;
import java.util.Optional;

import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.MoverType;
import net.minecraft.entity.item.EntityItem;
Expand Down Expand Up @@ -46,20 +45,21 @@
import com.flansmod.common.driveables.EntitySeat;
import com.flansmod.common.driveables.EnumDriveablePart;
import com.flansmod.common.guns.BulletType;
import com.flansmod.common.guns.EntityGrenade;
import com.flansmod.common.guns.EnumFireMode;
import com.flansmod.common.guns.FireableGun;
import com.flansmod.common.guns.FiredShot;
import com.flansmod.common.guns.GrenadeType;
import com.flansmod.common.guns.GunType;
import com.flansmod.common.guns.InventoryHelper;
import com.flansmod.common.guns.ItemBullet;
import com.flansmod.common.guns.ItemGrenade;
import com.flansmod.common.guns.ItemGun;
import com.flansmod.common.guns.ItemShootable;
import com.flansmod.common.guns.ShootableType;
import com.flansmod.common.guns.ShotHandler;
import com.flansmod.common.network.PacketDriveableDamage;
import com.flansmod.common.network.PacketDriveableGUI;
import com.flansmod.common.network.PacketDriveableKey;
import com.flansmod.common.network.PacketGunAnimation;
import com.flansmod.common.network.PacketMechaControl;
import com.flansmod.common.network.PacketPlaySound;
import com.flansmod.common.teams.TeamsManager;
Expand Down Expand Up @@ -335,7 +335,7 @@ else if(heldItem instanceof ItemGun)
gunItem.Reload(heldStack, world, this, driveableData, left ? EnumHand.MAIN_HAND : EnumHand.OFF_HAND, true, true, (infiniteAmmo() || creative()));
}
//A bullet stack was found, so try shooting with it
else if(bulletStack.getItem() instanceof ItemBullet)
else if(bulletStack.getItem() instanceof ItemBullet || bulletStack.getItem() instanceof ItemGrenade)
{
//Shoot
shoot(heldStack, gunType, bulletStack, creative(), left);
Expand Down Expand Up @@ -391,10 +391,22 @@ private void shoot(ItemStack stack, GunType gunType, ItemStack bulletStack, bool
if(!world.isRemote)
{
ShootableType shootableType = ((ItemShootable)bulletStack.getItem()).type;
FireableGun fireableGun = new FireableGun(gunType, gunType.getDamage(stack), gunType.getSpread(stack), gunType.getBulletSpeed(stack));
//TODO unchecked cast, grenades will cause an error
FiredShot shot = new FiredShot(fireableGun, (BulletType)shootableType, this, (EntityPlayerMP) getDriver());
ShotHandler.fireGun(world, shot, gunType.numBullets*bulletType.numBullets, bulletOrigin, armVector);
if (shootableType instanceof BulletType)
{
FireableGun fireableGun = new FireableGun(gunType, gunType.getDamage(stack), gunType.getSpread(stack), gunType.getBulletSpeed(stack));
FiredShot shot = new FiredShot(fireableGun, (BulletType)shootableType, this, (EntityPlayerMP) getDriver());
ShotHandler.fireGun(world, shot, gunType.numBullets*bulletType.numBullets, bulletOrigin, armVector);
}
else if (shootableType instanceof GrenadeType)
{
double yaw = Math.atan2(armVector.z, armVector.x);
double pitch = Math.atan2(Math.sqrt(armVector.z * armVector.z + armVector.x * armVector.x), armVector.y) - Math.PI/2;
Optional<Entity> ent = Optional.of(this);
Optional<EntityPlayer> player = Optional.of(getDriver());

EntityGrenade grenade = new EntityGrenade(world, bulletOrigin, (GrenadeType) shootableType, (float)Math.toDegrees(pitch), (float)Math.toDegrees(yaw + Math.PI*1.5), player, ent);
world.spawnEntity(grenade);
}
}

if(left)
Expand Down
Expand Up @@ -8,8 +8,7 @@

import com.flansmod.common.FlansMod;
import com.flansmod.common.PlayerHandler;
import com.flansmod.common.guns.EntityBullet;
import com.flansmod.common.guns.EntityGrenade;
import com.flansmod.common.guns.EntityDamageSourceFlan;
import com.flansmod.common.network.PacketKillMessage;
import com.flansmod.common.teams.Team;

Expand All @@ -24,49 +23,18 @@ public PlayerDeathEventListener()
@SubscribeEvent
public void PlayerDied(LivingDeathEvent event)
{
if((event.getSource().getDamageType().equalsIgnoreCase("explosion") &&
((event.getSource().getTrueSource() instanceof EntityGrenade) || (event.getSource().getTrueSource() instanceof EntityBullet)))
&& event.getEntityLiving() instanceof EntityPlayer)
if (event.getEntity().world.isRemote)
return;

if (event.getSource() instanceof EntityDamageSourceFlan && event.getEntity() instanceof EntityPlayer)
{
boolean isGrenade;
if(event.getSource().getTrueSource() instanceof EntityGrenade)
{
isGrenade = true;
}
else
{
isGrenade = false;
}
EntityPlayer killer = null;
EntityPlayer killed = (EntityPlayer)event.getEntityLiving();
Team killerTeam = null;
Team killedTeam = null;
if(isGrenade)
{
killer = (EntityPlayer)((EntityGrenade)event.getSource().getTrueSource()).thrower;
}
else
{
killer = (EntityPlayer)((EntityBullet)event.getSource().getTrueSource()).getFiredShot().getPlayerOptional().orElse(null);
}
killerTeam = PlayerHandler.getPlayerData(killer).team;
killedTeam = PlayerHandler.getPlayerData(killed).team;
if(event.getEntityLiving() instanceof EntityPlayer && !isGrenade)
{
FlansMod.getPacketHandler().sendToDimension(
new PacketKillMessage(false, ((EntityBullet)event.getSource().getTrueSource()).getFiredShot().getBulletType(),
(killedTeam == null ? "f" : killedTeam.textColour) + event.getEntity().getDisplayName().getFormattedText(),
(killerTeam == null ? "f" : killedTeam.textColour) + event.getSource().getTrueSource().getDisplayName().getFormattedText()),
event.getEntityLiving().dimension);
}
if(event.getEntityLiving() instanceof EntityPlayer && isGrenade)
{
FlansMod.getPacketHandler().sendToDimension(
new PacketKillMessage(false, ((EntityGrenade)event.getSource().getTrueSource()).type,
(killedTeam == null ? "f" : killedTeam.textColour) + event.getEntity().getDisplayName().getFormattedText(),
(killerTeam == null ? "f" : killedTeam.textColour) + event.getSource().getTrueSource().getDisplayName().getFormattedText()),
event.getEntityLiving().dimension);
}
EntityDamageSourceFlan source = (EntityDamageSourceFlan) event.getSource();
EntityPlayer died = (EntityPlayer) event.getEntity();

Team killedTeam = PlayerHandler.getPlayerData(died).team;
Team killerTeam = PlayerHandler.getPlayerData(source.getCausedPlayer()).team;

FlansMod.getPacketHandler().sendToDimension(new PacketKillMessage(source.isHeadshot(), source.getWeapon(), (killedTeam == null ? "f" : killedTeam.textColour) + died.getName(), (killerTeam == null ? "f" : killerTeam.textColour) + source.getCausedPlayer().getName()), died.dimension);
}
}
}
78 changes: 78 additions & 0 deletions src/main/java/com/flansmod/common/guns/EntityDamageSourceFlan.java
@@ -0,0 +1,78 @@
package com.flansmod.common.guns;

import com.flansmod.common.PlayerHandler;
import com.flansmod.common.types.InfoType;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.EntityDamageSourceIndirect;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;

public class EntityDamageSourceFlan extends EntityDamageSourceIndirect{

private InfoType weapon;
private EntityPlayer shooter;
private boolean headshot;
/**
* @param s Name of the damage source (Usually the shortName of the gun)
* @param entity The Entity causing the damage (e.g. Grenade). Can be the same as 'player'
* @param player The Player responsible for the damage
* @param wep The InfoType of weapon used
*/
public EntityDamageSourceFlan(String s, Entity entity, EntityPlayer player, InfoType wep)
{
this(s, entity, player, wep, false);
}

/**
* @param s Name of the damage source (Usually the shortName of the gun)
* @param entity The Entity causing the damage (e.g. Grenade). Can be the same as 'player'
* @param player The Player responsible for the damage
* @param wep The InfoType of weapon used
* @param headshot True if this was a headshot, false if not
*/
public EntityDamageSourceFlan(String s, Entity entity, EntityPlayer player, InfoType wep, boolean headshot)
{
super(s, entity, player);
weapon = wep;
shooter = player;
this.headshot = headshot;
}

@Override
public ITextComponent getDeathMessage(EntityLivingBase living)
{
if(!(living instanceof EntityPlayer) || shooter == null || PlayerHandler.getPlayerData(shooter) == null)
{
return super.getDeathMessage(living);
}

return new TextComponentString("#flansmod");
}

/**
* @return The weapon (InfoType) used to cause this damage
*/
public InfoType getWeapon()
{
return weapon;
}

/**
* @return The Player responsible for this damage
*/
public EntityPlayer getCausedPlayer()
{
return shooter;
}

/**
* @return True if this is a headshot, false if not
*/
public boolean isHeadshot()
{
return headshot;
}
}

0 comments on commit 7a0b65e

Please sign in to comment.