This commit is contained in:
2024-07-24 09:58:43 +02:00
commit 97c2285f77
31 changed files with 1019 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
package systems.brn.plasticgun;
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import net.fabricmc.api.ModInitializer;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import systems.brn.plasticgun.bullets.BulletEntity;
import systems.brn.plasticgun.bullets.BulletItem;
import systems.brn.plasticgun.guns.Gun;
import java.util.ArrayList;
import static systems.brn.plasticgun.lib.Util.id;
public class PlasticGun implements ModInitializer {
public static final String MOD_ID = "plasticgun";
public static final ArrayList<Gun> guns = new ArrayList<>();
public static final ArrayList<BulletItem> bullets = new ArrayList<>();
public static EntityType<BulletEntity> BULLET_ENTITY_TYPE;
@Override
public void onInitialize() {
// Bullets - Batch 1 (Better Bullets First)
bullets.add(new BulletItem("357_magnum", 1.2, 357));
bullets.add(new BulletItem("32_acp_high_velocity", 0.9, 32));
bullets.add(new BulletItem("45_acp_hollow_point", 1.1, 45));
bullets.add(new BulletItem("9mm_jhp", 1.05, 9));
bullets.add(new BulletItem("38_special_p", 1.1, 38));
bullets.add(new BulletItem("762_tokarev_ap", 1.2, 762));
// 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));
// Guns
guns.add(new Gun("357_revolver", 0.5, 3, 6, 40, 357));
guns.add(new Gun("colt_1903", 0.4, 2, 8, 35, 32));
guns.add(new Gun("colt_45", 0.7, 2, 7, 45, 45));
guns.add(new Gun("colt_peacemaker", 0.6, 4, 6, 40, 45));
guns.add(new Gun("p2022", 0.5, 2, 10, 38, 9));
guns.add(new Gun("snub_nosed_revolver", 0.5, 3, 5, 35, 38));
guns.add(new Gun("tokarev_tt_33", 0.6, 2, 8, 42, 762));
BULLET_ENTITY_TYPE = Registry.register(
Registries.ENTITY_TYPE,
id("bullet"),
EntityType.Builder.<BulletEntity>create(BulletEntity::new, SpawnGroup.MISC).build()
);
PolymerEntityUtils.registerType(BULLET_ENTITY_TYPE);
PolymerResourcePackUtils.addModAssets(MOD_ID);
PolymerResourcePackUtils.markAsRequired();
}
}

View File

@@ -0,0 +1,63 @@
package systems.brn.plasticgun.bullets;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.PersistentProjectileEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import eu.pb4.polymer.core.api.entity.PolymerEntity;
import systems.brn.plasticgun.guns.Gun;
import static net.minecraft.particle.ParticleTypes.CRIT;
import static systems.brn.plasticgun.PlasticGun.BULLET_ENTITY_TYPE;
import static systems.brn.plasticgun.PlasticGun.bullets;
public class BulletEntity extends PersistentProjectileEntity implements PolymerEntity {
private final Gun gun;
public BulletEntity(Vec3d pos, ServerPlayerEntity player, ItemStack stack, ItemStack weapon, Gun gun, double damage, int speed) {
super(BULLET_ENTITY_TYPE, pos.x, pos.y + 1.5d, pos.z, player.getEntityWorld(), stack, weapon);
this.setVelocity(player, player.getPitch(), player.getYaw(), 0.0F, speed, 0);
this.pickupType = PickupPermission.CREATIVE_ONLY;
player.setPitch(player.getPitch() + 5);
this.setDamage(damage);
this.setSound(SoundEvents.ENTITY_GENERIC_EXPLODE.value());
this.gun = gun;
}
public BulletEntity(EntityType<BulletEntity> entityType, World world) {
super(entityType, world);
this.gun = null;
}
@Override
protected ItemStack getDefaultItemStack() {
if (gun != null){
return gun.ammo.getFirst().getDefaultStack();
} else {
return bullets.getFirst().getDefaultStack();
}
}
@Override
public EntityType<?> getPolymerEntityType(ServerPlayerEntity player) {
return EntityType.ARROW;
}
@Override
protected void onEntityHit(EntityHitResult entityHitResult) {
Vec3d pos = entityHitResult.getPos();
entityHitResult.getEntity().getEntityWorld().addParticle(CRIT, true, pos.x, pos.y, pos.z, 3, 0, 0);
Vec3d diff = entityHitResult.getPos();
diff.subtract(entityHitResult.getEntity().getPos());
double height = diff.y;
if (entityHitResult.getEntity() instanceof PlayerEntity && height >= 1.75 && height <= 2) {
this.setDamage(2);
}
super.onEntityHit(entityHitResult);
}
}

