-
Notifications
You must be signed in to change notification settings - Fork 18
/
FabricShieldLib.java
127 lines (106 loc) · 5.5 KB
/
FabricShieldLib.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.github.crimsondawn45.fabricshieldlib.initializers;
import com.github.crimsondawn45.fabricshieldlib.lib.config.FabricShieldLibConfig;
import com.github.crimsondawn45.fabricshieldlib.lib.event.ShieldBlockCallback;
import com.github.crimsondawn45.fabricshieldlib.lib.event.ShieldDisabledCallback;
import com.github.crimsondawn45.fabricshieldlib.lib.object.FabricBannerShieldItem;
import com.github.crimsondawn45.fabricshieldlib.lib.object.FabricShieldDecoratorRecipe;
import com.github.crimsondawn45.fabricshieldlib.lib.object.FabricShieldEnchantment;
import com.github.crimsondawn45.fabricshieldlib.lib.object.FabricShieldItem;
import eu.midnightdust.lib.config.MidnightConfig;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Items;
import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.SpecialRecipeSerializer;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Main class for Fabric Shield Lib.
*/
public class FabricShieldLib implements ModInitializer {
/**
* Fabric Shield Lib's mod id.
*/
public static final String MOD_ID = "fabricshieldlib";
/**
* Fabric Shield Lib's logger.
*/
public static final Logger logger = LoggerFactory.getLogger(MOD_ID);
/**
* Test shield item.
*/
public static FabricBannerShieldItem fabric_banner_shield;
/**
* Test shield item that does not support banners.
*/
public static FabricShieldItem fabric_shield;
/**
* Test shield enchantment.
*/
public static FabricShieldEnchantment reflect_enchantment;
/**
* Recipe type and serializer for banner decoration recipe.
*/
public static final SpecialRecipeSerializer<FabricShieldDecoratorRecipe> FABRIC_SHIELD_DECORATION_SERIALIZER;
public static final RecipeType<FabricShieldDecoratorRecipe> FABRIC_SHIELD_DECORATION;
static {
//Registering Banner Recipe (Lib only)
FABRIC_SHIELD_DECORATION = Registry.register(Registry.RECIPE_TYPE, new Identifier(MOD_ID, "fabric_shield_decoration"), new RecipeType<FabricShieldDecoratorRecipe>() {
@Override
public String toString() {return "test_recipe";}
});
FABRIC_SHIELD_DECORATION_SERIALIZER = Registry.register(Registry.RECIPE_SERIALIZER, new Identifier(MOD_ID, "fabric_shield_decoration"), new SpecialRecipeSerializer<>(FabricShieldDecoratorRecipe::new));
}
@Override
public void onInitialize() {
//Register Config
MidnightConfig.init(MOD_ID, FabricShieldLibConfig.class);
//Dev environment code.
if(FabricLoader.getInstance().isDevelopmentEnvironment()) {
//Warn about dev code
logger.warn("FABRIC SHIELD LIB DEVELOPMENT CODE RAN!!!, if you are not in a development environment this is very bad! Test items and test enchantments will be ingame!");
//Register Custom Shield
fabric_banner_shield = Registry.register(Registry.ITEM, new Identifier(MOD_ID, "fabric_banner_shield"),
new FabricBannerShieldItem(new Item.Settings().maxDamage(336).group(ItemGroup.COMBAT), 85, 9, Items.OAK_PLANKS, Items.SPRUCE_PLANKS));
fabric_shield = Registry.register(Registry.ITEM, new Identifier(MOD_ID, "fabric_shield"),
new FabricShieldItem(new Item.Settings().maxDamage(336).group(ItemGroup.COMBAT), 100, 9, Items.OAK_PLANKS, Items.SPRUCE_PLANKS));
reflect_enchantment = Registry.register(Registry.ENCHANTMENT, new Identifier(MOD_ID, "reflect_enchantment"),
new FabricShieldEnchantment(Enchantment.Rarity.COMMON, false, false));
//Test event: makes any shield with new enchantment reflect a 1/3rd of damage back to attacker
ShieldBlockCallback.EVENT.register((defender, source, amount, hand, shield) -> {
if(reflect_enchantment.hasEnchantment(shield)) {
Entity attacker = source.getAttacker();
if(attacker.equals(null)) {
return ActionResult.CONSUME;
}
if(defender.blockedByShield(source)){
if(defender instanceof PlayerEntity) { //Defender should always be a player, but check anyway
attacker.damage(DamageSource.player((PlayerEntity) defender), Math.round(amount * 0.33F));
} else {
attacker.damage(DamageSource.mob(defender), Math.round(amount * 0.33F));
}
}
}
return ActionResult.PASS;
});
//Test Event: if your shield gets disabled, give player speed
ShieldDisabledCallback.EVENT.register((defender, hand, shield) -> {
defender.addStatusEffect(new StatusEffectInstance(StatusEffects.SPEED, 10, 1, true, false));
return ActionResult.PASS;
});
}
//Announce having finished starting up
logger.info("Fabric Shield Lib Initialized!");
}
}