优化代码并调整部分逻辑

This commit is contained in:
17146 2024-05-25 19:19:20 +08:00
parent a863e9a0c2
commit a276a166b9
11 changed files with 29 additions and 98 deletions

View file

@ -10,10 +10,7 @@ import net.mcreator.target.network.message.ClientIndicatorMessage;
import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
@ -30,7 +27,6 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.network.NetworkHooks; import net.minecraftforge.network.NetworkHooks;
import net.minecraftforge.network.PacketDistributor; import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.PlayMessages;
import java.util.Optional; import java.util.Optional;
@ -38,10 +34,6 @@ import java.util.Optional;
public class BocekArrowEntity extends AbstractArrow implements ItemSupplier { public class BocekArrowEntity extends AbstractArrow implements ItemSupplier {
public static final ItemStack PROJECTILE_ITEM = new ItemStack(Items.ARROW); public static final ItemStack PROJECTILE_ITEM = new ItemStack(Items.ARROW);
public BocekArrowEntity(PlayMessages.SpawnEntity packet, Level world) {
super(TargetModEntities.BOCEK_ARROW.get(), world);
}
public BocekArrowEntity(EntityType<? extends BocekArrowEntity> type, Level world) { public BocekArrowEntity(EntityType<? extends BocekArrowEntity> type, Level world) {
super(type, world); super(type, world);
} }
@ -50,8 +42,8 @@ public class BocekArrowEntity extends AbstractArrow implements ItemSupplier {
super(type, x, y, z, world); super(type, x, y, z, world);
} }
public BocekArrowEntity(EntityType<? extends BocekArrowEntity> type, LivingEntity entity, Level world) { public BocekArrowEntity(LivingEntity entity, Level level) {
super(type, entity, world); super(TargetModEntities.BOCEK_ARROW.get(), entity, level);
} }
@Override @Override
@ -170,34 +162,4 @@ public class BocekArrowEntity extends AbstractArrow implements ItemSupplier {
} }
} }
public static BocekArrowEntity shoot(Level world, LivingEntity entity, RandomSource source) {
return shoot(world, entity, source, 1f, 5, 0);
}
public static BocekArrowEntity shoot(Level world, LivingEntity entity, RandomSource random, float power, double damage, int knockback) {
BocekArrowEntity bocekArrowEntity = new BocekArrowEntity(TargetModEntities.BOCEK_ARROW.get(), entity, world);
bocekArrowEntity.shoot(entity.getViewVector(1).x, entity.getViewVector(1).y, entity.getViewVector(1).z, power * 2, 0);
bocekArrowEntity.setSilent(true);
bocekArrowEntity.setCritArrow(false);
bocekArrowEntity.setBaseDamage(damage);
bocekArrowEntity.setKnockback(knockback);
world.addFreshEntity(bocekArrowEntity);
world.playSound(null, entity.getX(), entity.getY(), entity.getZ(), SoundEvents.ARROW_SHOOT, SoundSource.PLAYERS, 1, 1f / (random.nextFloat() * 0.5f + 1) + (power / 2));
return bocekArrowEntity;
}
public static BocekArrowEntity shoot(LivingEntity entity, LivingEntity target) {
BocekArrowEntity bocekArrowEntity = new BocekArrowEntity(TargetModEntities.BOCEK_ARROW.get(), entity, entity.level());
double dx = target.getX() - entity.getX();
double dy = target.getY() + target.getEyeHeight() - 1.1;
double dz = target.getZ() - entity.getZ();
bocekArrowEntity.shoot(dx, dy - bocekArrowEntity.getY() + Math.hypot(dx, dz) * 0.2F, dz, 1f * 2, 12.0F);
bocekArrowEntity.setSilent(true);
bocekArrowEntity.setBaseDamage(5);
bocekArrowEntity.setKnockback(5);
bocekArrowEntity.setCritArrow(false);
entity.level().addFreshEntity(bocekArrowEntity);
entity.level().playSound(null, entity.getX(), entity.getY(), entity.getZ(), SoundEvents.ARROW_SHOOT, SoundSource.PLAYERS, 1, 1f / (RandomSource.create().nextFloat() * 0.5f + 1));
return bocekArrowEntity;
}
} }

View file

