Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/net/povstalec/sgjourney/StargateJourney.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.povstalec.sgjourney.common.entities.Jaffa;
import net.povstalec.sgjourney.common.init.*;
import net.povstalec.sgjourney.common.misc.RenderAMD;
import net.povstalec.sgjourney.common.scheduler.SchedulerInit;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;

Expand Down Expand Up @@ -106,6 +107,7 @@ public StargateJourney()
RecipeTypeInit.register(eventBus);
StatisticsInit.register(eventBus);
CommandInit.register(eventBus);
SchedulerInit.register();

GalaxyInit.register(eventBus);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,28 @@ public CompoundTag serializeStargateInfo(CompoundTag tag)

return tag;
}

public void refreshPosInNetwork()
{
if (!id9ChevronAddress.isEmpty())
{
var network = StargateNetwork.get(level);
var sg = network.getStargate(id9ChevronAddress.immutable());
// Some mods might place the new gate before removing the old one when moving the stargate.
// We want to remove the duplicate now so it won't linger in the stargate list forever.
if (sg != null)
{
if (sg.isSamePosition(this))
{
return; // No need to replace it if it hasn't moved.
}
// We assume whichever gate was placed most recently is the "correct" one with that address.
network.removeStargate(sg);
}
network.addStargate(this);
resetStargate(StargateInfo.Feedback.NONE);
}
}

