Skip to content

Commit

Permalink
Cleanup flame tick logic and some places where we get the current server
Browse files Browse the repository at this point in the history
  • Loading branch information
pupnewfster committed Mar 3, 2024
1 parent 5f6deb9 commit c17cf16
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 61 deletions.
Expand Up @@ -2,10 +2,10 @@

import java.util.Optional;
import mekanism.api.text.IHasTranslationKey;
import mekanism.api.text.TextComponentUtil;
import net.minecraft.DetectedVersion;
import net.minecraft.data.PackOutput;
import net.minecraft.data.metadata.PackMetadataGenerator;
import net.minecraft.network.chat.Component;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
import net.minecraft.util.InclusiveRange;
Expand All @@ -22,7 +22,7 @@ public BasePackMetadataGenerator(PackOutput output, IHasTranslationKey descripti
minVersion = Math.min(minVersion, version);
}
add(PackMetadataSection.TYPE, new PackMetadataSection(
Component.translatable(description.getTranslationKey()),
TextComponentUtil.build(description),
maxVersion,
Optional.of(new InclusiveRange<>(minVersion, maxVersion))
));
Expand Down
64 changes: 27 additions & 37 deletions src/main/java/mekanism/common/entity/EntityFlame.java
Expand Up @@ -17,7 +17,6 @@
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.ItemEntity;
Expand All @@ -42,6 +41,7 @@
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.HitResult.Type;
import net.minecraft.world.phys.Vec3;
import net.neoforged.neoforge.common.NeoForge;
Expand All @@ -57,8 +57,6 @@ public class EntityFlame extends Projectile implements IEntityWithComplexSpawn {
public static final int LIFESPAN = 4 * SharedConstants.TICKS_PER_SECOND;
private static final int DAMAGE = 10;

private FlamethrowerMode mode = FlamethrowerMode.COMBAT;

public EntityFlame(EntityType<EntityFlame> type, Level world) {
super(type, world);
}
Expand All @@ -84,51 +82,43 @@ public static EntityFlame create(Player player) {
//Attempt to ray trace the area between the player and where the flame would actually start
// if it hits a block instead just have the flame hit the block directly to avoid being able
// to shoot a flamethrower through one thick walls.
BlockHitResult blockRayTrace = player.level().clip(new ClipContext(playerPos, mergedVec, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, flame));
BlockHitResult blockRayTrace = player.level().clip(new ClipContext(playerPos, mergedVec, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, flame));
if (blockRayTrace.getType() != Type.MISS) {
flame.onHit(blockRayTrace);
}
return flame;
}

@Override
public void baseTick() {
if (!isAlive()) {
return;
}
tickCount++;

xo = getX();
yo = getY();
zo = getZ();

xRotO = getXRot();
yRotO = getYRot();

Vec3 motion = getDeltaMovement();
setPosRaw(getX() + motion.x(), getY() + motion.y(), getZ() + motion.z());

setPos(getX(), getY(), getZ());

calculateVector();
public void tick() {
super.tick();
if (tickCount > LIFESPAN) {
discard();
}
}
} else {
Vec3 localVec = position();
Vec3 motion = getDeltaMovement();
Vec3 motionVec = localVec.add(motion);
HitResult hitResult = level().clip(new ClipContext(localVec, motionVec, ClipContext.Block.COLLIDER, ClipContext.Fluid.ANY, this));
if (hitResult.getType() != Type.MISS) {
motionVec = hitResult.getLocation();
}
EntityHitResult entityResult = ProjectileUtil.getEntityHitResult(level(), this, localVec, motionVec,
getBoundingBox().expandTowards(getDeltaMovement()).inflate(1.0), this::canHitEntity);
if (entityResult != null && entityResult.getType() == Type.ENTITY) {
if (entityResult.getEntity() instanceof Player target && getOwner() instanceof Player owner && !owner.canHarmPlayer(target)) {
hitResult = null;
} else {
hitResult = entityResult;
}
}
if (hitResult != null && hitResult.getType() != HitResult.Type.MISS) {
if (!EventHooks.onProjectileImpact(this, hitResult)) {
onHit(hitResult);
}
}

private void calculateVector() {
Vec3 localVec = new Vec3(getX(), getY(), getZ());
Vec3 motion = getDeltaMovement();
Vec3 motionVec = new Vec3(getX() + motion.x() * 2, getY() + motion.y() * 2, getZ() + motion.z() * 2);
BlockHitResult blockRayTrace = level().clip(new ClipContext(localVec, motionVec, ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, this));
localVec = new Vec3(getX(), getY(), getZ());
motionVec = new Vec3(getX() + motion.x(), getY() + motion.y(), getZ() + motion.z());
if (blockRayTrace.getType() != Type.MISS) {
motionVec = blockRayTrace.getLocation();
setPos(motionVec.x, motionVec.y, motionVec.z);
}
EntityHitResult entityResult = ProjectileUtil.getEntityHitResult(level(), this, localVec, motionVec,
getBoundingBox().expandTowards(getDeltaMovement()).inflate(1.0D, 1.0D, 1.0D), EntitySelector.NO_SPECTATORS);
onHit(entityResult == null ? blockRayTrace : entityResult);
}

@Override
Expand Down
42 changes: 28 additions & 14 deletions src/main/java/mekanism/common/entity/EntityRobit.java
Expand Up @@ -89,6 +89,7 @@
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.TicketType;
Expand Down Expand Up @@ -124,7 +125,6 @@
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.common.util.ITeleporter;
import net.neoforged.neoforge.items.ItemHandlerHelper;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -290,6 +290,33 @@ public void onRemovedFromWorld() {
super.onRemovedFromWorld();
}

@Override
public void tick() {
Level level = level();
if (!level.isClientSide) {
if (homeLocation == null) {
discard();
return;
}
if (tickCount % SharedConstants.TICKS_PER_SECOND == 0) {
Level serverWorld;
if (level.dimension() == homeLocation.dimension()) {
serverWorld = level;
} else {
MinecraftServer server = getServer();
serverWorld = server == null ? null : server.getLevel(homeLocation.dimension());
}
BlockPos homePos = homeLocation.pos();
if (WorldUtils.isBlockLoaded(serverWorld, homePos) && WorldUtils.getTileEntity(TileEntityChargepad.class, serverWorld, homePos) == null) {
drop();
discard();
return;
}
}
}
super.tick();
}

@Override
public void baseTick() {
if (!level().isClientSide) {
Expand All @@ -307,19 +334,6 @@ public void baseTick() {
if (getDropPickup()) {
collectItems();
}
if (homeLocation == null) {
discard();
return;
}

if (tickCount % SharedConstants.TICKS_PER_SECOND == 0) {
Level serverWorld = ServerLifecycleHooks.getCurrentServer().getLevel(homeLocation.dimension());
BlockPos homePos = homeLocation.pos();
if (WorldUtils.isBlockLoaded(serverWorld, homePos) && WorldUtils.getTileEntity(TileEntityChargepad.class, serverWorld, homePos) == null) {
drop();
discard();
}
}

if (energyContainer.isEmpty() && !isOnChargepad()) {
goHome();
Expand Down
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.core.GlobalPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
Expand All @@ -29,7 +30,6 @@
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.jetbrains.annotations.NotNull;

public record PacketPortableTeleporterTeleport(InteractionHand currentHand, FrequencyIdentity identity) implements IMekanismPacket<PlayPayloadContext> {
Expand Down Expand Up @@ -61,7 +61,8 @@ public void handle(PlayPayloadContext context) {
}
GlobalPos coords = found.getClosestCoords(GlobalPos.of(player.level().dimension(), player.blockPosition()));
if (coords != null) {
Level teleWorld = ServerLifecycleHooks.getCurrentServer().getLevel(coords.dimension());
MinecraftServer server = player.level().getServer();
Level teleWorld = server == null ? null : server.getLevel(coords.dimension());
TileEntityTeleporter teleporter = WorldUtils.getTileEntity(TileEntityTeleporter.class, teleWorld, coords.pos());
if (teleporter != null) {
FloatingLong energyCost;
Expand Down
@@ -1,5 +1,6 @@
package mekanism.common.tile;

import java.util.Optional;
import java.util.UUID;
import mekanism.api.IContentsListener;
import mekanism.api.security.ISecurityUtils;
Expand All @@ -20,7 +21,6 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -121,8 +121,11 @@ public void setSecurityDeskMode(SecurityMode mode) {

public void addTrusted(String name) {
SecurityFrequency frequency = getFreq();
if (frequency != null) {
ServerLifecycleHooks.getCurrentServer().getProfileCache().get(name).ifPresent(profile -> frequency.addTrusted(profile.getId(), profile.getName()));
if (frequency != null && level != null) {
Optional.ofNullable(level.getServer())
.map(MinecraftServer::getProfileCache)
.flatMap(cache -> cache.get(name))
.ifPresent(profile -> frequency.addTrusted(profile.getId(), profile.getName()));
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/mekanism/common/tile/TileEntityTeleporter.java
Expand Up @@ -75,7 +75,6 @@
import net.neoforged.neoforge.common.util.FakePlayer;
import net.neoforged.neoforge.common.util.ITeleporter;
import net.neoforged.neoforge.entity.PartEntity;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -287,7 +286,10 @@ private void teleport(TeleporterFrequency frequency, TeleportInfo teleportInfo)
if (teleportInfo.closest == null || level == null || teleportInfo.toTeleport.isEmpty()) {
return;
}
MinecraftServer currentServer = ServerLifecycleHooks.getCurrentServer();
MinecraftServer currentServer = level.getServer();
if (currentServer == null) {
return;
}
boolean sameDimension = level.dimension() == teleportInfo.closest.dimension();
Level teleWorld = sameDimension ? level : currentServer.getLevel(teleportInfo.closest.dimension());
BlockPos closestPos = teleportInfo.closest.pos();
Expand Down Expand Up @@ -449,7 +451,7 @@ private List<Entity> getToTeleport(boolean sameDimension) {
*/
@Nullable
public static FloatingLong calculateEnergyCost(Entity entity, GlobalPos pos) {
MinecraftServer currentServer = ServerLifecycleHooks.getCurrentServer();
MinecraftServer currentServer = entity.getServer();
if (currentServer != null) {
Level targetWorld = currentServer.getLevel(pos.dimension());
if (targetWorld != null) {
Expand Down

0 comments on commit c17cf16

Please sign in to comment.