Add new guns

This commit is contained in:
Bruno Rybársky 2024-07-25 13:18:21 +02:00
parent 67817c4ec3
commit 14b75deec0
5 changed files with 136 additions and 24 deletions

@ -36,29 +36,36 @@ public class PlasticGun implements ModInitializer {
public void onInitialize() {
// Bullets - Batch 1 (Better Bullets First)
bullets.add(new BulletItem("357_magnum", 1.4, 357));
bullets.add(new BulletItem("32_acp_high_velocity", 0.9, 32));
bullets.add(new BulletItem("45_acp_hollow_point", 1.2, 45));
bullets.add(new BulletItem("9mm_jhp", 1.05, 9));
bullets.add(new BulletItem("38_special_p", 1.3, 38));
bullets.add(new BulletItem("762_tokarev_ap", 1.2, 762));
bullets.add(new BulletItem("357_magnum", 1.4, 357, false, 0, 0));
bullets.add(new BulletItem("32_acp_high_velocity", 0.9, 32, false, 0, 0));
bullets.add(new BulletItem("45_acp_hollow_point", 1.2, 45, false, 0, 0));
bullets.add(new BulletItem("9mm_jhp", 1.05, 9, false, 0, 0));
bullets.add(new BulletItem("38_special_p", 1.3, 38, false, 0, 0));
bullets.add(new BulletItem("762_tokarev_ap", 1.2, 762, false, 0, 0));
// Bullets - Batch 2 (Standard Bullets)
bullets.add(new BulletItem("357_standard", 1, 357));
bullets.add(new BulletItem("32_acp", 0.8, 32));
bullets.add(new BulletItem("45_acp", 1, 45));
bullets.add(new BulletItem("9mm_parabellum", 0.9, 9));
bullets.add(new BulletItem("38_special", 0.95, 38));
bullets.add(new BulletItem("762_tokarev", 1.1, 762));
bullets.add(new BulletItem("357_standard", 1, 357, false, 0, 0));
bullets.add(new BulletItem("32_acp", 0.8, 32, false, 0, 0));
bullets.add(new BulletItem("45_acp", 1, 45, false, 0, 0));
bullets.add(new BulletItem("9mm_parabellum", 0.9, 9, false, 0, 0));
bullets.add(new BulletItem("38_special", 0.95, 38, false, 0, 0));
bullets.add(new BulletItem("762_tokarev", 1.1, 762, false, 0, 0));
bullets.add(new BulletItem("rpg_shell_incendiary", 1.1, 999, true, 1, 0));
bullets.add(new BulletItem("rpg_shell", 1.1, 999, false, 1, 0));
bullets.add(new BulletItem("force_container", 0, 888, false, 0, 1));
// Guns
guns.add(new Gun("357_revolver", 0.3, 3, 6, 43, 357));
guns.add(new Gun("colt_1903", 0.25, 2, 8, 38, 32));
guns.add(new Gun("colt_45", 0.4, 2, 7, 48, 45));
guns.add(new Gun("colt_peacemaker", 0.35, 4, 6, 43, 45));
guns.add(new Gun("p2022", 0.3, 2, 10, 41, 9));
guns.add(new Gun("snub_nosed_revolver", 0.3, 3, 5, 36, 38));
guns.add(new Gun("tokarev_tt_33", 0.35, 2, 8, 45, 762));
guns.add(new Gun("357_revolver", 0.3, 3, 6, 43, 357, 0, 0));
guns.add(new Gun("colt_1903", 0.25, 2, 8, 38, 32, 0, 0));
guns.add(new Gun("colt_45", 0.4, 2, 7, 48, 45, 0, 0));
guns.add(new Gun("colt_peacemaker", 0.35, 4, 6, 43, 45, 0, 0));
guns.add(new Gun("p2022", 0.3, 2, 10, 41, 9, 0, 0));
guns.add(new Gun("snub_nosed_revolver", 0.3, 3, 5, 36, 38, 0, 0));
guns.add(new Gun("tokarev_tt_33", 0.35, 2, 8, 45, 762, 0, 0));
guns.add(new Gun("rpg9", 2, 2, 8, 10, 999, 8, 0));
guns.add(new Gun("forcegun", 0, 2, 8, 10, 888, 0, 20));
BULLET_ENTITY_TYPE = Registry.register(

@ -1,6 +1,7 @@
package systems.brn.plasticgun.bullets;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.projectile.PersistentProjectileEntity;
import net.minecraft.item.ItemStack;
@ -13,17 +14,23 @@ import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import eu.pb4.polymer.core.api.entity.PolymerEntity;
import net.minecraft.world.explosion.Explosion;
import net.minecraft.world.explosion.ExplosionBehavior;
import systems.brn.plasticgun.guns.Gun;
import java.lang.reflect.Method;
import static systems.brn.plasticgun.PlasticGun.BULLET_ENTITY_TYPE;
import static systems.brn.plasticgun.PlasticGun.bullets;
import static systems.brn.plasticgun.lib.Util.applyKnockbackToEntities;
public class BulletEntity extends PersistentProjectileEntity implements PolymerEntity {
private final Gun gun;
private final double explosionPower;
private final double repulsionPower;
private final boolean isIncendiary;
public BulletEntity(Vec3d pos, ServerPlayerEntity player, ItemStack stack, ItemStack weapon, Gun gun, double damage, int speed) {
public BulletEntity(Vec3d pos, ServerPlayerEntity player, ItemStack stack, ItemStack weapon, Gun gun, double damage, int speed, double explosionPower, double repulsionPower, boolean isIncendiary) {
super(BULLET_ENTITY_TYPE, pos.x, pos.y + 1.5d, pos.z, player.getEntityWorld(), stack, weapon);
this.setOwner(player);
this.setVelocity(player, player.getPitch(), player.getYaw(), 0.0F, speed, 0);
@ -32,6 +39,9 @@ public class BulletEntity extends PersistentProjectileEntity implements PolymerE
this.setSilent(true);
this.gun = gun;
this.setCustomPierceLevel((byte) 1);
this.explosionPower = explosionPower;
this.repulsionPower = repulsionPower;
this.isIncendiary = isIncendiary;
}
public void setCustomPierceLevel(byte level) {
@ -46,6 +56,9 @@ public class BulletEntity extends PersistentProjectileEntity implements PolymerE
public BulletEntity(EntityType<BulletEntity> entityType, World world) {
super(entityType, world);
this.gun = null;
this.explosionPower = 0;
this.repulsionPower = 0;
this.isIncendiary = false;
}
@Override
@ -62,6 +75,15 @@ public class BulletEntity extends PersistentProjectileEntity implements PolymerE
return EntityType.ARROW;
}
private void hitDamage(Vec3d pos){
if(explosionPower > 0) {
getWorld().createExplosion(this, Explosion.createDamageSource(this.getWorld(), this), null, pos.getX(), pos.getY(), pos.getZ(), (float) explosionPower, isIncendiary, World.ExplosionSourceType.TNT);
}
if (repulsionPower > 0){
applyKnockbackToEntities(this, pos, repulsionPower * 100, repulsionPower);
}
}
@Override
protected void onBlockHit(BlockHitResult blockHitResult) {
if (blockHitResult.getType() == HitResult.Type.BLOCK) {
@ -75,6 +97,7 @@ public class BulletEntity extends PersistentProjectileEntity implements PolymerE
this.setOnFire(true);
super.onBlockHit(blockHitResult);
this.setOnFire(false);
hitDamage(blockHitResult.getPos());
this.discard();
}
@ -85,6 +108,7 @@ public class BulletEntity extends PersistentProjectileEntity implements PolymerE
setSilent(true);
super.onEntityHit(entityHitResult);
hitDamage(entityHitResult.getPos());
this.discard();
}

@ -13,10 +13,16 @@ import static systems.brn.plasticgun.lib.Util.id;
public class BulletItem extends SimpleItem {
public final double damageCoefficient;
public final int caliber;
public BulletItem(String path, double damageCoefficient, int caliber) {
public final boolean isIncendiary;
public final double explosionPowerCoefficient;
public final double repulsionPowerCoefficient;
public BulletItem(String path, double damageCoefficient, int caliber, boolean isIncendiary, double explosionPowerCoefficient, double repulsionPowerCoefficient) {
super(new Settings().maxCount(99), id(path), Items.STICK);
this.damageCoefficient = damageCoefficient;
this.caliber = caliber;
this.isIncendiary = isIncendiary;
this.explosionPowerCoefficient = explosionPowerCoefficient;
this.repulsionPowerCoefficient = repulsionPowerCoefficient;
Item item = Registry.register(Registries.ITEM, this.identifier, this);
ItemGroupEvents.modifyEntriesEvent(ItemGroups.FUNCTIONAL).register(content -> content.add(item));
}

@ -33,8 +33,10 @@ public class Gun extends SimpleItem implements PolymerItem {
public final int speed;
public final ArrayList<Item> ammo;
public final int caliber;
private final double explosionPowerGun;
private final double repulsionPowerGun;
public Gun(String path, double damage, int reloadCount, int clipSize, int speed, int caliber) {
public Gun(String path, double damage, int reloadCount, int clipSize, int speed, int caliber, double explosionPowerGun, double repulsionPowerGun) {
super(
new Settings()
.maxCount(1)
@ -56,6 +58,8 @@ public class Gun extends SimpleItem implements PolymerItem {
}
this.ammo = ammo;
this.caliber = caliber;
this.explosionPowerGun = explosionPowerGun;
this.repulsionPowerGun = repulsionPowerGun;
}
public void reload(World world, PlayerEntity user, Hand hand) {
@ -138,8 +142,26 @@ public class Gun extends SimpleItem implements PolymerItem {
int currentReload = stack.getOrDefault(GUN_LOADING_COMPONENT, 1);
ItemStack chamber = stack.getOrDefault(GUN_AMMO_COMPONENT, ItemStack.EMPTY).copy();
BulletItem bullet = null;
for (BulletItem bulletTemp : bullets) {
if (bulletTemp == chamber.getItem()) {
bullet = bulletTemp;
break;
}
}
if (!chamber.isEmpty() && currentReload == 1) {
BulletEntity bulletEntity = new BulletEntity(user.getPos(), player, chamber, user.getStackInHand(hand), this, damage, speed);
boolean isIncendiary = false;
double explosionPower = explosionPowerGun;
double repulsionPower = repulsionPowerGun;
if (bullet != null){
isIncendiary = bullet.isIncendiary;
explosionPower *= bullet.explosionPowerCoefficient;
repulsionPower *= bullet.repulsionPowerCoefficient;
}
BulletEntity bulletEntity = new BulletEntity(user.getPos(), player, chamber, user.getStackInHand(hand), this, damage, speed, explosionPower, repulsionPower, isIncendiary);
world.spawnEntity(bulletEntity);
world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENTITY_GENERIC_EXPLODE.value(), SoundCategory.PLAYERS, 0.1f, 1.2f);
chamber.decrement(1);

@ -1,14 +1,24 @@
package systems.brn.plasticgun.lib;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.TntEntity;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import java.util.ArrayList;
import java.util.List;
import static net.minecraft.world.explosion.Explosion.getExposure;
import static systems.brn.plasticgun.PlasticGun.MOD_ID;
public class Util {
@ -94,6 +104,49 @@ public class Util {
return;
}
}
}
public static List<Entity> getEntitiesAround(Entity entity, double radius) {
Vec3d pos = entity.getPos();
int minX = MathHelper.floor(pos.x - radius - 1.0);
int maxX = MathHelper.floor(pos.x + radius + 1.0);
int minY = MathHelper.floor(pos.y - radius - 1.0);
int maxY = MathHelper.floor(pos.y + radius + 1.0);
int minZ = MathHelper.floor(pos.z - radius - 1.0);
int maxZ = MathHelper.floor(pos.z + radius + 1.0);
Box box = new Box(minX, minY, minZ, maxX, maxY, maxZ);
return entity.getEntityWorld().getOtherEntities(entity, box);
}
public static void applyKnockbackToEntities(Entity explodingEntity, Vec3d explosionPos, double power, double radius) {
List<Entity> entities = getEntitiesAround(explodingEntity, radius);
for (Entity entity : entities) {
double distanceRatio = Math.sqrt(entity.squaredDistanceTo(explosionPos)) / power;
if (distanceRatio > 1.0) {
continue;
}
double dx = entity.getX() - explosionPos.x;
double dy = entity.getY() - explosionPos.y;
double dz = entity.getZ() - explosionPos.z;
double distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
if (distance == 0.0) {
continue;
}
dx /= distance;
dy /= distance;
dz /= distance;
double knockbackStrength = (1.0 - distanceRatio) * getExposure(explosionPos, entity);
double knockback = knockbackStrength * (1.0 - (entity instanceof LivingEntity livingEntity ? livingEntity.getAttributeValue(EntityAttributes.GENERIC_EXPLOSION_KNOCKBACK_RESISTANCE) : 0.0));
Vec3d knockbackVec = new Vec3d(dx * knockback, dy * knockback, dz * knockback);
entity.setVelocity(entity.getVelocity().add(knockbackVec));
}
}
}