View File

@@ -0,0 +1,22 @@
package systems.brn.plasticgun.bullets;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import systems.brn.plasticgun.lib.SimpleItem;
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) {
super(new Settings().maxCount(99), id(path));
this.damageCoefficient = damageCoefficient;
this.caliber = caliber;
Item item = Registry.register(Registries.ITEM, this.identifier, this);
ItemGroupEvents.modifyEntriesEvent(ItemGroups.FUNCTIONAL).register(content -> content.add(item));
}
}

View File

@@ -0,0 +1,103 @@
package systems.brn.plasticgun.guns;
import eu.pb4.polymer.core.api.item.PolymerItem;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import systems.brn.plasticgun.bullets.BulletEntity;
import systems.brn.plasticgun.bullets.BulletItem;
import systems.brn.plasticgun.lib.SimpleItem;
import java.util.ArrayList;
import static systems.brn.plasticgun.PlasticGun.bullets;
import static systems.brn.plasticgun.lib.GunComponents.GUN_AMMO_COMPONENT;
import static systems.brn.plasticgun.lib.GunComponents.GUN_LOADING_COMPONENT;
import static systems.brn.plasticgun.lib.Util.*;
public class Gun extends SimpleItem implements PolymerItem {
public final double damage;
public final int reloadCount;
public final int clipSize;
public final int speed;
public final ArrayList<Item> ammo;
public final int caliber;
public Gun(String path, double damage, int reloadCount, int clipSize, int speed, int caliber) {
super(new Settings().maxCount(1).component(GUN_AMMO_COMPONENT, ItemStack.EMPTY).maxDamage(clipSize + 1), id(path));
Item item = Registry.register(Registries.ITEM, id(path), this);
ItemGroupEvents.modifyEntriesEvent(ItemGroups.COMBAT).register(content -> content.add(item));
this.damage = damage;
this.reloadCount = reloadCount;
this.clipSize = clipSize;
this.speed = speed;
ArrayList<Item> ammo = new ArrayList<>();
for (BulletItem bullet : bullets) {
if(bullet.caliber == caliber){
ammo.add(bullet);
}
}
this.ammo = ammo;
this.caliber = caliber;
}
@Override
public ActionResult useOnBlock(ItemUsageContext context) {
if (context.getPlayer() instanceof ServerPlayerEntity player) {
use(context.getWorld(), player, context.getHand());
}
return super.useOnBlock(context);
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
if (user instanceof ServerPlayerEntity player) {
ItemStack stack = user.getStackInHand(hand);
int currentReload = stack.getOrDefault(GUN_LOADING_COMPONENT, 1);
ItemStack chamber = stack.getOrDefault(GUN_AMMO_COMPONENT, ItemStack.EMPTY).copy();
int numBullets = chamber.getCount();
if (numBullets > 0) {
BulletEntity bulletEntity = new BulletEntity(user.getPos(), player, chamber, user.getStackInHand(hand), this, damage, speed);
world.spawnEntity(bulletEntity);
chamber.decrement(1);
stack.set(GUN_AMMO_COMPONENT, chamber);
} else {
ItemStack bulletStack = findBulletStack(ammo, player);
if (bulletStack != null && !bulletStack.isEmpty() && !isCreative(player)) {
if (currentReload < reloadCount) {
stack.set(GUN_LOADING_COMPONENT, currentReload + 1);
} else if (currentReload == reloadCount) {
int addedBullets = Math.min(bulletStack.getCount(), clipSize);
bulletStack.decrement(addedBullets);
ItemStack clipStack = bulletStack.copy();
clipStack.setCount(Math.min(clipStack.getCount(), clipSize));
stack.set(GUN_AMMO_COMPONENT, clipStack);
stack.set(GUN_LOADING_COMPONENT, 1);
}
}
if (player.isCreative()) {
stack.set(GUN_AMMO_COMPONENT, new ItemStack(ammo.getFirst(), clipSize));
}
}
getDefaultStack().setDamage(clipSize - numBullets);
}
return super.use(world, user, hand);
}
private static boolean isCreative(ServerPlayerEntity player) {
return player.isCreative();
}
}