@ -48,7 +48,7 @@ import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnData { public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnData {
private static final Predicate<Entity> PROJECTILE_TARGETS = input -> input != null && input.isPickable() && !input.isSpectator(); private static final Predicate<Entity> PROJECTILE_TARGETS = input -> input != null && input.isPickable() && !input.isSpectator() && input.isAlive();
private static final Predicate<BlockState> IGNORE_LEAVES = input -> input != null && input.getBlock() instanceof LeavesBlock; private static final Predicate<BlockState> IGNORE_LEAVES = input -> input != null && input.getBlock() instanceof LeavesBlock;
protected LivingEntity shooter; protected LivingEntity shooter;

View file

@ -166,13 +166,14 @@ public class Target1Entity extends PathfinderMob implements GeoEntity, AnimatedE
@SubscribeEvent @SubscribeEvent
public static void onTarget1Down(LivingDeathEvent event) { public static void onTarget1Down(LivingDeathEvent event) {
event.setCanceled(true);
var entity = event.getEntity(); var entity = event.getEntity();
var sourceEntity = event.getSource().getEntity(); var sourceEntity = event.getSource().getEntity();
if (entity == null || sourceEntity == null) return; if (entity == null || sourceEntity == null) return;
if (entity instanceof Target1Entity target1) { if (entity instanceof Target1Entity target1) {
event.setCanceled(true);
target1.setHealth(target1.getMaxHealth()); target1.setHealth(target1.getMaxHealth());
if (sourceEntity instanceof Player player) { if (sourceEntity instanceof Player player) {
@ -307,10 +308,6 @@ public class Target1Entity extends PathfinderMob implements GeoEntity, AnimatedE
@Override @Override
protected void tickDeath() { protected void tickDeath() {
++this.deathTime; ++this.deathTime;
if (this.deathTime == 114514) {
// this.remove(Target1Entity.RemovalReason.KILLED);
// this.dropExperience();
}
} }
public String getSyncedAnimation() { public String getSyncedAnimation() {

View file

@ -211,10 +211,6 @@ public class TargetEntity extends PathfinderMob implements GeoEntity, AnimatedEn
@Override @Override
protected void tickDeath() { protected void tickDeath() {
++this.deathTime; ++this.deathTime;
if (this.deathTime == 20) {
this.remove(TargetEntity.RemovalReason.KILLED);
this.dropExperience();
}
} }
@Override @Override

View file

@ -29,7 +29,7 @@ public class ClientEventHandler {
ClientLevel level = Minecraft.getInstance().level; ClientLevel level = Minecraft.getInstance().level;
Entity entity = event.getCamera().getEntity(); Entity entity = event.getCamera().getEntity();
if (level != null && entity instanceof LivingEntity living) { if (level != null && entity instanceof LivingEntity living) {
handleWeaponCrosshair(living); handleWeaponCrossHair(living);
handleWeaponSway(living); handleWeaponSway(living);
handleWeaponMove(living); handleWeaponMove(living);
handleWeaponZoom(living); handleWeaponZoom(living);
@ -40,7 +40,7 @@ public class ClientEventHandler {
} }
} }
private static void handleWeaponCrosshair(LivingEntity entity) { private static void handleWeaponCrossHair(LivingEntity entity) {
if (entity.getMainHandItem().is(TargetModTags.Items.GUN)) { if (entity.getMainHandItem().is(TargetModTags.Items.GUN)) {
float fps = Minecraft.getInstance().getFps(); float fps = Minecraft.getInstance().getFps();
if (fps <= 30) { if (fps <= 30) {
@ -48,12 +48,12 @@ public class ClientEventHandler {
} }
float times = 90f / fps; float times = 90f / fps;
var data = entity.getPersistentData(); var data = entity.getPersistentData();
double spread = entity.getAttribute(TargetModAttributes.SPREAD.get()).getBaseValue(); double spread = entity.getAttributeBaseValue(TargetModAttributes.SPREAD.get());
if (data.getDouble("crosshair") > spread){ if (data.getDouble("crosshair") > spread) {
data.putDouble("crosshair",data.getDouble("crosshair") - 0.05 *Math.pow(spread - data.getDouble("crosshair"), 2) * times); data.putDouble("crosshair", data.getDouble("crosshair") - 0.05 * Math.pow(spread - data.getDouble("crosshair"), 2) * times);
} else { } else {
data.putDouble("crosshair",data.getDouble("crosshair") + 0.05 *Math.pow(spread - data.getDouble("crosshair"), 2) * times); data.putDouble("crosshair", data.getDouble("crosshair") + 0.05 * Math.pow(spread - data.getDouble("crosshair"), 2) * times);
} }
} }
} }

View file

@ -57,7 +57,6 @@ public class PlayerEventHandler {
handlePlayerSprint(player); handlePlayerSprint(player);
handleWeaponLevel(player); handleWeaponLevel(player);
handleAmmoCount(player); handleAmmoCount(player);
handleFireTime(player);
handleGround(player); handleGround(player);
handlePrepareZoom(player); handlePrepareZoom(player);
handleSpecialWeaponAmmo(player); handleSpecialWeaponAmmo(player);
@ -169,13 +168,6 @@ public class PlayerEventHandler {
} }
} }
private static void handleFireTime(Player player) {
if (player == null) {
return;
}
}
private static void handleGround(Player player) { private static void handleGround(Player player) {
if (player.onGround()) { if (player.onGround()) {
player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {

View file

@ -38,7 +38,7 @@ public class TargetModEntities {
public static final RegistryObject<EntityType<MortarShellEntity>> MORTAR_SHELL = register("projectile_mortar_shell", public static final RegistryObject<EntityType<MortarShellEntity>> MORTAR_SHELL = register("projectile_mortar_shell",
EntityType.Builder.<MortarShellEntity>of(MortarShellEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f)); EntityType.Builder.<MortarShellEntity>of(MortarShellEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f));
public static final RegistryObject<EntityType<BocekArrowEntity>> BOCEK_ARROW = register("projectile_bocekarrow", public static final RegistryObject<EntityType<BocekArrowEntity>> BOCEK_ARROW = register("projectile_bocekarrow",
EntityType.Builder.<BocekArrowEntity>of(BocekArrowEntity::new, MobCategory.MISC).setCustomClientFactory(BocekArrowEntity::new).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f)); EntityType.Builder.<BocekArrowEntity>of(BocekArrowEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f));
public static final RegistryObject<EntityType<ProjectileEntity>> PROJECTILE = register("projectile", public static final RegistryObject<EntityType<ProjectileEntity>> PROJECTILE = register("projectile",
EntityType.Builder.<ProjectileEntity>of(ProjectileEntity::new, MobCategory.MISC).setTrackingRange(512).sized(0.5f, 0.5f)); EntityType.Builder.<ProjectileEntity>of(ProjectileEntity::new, MobCategory.MISC).setTrackingRange(512).sized(0.5f, 0.5f));

View file

@ -2,19 +2,15 @@ package net.mcreator.target.network.message;
import net.mcreator.target.entity.BocekArrowEntity; import net.mcreator.target.entity.BocekArrowEntity;
import net.mcreator.target.entity.ProjectileEntity; import net.mcreator.target.entity.ProjectileEntity;
import net.mcreator.target.init.TargetModAttributes;
import net.mcreator.target.init.TargetModEntities;
import net.mcreator.target.init.TargetModItems; import net.mcreator.target.init.TargetModItems;
import net.mcreator.target.init.TargetModSounds; import net.mcreator.target.init.TargetModSounds;
import net.mcreator.target.network.TargetModVariables; import net.mcreator.target.network.TargetModVariables;
import net.mcreator.target.procedures.PressFireProcedure; import net.mcreator.target.procedures.PressFireProcedure;
import net.mcreator.target.tools.GunsTool;
import net.mcreator.target.tools.SoundTool; import net.mcreator.target.tools.SoundTool;
import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.AbstractArrow; import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -52,7 +48,7 @@ public class FireMessage {
public static void pressAction(Player player, int type) { public static void pressAction(Player player, int type) {
Level world = player.level(); Level world = player.level();
if (!world.hasChunkAt(player.blockPosition())) { if (!world.isLoaded(player.blockPosition())) {
return; return;
} }
@ -79,11 +75,9 @@ public class FireMessage {
double power = stack.getOrCreateTag().getDouble("power"); double power = stack.getOrCreateTag().getDouble("power");
if (!player.level().isClientSide() && player.getServer() != null) { if (player instanceof ServerPlayer serverPlayer) {
player.getServer().getCommands().performPrefixedCommand(new CommandSourceStack(CommandSource.NULL, player.position(), player.getRotationVector(), (ServerLevel) player.level(), 4, SoundTool.stopSound(serverPlayer, TargetModSounds.BOCEK_PULL_1P.getId(), SoundSource.PLAYERS);
player.getName().getString(), player.getDisplayName(), player.level().getServer(), player), "stopsound @a player target:bocek_pull_1p"); SoundTool.stopSound(serverPlayer, TargetModSounds.BOCEK_PULL_3P.getId(), SoundSource.PLAYERS);
player.getServer().getCommands().performPrefixedCommand(new CommandSourceStack(CommandSource.NULL, player.position(), player.getRotationVector(), (ServerLevel) player.level(), 4,
player.getName().getString(), player.getDisplayName(), player.level().getServer(), player), "stopsound @a player target:bocek_pull_3p");
} }
if (stack.getOrCreateTag().getDouble("power") >= 6) { if (stack.getOrCreateTag().getDouble("power") >= 6) {
@ -93,8 +87,7 @@ public class FireMessage {
if (!level.isClientSide()) { if (!level.isClientSide()) {
float damage = (float) (0.02 * stack.getOrCreateTag().getDouble("damage") * (1 + 0.05 * stack.getOrCreateTag().getInt("level"))); float damage = (float) (0.02 * stack.getOrCreateTag().getDouble("damage") * (1 + 0.05 * stack.getOrCreateTag().getInt("level")));
BocekArrowEntity arrow = new BocekArrowEntity(TargetModEntities.BOCEK_ARROW.get(), level); BocekArrowEntity arrow = new BocekArrowEntity(player, level);
arrow.setOwner(player);
arrow.setBaseDamage(damage); arrow.setBaseDamage(damage);
arrow.setKnockback(0); arrow.setKnockback(0);
arrow.setSilent(true); arrow.setSilent(true);
@ -139,7 +132,7 @@ public class FireMessage {
} }
} }
public static void spawnBullet(Player player) { private static void spawnBullet(Player player) {
ItemStack heldItem = player.getMainHandItem(); ItemStack heldItem = player.getMainHandItem();
player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.recoilHorizon = Math.random() < 0.5 ? -1 : 1; capability.recoilHorizon = Math.random() < 0.5 ? -1 : 1;
@ -168,4 +161,5 @@ public class FireMessage {
projectile.damage((float) damage); projectile.damage((float) damage);
player.level().addFreshEntity(projectile); player.level().addFreshEntity(projectile);
} }
} }

View file

@ -7,7 +7,6 @@ import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.network.NetworkEvent;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -40,24 +39,21 @@ public class MortarGUIButtonMessage {
NetworkEvent.Context context = contextSupplier.get(); NetworkEvent.Context context = contextSupplier.get();
context.enqueueWork(() -> { context.enqueueWork(() -> {
Player entity = context.getSender(); Player entity = context.getSender();
if (entity == null) return;
int buttonID = message.buttonID; int buttonID = message.buttonID;
int x = message.x; int x = message.x;
int y = message.y; int y = message.y;
int z = message.z; int z = message.z;
handleButtonAction(entity, buttonID, x, y, z);
if (!entity.level().isLoaded(new BlockPos(x, y, z))) return;
handleButtonAction(entity, buttonID);
}); });
context.setPacketHandled(true); context.setPacketHandled(true);
} }
public static void handleButtonAction(Player entity, int buttonID, int x, int y, int z) {
Level world = entity.level();
// security measure to prevent arbitrary chunk generation
if (!world.hasChunkAt(new BlockPos(x, y, z)))
return;
handleButtonAction(entity, buttonID);
}
private static void handleButtonAction(Player player, int buttonID) { private static void handleButtonAction(Player player, int buttonID) {
Entity looking = TraceTool.findLookingEntity(player, 6); Entity looking = TraceTool.findLookingEntity(player, 6);
if (looking == null) return; if (looking == null) return;

View file

@ -2,15 +2,10 @@ package net.mcreator.target.tools;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import net.mcreator.target.TargetMod; import net.mcreator.target.TargetMod;
import net.mcreator.target.entity.ProjectileEntity;
import net.mcreator.target.init.TargetModAttributes;
import net.mcreator.target.init.TargetModItems;
import net.mcreator.target.network.TargetModVariables; import net.mcreator.target.network.TargetModVariables;
import net.mcreator.target.network.message.GunsDataMessage; import net.mcreator.target.network.message.GunsDataMessage;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.entity.player.PlayerEvent;
@ -92,5 +87,4 @@ public class GunsTool {
initJsonData(event.getServer().getResourceManager()); initJsonData(event.getServer().getResourceManager());
} }
} }

View file

@ -37,7 +37,7 @@ public class ParticleTool {
level.playSound(null, BlockPos.containing(x, y + 1, z), TargetModSounds.EXPLOSION_CLOSE.get(), SoundSource.BLOCKS, 8, 1); level.playSound(null, BlockPos.containing(x, y + 1, z), TargetModSounds.EXPLOSION_CLOSE.get(), SoundSource.BLOCKS, 8, 1);
level.playSound(null, BlockPos.containing(x, y + 1, z), TargetModSounds.EXPLOSION_FAR.get(), SoundSource.BLOCKS, 16, 1); level.playSound(null, BlockPos.containing(x, y + 1, z), TargetModSounds.EXPLOSION_FAR.get(), SoundSource.BLOCKS, 16, 1);
level.playSound(null, BlockPos.containing(x, y + 1, z), TargetModSounds.EXPLOSION_VERY_FAR.get(), SoundSource.BLOCKS, 32, 1); level.playSound(null, BlockPos.containing(x, y + 1, z), TargetModSounds.EXPLOSION_VERY_FAR.get(), SoundSource.BLOCKS, 32, 1);
} else { } else {
if ((level.getBlockState(BlockPos.containing(x, y, z))).getBlock() == Blocks.WATER) { if ((level.getBlockState(BlockPos.containing(x, y, z))).getBlock() == Blocks.WATER) {
level.playLocalSound(x, (y + 1), z, TargetModSounds.EXPLOSION_WATER.get(), SoundSource.BLOCKS, 3, 1, false); level.playLocalSound(x, (y + 1), z, TargetModSounds.EXPLOSION_WATER.get(), SoundSource.BLOCKS, 3, 1, false);
} }