优化部分代码

This commit is contained in:
17146 2024-07-28 22:31:39 +08:00
parent 1ad4ea56a9
commit d0885f4638
5 changed files with 118 additions and 119 deletions

View file

@ -71,12 +71,12 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
public static final EntityDataAccessor<String> CONTROLLER = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.STRING); public static final EntityDataAccessor<String> CONTROLLER = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.STRING);
public static final EntityDataAccessor<Integer> AMMO = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.INT); public static final EntityDataAccessor<Integer> AMMO = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.INT);
public static final EntityDataAccessor<Boolean> KAMIKAZE = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.BOOLEAN); public static final EntityDataAccessor<Boolean> KAMIKAZE = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.BOOLEAN);
public static final EntityDataAccessor<Float> MOVEX = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor<Float> MOVE_X = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> MOVEY = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor<Float> MOVE_Y = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> MOVEZ = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor<Float> MOVE_Z = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> ROT_X = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> ROT_Z = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> ROTX = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> ROTZ = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private boolean move = false; private boolean move = false;
@ -108,11 +108,11 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
this.entityData.define(LINKED, false); this.entityData.define(LINKED, false);
this.entityData.define(AMMO, 0); this.entityData.define(AMMO, 0);
this.entityData.define(KAMIKAZE, false); this.entityData.define(KAMIKAZE, false);
this.entityData.define(MOVEX, 0f); this.entityData.define(MOVE_X, 0f);
this.entityData.define(MOVEY, 0f); this.entityData.define(MOVE_Y, 0f);
this.entityData.define(MOVEZ, 0f); this.entityData.define(MOVE_Z, 0f);
this.entityData.define(ROTX, 0f); this.entityData.define(ROT_X, 0f);
this.entityData.define(ROTZ, 0f); this.entityData.define(ROT_Z, 0f);
} }
@Override @Override
@ -153,11 +153,11 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
compound.putString("Controller", this.entityData.get(CONTROLLER)); compound.putString("Controller", this.entityData.get(CONTROLLER));
compound.putInt("ammo", this.entityData.get(AMMO)); compound.putInt("ammo", this.entityData.get(AMMO));
compound.putBoolean("Kamikaze", this.entityData.get(KAMIKAZE)); compound.putBoolean("Kamikaze", this.entityData.get(KAMIKAZE));
compound.putFloat("moveX", this.entityData.get(MOVEX)); compound.putFloat("moveX", this.entityData.get(MOVE_X));
compound.putFloat("moveY", this.entityData.get(MOVEY)); compound.putFloat("moveY", this.entityData.get(MOVE_Y));
compound.putFloat("moveZ", this.entityData.get(MOVEZ)); compound.putFloat("moveZ", this.entityData.get(MOVE_Z));
compound.putFloat("rotX", this.entityData.get(ROTX)); compound.putFloat("rotX", this.entityData.get(ROT_X));
compound.putFloat("rotZ", this.entityData.get(ROTZ)); compound.putFloat("rotZ", this.entityData.get(ROT_Z));
} }
@Override @Override
@ -173,15 +173,15 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
if (compound.contains("Kamikaze")) if (compound.contains("Kamikaze"))
this.entityData.set(KAMIKAZE, compound.getBoolean("Kamikaze")); this.entityData.set(KAMIKAZE, compound.getBoolean("Kamikaze"));
if (compound.contains("moveX")) if (compound.contains("moveX"))
this.entityData.set(MOVEX, compound.getFloat("moveX")); this.entityData.set(MOVE_X, compound.getFloat("moveX"));
if (compound.contains("moveY")) if (compound.contains("moveY"))
this.entityData.set(MOVEY, compound.getFloat("moveY")); this.entityData.set(MOVE_Y, compound.getFloat("moveY"));
if (compound.contains("moveZ")) if (compound.contains("moveZ"))
this.entityData.set(MOVEZ, compound.getFloat("moveZ")); this.entityData.set(MOVE_Z, compound.getFloat("moveZ"));
if (compound.contains("rotX")) if (compound.contains("rotX"))
this.entityData.set(ROTX, compound.getFloat("rotX")); this.entityData.set(ROT_X, compound.getFloat("rotX"));
if (compound.contains("rotZ")) if (compound.contains("rotZ"))
this.entityData.set(ROTZ, compound.getFloat("rotZ")); this.entityData.set(ROT_Z, compound.getFloat("rotZ"));
} }
@Override @Override
@ -189,66 +189,66 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
super.baseTick(); super.baseTick();
if (this.getPersistentData().getBoolean("left")) { if (this.getPersistentData().getBoolean("left")) {
this.entityData.set(MOVEX,-1.5f); this.entityData.set(MOVE_X, -1.5f);
this.entityData.set(ROTX,Mth.clamp(this.entityData.get(ROTX) + 0.05f, -0.5f, 0.5f)); this.entityData.set(ROT_X, Mth.clamp(this.entityData.get(ROT_X) + 0.05f, -0.5f, 0.5f));
} }
if (this.getPersistentData().getBoolean("right")) { if (this.getPersistentData().getBoolean("right")) {
this.entityData.set(MOVEX,1.5f); this.entityData.set(MOVE_X, 1.5f);
this.entityData.set(ROTX,Mth.clamp(this.entityData.get(ROTX) - 0.05f, -0.5f, 0.5f)); this.entityData.set(ROT_X, Mth.clamp(this.entityData.get(ROT_X) - 0.05f, -0.5f, 0.5f));
} }
if (this.entityData.get(ROTX) > 0) { if (this.entityData.get(ROT_X) > 0) {
this.entityData.set(ROTX, Mth.clamp(this.entityData.get(ROTX) - 0.025f, 0, 0.5f)); this.entityData.set(ROT_X, Mth.clamp(this.entityData.get(ROT_X) - 0.025f, 0, 0.5f));
} else { } else {
this.entityData.set(ROTX, Mth.clamp(this.entityData.get(ROTX) + 0.025f, -0.5f, 0)); this.entityData.set(ROT_X, Mth.clamp(this.entityData.get(ROT_X) + 0.025f, -0.5f, 0));
} }
if (!this.getPersistentData().getBoolean("left") && !this.getPersistentData().getBoolean("right")) { if (!this.getPersistentData().getBoolean("left") && !this.getPersistentData().getBoolean("right")) {
if (this.entityData.get(MOVEX) >= 0) { if (this.entityData.get(MOVE_X) >= 0) {
this.entityData.set(MOVEX,Mth.clamp(this.entityData.get(MOVEX) - 0.3f, 0, 1)); this.entityData.set(MOVE_X, Mth.clamp(this.entityData.get(MOVE_X) - 0.3f, 0, 1));
} else { } else {
this.entityData.set(MOVEX,Mth.clamp(this.entityData.get(MOVEX) + 0.3f, -1, 0)); this.entityData.set(MOVE_X, Mth.clamp(this.entityData.get(MOVE_X) + 0.3f, -1, 0));
} }
} }
if (this.getPersistentData().getBoolean("forward")) { if (this.getPersistentData().getBoolean("forward")) {
this.entityData.set(MOVEZ,this.entityData.get(MOVEZ) - 0.15f); this.entityData.set(MOVE_Z, this.entityData.get(MOVE_Z) - 0.15f);
this.entityData.set(ROTZ,Mth.clamp(this.entityData.get(ROTZ) - 0.05f, -0.5f, 0.5f)); this.entityData.set(ROT_Z, Mth.clamp(this.entityData.get(ROT_Z) - 0.05f, -0.5f, 0.5f));
} }
if (this.getPersistentData().getBoolean("backward")) { if (this.getPersistentData().getBoolean("backward")) {
this.entityData.set(MOVEZ,this.entityData.get(MOVEZ) + 0.15f); this.entityData.set(MOVE_Z, this.entityData.get(MOVE_Z) + 0.15f);
this.entityData.set(ROTZ,Mth.clamp(this.entityData.get(ROTZ) + 0.05f, -0.5f, 0.5f)); this.entityData.set(ROT_Z, Mth.clamp(this.entityData.get(ROT_Z) + 0.05f, -0.5f, 0.5f));
} }
if (this.entityData.get(ROTZ) > 0) { if (this.entityData.get(ROT_Z) > 0) {
this.entityData.set(ROTZ, Mth.clamp(this.entityData.get(ROTZ) - 0.025f, 0, 0.5f)); this.entityData.set(ROT_Z, Mth.clamp(this.entityData.get(ROT_Z) - 0.025f, 0, 0.5f));
} else { } else {
this.entityData.set(ROTZ, Mth.clamp(this.entityData.get(ROTZ) + 0.025f, -0.5f, 0)); this.entityData.set(ROT_Z, Mth.clamp(this.entityData.get(ROT_Z) + 0.025f, -0.5f, 0));
} }
if (this.entityData.get(MOVEZ) >= 0) { if (this.entityData.get(MOVE_Z) >= 0) {
this.entityData.set(MOVEZ,Mth.clamp(this.entityData.get(MOVEZ) - 0.1f, 0, 1)); this.entityData.set(MOVE_Z, Mth.clamp(this.entityData.get(MOVE_Z) - 0.1f, 0, 1));
} else { } else {
this.entityData.set(MOVEZ,Mth.clamp(this.entityData.get(MOVEZ) + 0.1f, -1, 0)); this.entityData.set(MOVE_Z, Mth.clamp(this.entityData.get(MOVE_Z) + 0.1f, -1, 0));
} }
if (this.getPersistentData().getBoolean("up")) { if (this.getPersistentData().getBoolean("up")) {
this.entityData.set(MOVEY,-1.5f); this.entityData.set(MOVE_Y, -1.5f);
} }
if (this.getPersistentData().getBoolean("down")) { if (this.getPersistentData().getBoolean("down")) {
this.entityData.set(MOVEY,1.5f); this.entityData.set(MOVE_Y, 1.5f);
} }
if (this.entityData.get(MOVEY) >= 0) { if (this.entityData.get(MOVE_Y) >= 0) {
this.entityData.set(MOVEY,Mth.clamp(this.entityData.get(MOVEY) - 0.3f, 0, 1)); this.entityData.set(MOVE_Y, Mth.clamp(this.entityData.get(MOVE_Y) - 0.3f, 0, 1));
} else { } else {
this.entityData.set(MOVEY,Mth.clamp(this.entityData.get(MOVEY)+ 0.3f, -1, 0)); this.entityData.set(MOVE_Y, Mth.clamp(this.entityData.get(MOVE_Y) + 0.3f, -1, 0));
} }
this.setDeltaMovement(new Vec3( this.setDeltaMovement(new Vec3(
this.getDeltaMovement().x + -this.entityData.get(MOVEZ) * 0.1f * this.getLookAngle().x, this.getDeltaMovement().x + -this.entityData.get(MOVE_Z) * 0.1f * this.getLookAngle().x,
this.getDeltaMovement().y + -this.entityData.get(MOVEY) * 0.05f, this.getDeltaMovement().y + -this.entityData.get(MOVE_Y) * 0.05f,
this.getDeltaMovement().z + -this.entityData.get(MOVEZ) * 0.1f * this.getLookAngle().z this.getDeltaMovement().z + -this.entityData.get(MOVE_Z) * 0.1f * this.getLookAngle().z
)); ));
this.move = this.getPersistentData().getBoolean("left") this.move = this.getPersistentData().getBoolean("left")
@ -392,8 +392,8 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
this.yBodyRot = control.getYRot(); this.yBodyRot = control.getYRot();
this.yHeadRot = control.getYRot(); this.yHeadRot = control.getYRot();
this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED)); this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED));
float strafe = -this.entityData.get(MOVEX); float strafe = -this.entityData.get(MOVE_X);
super.travel(new Vec3(2 * strafe, -this.entityData.get(MOVEY), -this.entityData.get(MOVEZ))); super.travel(new Vec3(2 * strafe, -this.entityData.get(MOVE_Y), -this.entityData.get(MOVE_Z)));
Vec3 vec3 = this.getDeltaMovement(); Vec3 vec3 = this.getDeltaMovement();
if (this.onGround()) { if (this.onGround()) {
this.setDeltaMovement(vec3.multiply(0.7, 0.98, 0.7)); this.setDeltaMovement(vec3.multiply(0.7, 0.98, 0.7));
@ -432,7 +432,6 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
}); });
} }
if (this.entityData.get(KAMIKAZE)) { if (this.entityData.get(KAMIKAZE)) {
kamikazeExplosion(source.getEntity()); kamikazeExplosion(source.getEntity());
} }
@ -490,7 +489,7 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
return builder; return builder;
} }
private PlayState movementPredicate(AnimationState event) { private PlayState movementPredicate(AnimationState<DroneEntity> event) {
if (this.animationprocedure.equals("empty")) { if (this.animationprocedure.equals("empty")) {
if (!this.onGround()) { if (!this.onGround()) {
return event.setAndContinue(RawAnimation.begin().thenLoop("animation.drone.fly")); return event.setAndContinue(RawAnimation.begin().thenLoop("animation.drone.fly"));
@ -500,7 +499,7 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
return PlayState.STOP; return PlayState.STOP;
} }
private PlayState procedurePredicate(AnimationState event) { private PlayState procedurePredicate(AnimationState<DroneEntity> event) {
if (!animationprocedure.equals("empty") && event.getController().getAnimationState() == AnimationController.State.STOPPED) { if (!animationprocedure.equals("empty") && event.getController().getAnimationState() == AnimationController.State.STOPPED) {
event.getController().setAnimation(RawAnimation.begin().thenPlay(this.animationprocedure)); event.getController().setAnimation(RawAnimation.begin().thenPlay(this.animationprocedure));
if (event.getController().getAnimationState() == AnimationController.State.STOPPED) { if (event.getController().getAnimationState() == AnimationController.State.STOPPED) {
@ -535,9 +534,10 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
private final ItemStackHandler inventory = new ItemStackHandler(9) { private final ItemStackHandler inventory = new ItemStackHandler(9) {
@Override @Override
public int getSlotLimit(int slot) { public int getSlotLimit(int slot) {
return 64; return super.getSlotLimit(slot);
} }
}; };
private final CombinedInvWrapper combined = new CombinedInvWrapper(inventory, new EntityHandsInvWrapper(this), new EntityArmorInvWrapper(this)); private final CombinedInvWrapper combined = new CombinedInvWrapper(inventory, new EntityHandsInvWrapper(this), new EntityArmorInvWrapper(this));
@Override @Override

View file

@ -22,7 +22,6 @@ import net.minecraftforge.network.PlayMessages;
public class DroneGrenadeEntity extends ThrowableItemProjectile { public class DroneGrenadeEntity extends ThrowableItemProjectile {
public DroneGrenadeEntity(EntityType<? extends DroneGrenadeEntity> type, Level world) { public DroneGrenadeEntity(EntityType<? extends DroneGrenadeEntity> type, Level world) {
super(type, world); super(type, world);
} }

View file

@ -6,6 +6,7 @@ import software.bernie.geckolib.core.animation.AnimationState;
import software.bernie.geckolib.model.GeoModel; import software.bernie.geckolib.model.GeoModel;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.mcreator.superbwarfare.entity.DroneEntity; import net.mcreator.superbwarfare.entity.DroneEntity;
import static net.mcreator.superbwarfare.entity.DroneEntity.*; import static net.mcreator.superbwarfare.entity.DroneEntity.*;
public class DroneModel extends GeoModel<DroneEntity> { public class DroneModel extends GeoModel<DroneEntity> {
@ -44,8 +45,7 @@ public class DroneModel extends GeoModel<DroneEntity> {
ammo1.setHidden(animatable.getEntityData().get(AMMO) <= 0); ammo1.setHidden(animatable.getEntityData().get(AMMO) <= 0);
shell.setHidden(!animatable.getEntityData().get(KAMIKAZE)); shell.setHidden(!animatable.getEntityData().get(KAMIKAZE));
body.setRotZ(animatable.getEntityData().get(ROT_X));
body.setRotZ(animatable.getEntityData().get(ROTX)); body.setRotX(animatable.getEntityData().get(ROT_Z));
body.setRotX(animatable.getEntityData().get(ROTZ));
} }
} }

View file

@ -28,8 +28,8 @@ import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod;
import static net.mcreator.superbwarfare.entity.DroneEntity.ROTX; import static net.mcreator.superbwarfare.entity.DroneEntity.ROT_X;
import static net.mcreator.superbwarfare.entity.DroneEntity.ROTZ; import static net.mcreator.superbwarfare.entity.DroneEntity.ROT_Z;
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT) @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class ClientEventHandler { public class ClientEventHandler {
@ -92,16 +92,16 @@ public class ClientEventHandler {
if (drone != null) { if (drone != null) {
if (data.getDouble("droneRotZ") > drone.getEntityData().get(ROTZ)) { if (data.getDouble("droneRotZ") > drone.getEntityData().get(ROT_Z)) {
data.putDouble("droneRotZ", Mth.clamp(data.getDouble("droneRotZ") - 0.3 * Math.pow(drone.getEntityData().get(ROTZ) - data.getDouble("droneRotZ"), 2),drone.getEntityData().get(ROTZ),Double.POSITIVE_INFINITY)); data.putDouble("droneRotZ", Mth.clamp(data.getDouble("droneRotZ") - 0.3 * Math.pow(drone.getEntityData().get(ROT_Z) - data.getDouble("droneRotZ"), 2),drone.getEntityData().get(ROT_Z),Double.POSITIVE_INFINITY));
} else { } else {
data.putDouble("droneRotZ", Mth.clamp(data.getDouble("droneRotZ") + 0.3 * Math.pow(drone.getEntityData().get(ROTZ) - data.getDouble("droneRotZ"), 2),Double.NEGATIVE_INFINITY,drone.getEntityData().get(ROTZ))); data.putDouble("droneRotZ", Mth.clamp(data.getDouble("droneRotZ") + 0.3 * Math.pow(drone.getEntityData().get(ROT_Z) - data.getDouble("droneRotZ"), 2),Double.NEGATIVE_INFINITY,drone.getEntityData().get(ROT_Z)));
} }
if (data.getDouble("droneRotX") > drone.getEntityData().get(ROTX)) { if (data.getDouble("droneRotX") > drone.getEntityData().get(ROT_X)) {
data.putDouble("droneRotX", Mth.clamp(data.getDouble("droneRotX") - 0.2 * Math.pow(drone.getEntityData().get(ROTX) - data.getDouble("droneRotX"), 2),drone.getEntityData().get(ROTX),Double.POSITIVE_INFINITY)); data.putDouble("droneRotX", Mth.clamp(data.getDouble("droneRotX") - 0.2 * Math.pow(drone.getEntityData().get(ROT_X) - data.getDouble("droneRotX"), 2),drone.getEntityData().get(ROT_X),Double.POSITIVE_INFINITY));
} else { } else {
data.putDouble("droneRotX", Mth.clamp(data.getDouble("droneRotX") + 0.2 * Math.pow(drone.getEntityData().get(ROTX) - data.getDouble("droneRotX"), 2),Double.NEGATIVE_INFINITY,drone.getEntityData().get(ROTX))); data.putDouble("droneRotX", Mth.clamp(data.getDouble("droneRotX") + 0.2 * Math.pow(drone.getEntityData().get(ROT_X) - data.getDouble("droneRotX"), 2),Double.NEGATIVE_INFINITY,drone.getEntityData().get(ROT_X)));
} }
event.setPitch((float) (pitch + data.getDouble("droneCameraRotX") - 0.15f * Mth.RAD_TO_DEG * data.getDouble("droneRotZ"))); event.setPitch((float) (pitch + data.getDouble("droneCameraRotX") - 0.15f * Mth.RAD_TO_DEG * data.getDouble("droneRotZ")));