public void addStargateToNetwork()
{
Expand All @@ -354,7 +376,7 @@ public void addStargateToNetwork()

public void removeStargateFromNetwork()
{
StargateNetwork.get(level).removeStargate(id9ChevronAddress.immutable());
StargateNetwork.get(level).removeStargate(this);
}

public void set9ChevronAddress(Address address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ public Transporter getTransporter()
//TODO Maybe start caching it?
return TransporterNetwork.get(level).getTransporter(id);
}

public void refreshPosInNetwork()
{
if (id != null && level != null)
{
var network = TransporterNetwork.get(level);
var existing = network.getTransporter(id);
// Handle case where other mod creates a temporary duplicate.
if (existing != null)
{
if (existing.isSamePosition(this))
{
return;
}
network.removeTransporter(existing.getLevel(level.getServer()), id);
}
network.addTransporter(this);
resetTransporter();
}
}

public void addTransporterToNetwork()
{
Expand All @@ -131,7 +151,7 @@ public void addTransporterToNetwork()

public void removeTransporterFromNetwork()
{
TransporterNetwork.get(level).removeTransporter(level, this.id);
TransporterNetwork.get(level).removeTransporter(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
Expand Down Expand Up @@ -47,6 +49,7 @@
import net.povstalec.sgjourney.common.sgjourney.Address;
import net.povstalec.sgjourney.common.sgjourney.StargateInfo;
import net.povstalec.sgjourney.common.sgjourney.StargateVariant;
import org.jetbrains.annotations.NotNull;

public abstract class AbstractStargateBaseBlock extends AbstractStargateBlock implements EntityBlock
{
Expand Down Expand Up @@ -167,6 +170,26 @@ public BlockState getStateForPlacement(BlockPlaceContext context)
public abstract BlockEntity newBlockEntity(BlockPos pos, BlockState state);

public abstract BlockState ringState();

@SuppressWarnings("deprecation")
@Override
public void onPlace(@NotNull BlockState pState, @NotNull Level pLevel, @NotNull BlockPos pPos, @NotNull BlockState pOldState, boolean pMovedByPiston)
{
super.onPlace(pState, pLevel, pPos, pOldState, pMovedByPiston);
// We want to run refreshPosInNetwork, but can't because the block entity might not exist yet.
// We don't use IForgeBlock::onBlockStateChange because we can't guarantee it's fired.
pLevel.scheduleTick(pPos, this, 1);
}

@SuppressWarnings("deprecation")
@Override
public void tick(@NotNull BlockState pState, @NotNull ServerLevel pLevel, @NotNull BlockPos pPos, @NotNull RandomSource pRandom) {
super.tick(pState, pLevel, pPos, pRandom);
if (pLevel.getBlockEntity(pPos) instanceof AbstractStargateEntity sg)
{
sg.refreshPosInNetwork();
}
}

@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack)
Expand Down Expand Up @@ -205,7 +228,7 @@ public void onRemove(BlockState oldState, Level level, BlockPos pos, BlockState
stargate.removeStargateFromNetwork();
}

destroyStargate(level, pos, getParts(), getShieldingParts(), oldState.getValue(FACING), oldState.getValue(ORIENTATION), oldState.getValue(PART));
destroyStargate(level, pos, getParts(), getShieldingParts(), oldState.getValue(FACING), oldState.getValue(ORIENTATION));

super.onRemove(oldState, level, pos, newState, isMoving);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import net.povstalec.sgjourney.common.items.StargateIrisItem;
import net.povstalec.sgjourney.common.misc.CoverBlockPlaceContext;
import net.povstalec.sgjourney.common.misc.VoxelShapeProvider;
import net.povstalec.sgjourney.common.scheduler.StargateDestruction;
import net.povstalec.sgjourney.common.sgjourney.StargateInfo;
import net.povstalec.sgjourney.common.sgjourney.StargateBlockCover;

Expand Down Expand Up @@ -203,8 +204,14 @@ public void playerWillDestroy(Level level, BlockPos pos, BlockState state, Playe

super.playerWillDestroy(level, pos, state, player);
}


@Deprecated // Use the one below instead.
public void destroyStargate(Level level, BlockPos pos, ArrayList<StargatePart> parts, ArrayList<ShieldingPart> shieldingParts, Direction direction, Orientation orientation, StargatePart stargatePart)
{
destroyStargate(level, pos, parts, shieldingParts, direction, orientation);
}

public void destroyStargate(Level level, BlockPos pos, ArrayList<StargatePart> parts, ArrayList<ShieldingPart> shieldingParts, Direction direction, Orientation orientation)
{
if(direction == null)
{
Expand All @@ -219,22 +226,11 @@ public void destroyStargate(Level level, BlockPos pos, ArrayList<StargatePart> p
}

AbstractShieldingBlock.destroyShielding(level, pos, shieldingParts, direction, orientation);

for(StargatePart part : parts)
{
if(!stargatePart.equals(part))
{
BlockPos ringPos = part.getRingPos(pos, direction, orientation);
BlockState state = level.getBlockState(ringPos);

if(state.getBlock() instanceof AbstractStargateBlock)
{
boolean waterlogged = state.getBlock() instanceof AbstractStargateRingBlock ? state.getValue(AbstractStargateRingBlock.WATERLOGGED) : false;

level.setBlock(ringPos, waterlogged ? Blocks.WATER.defaultBlockState() : Blocks.AIR.defaultBlockState(), 3);
}
}
}

level.getServer().getWorldData().overworldData().getScheduledEvents().schedule(
StargateJourney.MODID + ":stargate_destruction_" + pos.asLong(),
level.getServer().overworld().getGameTime() + 1,
new StargateDestruction(level.dimension(), pos, parts, direction, orientation));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void onRemove(BlockState oldState, Level level, BlockPos pos, BlockState
{
BlockPos baseBlockPos = oldState.getValue(PART).getBaseBlockPos(pos, oldState.getValue(FACING), oldState.getValue(ORIENTATION));

destroyStargate(level, baseBlockPos, getParts(false), getShieldingParts(), oldState.getValue(FACING), oldState.getValue(ORIENTATION), oldState.getValue(PART));
destroyStargate(level, baseBlockPos, getParts(false), getShieldingParts(), oldState.getValue(FACING), oldState.getValue(ORIENTATION));

super.onRemove(oldState, level, pos, newState, isMoving);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import net.povstalec.sgjourney.common.config.CommonIrisConfig;
import net.povstalec.sgjourney.common.init.TagInit;
import net.povstalec.sgjourney.common.misc.VoxelShapeProvider;
import net.povstalec.sgjourney.common.scheduler.ShieldingDestruction;

public abstract class AbstractShieldingBlock extends Block implements SimpleWaterloggedBlock, ProtectedBlock
{
Expand Down Expand Up @@ -235,8 +236,7 @@ public void onRemove(BlockState oldState, Level level, BlockPos pos, BlockState
{
if(shieldingPart.shieldingState().isBefore(irisStargate.irisInfo().getIrisProgress()))
{
AbstractShieldingBlock.destroyShielding(level, baseBlockPos, getShieldingParts(), oldState.getValue(FACING), oldState.getValue(ORIENTATION));
stargateBlock.unsetIris(stargateState, level, baseBlockPos);
AbstractShieldingBlock.destroyShielding(level, baseBlockPos, getShieldingParts(), oldState.getValue(FACING), oldState.getValue(ORIENTATION), true);
}
}
}
Expand All @@ -248,6 +248,11 @@ public void onRemove(BlockState oldState, Level level, BlockPos pos, BlockState
public abstract ArrayList<ShieldingPart> getShieldingParts();

public static void destroyShielding(Level level, BlockPos baseBlockPos, ArrayList<ShieldingPart> parts, Direction direction, Orientation orientation)
{
destroyShielding(level, baseBlockPos, parts, direction, orientation, false);
}

public static void destroyShielding(Level level, BlockPos baseBlockPos, ArrayList<ShieldingPart> parts, Direction direction, Orientation orientation, boolean updateStargate)
{
if(direction == null)
{
Expand All @@ -260,19 +265,11 @@ public static void destroyShielding(Level level, BlockPos baseBlockPos, ArrayLis
StargateJourney.LOGGER.error("Failed to destroy Shielding because orientation is null");
return;
}

for(ShieldingPart part : parts)
{
BlockPos ringPos = part.getShieldingPos(baseBlockPos, direction, orientation);
BlockState state = level.getBlockState(ringPos);

if(state.getBlock() instanceof AbstractShieldingBlock)
{
boolean waterlogged = state.getValue(AbstractShieldingBlock.WATERLOGGED);

level.setBlock(ringPos, waterlogged ? Blocks.WATER.defaultBlockState() : Blocks.AIR.defaultBlockState(), 3);
}
}

level.getServer().getWorldData().overworldData().getScheduledEvents().schedule(
StargateJourney.MODID + ":shielding_destruction_" + baseBlockPos.asLong(),
level.getServer().overworld().getGameTime() + 1,
new ShieldingDestruction(level.dimension(), baseBlockPos, parts, direction, orientation, updateStargate));
}

public static void setIrisState(AbstractShieldingBlock irisBlock, Level level, BlockPos baseBlockPos, ArrayList<ShieldingPart> parts, Direction direction, Orientation orientation, ShieldingState shieldingState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
Expand All @@ -22,6 +24,7 @@
import net.povstalec.sgjourney.common.block_entities.transporter.AbstractTransporterEntity;
import net.povstalec.sgjourney.common.init.BlockInit;
import net.povstalec.sgjourney.common.misc.InventoryUtil;
import org.jetbrains.annotations.NotNull;

public abstract class AbstractTransporterBlock extends BaseEntityBlock
{
Expand Down Expand Up @@ -50,6 +53,26 @@ public void onRemove(BlockState state, Level level, BlockPos pos, BlockState new
}
super.onRemove(state, level, pos, newState, isMoving);
}

@SuppressWarnings("deprecation")
@Override
public void onPlace(@NotNull BlockState pState, @NotNull Level pLevel, @NotNull BlockPos pPos, @NotNull BlockState pOldState, boolean pMovedByPiston)
{
super.onPlace(pState, pLevel, pPos, pOldState, pMovedByPiston);
// We want to run refreshPosInNetwork, but can't because the block entity might not exist yet.
// We don't use IForgeBlock::onBlockStateChange because we can't guarantee it's fired.
pLevel.scheduleTick(pPos, this, 1);
}

@SuppressWarnings("deprecation")
@Override
public void tick(@NotNull BlockState pState, @NotNull ServerLevel pLevel, @NotNull BlockPos pPos, @NotNull RandomSource pRandom) {
super.tick(pState, pLevel, pPos, pRandom);
if (pLevel.getBlockEntity(pPos) instanceof AbstractTransporterEntity transporter)
{
transporter.refreshPosInNetwork();
}
}

@Override
public void playerWillDestroy(Level level, BlockPos pos, BlockState state, Player player)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.povstalec.sgjourney.common.blockstates;

import com.mojang.serialization.Codec;
import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
import net.minecraft.util.StringRepresentable;
Expand All @@ -11,6 +12,8 @@ public enum Orientation implements StringRepresentable
REGULAR("regular", 1, 0),
UPWARD("upward", 0, 1),
DOWNWARD("downward", 2, -1);

public static final Codec<Orientation> CODEC = StringRepresentable.fromEnum(Orientation::values);

private static final Vec3[] VECTORS = new Vec3[]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;

import com.mojang.serialization.Codec;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.StringRepresentable;
Expand Down Expand Up @@ -40,7 +41,9 @@ public enum ShieldingPart implements StringRepresentable
LEFT_ABOVE("left_above", 1, 1, ShieldingState.MOVING_1),
ABOVE("above", 0, 1, ShieldingState.MOVING_1),
RIGHT_ABOVE("right_above", -1, 1, ShieldingState.MOVING_1);


public static final Codec<ShieldingPart> CODEC = StringRepresentable.fromEnum(ShieldingPart::values);

public static final ArrayList<ShieldingPart> DEFAULT_PARTS = getParts();

private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;

import com.mojang.serialization.Codec;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.StringRepresentable;
Expand Down Expand Up @@ -55,7 +56,9 @@ public enum StargatePart implements StringRepresentable
ABOVE5("above5", 0, 5),
RIGHT_ABOVE5("right_above5", -1, 5),
RIGHT2_ABOVE4("right2_above4", -2, 4);


public static final Codec<StargatePart> CODEC = StringRepresentable.fromEnum(StargatePart::values);

public static final ArrayList<StargatePart> DEFAULT_PARTS = getParts(false, false);
public static final ArrayList<StargatePart> DEFAULT_SHIELDED_PARTS = getParts(false, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ public final void addStargate(AbstractStargateEntity stargateEntity)
{
addStargate(BlockEntityList.get(server).addStargate(stargateEntity));
}

public void removeStargate(AbstractStargateEntity stargateEntity)
{
var address = stargateEntity.get9ChevronAddress();
if (address == null)
return;
var sg = getStargate(address.immutable());
// Some mods might remove the old one after placing the new one when moving the stargate.
// We need to make sure we don't delete the new one.
if (sg != null && sg.isSamePosition(stargateEntity))
{
removeStargate(sg);
}
}

public final void removeStargate(Stargate stargate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ public void addTransporter(AbstractTransporterEntity transporterEntity)
if(transporter != null && transporterEntity.getID() != null && transporterEntity.getID().equals(transporter.getID()))
addTransporter(transporter);
}

public void removeTransporter(AbstractTransporterEntity transporterEntity)
{
var uuid = transporterEntity.getID();
if (uuid == null || transporterEntity.getLevel() == null)
return;

var transporter = getTransporter(uuid);
// Handle case where other mod creates a temporary duplicate.
if (transporter == null || transporter.isSamePosition(transporterEntity))
{
removeTransporter(transporterEntity.getLevel(), uuid);
}
}

public void removeTransporter(Level level, UUID uuid)
{
Expand Down
Loading