优化代码并调整部分逻辑
This commit is contained in:
parent
a863e9a0c2
commit
a276a166b9
11 changed files with 29 additions and 98 deletions
|
@ -10,10 +10,7 @@ import net.mcreator.target.network.message.ClientIndicatorMessage;
|
|||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
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.RandomSource;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
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.network.NetworkHooks;
|
||||
import net.minecraftforge.network.PacketDistributor;
|
||||
import net.minecraftforge.network.PlayMessages;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
@ -38,10 +34,6 @@ import java.util.Optional;
|
|||
public class BocekArrowEntity extends AbstractArrow implements ItemSupplier {
|
||||
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) {
|
||||
super(type, world);
|
||||
}
|
||||
|
@ -50,8 +42,8 @@ public class BocekArrowEntity extends AbstractArrow implements ItemSupplier {
|
|||
super(type, x, y, z, world);
|
||||
}
|
||||
|
||||
public BocekArrowEntity(EntityType<? extends BocekArrowEntity> type, LivingEntity entity, Level world) {
|
||||
super(type, entity, world);
|
||||
public BocekArrowEntity(LivingEntity entity, Level level) {
|
||||
super(TargetModEntities.BOCEK_ARROW.get(), entity, level);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ import java.util.function.Function;
|
|||
import java.util.function.Predicate;
|
||||
|
||||
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;
|
||||
protected LivingEntity shooter;
|
||||
|
|
|
@ -166,13 +166,14 @@ public class Target1Entity extends PathfinderMob implements GeoEntity, AnimatedE
|
|||
|
||||
@SubscribeEvent
|
||||
public static void onTarget1Down(LivingDeathEvent event) {
|
||||
event.setCanceled(true);
|
||||
var entity = event.getEntity();
|
||||
var sourceEntity = event.getSource().getEntity();
|
||||
|
||||
if (entity == null || sourceEntity == null) return;
|
||||
|
||||
if (entity instanceof Target1Entity target1) {
|
||||
event.setCanceled(true);
|
||||
|
||||
target1.setHealth(target1.getMaxHealth());
|
||||
|
||||
if (sourceEntity instanceof Player player) {
|
||||
|
@ -307,10 +308,6 @@ public class Target1Entity extends PathfinderMob implements GeoEntity, AnimatedE
|
|||
@Override
|
||||
protected void tickDeath() {
|
||||
++this.deathTime;
|
||||
if (this.deathTime == 114514) {
|
||||
// this.remove(Target1Entity.RemovalReason.KILLED);
|
||||
// this.dropExperience();
|
||||
}
|
||||
}
|
||||
|
||||
public String getSyncedAnimation() {
|
||||
|
|
|
@ -211,10 +211,6 @@ public class TargetEntity extends PathfinderMob implements GeoEntity, AnimatedEn
|
|||
@Override
|
||||
protected void tickDeath() {
|
||||
++this.deathTime;
|
||||
if (this.deathTime == 20) {
|
||||
this.remove(TargetEntity.RemovalReason.KILLED);
|
||||
this.dropExperience();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -29,7 +29,7 @@ public class ClientEventHandler {
|
|||
ClientLevel level = Minecraft.getInstance().level;
|
||||
Entity entity = event.getCamera().getEntity();
|
||||
if (level != null && entity instanceof LivingEntity living) {
|
||||
handleWeaponCrosshair(living);
|
||||
handleWeaponCrossHair(living);
|
||||
handleWeaponSway(living);
|
||||
handleWeaponMove(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)) {
|
||||
float fps = Minecraft.getInstance().getFps();
|
||||
if (fps <= 30) {
|
||||
|
@ -48,7 +48,7 @@ public class ClientEventHandler {
|
|||
}
|
||||
float times = 90f / fps;
|
||||
var data = entity.getPersistentData();
|
||||
double spread = entity.getAttribute(TargetModAttributes.SPREAD.get()).getBaseValue();
|
||||
double spread = entity.getAttributeBaseValue(TargetModAttributes.SPREAD.get());
|
||||
|
||||
if (data.getDouble("crosshair") > spread) {
|
||||
data.putDouble("crosshair", data.getDouble("crosshair") - 0.05 * Math.pow(spread - data.getDouble("crosshair"), 2) * times);
|
||||
|
|
|
@ -57,7 +57,6 @@ public class PlayerEventHandler {
|
|||
handlePlayerSprint(player);
|
||||
handleWeaponLevel(player);
|
||||
handleAmmoCount(player);
|
||||
handleFireTime(player);
|
||||
handleGround(player);
|
||||
handlePrepareZoom(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) {
|
||||
if (player.onGround()) {
|
||||
player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
|
||||
|
|
|
@ -38,7 +38,7 @@ public class TargetModEntities {
|
|||
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));
|
||||
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",
|
||||
EntityType.Builder.<ProjectileEntity>of(ProjectileEntity::new, MobCategory.MISC).setTrackingRange(512).sized(0.5f, 0.5f));
|
||||
|
||||
|
|
|
@ -2,19 +2,15 @@ package net.mcreator.target.network.message;
|
|||
|
||||
import net.mcreator.target.entity.BocekArrowEntity;
|
||||
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.TargetModSounds;
|
||||
import net.mcreator.target.network.TargetModVariables;
|
||||
import net.mcreator.target.procedures.PressFireProcedure;
|
||||
import net.mcreator.target.tools.GunsTool;
|
||||
import net.mcreator.target.tools.SoundTool;
|
||||
import net.minecraft.commands.CommandSource;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
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.projectile.AbstractArrow;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
@ -52,7 +48,7 @@ public class FireMessage {
|
|||
public static void pressAction(Player player, int type) {
|
||||
Level world = player.level();
|
||||
|
||||
if (!world.hasChunkAt(player.blockPosition())) {
|
||||
if (!world.isLoaded(player.blockPosition())) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -79,11 +75,9 @@ public class FireMessage {
|
|||
|
||||
double power = stack.getOrCreateTag().getDouble("power");
|
||||
|
||||
if (!player.level().isClientSide() && player.getServer() != null) {
|
||||
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_1p");
|
||||
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 (player instanceof ServerPlayer serverPlayer) {
|
||||
SoundTool.stopSound(serverPlayer, TargetModSounds.BOCEK_PULL_1P.getId(), SoundSource.PLAYERS);
|
||||
SoundTool.stopSound(serverPlayer, TargetModSounds.BOCEK_PULL_3P.getId(), SoundSource.PLAYERS);
|
||||
}
|
||||
|
||||
if (stack.getOrCreateTag().getDouble("power") >= 6) {
|
||||
|
@ -93,8 +87,7 @@ public class FireMessage {
|
|||
if (!level.isClientSide()) {
|
||||
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);
|
||||
arrow.setOwner(player);
|
||||
BocekArrowEntity arrow = new BocekArrowEntity(player, level);
|
||||
arrow.setBaseDamage(damage);
|
||||
arrow.setKnockback(0);
|
||||
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();
|
||||
player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
|
||||
capability.recoilHorizon = Math.random() < 0.5 ? -1 : 1;
|
||||
|
@ -168,4 +161,5 @@ public class FireMessage {
|
|||
projectile.damage((float) damage);
|
||||
player.level().addFreshEntity(projectile);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ import net.minecraft.network.FriendlyByteBuf;
|
|||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
@ -40,22 +39,19 @@ public class MortarGUIButtonMessage {
|
|||
NetworkEvent.Context context = contextSupplier.get();
|
||||
context.enqueueWork(() -> {
|
||||
Player entity = context.getSender();
|
||||
|
||||
if (entity == null) return;
|
||||
|
||||
int buttonID = message.buttonID;
|
||||
int x = message.x;
|
||||
int y = message.y;
|
||||
int z = message.z;
|
||||
handleButtonAction(entity, buttonID, x, y, z);
|
||||
});
|
||||
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;
|
||||
if (!entity.level().isLoaded(new BlockPos(x, y, z))) return;
|
||||
|
||||
handleButtonAction(entity, buttonID);
|
||||
});
|
||||
context.setPacketHandled(true);
|
||||
}
|
||||
|
||||
private static void handleButtonAction(Player player, int buttonID) {
|
||||
|
|
|
@ -2,15 +2,10 @@ package net.mcreator.target.tools;
|
|||
|
||||
import com.google.gson.stream.JsonReader;
|
||||
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.message.GunsDataMessage;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.packs.resources.ResourceManager;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||
|
@ -92,5 +87,4 @@ public class GunsTool {
|
|||
initJsonData(event.getServer().getResourceManager());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue