Skip to content

Commit 7a0b65e

Browse files
LiruxoChrisLane
authored andcommitted
Fix grenades in mechs and complete headshots (#1133)
* 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
1 parent 254fa35 commit 7a0b65e

File tree

16 files changed

+254
-179
lines changed

16 files changed

+254
-179
lines changed

src/main/java/com/flansmod/client/ClientProxy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ public void registerModels(ModelRegistryEvent event)
215215
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(FlansMod.paintjobTable), 0, new ModelResourceLocation("flansmod:paintjobTable", "inventory"));
216216
ModelLoader.registerItemVariants(Item.getItemFromBlock(FlansMod.paintjobTable),
217217
new ResourceLocation("flansmod:paintjobTable"));
218+
ModelLoader.setCustomModelResourceLocation(FlansMod.crosshairsymbol, 0, new ModelResourceLocation("flansmod:crosshairsymbol", "inventory"));
218219
}
219220

220221
/**

src/main/java/com/flansmod/client/ClientRenderHooks.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -921,8 +921,9 @@ private void renderKillMessages(int i, int j)
921921
{
922922
for(KillMessage killMessage : killMessages)
923923
{
924-
mc.fontRenderer.drawString("\u00a7" + killMessage.killerName + " " + "\u00a7" + killMessage.killedName,
925-
i - mc.fontRenderer.getStringWidth(killMessage.killerName + " " + killMessage.killedName) - 6,
924+
String message = "\u00a7" + killMessage.killerName + (killMessage.headshot ? " ":" ") + "\u00a7" + killMessage.killedName;
925+
mc.fontRenderer.drawString(message,
926+
i - mc.fontRenderer.getStringWidth(message) - 6,
926927
j - 32 - killMessage.line * 16, 0xffffff);
927928
}
928929

@@ -935,10 +936,14 @@ private void renderKillMessages(int i, int j)
935936
for(KillMessage killMessage : killMessages)
936937
{
937938
drawSlotInventory(mc.fontRenderer, new ItemStack(killMessage.weapon.item, 1, killMessage.paint),
938-
i - mc.fontRenderer.getStringWidth(" " + killMessage.killedName) - 12,
939+
i - mc.fontRenderer.getStringWidth((killMessage.headshot ? " ":" ") + killMessage.killedName),
940+
j - 36 - killMessage.line * 16);
941+
if (killMessage.headshot)
942+
drawSlotInventory(mc.fontRenderer, new ItemStack(FlansMod.crosshairsymbol),
943+
i - mc.fontRenderer.getStringWidth(" " + killMessage.killedName),
939944
j - 36 - killMessage.line * 16);
940945
}
941-
GL11.glDisable(3042 /*GL_BLEND*/);
946+
GL11.glDisable(GL11.GL_BLEND);
942947
RenderHelper.disableStandardItemLighting();
943948
}
944949

@@ -1022,6 +1027,6 @@ public KillMessage(boolean head, InfoType infoType, String killer, String killed
10221027
public int paint = 0;
10231028
public int timer = 0;
10241029
public int line = 0;
1025-
public boolean headshot = false;
1030+
public boolean headshot;
10261031
}
10271032
}

src/main/java/com/flansmod/common/FlansMod.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public class FlansMod
170170
public static ItemBlockManyNames spawnerItem;
171171
public static ItemOpStick opStick;
172172
public static ItemFlagpole flag;
173+
public static Item crosshairsymbol;
173174
public static ArrayList<ItemPart> partItems = new ArrayList<>();
174175
public static ArrayList<ItemMecha> mechaItems = new ArrayList<>();
175176
public static ArrayList<ItemTool> toolItems = new ArrayList<>();
@@ -240,7 +241,7 @@ public void preInit(FMLPreInitializationEvent event)
240241
paintjobTable = new BlockPaintjobTable();
241242
workbenchItem = new ItemBlockManyNames(workbench);
242243
spawnerItem = new ItemBlockManyNames(spawner);
243-
244+
crosshairsymbol = new Item().setTranslationKey("crosshairsymbol").setRegistryName("crosshairsymbol");
244245

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

351352
event.getRegistry().register(workbenchItem);
352353
event.getRegistry().register(spawnerItem);
354+
event.getRegistry().register(crosshairsymbol);
353355
}
354356

355357
@SubscribeEvent

src/main/java/com/flansmod/common/FlansModExplosion.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44
import java.util.Map;
5+
import java.util.Optional;
56
import java.util.Random;
67
import java.util.Set;
78

@@ -30,7 +31,7 @@
3031
import net.minecraft.world.World;
3132
import net.minecraftforge.event.ForgeEventFactory;
3233

33-
import com.flansmod.common.guns.EntityDamageSourceGun;
34+
import com.flansmod.common.guns.EntityDamageSourceFlan;
3435
import com.flansmod.common.teams.TeamsManager;
3536
import com.flansmod.common.types.InfoType;
3637

@@ -42,22 +43,22 @@ public class FlansModExplosion extends Explosion
4243
private final Random random;
4344
private final World world;
4445
private final double x, y, z;
45-
private final EntityLivingBase detonator;
46+
private final Optional<? extends EntityPlayer> player;
4647
private final Entity explosive;
4748
private final float size;
4849
private final List<BlockPos> affectedBlockPositions;
4950
private final Map<EntityPlayer, Vec3d> playerKnockbackMap;
5051
private final Vec3d position;
5152
private final InfoType type; // type of Flan's Mod weapon causing explosion
5253

53-
public FlansModExplosion(World world, Entity entity, EntityLivingBase detonator, InfoType type, double x, double y, double z, float size, boolean causesFire, boolean smoking, boolean breaksBlocks)
54+
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)
5455
{
5556
super(world, entity, x, y, z, size, causesFire, smoking);
5657
this.random = new Random();
5758
this.affectedBlockPositions = Lists.newArrayList();
5859
this.playerKnockbackMap = Maps.newHashMap();
5960
this.world = world;
60-
this.detonator = detonator;
61+
this.player = player;
6162
this.size = size;
6263
this.x = x;
6364
this.y = y;
@@ -168,9 +169,9 @@ public void doExplosionA()
168169
d9 /= d13;
169170
double d14 = (double)this.world.getBlockDensity(vec3d, entity.getEntityBoundingBox());
170171
double d10 = (1.0D - d12) * d14;
171-
if(detonator instanceof EntityPlayer)
172+
if(player.isPresent())
172173
{
173-
entity.attackEntityFrom(new EntityDamageSourceGun(type.shortName, explosive, (EntityPlayer)detonator, type, false),
174+
entity.attackEntityFrom(new EntityDamageSourceFlan(type.shortName, explosive, player.get(), type),
174175
(float)((int)((d10 * d10 + d10) / 2.0D * 7.0D * (double)f3 + 1.0D)));
175176
} else {
176177
entity.attackEntityFrom(DamageSource.causeExplosionDamage(this), (float)((int)((d10 * d10 + d10) / 2.0D * 7.0D * (double)f3 + 1.0D)));
@@ -275,15 +276,6 @@ public Map<EntityPlayer, Vec3d> getPlayerKnockbackMap()
275276
return this.playerKnockbackMap;
276277
}
277278

278-
/**
279-
* Returns either the entity that placed the explosive block, the entity that caused the explosion or null.
280-
*/
281-
@Override
282-
public EntityLivingBase getExplosivePlacedBy()
283-
{
284-
return detonator;
285-
}
286-
287279
@Override
288280
public void clearAffectedBlockPositions()
289281
{

src/main/java/com/flansmod/common/driveables/mechas/EntityMecha.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.flansmod.common.driveables.mechas;
22

33
import java.util.ArrayList;
4+
import java.util.Optional;
45

56
import io.netty.buffer.ByteBuf;
67
import net.minecraft.block.Block;
78
import net.minecraft.block.material.Material;
89
import net.minecraft.block.state.IBlockState;
9-
import net.minecraft.client.Minecraft;
1010
import net.minecraft.entity.Entity;
11-
import net.minecraft.entity.EntityLiving;
1211
import net.minecraft.entity.EntityLivingBase;
1312
import net.minecraft.entity.MoverType;
1413
import net.minecraft.entity.item.EntityItem;
@@ -46,20 +45,21 @@
4645
import com.flansmod.common.driveables.EntitySeat;
4746
import com.flansmod.common.driveables.EnumDriveablePart;
4847
import com.flansmod.common.guns.BulletType;
48+
import com.flansmod.common.guns.EntityGrenade;
4949
import com.flansmod.common.guns.EnumFireMode;
5050
import com.flansmod.common.guns.FireableGun;
5151
import com.flansmod.common.guns.FiredShot;
52+
import com.flansmod.common.guns.GrenadeType;
5253
import com.flansmod.common.guns.GunType;
5354
import com.flansmod.common.guns.InventoryHelper;
5455
import com.flansmod.common.guns.ItemBullet;
56+
import com.flansmod.common.guns.ItemGrenade;
5557
import com.flansmod.common.guns.ItemGun;
5658
import com.flansmod.common.guns.ItemShootable;
5759
import com.flansmod.common.guns.ShootableType;
5860
import com.flansmod.common.guns.ShotHandler;
5961
import com.flansmod.common.network.PacketDriveableDamage;
6062
import com.flansmod.common.network.PacketDriveableGUI;
61-
import com.flansmod.common.network.PacketDriveableKey;
62-
import com.flansmod.common.network.PacketGunAnimation;
6363
import com.flansmod.common.network.PacketMechaControl;
6464
import com.flansmod.common.network.PacketPlaySound;
6565
import com.flansmod.common.teams.TeamsManager;
@@ -335,7 +335,7 @@ else if(heldItem instanceof ItemGun)
335335
gunItem.Reload(heldStack, world, this, driveableData, left ? EnumHand.MAIN_HAND : EnumHand.OFF_HAND, true, true, (infiniteAmmo() || creative()));
336336
}
337337
//A bullet stack was found, so try shooting with it
338-
else if(bulletStack.getItem() instanceof ItemBullet)
338+
else if(bulletStack.getItem() instanceof ItemBullet || bulletStack.getItem() instanceof ItemGrenade)
339339
{
340340
//Shoot
341341
shoot(heldStack, gunType, bulletStack, creative(), left);
@@ -391,10 +391,22 @@ private void shoot(ItemStack stack, GunType gunType, ItemStack bulletStack, bool
391391
if(!world.isRemote)
392392
{
393393
ShootableType shootableType = ((ItemShootable)bulletStack.getItem()).type;
394-
FireableGun fireableGun = new FireableGun(gunType, gunType.getDamage(stack), gunType.getSpread(stack), gunType.getBulletSpeed(stack));
395-
//TODO unchecked cast, grenades will cause an error
396-
FiredShot shot = new FiredShot(fireableGun, (BulletType)shootableType, this, (EntityPlayerMP) getDriver());
397-
ShotHandler.fireGun(world, shot, gunType.numBullets*bulletType.numBullets, bulletOrigin, armVector);
394+
if (shootableType instanceof BulletType)
395+
{
396+
FireableGun fireableGun = new FireableGun(gunType, gunType.getDamage(stack), gunType.getSpread(stack), gunType.getBulletSpeed(stack));
397+
FiredShot shot = new FiredShot(fireableGun, (BulletType)shootableType, this, (EntityPlayerMP) getDriver());
398+
ShotHandler.fireGun(world, shot, gunType.numBullets*bulletType.numBullets, bulletOrigin, armVector);
399+
}
400+
else if (shootableType instanceof GrenadeType)
401+
{
402+
double yaw = Math.atan2(armVector.z, armVector.x);
403+
double pitch = Math.atan2(Math.sqrt(armVector.z * armVector.z + armVector.x * armVector.x), armVector.y) - Math.PI/2;
404+
Optional<Entity> ent = Optional.of(this);
405+
Optional<EntityPlayer> player = Optional.of(getDriver());
406+
407+
EntityGrenade grenade = new EntityGrenade(world, bulletOrigin, (GrenadeType) shootableType, (float)Math.toDegrees(pitch), (float)Math.toDegrees(yaw + Math.PI*1.5), player, ent);
408+
world.spawnEntity(grenade);
409+
}
398410
}
399411

400412
if(left)

src/main/java/com/flansmod/common/eventhandlers/PlayerDeathEventListener.java

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
import com.flansmod.common.FlansMod;
1010
import com.flansmod.common.PlayerHandler;
11-
import com.flansmod.common.guns.EntityBullet;
12-
import com.flansmod.common.guns.EntityGrenade;
11+
import com.flansmod.common.guns.EntityDamageSourceFlan;
1312
import com.flansmod.common.network.PacketKillMessage;
1413
import com.flansmod.common.teams.Team;
1514

@@ -24,49 +23,18 @@ public PlayerDeathEventListener()
2423
@SubscribeEvent
2524
public void PlayerDied(LivingDeathEvent event)
2625
{
27-
if((event.getSource().getDamageType().equalsIgnoreCase("explosion") &&
28-
((event.getSource().getTrueSource() instanceof EntityGrenade) || (event.getSource().getTrueSource() instanceof EntityBullet)))
29-
&& event.getEntityLiving() instanceof EntityPlayer)
26+
if (event.getEntity().world.isRemote)
27+
return;
28+
29+
if (event.getSource() instanceof EntityDamageSourceFlan && event.getEntity() instanceof EntityPlayer)
3030
{
31-
boolean isGrenade;
32-
if(event.getSource().getTrueSource() instanceof EntityGrenade)
33-
{
34-
isGrenade = true;
35-
}
36-
else
37-
{
38-
isGrenade = false;
39-
}
40-
EntityPlayer killer = null;
41-
EntityPlayer killed = (EntityPlayer)event.getEntityLiving();
42-
Team killerTeam = null;
43-
Team killedTeam = null;
44-
if(isGrenade)
45-
{
46-
killer = (EntityPlayer)((EntityGrenade)event.getSource().getTrueSource()).thrower;
47-
}
48-
else
49-
{
50-
killer = (EntityPlayer)((EntityBullet)event.getSource().getTrueSource()).getFiredShot().getPlayerOptional().orElse(null);
51-
}
52-
killerTeam = PlayerHandler.getPlayerData(killer).team;
53-
killedTeam = PlayerHandler.getPlayerData(killed).team;
54-
if(event.getEntityLiving() instanceof EntityPlayer && !isGrenade)
55-
{
56-
FlansMod.getPacketHandler().sendToDimension(
57-
new PacketKillMessage(false, ((EntityBullet)event.getSource().getTrueSource()).getFiredShot().getBulletType(),
58-
(killedTeam == null ? "f" : killedTeam.textColour) + event.getEntity().getDisplayName().getFormattedText(),
59-
(killerTeam == null ? "f" : killedTeam.textColour) + event.getSource().getTrueSource().getDisplayName().getFormattedText()),
60-
event.getEntityLiving().dimension);
61-
}
62-
if(event.getEntityLiving() instanceof EntityPlayer && isGrenade)
63-
{
64-
FlansMod.getPacketHandler().sendToDimension(
65-
new PacketKillMessage(false, ((EntityGrenade)event.getSource().getTrueSource()).type,
66-
(killedTeam == null ? "f" : killedTeam.textColour) + event.getEntity().getDisplayName().getFormattedText(),
67-
(killerTeam == null ? "f" : killedTeam.textColour) + event.getSource().getTrueSource().getDisplayName().getFormattedText()),
68-
event.getEntityLiving().dimension);
69-
}
31+
EntityDamageSourceFlan source = (EntityDamageSourceFlan) event.getSource();
32+
EntityPlayer died = (EntityPlayer) event.getEntity();
33+
34+
Team killedTeam = PlayerHandler.getPlayerData(died).team;
35+
Team killerTeam = PlayerHandler.getPlayerData(source.getCausedPlayer()).team;
36+
37+
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);
7038
}
7139
}
7240
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.flansmod.common.guns;
2+
3+
import com.flansmod.common.PlayerHandler;
4+
import com.flansmod.common.types.InfoType;
5+
6+
import net.minecraft.entity.Entity;
7+
import net.minecraft.entity.EntityLivingBase;
8+
import net.minecraft.entity.player.EntityPlayer;
9+
import net.minecraft.util.EntityDamageSourceIndirect;
10+
import net.minecraft.util.text.ITextComponent;
11+
import net.minecraft.util.text.TextComponentString;
12+
13+
public class EntityDamageSourceFlan extends EntityDamageSourceIndirect{
14+
15+
private InfoType weapon;
16+
private EntityPlayer shooter;
17+
private boolean headshot;
18+
/**
19+
* @param s Name of the damage source (Usually the shortName of the gun)
20+
* @param entity The Entity causing the damage (e.g. Grenade). Can be the same as 'player'
21+
* @param player The Player responsible for the damage
22+
* @param wep The InfoType of weapon used
23+
*/
24+
public EntityDamageSourceFlan(String s, Entity entity, EntityPlayer player, InfoType wep)
25+
{
26+
this(s, entity, player, wep, false);
27+
}
28+
29+
/**
30+
* @param s Name of the damage source (Usually the shortName of the gun)
31+
* @param entity The Entity causing the damage (e.g. Grenade). Can be the same as 'player'
32+
* @param player The Player responsible for the damage
33+
* @param wep The InfoType of weapon used
34+
* @param headshot True if this was a headshot, false if not
35+
*/
36+
public EntityDamageSourceFlan(String s, Entity entity, EntityPlayer player, InfoType wep, boolean headshot)
37+
{
38+
super(s, entity, player);
39+
weapon = wep;
40+
shooter = player;
41+
this.headshot = headshot;
42+
}
43+
44+
@Override
45+
public ITextComponent getDeathMessage(EntityLivingBase living)
46+
{
47+
if(!(living instanceof EntityPlayer) || shooter == null || PlayerHandler.getPlayerData(shooter) == null)
48+
{
49+
return super.getDeathMessage(living);
50+
}
51+
52+
return new TextComponentString("#flansmod");
53+
}
54+
55+
/**
56+
* @return The weapon (InfoType) used to cause this damage
57+
*/
58+
public InfoType getWeapon()
59+
{
60+
return weapon;
61+
}
62+
63+
/**
64+
* @return The Player responsible for this damage
65+
*/
66+
public EntityPlayer getCausedPlayer()
67+
{
68+
return shooter;
69+
}
70+
71+
/**
72+
* @return True if this is a headshot, false if not
73+
*/
74+
public boolean isHeadshot()
75+
{
76+
return headshot;
77+
}
78+
}

0 commit comments

Comments
 (0)