Skip to content

Commit

Permalink
huge refactor of how weapon registration and holders are handled
Browse files Browse the repository at this point in the history
Signed-off-by: June <[email protected]>
  • Loading branch information
Khazoda committed Nov 5, 2024
1 parent c30a60b commit 61065f5
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 198 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/seacroak/basicweapons/BasicWeapons.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.seacroak.basicweapons;

import com.seacroak.basicweapons.registry.BWItems;
import com.seacroak.basicweapons.registry.LootTableModification;
import com.seacroak.basicweapons.registry.MainRegistry;
import com.seacroak.basicweapons.registry.WeaponRegistry;
import net.fabricmc.api.ModInitializer;

import static com.seacroak.basicweapons.Constants.BW_LOG;
Expand All @@ -11,7 +11,7 @@ public class BasicWeapons implements ModInitializer {

@Override
public void onInitialize() {
BWItems.init();
WeaponRegistry.init();
MainRegistry.init();
LootTableModification.init();
BW_LOG.info("[Basic Weapons] Armaments added");
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/seacroak/basicweapons/data/WEAPON_MATERIAL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.seacroak.basicweapons.data;

public enum WEAPON_MATERIAL {
WOOD,
STONE,
IRON,
BRONZE,
GOLD,
DIAMOND,
NETHERITE
}
10 changes: 10 additions & 0 deletions src/main/java/com/seacroak/basicweapons/data/WEAPON_TYPE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.seacroak.basicweapons.data;

public enum WEAPON_TYPE {
DAGGER,
CLUB,
HAMMER,
SPEAR,
QUARTERSTAFF,
GLAIVE
}
5 changes: 5 additions & 0 deletions src/main/java/com/seacroak/basicweapons/data/WeaponStats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.seacroak.basicweapons.data;

public record WeaponStats(WEAPON_TYPE weaponType, float damage, float speed, double range) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.minecraft.item.ToolMaterial;

public class GlaiveItem extends BasicWeaponItem {
public GlaiveItem(ToolMaterial tier, float attackDamage, float attackSpeed, Settings settings) {
public GlaiveItem(ToolMaterial tier, float attackDamage, float attackSpeed, double reach, Settings settings) {
super(tier, attackDamage, attackSpeed, settings.component(DataComponentTypes.TOOL, createToolComponent()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.minecraft.item.ToolMaterial;

public class QuarterstaffItem extends BasicWeaponItem {
public QuarterstaffItem(ToolMaterial tier, float attackDamage, float attackSpeed, Settings settings) {
public QuarterstaffItem(ToolMaterial tier, float attackDamage, float attackSpeed, double reach, Settings settings) {
super(tier, attackDamage, attackSpeed, settings.component(DataComponentTypes.TOOL, createToolComponent()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.minecraft.registry.tag.BlockTags;

public class SpearItem extends BasicWeaponSweeplessItem {
public SpearItem(ToolMaterial tier, float attackDamage, float attackSpeed, Settings settings) {
public SpearItem(ToolMaterial tier, float attackDamage, float attackSpeed, double reach, Settings settings) {
super(tier, BlockTags.SWORD_EFFICIENT, attackDamage, attackSpeed, settings.component(DataComponentTypes.TOOL, createToolComponent()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.seacroak.basicweapons.registry;
package com.seacroak.basicweapons.material;

import net.minecraft.item.ToolMaterial;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.util.Identifier;

public class BWToolMaterial {
public class BWToolMaterials {

public static final ToolMaterial BRONZE = new ToolMaterial(BlockTags.INCORRECT_FOR_IRON_TOOL, 350, 7.0F, 2.5F, 13,
TagKey.of(RegistryKeys.ITEM, Identifier.of("bronze", "bronze.item.bronze_ingot")));
Expand Down
145 changes: 0 additions & 145 deletions src/main/java/com/seacroak/basicweapons/registry/BWItems.java

This file was deleted.

76 changes: 32 additions & 44 deletions src/main/java/com/seacroak/basicweapons/registry/MainRegistry.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seacroak.basicweapons.registry;

import com.seacroak.basicweapons.item.*;
import com.seacroak.basicweapons.registry.WeaponRegistry.ItemInfo;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.item.Item;
Expand All @@ -12,10 +13,13 @@
import java.util.List;
import java.util.Map;

import static com.seacroak.basicweapons.material.BWToolMaterials.BRONZE;
import static net.minecraft.item.ToolMaterial.*;

public class MainRegistry {
public final static boolean bronze_mod_loaded = FabricLoader.getInstance().isModLoaded("bronze");

public final static Map<Integer, RegisteredItemInfo> registeredItems = new HashMap<>();
public final static Map<Integer, ItemInfo> registeredItems = new HashMap<>();
/*
These holders are for loot table creation use
to simplify selection of weapons by type or material
Expand All @@ -36,61 +40,45 @@ public class MainRegistry {
public static List<Item> netheriteWeapons = new LinkedList<>();

public static void init() {


int x = 0;
for (BWItems.UnregisteredItemInfo item : BWItems.items
) {
registeredItems.put(x, new RegisteredItemInfo(item.weaponType, item.weaponMaterial, item.name, Reggie.register(item.name, item.itemSupplier.get())));
x += 1;
for (ItemInfo itemWithInfo : WeaponRegistry.itemsWithInfo) {
registeredItems.put(x++, new ItemInfo(itemWithInfo.weaponType, itemWithInfo.weaponMaterial, itemWithInfo.name, itemWithInfo.item));
}

for (Map.Entry<Integer, RegisteredItemInfo> item : registeredItems.entrySet()) {
switch (item.getValue().weaponType) {
case DAGGER -> daggers.add((DaggerItem) item.getValue().item);
case CLUB -> clubs.add((ClubItem) item.getValue().item);
case HAMMER -> hammers.add((HammerItem) item.getValue().item);
case SPEAR -> spears.add((SpearItem) item.getValue().item);
case QUARTERSTAFF -> quarterstaves.add((QuarterstaffItem) item.getValue().item);
case GLAIVE -> glaives.add((GlaiveItem) item.getValue().item);
default -> {
break;
}
// Populate type lists
for (Map.Entry<Integer, ItemInfo> entry : registeredItems.entrySet()) {
Item item = entry.getValue().item;
switch (entry.getValue().weaponType) {
case DAGGER -> daggers.add((DaggerItem) item);
case CLUB -> clubs.add((ClubItem) item);
case HAMMER -> hammers.add((HammerItem) item);
case SPEAR -> spears.add((SpearItem) item);
case QUARTERSTAFF -> quarterstaves.add((QuarterstaffItem) item);
case GLAIVE -> glaives.add((GlaiveItem) item);
}
switch (item.getValue().weaponMaterial) {
case WOOD -> woodenWeapons.add(item.getValue().item);
case STONE -> stoneWeapons.add(item.getValue().item);
case IRON -> ironWeapons.add(item.getValue().item);
case BRONZE -> bronzeWeapons.add(item.getValue().item);
case GOLD -> goldenWeapons.add(item.getValue().item);
case DIAMOND -> diamondWeapons.add(item.getValue().item);
case NETHERITE -> netheriteWeapons.add(item.getValue().item);
default -> {
break;
}
if (entry.getValue().weaponMaterial.equals(WOOD)) {
woodenWeapons.add(item);
} else if (entry.getValue().weaponMaterial.equals(STONE)) {
stoneWeapons.add(item);
} else if (entry.getValue().weaponMaterial.equals(IRON)) {
ironWeapons.add(item);
} else if (entry.getValue().weaponMaterial.equals(BRONZE)) {
bronzeWeapons.add(item);
} else if (entry.getValue().weaponMaterial.equals(GOLD)) {
goldenWeapons.add(item);
} else if (entry.getValue().weaponMaterial.equals(DIAMOND)) {
diamondWeapons.add(item);
} else if (entry.getValue().weaponMaterial.equals(NETHERITE)) {
netheriteWeapons.add(item);
}
}

// Item group registration remains the same
ItemGroupEvents.modifyEntriesEvent(ItemGroups.COMBAT).register(content -> {
content.addAfter(Items.NETHERITE_AXE, registeredItems.get(0).item);
for (int i = 1; i < registeredItems.size(); i++) {
content.addAfter(registeredItems.get(i - 1).item, registeredItems.get(i).item);
}
});
}

public static class RegisteredItemInfo {
public final BWItems.WEAPON_TYPE weaponType;
public final BWItems.WEAPON_MATERIAL weaponMaterial;
public final String name;
public final Item item;

public RegisteredItemInfo(BWItems.WEAPON_TYPE weaponType, BWItems.WEAPON_MATERIAL weaponMaterial, String name, Item item) {
this.weaponType = weaponType;
this.weaponMaterial = weaponMaterial;
this.name = name;
this.item = item;
}
}

}
129 changes: 129 additions & 0 deletions src/main/java/com/seacroak/basicweapons/registry/WeaponRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.seacroak.basicweapons.registry;

import com.seacroak.basicweapons.data.WEAPON_TYPE;
import com.seacroak.basicweapons.item.*;
import com.seacroak.basicweapons.material.BWToolMaterials;
import com.seacroak.basicweapons.util.ID;
import com.seacroak.basicweapons.util.Reggie;
import net.minecraft.item.Item;
import net.minecraft.item.ToolMaterial;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;

import java.util.LinkedList;
import java.util.List;

import static com.seacroak.basicweapons.registry.MainRegistry.bronze_mod_loaded;

public class WeaponRegistry {
public static final float daggerDamage = 1f;
public static final float daggerSpeed = -1.6f;
public static final double daggerRange = 0;
public static final float hammerDamage = 7f;
public static final float hammerSpeed = -3.4f;
public static final double hammerRange = 0;
public static final float clubDamage = 5f;
public static final float clubSpeed = -3.0f;
public static final double clubRange = 0;
public static final float spearDamage = 2f;
public static final float spearSpeed = -2.8f;
public static final double spearRange = 2;
public static final float quarterstaffDamage = 1f;
public static final float quarterstaffSpeed = -2.3f;
public static final double quarterstaffRange = 1.25;
public static final float glaiveDamage = 5f;
public static final float glaiveSpeed = -3.2f;
public static final double glaiveRange = 1.25;

/* When referencing items, use the static fields in this class, not the itemsWithInfo list */
public static List<ItemInfo> itemsWithInfo = new LinkedList<>();
public static final Item WOODEN_DAGGER = registerWeapon("wooden_dagger", ToolMaterial.WOOD, WEAPON_TYPE.DAGGER, daggerDamage, daggerSpeed, daggerRange, DaggerItem::new);
public static final Item STONE_DAGGER = registerWeapon("stone_dagger", ToolMaterial.STONE, WEAPON_TYPE.DAGGER, daggerDamage, daggerSpeed, daggerRange, DaggerItem::new);
public static final Item IRON_DAGGER = registerWeapon("iron_dagger", ToolMaterial.IRON, WEAPON_TYPE.DAGGER, daggerDamage, daggerSpeed, daggerRange, DaggerItem::new);
public static final Item GOLDEN_DAGGER = registerWeapon("golden_dagger", ToolMaterial.GOLD, WEAPON_TYPE.DAGGER, daggerDamage - 1, daggerSpeed + 1, daggerRange, DaggerItem::new);
public static final Item DIAMOND_DAGGER = registerWeapon("diamond_dagger", ToolMaterial.DIAMOND, WEAPON_TYPE.DAGGER, daggerDamage, daggerSpeed, daggerRange, DaggerItem::new);
public static final Item NETHERITE_DAGGER = registerWeapon("netherite_dagger", ToolMaterial.NETHERITE, WEAPON_TYPE.DAGGER, daggerDamage, daggerSpeed, daggerRange, DaggerItem::new);
public static final Item WOODEN_HAMMER = registerWeapon("wooden_hammer", ToolMaterial.WOOD, WEAPON_TYPE.HAMMER, hammerDamage - 6, hammerSpeed + 0.4f, hammerRange, HammerItem::new);
public static final Item STONE_HAMMER = registerWeapon("stone_hammer", ToolMaterial.STONE, WEAPON_TYPE.HAMMER, hammerDamage - 3, hammerSpeed + 0.2f, hammerRange, HammerItem::new);
public static final Item IRON_HAMMER = registerWeapon("iron_hammer", ToolMaterial.IRON, WEAPON_TYPE.HAMMER, hammerDamage, hammerSpeed, hammerRange, HammerItem::new);
public static final Item GOLDEN_HAMMER = registerWeapon("golden_hammer", ToolMaterial.GOLD, WEAPON_TYPE.HAMMER, hammerDamage - 6, hammerSpeed + 0.6f, hammerRange, HammerItem::new);
public static final Item DIAMOND_HAMMER = registerWeapon("diamond_hammer", ToolMaterial.DIAMOND, WEAPON_TYPE.HAMMER, hammerDamage - 1, hammerSpeed + 0.1f, hammerRange, HammerItem::new);
public static final Item NETHERITE_HAMMER = registerWeapon("netherite_hammer", ToolMaterial.NETHERITE, WEAPON_TYPE.HAMMER, hammerDamage - 1, hammerSpeed + 0.2f, hammerRange, HammerItem::new);
public static final Item WOODEN_CLUB = registerWeapon("wooden_club", ToolMaterial.WOOD, WEAPON_TYPE.CLUB, clubDamage, clubSpeed, clubRange, ClubItem::new);
public static final Item STONE_CLUB = registerWeapon("stone_club", ToolMaterial.STONE, WEAPON_TYPE.CLUB, clubDamage, clubSpeed, clubRange, ClubItem::new);
public static final Item IRON_CLUB = registerWeapon("iron_club", ToolMaterial.IRON, WEAPON_TYPE.CLUB, clubDamage, clubSpeed, clubRange, ClubItem::new);
public static final Item GOLDEN_CLUB = registerWeapon("golden_club", ToolMaterial.GOLD, WEAPON_TYPE.CLUB, clubDamage, clubSpeed, clubRange, ClubItem::new);
public static final Item DIAMOND_CLUB = registerWeapon("diamond_club", ToolMaterial.DIAMOND, WEAPON_TYPE.CLUB, clubDamage, clubSpeed, clubRange, ClubItem::new);
public static final Item NETHERITE_CLUB = registerWeapon("netherite_club", ToolMaterial.NETHERITE, WEAPON_TYPE.CLUB, clubDamage, clubSpeed, clubRange, ClubItem::new);
public static final Item WOODEN_SPEAR = registerWeapon("wooden_spear", ToolMaterial.WOOD, WEAPON_TYPE.SPEAR, spearDamage, spearSpeed, spearRange, SpearItem::new);
public static final Item STONE_SPEAR = registerWeapon("stone_spear", ToolMaterial.STONE, WEAPON_TYPE.SPEAR, spearDamage, spearSpeed, spearRange, SpearItem::new);
public static final Item IRON_SPEAR = registerWeapon("iron_spear", ToolMaterial.IRON, WEAPON_TYPE.SPEAR, spearDamage, spearSpeed, spearRange, SpearItem::new);
public static final Item GOLDEN_SPEAR = registerWeapon("golden_spear", ToolMaterial.GOLD, WEAPON_TYPE.SPEAR, spearDamage, spearSpeed, spearRange, SpearItem::new);
public static final Item DIAMOND_SPEAR = registerWeapon("diamond_spear", ToolMaterial.DIAMOND, WEAPON_TYPE.SPEAR, spearDamage, spearSpeed, spearRange, SpearItem::new);
public static final Item NETHERITE_SPEAR = registerWeapon("netherite_spear", ToolMaterial.NETHERITE, WEAPON_TYPE.SPEAR, spearDamage, spearSpeed, spearRange, SpearItem::new);
public static final Item WOODEN_QUARTERSTAFF = registerWeapon("wooden_quarterstaff", ToolMaterial.WOOD, WEAPON_TYPE.QUARTERSTAFF, quarterstaffDamage, quarterstaffSpeed, quarterstaffRange, QuarterstaffItem::new);
public static final Item STONE_QUARTERSTAFF = registerWeapon("stone_quarterstaff", ToolMaterial.STONE, WEAPON_TYPE.QUARTERSTAFF, quarterstaffDamage, quarterstaffSpeed, quarterstaffRange, QuarterstaffItem::new);
public static final Item IRON_QUARTERSTAFF = registerWeapon("iron_quarterstaff", ToolMaterial.IRON, WEAPON_TYPE.QUARTERSTAFF, quarterstaffDamage, quarterstaffSpeed, quarterstaffRange, QuarterstaffItem::new);
public static final Item GOLDEN_QUARTERSTAFF = registerWeapon("golden_quarterstaff", ToolMaterial.GOLD, WEAPON_TYPE.QUARTERSTAFF, quarterstaffDamage, quarterstaffSpeed, quarterstaffRange, QuarterstaffItem::new);
public static final Item DIAMOND_QUARTERSTAFF = registerWeapon("diamond_quarterstaff", ToolMaterial.DIAMOND, WEAPON_TYPE.QUARTERSTAFF, quarterstaffDamage, quarterstaffSpeed, quarterstaffRange, QuarterstaffItem::new);
public static final Item NETHERITE_QUARTERSTAFF = registerWeapon("netherite_quarterstaff", ToolMaterial.NETHERITE, WEAPON_TYPE.QUARTERSTAFF, quarterstaffDamage, quarterstaffSpeed, quarterstaffRange, QuarterstaffItem::new);
public static final Item WOODEN_GLAIVE = registerWeapon("wooden_glaive", ToolMaterial.WOOD, WEAPON_TYPE.GLAIVE, glaiveDamage, glaiveSpeed, glaiveRange, GlaiveItem::new);
public static final Item STONE_GLAIVE = registerWeapon("stone_glaive", ToolMaterial.STONE, WEAPON_TYPE.GLAIVE, glaiveDamage, glaiveSpeed, glaiveRange, GlaiveItem::new);
public static final Item IRON_GLAIVE = registerWeapon("iron_glaive", ToolMaterial.IRON, WEAPON_TYPE.GLAIVE, glaiveDamage, glaiveSpeed, glaiveRange, GlaiveItem::new);
public static final Item GOLDEN_GLAIVE = registerWeapon("golden_glaive", ToolMaterial.GOLD, WEAPON_TYPE.GLAIVE, glaiveDamage, glaiveSpeed, glaiveRange, GlaiveItem::new);
public static final Item DIAMOND_GLAIVE = registerWeapon("diamond_glaive", ToolMaterial.DIAMOND, WEAPON_TYPE.GLAIVE, glaiveDamage, glaiveSpeed, glaiveRange, GlaiveItem::new);
public static final Item NETHERITE_GLAIVE = registerWeapon("netherite_glaive", ToolMaterial.NETHERITE, WEAPON_TYPE.GLAIVE, glaiveDamage, glaiveSpeed, glaiveRange, GlaiveItem::new);
public static final Item BRONZE_DAGGER = bronze_mod_loaded ? registerWeapon("bronze_dagger", BWToolMaterials.BRONZE, WEAPON_TYPE.DAGGER, daggerDamage, daggerSpeed, daggerRange, DaggerItem::new) : null;
public static final Item BRONZE_HAMMER = bronze_mod_loaded ? registerWeapon("bronze_hammer", BWToolMaterials.BRONZE, WEAPON_TYPE.HAMMER, hammerDamage - 0.5f, hammerSpeed + 0.1f, hammerRange, HammerItem::new) : null;
public static final Item BRONZE_CLUB = bronze_mod_loaded ? registerWeapon("bronze_club", BWToolMaterials.BRONZE, WEAPON_TYPE.CLUB, clubDamage, clubSpeed, clubRange, ClubItem::new) : null;
public static final Item BRONZE_SPEAR = bronze_mod_loaded ? registerWeapon("bronze_spear", BWToolMaterials.BRONZE, WEAPON_TYPE.SPEAR, spearDamage, spearSpeed, spearRange, SpearItem::new) : null;
public static final Item BRONZE_QUARTERSTAFF = bronze_mod_loaded ? registerWeapon("bronze_quarterstaff", BWToolMaterials.BRONZE, WEAPON_TYPE.QUARTERSTAFF, quarterstaffDamage, quarterstaffSpeed, quarterstaffRange, QuarterstaffItem::new) : null;
public static final Item BRONZE_GLAIVE = bronze_mod_loaded ? registerWeapon("bronze_glaive", BWToolMaterials.BRONZE, WEAPON_TYPE.GLAIVE, glaiveDamage, glaiveSpeed, glaiveRange, GlaiveItem::new) : null;

private static Item addItem(WEAPON_TYPE type, ToolMaterial material, String name, Item item) {
itemsWithInfo.add(new ItemInfo(type, material, name, item));
return item;
}

private static Item registerWeapon(String name, ToolMaterial material, WEAPON_TYPE type, float damage, float speed, double range, WeaponFactory<? extends Item> factory) {
return addItem(type, material, name, Reggie.register(name, (settings) -> {
Item.Settings itemSettings = attachDefaultSettings(name, type, material, damage, speed, range);
if (material == ToolMaterial.NETHERITE) itemSettings = itemSettings.fireproof();
return factory.create(material, damage, speed, range, itemSettings);
}));
}

private static Item.Settings attachDefaultSettings(String name, WEAPON_TYPE weaponType, ToolMaterial material, float damage, float speed, double range) {
Item.Settings itemSettings = new Item.Settings().registryKey(RegistryKey.of(RegistryKeys.ITEM, ID.of(name)));
switch (weaponType) {
case DAGGER, HAMMER, CLUB, SPEAR ->
itemSettings = itemSettings.attributeModifiers(BasicWeaponSweeplessItem.createAttributeModifiers(material, damage, speed, range));
case QUARTERSTAFF, GLAIVE ->
itemSettings = itemSettings.attributeModifiers(BasicWeaponItem.createAttributeModifiers(material, damage, speed, range));
}
return itemSettings;
}

public static void init() {

}

@FunctionalInterface
private interface WeaponFactory<T extends Item> {
T create(ToolMaterial material, float damage, float speed, double range, Item.Settings settings);
}

public static class ItemInfo {
public final WEAPON_TYPE weaponType;
public final ToolMaterial weaponMaterial;
public final String name;
public final Item item;

public ItemInfo(WEAPON_TYPE weaponType, ToolMaterial weaponMaterial, String name, Item item) {
this.weaponType = weaponType;
this.weaponMaterial = weaponMaterial;
this.name = name;
this.item = item;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.seacroak.basicweapons.registry;
package com.seacroak.basicweapons.util;

import com.seacroak.basicweapons.util.ID;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
Expand Down

0 comments on commit 61065f5

Please sign in to comment.