|
| 1 | +package com.kd8lvt.exclusionzone.init.Items.PersonaWeapons; |
| 2 | + |
| 3 | +import com.kd8lvt.exclusionzone.ExclusionZone; |
| 4 | +import com.kd8lvt.exclusionzone.init.Items.PersonaWeapons.Traits.PTrait; |
| 5 | +import com.kd8lvt.exclusionzone.init.ModItems; |
| 6 | +import net.minecraft.block.BlockState; |
| 7 | +import net.minecraft.client.item.TooltipType; |
| 8 | +import net.minecraft.component.ComponentMap; |
| 9 | +import net.minecraft.component.DataComponentTypes; |
| 10 | +import net.minecraft.entity.Entity; |
| 11 | +import net.minecraft.entity.ItemEntity; |
| 12 | +import net.minecraft.entity.LivingEntity; |
| 13 | +import net.minecraft.entity.player.PlayerEntity; |
| 14 | +import net.minecraft.item.ItemStack; |
| 15 | +import net.minecraft.item.ItemUsageContext; |
| 16 | +import net.minecraft.item.SwordItem; |
| 17 | +import net.minecraft.item.ToolMaterial; |
| 18 | +import net.minecraft.text.Text; |
| 19 | +import net.minecraft.util.ActionResult; |
| 20 | +import net.minecraft.util.Hand; |
| 21 | +import net.minecraft.util.Identifier; |
| 22 | +import net.minecraft.util.math.BlockPos; |
| 23 | +import net.minecraft.util.math.Direction; |
| 24 | +import net.minecraft.util.math.random.Random; |
| 25 | +import net.minecraft.world.World; |
| 26 | +import org.jetbrains.annotations.Nullable; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Objects; |
| 31 | +import java.util.function.Consumer; |
| 32 | + |
| 33 | +public class PersonaWeapon extends SwordItem { |
| 34 | + boolean isHeld = false; |
| 35 | + @Nullable |
| 36 | + Integer prevDamage = this.getComponents().get(DataComponentTypes.DAMAGE); |
| 37 | + public PersonaWeapon(ToolMaterial toolMaterial, Settings settings) { |
| 38 | + super(toolMaterial, settings); |
| 39 | + } |
| 40 | + |
| 41 | + public void generateTraits(ItemStack stack) { |
| 42 | + ComponentMap comps = stack.getComponents(); |
| 43 | + Random random = Random.create(); |
| 44 | + int traitsToGen = random.nextBetween(1,3); |
| 45 | + List<Identifier> comp = comps.get(ModItems.DATA_COMPONENT_PWEAPON_TRAITS); |
| 46 | + if (comp == null) comp = new ArrayList<>(); |
| 47 | + for (int i=0;i<traitsToGen;i++) { |
| 48 | + comp.add(PersonaWeaponTraits.TRAITS.keySet().toArray(new Identifier[]{})[random.nextBetween(0,PersonaWeaponTraits.TRAITS.size()-1)]); |
| 49 | + } |
| 50 | + List<Identifier> finalComp = comp; |
| 51 | + stack.apply(ModItems.DATA_COMPONENT_PWEAPON_TRAITS,comp, edit-> finalComp); |
| 52 | + } |
| 53 | + |
| 54 | + public static void applyTraits(ItemStack stack, Consumer<PTrait> consumer) { |
| 55 | + List<Identifier> traits = stack.getComponents().get(ModItems.DATA_COMPONENT_PWEAPON_TRAITS); |
| 56 | + if (traits == null) return; |
| 57 | + for (Identifier traitId : traits) { |
| 58 | + consumer.accept(PersonaWeaponTraits.TRAITS.get(traitId)); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) { |
| 64 | + boolean shouldDamageItem = super.postHit(stack, target, attacker); |
| 65 | + applyTraits(stack, trait->{ |
| 66 | + trait.onHit(stack,target,attacker); |
| 67 | + if (trait.functions.containsKey(PersonaWeaponFunctionEvents.OnHitAttacker)) { |
| 68 | + ExclusionZone.runCommand("execute as "+attacker.getUuid()+" at @s run function "+trait.functions.get(PersonaWeaponFunctionEvents.OnHitAttacker).toString()); |
| 69 | + } |
| 70 | + if (trait.functions.containsKey(PersonaWeaponFunctionEvents.OnHitVictim)) { |
| 71 | + ExclusionZone.runCommand("execute as " + target.getUuid() + " at @s run function " + trait.functions.get(PersonaWeaponFunctionEvents.OnHitVictim).toString()); |
| 72 | + } |
| 73 | + if (trait.functions.containsKey(PersonaWeaponFunctionEvents.OnEntityKilled) && target.getHealth() <= 0) { |
| 74 | + ExclusionZone.runCommand("execute as " + target.getUuid() + " at @s run function " + trait.functions.get(PersonaWeaponFunctionEvents.OnEntityKilled).toString()); |
| 75 | + } |
| 76 | + if (trait.functions.containsKey(PersonaWeaponFunctionEvents.OnDurabilityLost) && shouldDamageItem) { |
| 77 | + ExclusionZone.runCommand("execute as " + attacker.getUuid() + " at @s run function " + trait.functions.get(PersonaWeaponFunctionEvents.OnDurabilityLost).toString()); |
| 78 | + } |
| 79 | + }); |
| 80 | + return shouldDamageItem; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public ActionResult useOnBlock(ItemUsageContext context) { |
| 85 | + ActionResult ar = super.useOnBlock(context); |
| 86 | + applyTraits(context.getStack(), trait->{ |
| 87 | + trait.onUseOnBlock(context); |
| 88 | + if (trait.functions.containsKey(PersonaWeaponFunctionEvents.OnUseOnBlock)) { |
| 89 | + ExclusionZone.runCommand("execute positioned "+context.getBlockPos().getX()+" "+context.getBlockPos().getY()+" "+context.getBlockPos().getZ()+" as "+ Objects.requireNonNull(context.getPlayer()).getUuidAsString()+" run function "+trait.functions.get(PersonaWeaponFunctionEvents.OnUseOnBlock).toString()); |
| 90 | + } |
| 91 | + }); |
| 92 | + return ar; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public ActionResult useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand) { |
| 97 | + ActionResult ar = super.useOnEntity(stack, user, entity, hand); |
| 98 | + applyTraits(stack, trait->{ |
| 99 | + trait.onUseOnEntity(stack,user,entity,hand); |
| 100 | + if (trait.functions.containsKey(PersonaWeaponFunctionEvents.OnUseOnEntity)) { |
| 101 | + ExclusionZone.runCommand("execute as "+entity.getUuidAsString()+" at @s run function "+trait.functions.get(PersonaWeaponFunctionEvents.OnUseOnEntity).toString()); |
| 102 | + } |
| 103 | + }); |
| 104 | + return ar; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) { |
| 109 | + super.inventoryTick(stack, world, entity, slot, selected); |
| 110 | + if (stack.getComponents().get(ModItems.DATA_COMPONENT_PWEAPON_TRAITS) == null) generateTraits(stack); |
| 111 | + applyTraits(stack, trait->{ |
| 112 | + if (trait.functions.containsKey(PersonaWeaponFunctionEvents.OnInventoryTick)) { |
| 113 | + ExclusionZone.runCommand("execute as "+entity.getUuidAsString()+" at @s run function "+trait.functions.get(PersonaWeaponFunctionEvents.OnInventoryTick).toString()); |
| 114 | + } |
| 115 | + |
| 116 | + if (!this.isHeld && selected) trait.onHeld(stack,world,entity,slot); |
| 117 | + if (this.isHeld && !selected) trait.onUnHeld(stack,world,entity,slot); |
| 118 | + |
| 119 | + if (trait.functions.containsKey(PersonaWeaponFunctionEvents.OnHeld)) { |
| 120 | + if (!this.isHeld && selected) { |
| 121 | + ExclusionZone.runCommand("execute as " + entity.getUuidAsString() + " at @s run function " + trait.functions.get(PersonaWeaponFunctionEvents.OnHeld).toString()); |
| 122 | + this.isHeld = true; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (trait.functions.containsKey(PersonaWeaponFunctionEvents.OnUnHeld)) { |
| 127 | + if (this.isHeld && !selected) { |
| 128 | + ExclusionZone.runCommand("execute as " + entity.getUuidAsString() + " at @s run function " + trait.functions.get(PersonaWeaponFunctionEvents.OnUnHeld).toString()); |
| 129 | + this.isHeld = false; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (trait.functions.containsKey(PersonaWeaponFunctionEvents.OnDurabilityLost) && !Objects.equals(this.prevDamage, this.getComponents().get(DataComponentTypes.DAMAGE))) { |
| 134 | + ExclusionZone.runCommand("execute as " + entity.getUuidAsString() + " at @s run function " + trait.functions.get(PersonaWeaponFunctionEvents.OnDurabilityLost).toString()); |
| 135 | + } |
| 136 | + |
| 137 | + trait.inventoryTick(stack,world,entity,slot,selected); |
| 138 | + |
| 139 | + if (!Objects.equals(this.prevDamage, this.getComponents().get(DataComponentTypes.DAMAGE))) trait.onDurabilityLost(stack,world,entity,slot,selected); |
| 140 | + |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) { |
| 146 | + applyTraits(stack,trait->{ |
| 147 | + if (!trait.tt.isEmpty()) { |
| 148 | + tooltip.addAll(trait.tt); |
| 149 | + } |
| 150 | + }); |
| 151 | + super.appendTooltip(stack, context, tooltip, type); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void onItemEntityDestroyed(ItemEntity entity) { //No despawning/dying for you! |
| 156 | + ItemEntity newEntity = entity.copy(); |
| 157 | + newEntity.age = -(Integer.MAX_VALUE-1); |
| 158 | + newEntity.setNeverDespawn(); |
| 159 | + entity.getEntityWorld().spawnEntity(newEntity); |
| 160 | + if (entity.getPos().y <= entity.getEntityWorld().getDimension().minY()) { |
| 161 | + float distFell = entity.fallDistance; |
| 162 | + World world = entity.getEntityWorld(); |
| 163 | + BlockPos pos = entity.getBlockPos().offset(Direction.UP, (int) distFell); |
| 164 | + for (int x=-2;x<2;x++) { |
| 165 | + for (int y=2;y>-2;y--) { |
| 166 | + for (int z=-2;z<2;z++) { |
| 167 | + BlockPos tmpPos = pos.add(x,y,z); |
| 168 | + BlockState state = world.getBlockState(tmpPos); |
| 169 | + if (!state.getCollisionShape(world,tmpPos).isEmpty()) { |
| 170 | + newEntity.teleport(tmpPos.getX(), tmpPos.getY(), tmpPos.getZ()); |
| 171 | + break; |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + super.onItemEntityDestroyed(entity); |
| 178 | + } |
| 179 | +} |
0 commit comments