View File

@@ -0,0 +1,26 @@
package systems.brn.plasticgun.lib;
import com.mojang.serialization.Codec;
import eu.pb4.polymer.core.api.other.PolymerComponent;
import net.minecraft.component.ComponentType;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import java.util.function.UnaryOperator;
public class GunComponents {
public static final ComponentType<ItemStack> GUN_AMMO_COMPONENT = register("gunammo", builder -> builder.codec(ItemStack.CODEC));
public static final ComponentType<Integer> GUN_LOADING_COMPONENT = register("gunload", builder -> builder.codec(Codec.INT));
private static <T> ComponentType<T> register(String id, UnaryOperator<ComponentType.Builder<T>> builderOperator) {
ComponentType<T> componentType = Registry.register(
Registries.DATA_COMPONENT_TYPE,
id,
builderOperator.apply(ComponentType.builder()).build()
);
PolymerComponent.registerDataComponent(componentType);
return componentType;
}
}

View File

@@ -0,0 +1,32 @@
package systems.brn.plasticgun.lib;
import eu.pb4.polymer.core.api.item.PolymerItem;
import eu.pb4.polymer.core.api.item.SimplePolymerItem;
import eu.pb4.polymer.resourcepack.api.PolymerModelData;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import net.minecraft.item.*;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
public abstract class SimpleItem extends SimplePolymerItem implements PolymerItem {
private final PolymerModelData polymerModel;
protected final Identifier identifier;
public SimpleItem(Settings settings, Identifier identifier) {
super(settings, Items.BARRIER);
this.identifier = identifier;
this.polymerModel = PolymerResourcePackUtils.requestModel(Items.BARRIER, identifier.withPath("item/" + identifier.getPath()));
}
@Override
public Item getPolymerItem(ItemStack itemStack, @Nullable ServerPlayerEntity player) {
return this.polymerModel.item();
}
@Override
public int getPolymerCustomModelData(ItemStack itemStack, @Nullable ServerPlayerEntity player) {
return this.polymerModel.value();
}
}

View File

@@ -0,0 +1,31 @@
package systems.brn.plasticgun.lib;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import java.util.ArrayList;
import static systems.brn.plasticgun.PlasticGun.MOD_ID;
public class Util {
public static Identifier id(String path) {
return Identifier.of(MOD_ID, path);
}
public static ItemStack findBulletStack(ArrayList<Item> bulletItem, ServerPlayerEntity player) {
if (bulletItem == null || bulletItem.isEmpty()) {
return ItemStack.EMPTY;
}
for (ItemStack itemStack : player.getInventory().main) {
for (Item item : bulletItem) {
if (item == itemStack.getItem()) {
return itemStack;
}
}
}
return ItemStack.EMPTY;
}
}

View File

@@ -0,0 +1,3 @@
{
"item.plasticgun.arrowhead": "Central Processing Unit"
}

View File

@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/357_revolver"
}
}

View File

@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/colt_1903"
}
}

View File

@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/colt_peacemaker"
}
}

View File

@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/p2022"
}
}

View File

@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/snub_nosed_revolver"
}
}

View File

@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/tokarev_tt_33"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 608 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 673 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 642 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 590 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 674 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 631 B

View File

@@ -0,0 +1,22 @@
{
"schemaVersion": 1,
"id": "plasticgun",
"version": "${version}",
"name": "plasticgun",
"description": "",
"authors": [],
"contact": {},
"license": "MIT",
"icon": "assets/plasticgun/icon.png",
"environment": "*",
"entrypoints": {
"main": [
"systems.brn.plasticgun.PlasticGun"
]
},
"depends": {
"fabricloader": ">=${loader_version}",
"fabric": "*",
"minecraft": "${minecraft_version}"
}
}