From 57eb7e5442d74b77480908751e436256a726495d Mon Sep 17 00:00:00 2001 From: Jenny Date: Tue, 11 Feb 2025 16:52:16 +0100 Subject: [PATCH] code improvements, fix claymoreArrow cash, faster claymoreArrow despawn --- gradle.properties | 2 +- .../EnhancedExplosives.java | 6 +- .../datagen/ModBlockStateProvider.java | 27 +-- .../datagen/ModItemModelProvider.java | 6 +- .../entities/arrows/baseArrow.java | 198 +----------------- .../entities/arrows/claymoreArrow.java | 12 +- .../entities/arrows/tunnelArrow.java | 6 +- .../entities/tnt/repulsivePrimedTNT.java | 3 +- 8 files changed, 40 insertions(+), 220 deletions(-) diff --git a/gradle.properties b/gradle.properties index 9c228fb..bc0f8e1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -38,7 +38,7 @@ mod_name=Enhanced Explosives # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=All Rights Reserved # The mod version. See https://semver.org/ -mod_version=0.12.0 +mod_version=0.12.1 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/com/jenny/enhancedexplosives/EnhancedExplosives.java b/src/main/java/com/jenny/enhancedexplosives/EnhancedExplosives.java index c8a46d8..d4c86c0 100644 --- a/src/main/java/com/jenny/enhancedexplosives/EnhancedExplosives.java +++ b/src/main/java/com/jenny/enhancedexplosives/EnhancedExplosives.java @@ -1,11 +1,11 @@ package com.jenny.enhancedexplosives; import com.jenny.enhancedexplosives.blocks.blocks; -import com.jenny.enhancedexplosives.config.ConfigCommon; -import com.jenny.enhancedexplosives.particles.particles; import com.jenny.enhancedexplosives.config.ConfigClient; +import com.jenny.enhancedexplosives.config.ConfigCommon; import com.jenny.enhancedexplosives.entities.entities; import com.jenny.enhancedexplosives.items.items; +import com.jenny.enhancedexplosives.particles.particles; import com.mojang.logging.LogUtils; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.common.MinecraftForge; @@ -27,6 +27,7 @@ public class EnhancedExplosives { public static final String MODID = "enhancedexplosives"; private static final Logger LOGGER = LogUtils.getLogger(); + @SuppressWarnings("removal") public EnhancedExplosives() { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); @@ -58,6 +59,7 @@ public class EnhancedExplosives { @Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) public static class ClientModEvents { + @SuppressWarnings("removal") @SubscribeEvent public static void onClientSetup(FMLClientSetupEvent event) { ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, ConfigClient.SPEC); diff --git a/src/main/java/com/jenny/enhancedexplosives/datagen/ModBlockStateProvider.java b/src/main/java/com/jenny/enhancedexplosives/datagen/ModBlockStateProvider.java index 1d3cdb6..67e8a39 100644 --- a/src/main/java/com/jenny/enhancedexplosives/datagen/ModBlockStateProvider.java +++ b/src/main/java/com/jenny/enhancedexplosives/datagen/ModBlockStateProvider.java @@ -1,16 +1,19 @@ package com.jenny.enhancedexplosives.datagen; import com.jenny.enhancedexplosives.blocks.blocks; - import net.minecraft.data.PackOutput; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.level.block.*; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; import net.minecraftforge.client.model.generators.BlockStateProvider; import net.minecraftforge.client.model.generators.ConfiguredModel; +import net.minecraftforge.client.model.generators.ModelFile; import net.minecraftforge.common.data.ExistingFileHelper; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegistryObject; -import net.minecraftforge.client.model.generators.ModelFile; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; import static com.jenny.enhancedexplosives.EnhancedExplosives.MODID; @@ -37,20 +40,20 @@ public class ModBlockStateProvider extends BlockStateProvider { SideOnlyTNT(blocks.TNT_HOMING); } - private void blockWithItem(RegistryObject blockRegistryObject) { + private void blockWithItem(@NotNull RegistryObject blockRegistryObject) { simpleBlockWithItem(blockRegistryObject.get(), cubeAll(blockRegistryObject.get())); } - private void SideTop(RegistryObject blockRegistryObject) { + private void SideTop(@NotNull RegistryObject blockRegistryObject) { simpleBlockWithItem(blockRegistryObject.get(), topSide(blockRegistryObject.get())); } - private void blockItem(RegistryObject blockRegistryObject) { + private void blockItem(@NotNull RegistryObject blockRegistryObject) { simpleBlockItem(blockRegistryObject.get(), new ModelFile.UncheckedModelFile(MODID + - ":block/" + ForgeRegistries.BLOCKS.getKey(blockRegistryObject.get()).getPath())); + ":block/" + Objects.requireNonNull(ForgeRegistries.BLOCKS.getKey(blockRegistryObject.get())).getPath())); } - private void topBottom2Sides(RegistryObject blockRegistryObject) { + private void topBottom2Sides(@NotNull RegistryObject blockRegistryObject) { simpleBlockWithItem(blockRegistryObject.get(), northEastTopBottom(blockRegistryObject.get())); } @@ -58,22 +61,22 @@ public class ModBlockStateProvider extends BlockStateProvider { return ForgeRegistries.BLOCKS.getKey(block); } - private String name(Block block) { + private @NotNull String name(Block block) { return key(block).getPath(); } - private ResourceLocation extend(ResourceLocation rl, String suffix) { + private ResourceLocation extend(@NotNull ResourceLocation rl, String suffix) { return new ResourceLocation(rl.getNamespace(), rl.getPath() + suffix); } - public void sideTopBottom(RegistryObject blockRegistryObject) { + public void sideTopBottom(@NotNull RegistryObject blockRegistryObject) { Block block = blockRegistryObject.get(); ModelFile model = models().cubeBottomTop(name(block), extend(blockTexture(block), "_side"), extend(blockTexture(block), "_bottom"), extend(blockTexture(block), "_top")); this.getVariantBuilder(block).forAllStates(blockState -> ConfiguredModel.builder().modelFile(model).build()); simpleBlockItem(block, model); } - public void SideOnlyTNT(RegistryObject blockRegistryObject) { + public void SideOnlyTNT(@NotNull RegistryObject blockRegistryObject) { Block block = blockRegistryObject.get(); ModelFile model = models().cubeBottomTop(name(block), extend(blockTexture(block), "_side"), extend(blockTexture(Blocks.TNT), "_bottom"), extend(blockTexture(Blocks.TNT), "_top")); this.getVariantBuilder(block).forAllStates(blockState -> ConfiguredModel.builder().modelFile(model).build()); diff --git a/src/main/java/com/jenny/enhancedexplosives/datagen/ModItemModelProvider.java b/src/main/java/com/jenny/enhancedexplosives/datagen/ModItemModelProvider.java index 2821bf6..c6d87a1 100644 --- a/src/main/java/com/jenny/enhancedexplosives/datagen/ModItemModelProvider.java +++ b/src/main/java/com/jenny/enhancedexplosives/datagen/ModItemModelProvider.java @@ -1,11 +1,9 @@ package com.jenny.enhancedexplosives.datagen; import com.jenny.enhancedexplosives.items.items; - import net.minecraft.data.PackOutput; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; -import net.minecraftforge.client.model.generators.ItemModelBuilder; import net.minecraftforge.client.model.generators.ItemModelProvider; import net.minecraftforge.common.data.ExistingFileHelper; import net.minecraftforge.registries.RegistryObject; @@ -25,8 +23,8 @@ public class ModItemModelProvider extends ItemModelProvider { simpleItem(items.TUNNEL_ARROW); } - private ItemModelBuilder simpleItem(RegistryObject item) { - return withExistingParent(item.getId().getPath(), + private void simpleItem(RegistryObject item) { + withExistingParent(item.getId().getPath(), new ResourceLocation("item/generated")).texture("layer0", new ResourceLocation(MODID,"item/" + item.getId().getPath())); } diff --git a/src/main/java/com/jenny/enhancedexplosives/entities/arrows/baseArrow.java b/src/main/java/com/jenny/enhancedexplosives/entities/arrows/baseArrow.java index cfef4e6..a9d86e1 100644 --- a/src/main/java/com/jenny/enhancedexplosives/entities/arrows/baseArrow.java +++ b/src/main/java/com/jenny/enhancedexplosives/entities/arrows/baseArrow.java @@ -1,38 +1,14 @@ package com.jenny.enhancedexplosives.entities.arrows; -import com.google.common.collect.Sets; -import net.minecraft.core.particles.ParticleTypes; -import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.network.syncher.EntityDataAccessor; -import net.minecraft.network.syncher.EntityDataSerializers; -import net.minecraft.network.syncher.SynchedEntityData; -import net.minecraft.world.effect.MobEffectInstance; -import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.projectile.AbstractArrow; -import net.minecraft.world.entity.projectile.Arrow; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; -import net.minecraft.world.item.alchemy.Potion; -import net.minecraft.world.item.alchemy.PotionUtils; -import net.minecraft.world.item.alchemy.Potions; import net.minecraft.world.level.Level; import org.jetbrains.annotations.NotNull; -import java.util.Collection; -import java.util.Set; - public class baseArrow extends AbstractArrow { - private static final int EXPOSED_POTION_DECAY_TIME = 600; - private static final int NO_EFFECT_COLOR = -1; - private static final EntityDataAccessor ID_EFFECT_COLOR = SynchedEntityData.defineId(Arrow.class, EntityDataSerializers.INT); - private static final byte EVENT_POTION_PUFF = 0; - private Potion potion = Potions.EMPTY; - private final Set effects = Sets.newHashSet(); - private boolean fixedColor; private int tick = 0; public baseArrow(EntityType pEntityType, Level pLevel) { @@ -44,190 +20,22 @@ public class baseArrow extends AbstractArrow { } public baseArrow(Level pLevel, LivingEntity pShooter, EntityType pEntityType) { - super(pEntityType, pShooter, pLevel); - } - - public void setEffectsFromItem(ItemStack pStack) { - if (pStack.is(Items.TIPPED_ARROW)) { - this.potion = PotionUtils.getPotion(pStack); - Collection collection = PotionUtils.getCustomEffects(pStack); - if (!collection.isEmpty()) { - for(MobEffectInstance mobeffectinstance : collection) { - this.effects.add(new MobEffectInstance(mobeffectinstance)); - } - } - - int i = getCustomColor(pStack); - if (i == -1) { - this.updateColor(); - } else { - this.setFixedColor(i); - } - } else if (pStack.is(Items.ARROW)) { - this.potion = Potions.EMPTY; - this.effects.clear(); - this.entityData.set(ID_EFFECT_COLOR, -1); - } - - } - - public static int getCustomColor(ItemStack pStack) { - CompoundTag compoundtag = pStack.getTag(); - return compoundtag != null && compoundtag.contains("CustomPotionColor", 99) ? compoundtag.getInt("CustomPotionColor") : -1; - } - - private void updateColor() { - this.fixedColor = false; - if (this.potion == Potions.EMPTY && this.effects.isEmpty()) { - this.entityData.set(ID_EFFECT_COLOR, -1); - } else { - this.entityData.set(ID_EFFECT_COLOR, PotionUtils.getColor(PotionUtils.getAllEffects(this.potion, this.effects))); - } - - } - - public void addEffect(MobEffectInstance pEffectInstance) { - this.effects.add(pEffectInstance); - this.getEntityData().set(ID_EFFECT_COLOR, PotionUtils.getColor(PotionUtils.getAllEffects(this.potion, this.effects))); - } - - protected void defineSynchedData() { - super.defineSynchedData(); - this.entityData.define(ID_EFFECT_COLOR, -1); + super(pEntityType, pLevel); } public void tick() { super.tick(); if (this.level().isClientSide) { - if (this.inGround) { - if (this.inGroundTime % 5 == 0) { - this.makeParticle(1); - } - } else { + if (!this.inGround) { spawnParticles(0); } this.tick++; - } else if (this.inGround && this.inGroundTime != 0 && !this.effects.isEmpty() && this.inGroundTime >= 600) { - this.level().broadcastEntityEvent(this, (byte)0); - this.potion = Potions.EMPTY; - this.effects.clear(); - this.entityData.set(ID_EFFECT_COLOR, -1); } } - private void makeParticle(int pParticleAmount) { - int i = this.getColor(); - if (i != -1 && pParticleAmount > 0) { - double d0 = (double)(i >> 16 & 255) / 255.0D; - double d1 = (double)(i >> 8 & 255) / 255.0D; - double d2 = (double)(i >> 0 & 255) / 255.0D; - - for(int j = 0; j < pParticleAmount; ++j) { - this.level().addParticle(ParticleTypes.ENTITY_EFFECT, this.getRandomX(0.5D), this.getRandomY(), this.getRandomZ(0.5D), d0, d1, d2); - } - - } - } - - public int getColor() { - return this.entityData.get(ID_EFFECT_COLOR); - } - - private void setFixedColor(int pFixedColor) { - this.fixedColor = true; - this.entityData.set(ID_EFFECT_COLOR, pFixedColor); - } - - public void addAdditionalSaveData(@NotNull CompoundTag pCompound) { - super.addAdditionalSaveData(pCompound); - if (this.potion != Potions.EMPTY) { - pCompound.putString("Potion", BuiltInRegistries.POTION.getKey(this.potion).toString()); - } - - if (this.fixedColor) { - pCompound.putInt("Color", this.getColor()); - } - - if (!this.effects.isEmpty()) { - ListTag listtag = new ListTag(); - - for(MobEffectInstance mobeffectinstance : this.effects) { - listtag.add(mobeffectinstance.save(new CompoundTag())); - } - - pCompound.put("CustomPotionEffects", listtag); - } - - } - - public void readAdditionalSaveData(@NotNull CompoundTag pCompound) { - super.readAdditionalSaveData(pCompound); - if (pCompound.contains("Potion", 8)) { - this.potion = PotionUtils.getPotion(pCompound); - } - - for(MobEffectInstance mobeffectinstance : PotionUtils.getCustomEffects(pCompound)) { - this.addEffect(mobeffectinstance); - } - - if (pCompound.contains("Color", 99)) { - this.setFixedColor(pCompound.getInt("Color")); - } else { - this.updateColor(); - } - - } - - protected void doPostHurtEffects(@NotNull LivingEntity pLiving) { - super.doPostHurtEffects(pLiving); - Entity entity = this.getEffectSource(); - - for(MobEffectInstance mobeffectinstance : this.potion.getEffects()) { - pLiving.addEffect(new MobEffectInstance(mobeffectinstance.getEffect(), Math.max(mobeffectinstance.mapDuration((p_268168_) -> { - return p_268168_ / 8; - }), 1), mobeffectinstance.getAmplifier(), mobeffectinstance.isAmbient(), mobeffectinstance.isVisible()), entity); - } - - if (!this.effects.isEmpty()) { - for(MobEffectInstance mobeffectinstance1 : this.effects) { - pLiving.addEffect(mobeffectinstance1, entity); - } - } - - } - @NotNull protected ItemStack getPickupItem() { - if (this.effects.isEmpty() && this.potion == Potions.EMPTY) { - return new ItemStack(Items.ARROW); - } else { - ItemStack itemstack = new ItemStack(Items.TIPPED_ARROW); - PotionUtils.setPotion(itemstack, this.potion); - PotionUtils.setCustomEffects(itemstack, this.effects); - if (this.fixedColor) { - itemstack.getOrCreateTag().putInt("CustomPotionColor", this.getColor()); - } - - return itemstack; - } - } - - public void handleEntityEvent(byte pId) { - if (pId == 0) { - int i = this.getColor(); - if (i != -1) { - double d0 = (double)(i >> 16 & 255) / 255.0D; - double d1 = (double)(i >> 8 & 255) / 255.0D; - double d2 = (double)(i >> 0 & 255) / 255.0D; - - for(int j = 0; j < 20; ++j) { - this.level().addParticle(ParticleTypes.ENTITY_EFFECT, this.getRandomX(0.5D), this.getRandomY(), this.getRandomZ(0.5D), d0, d1, d2); - } - } - } else { - super.handleEntityEvent(pId); - } - + return new ItemStack(Items.ARROW); } public void spawnParticles(float partialTicks) { diff --git a/src/main/java/com/jenny/enhancedexplosives/entities/arrows/claymoreArrow.java b/src/main/java/com/jenny/enhancedexplosives/entities/arrows/claymoreArrow.java index d8015a4..97780d8 100644 --- a/src/main/java/com/jenny/enhancedexplosives/entities/arrows/claymoreArrow.java +++ b/src/main/java/com/jenny/enhancedexplosives/entities/arrows/claymoreArrow.java @@ -6,12 +6,14 @@ import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; - import org.jetbrains.annotations.NotNull; public class claymoreArrow extends baseArrow{ + private final int r = level().isClientSide ? 0 : level().getRandom().nextInt(0, 20); + public claymoreArrow(EntityType pEntityType, Level pLevel) { super(pEntityType, pLevel); + } public claymoreArrow(@NotNull Level pLevel, @NotNull LivingEntity pShooter) { @@ -23,6 +25,14 @@ public class claymoreArrow extends baseArrow{ pTarget.hurt(damageSources().mobProjectile(this, (LivingEntity) getOwner()), 15); } + @Override + public void tick() { + super.tick(); + if (!level().isClientSide && this.inGroundTime > 20 + r) { + discard(); + } + } + @Override @NotNull protected ItemStack getPickupItem() { diff --git a/src/main/java/com/jenny/enhancedexplosives/entities/arrows/tunnelArrow.java b/src/main/java/com/jenny/enhancedexplosives/entities/arrows/tunnelArrow.java index 1105408..42fd70d 100644 --- a/src/main/java/com/jenny/enhancedexplosives/entities/arrows/tunnelArrow.java +++ b/src/main/java/com/jenny/enhancedexplosives/entities/arrows/tunnelArrow.java @@ -11,9 +11,9 @@ import net.minecraft.world.phys.Vec3; import org.jetbrains.annotations.NotNull; public class tunnelArrow extends baseArrow{ - protected static int explosionCount = 12; - protected static int spacing = 2; - protected static float power = 8; + protected static final int explosionCount = 12; + protected static final int spacing = 2; + protected static final float power = 8; public tunnelArrow(EntityType pEntityType, Level pLevel) { super(pEntityType, pLevel); diff --git a/src/main/java/com/jenny/enhancedexplosives/entities/tnt/repulsivePrimedTNT.java b/src/main/java/com/jenny/enhancedexplosives/entities/tnt/repulsivePrimedTNT.java index eeedb6a..09d0355 100644 --- a/src/main/java/com/jenny/enhancedexplosives/entities/tnt/repulsivePrimedTNT.java +++ b/src/main/java/com/jenny/enhancedexplosives/entities/tnt/repulsivePrimedTNT.java @@ -34,8 +34,7 @@ public class repulsivePrimedTNT extends basePrimedTNT { double dist = getTargetDist(target); float speed = getSpeed(); Vec3 mult = new Vec3(speed / dist, speed / dist, speed / dist); - Vec3 ret = target.position().subtract(this.position()).normalize().multiply(mult); - return ret; + return target.position().subtract(this.position()).normalize().multiply(mult); } public double getTargetDist(Entity target) {