Skip to content

Commit

Permalink
space station launching thing
Browse files Browse the repository at this point in the history
  • Loading branch information
screret committed Nov 2, 2023
1 parent 3dcd1c0 commit 707a33d
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ public interface ISpaceStationHolder {
* @param id the station's id
* @return the center of this station
*/
@Nullable
Vec2i getStationPos(int id);

/**
*
* @param id the station's id
* @return this station.
*/
SpaceStation getStation(int id);
@Nullable
SpaceStation getStation(@Nullable Integer id);

@Nullable
BlockPos getStationWorldPos(int id);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,26 @@ public int getClosestStationId(Vec2i position) {
return result == null ? -1 : result.getIntKey();
}

@Nullable
@Override
public Vec2i getStationPos(int id) {
var station = stations.get(id);
if (station == null) return Vec2i.MAX_NEGATIVE;
if (station == null) return null;
return station.position();
}

@Nullable
@Override
public SpaceStation getStation(int id) {
public SpaceStation getStation(@Nullable Integer id) {
if (id == null) return null;
return stations.get(id);
}

@Nullable
@Override
public BlockPos getStationWorldPos(int id) {
Vec2i stationPos = getStationPos(id);
if (stationPos == null) return null;
return new BlockPos(stationPos.x() * 16 * 16, SpaceLevelSource.PLATFORM_HEIGHT, stationPos.y() * 16 * 16);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class GCyRMachines {
public static final MachineDefinition SPACE_STATION_PACKAGER = REGISTRATE.multiblock("space_station_packager", SpaceStationPackagerMachine::new)
.langValue("Space Station Packager")
.rotationState(RotationState.NON_Y_AXIS)
.tier(GTValues.EV)
.tier(GTValues.LuV)
.pattern((definition) -> FactoryBlockPattern.start()
.aisle(" ", " K ", " K ", " K ", " K ", " K ")
.aisle(" BBBBBBB ", " ", " ", " ", " ", " ")
Expand Down Expand Up @@ -151,7 +151,7 @@ public class GCyRMachines {
.where('C', Blocks.WHITE_CONCRETE).build());
return shapeInfo;
})
.workableCasingRenderer(GTCEu.id("block/casings/voltage/ev/side"),
.workableCasingRenderer(GTCEu.id("block/casings/voltage/luv/side"),
GTCEu.id("block/multiblock/assembly_line"), false)
.register();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import argent_matter.gcyr.common.entity.data.EntityTemperatureSystem;
import argent_matter.gcyr.common.item.KeyCardBehaviour;
import argent_matter.gcyr.common.item.PlanetIdChipBehaviour;
import argent_matter.gcyr.common.item.StationContainerBehaviour;
import argent_matter.gcyr.data.recipe.GCyRTags;
import argent_matter.gcyr.util.PlatformUtils;
import argent_matter.gcyr.util.PosWithState;
Expand All @@ -32,9 +33,13 @@
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.GlobalPos;
import net.minecraft.core.Registry;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet;
Expand All @@ -43,6 +48,8 @@
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
Expand Down Expand Up @@ -70,7 +77,6 @@
import java.util.List;
import java.util.Set;

@SuppressWarnings("resource")
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class RocketEntity extends Entity implements HasCustomInventoryScreen, IUIHolder {
Expand All @@ -85,16 +91,20 @@ public class RocketEntity extends Entity implements HasCustomInventoryScreen, IU


private final FluidStorage fuelTank;
private final ItemStackTransfer configSlot;
private final ItemStackTransfer configSlot, satelliteSlot;
private Planet destination;
private boolean destinationIsSpaceStation;
@Nullable
private GlobalPos lastPos;

private final Set<BlockPos> thrusterPositions = new HashSet<>();

public RocketEntity(EntityType<?> entityType, Level level) {
super(entityType, level);
this.configSlot = new ItemStackTransfer(1);
this.configSlot.setFilter(stack -> GCyRItems.ID_CHIP.isIn(stack) || GCyRItems.KEYCARD.isIn(stack));
this.satelliteSlot = new ItemStackTransfer(1);
this.satelliteSlot.setFilter(stack -> GCyRItems.SPACE_STATION_PACKAGE.isIn(stack) || stack.is(GCyRTags.SATELLITES));
//noinspection deprecation
this.fuelTank = new FluidStorage(0, fluid -> fluid.getFluid().is(GCyRTags.VEHICLE_FUELS));
}
Expand Down Expand Up @@ -161,7 +171,8 @@ public ModularUI createUI(Player entityPlayer) {
return new ModularUI(176, 166, this, entityPlayer)
.widget(new LabelWidget(7, 7, this.getDisplayName().getString()))
.widget(new TankWidget(this.fuelTank, 16, 20, 20, 58, true, true).setBackground(GuiTextures.FLUID_TANK_BACKGROUND))
.widget(new SlotWidget(configSlot, 0, 40, 20, true, true))
.widget(new SlotWidget(configSlot, 0, 40, 20))
.widget(new SlotWidget(satelliteSlot, 0, 60, 20))
.widget(new ButtonWidget(40, 60, 36, 18, new GuiTextureGroup(GuiTextures.BUTTON.copy().setColor(0xFFAA0000), new TextTexture("menu.gcyr.launch")), (clickData) -> this.startRocket()))
.widget(new ButtonWidget(40, 40, 36, 18, new GuiTextureGroup(GuiTextures.BUTTON.copy().setColor(0xFFE0B900), new TextTexture("gcyr.multiblock.rocket.unbuild")), (clickData) -> this.unBuild()))
.widget(UITemplate.bindPlayerInventory(entityPlayer.getInventory(), GuiTextures.SLOT, 7, 84, true))
Expand Down Expand Up @@ -298,11 +309,11 @@ public void startRocket() {
this.destination = KeyCardBehaviour.getSavedPlanet(config);
}

if (PlanetIdChipBehaviour.getSpaceStationId(config) != SpaceStation.ID_EMPTY || KeyCardBehaviour.getSavedStation(config) != SpaceStation.ID_EMPTY) {
if (PlanetIdChipBehaviour.getSpaceStationId(config) != null || KeyCardBehaviour.getSavedStation(config) != null) {
this.destinationIsSpaceStation = true;
}

if (this.destination.rocketTier() >= determineRocketTier() || (!destinationIsSpaceStation && this.level().dimension().location().equals(this.destination.level().location()))) return;
if (this.destination.rocketTier() >= determineRocketTier() || (!destinationIsSpaceStation && this.level().dimension().equals(this.destination.level()))) return;
data.set(RocketEntity.ROCKET_STARTED, true);
//GCyRSoundEntries.ROCKET.play(this.level(), null, this.getX(), this.getY(), this.getZ(), 1, 1);
this.level().playSound(null, this, GCyRSoundEntries.ROCKET.getMainEvent(), SoundSource.NEUTRAL, 1, 1);
Expand Down Expand Up @@ -412,11 +423,23 @@ private void goToDestination() {
ItemStack configStack = configSlot.getStackInSlot(0);
if (this.destination == null && GCyRItems.ID_CHIP.isIn(configStack)) {
this.destination = PlanetIdChipBehaviour.getPlanetFromStack(configStack);
} else if (GCyRItems.KEYCARD.isIn(configStack) && KeyCardBehaviour.getSavedStation(configStack) != SpaceStation.ID_EMPTY) {
} else if (GCyRItems.KEYCARD.isIn(configStack) && KeyCardBehaviour.getSavedStation(configStack) != null) {
this.destinationIsSpaceStation = true;
// return if no valid station & no station kit
if (!this.satelliteSlot.getStackInSlot(0).is(GCyRItems.SPACE_STATION_PACKAGE.get()) && GCyRCapabilityHelper.getSpaceStations(this.getServer().getLevel(destination.orbitWorld())).getStation(KeyCardBehaviour.getSavedStation(configStack)) == null) {
this.destination = null;
this.destinationIsSpaceStation = false;
this.entityData.set(ROCKET_STARTED, false);
this.setDeltaMovement(0, -0.5, 0);
return;
}
}
ResourceKey<Level> destinationDim = this.destinationIsSpaceStation ? destination.orbitWorld() : destination.level();
final ServerLevel destinationLevel = this.getServer().getLevel(destinationDim);
BlockPos destinationPos = null;
if (lastPos != null && lastPos.dimension().equals(destinationDim)) {
destinationPos = lastPos.pos();
}
final ServerLevel destinationLevel = this.getServer().getLevel(this.destinationIsSpaceStation ? destination.orbitWorld() : destination.level());


Vec3 pos = this.position();

Expand All @@ -443,7 +466,7 @@ private void goToDestination() {

if (this.destinationIsSpaceStation) {
ISpaceStationHolder stations = GCyRCapabilityHelper.getSpaceStations(destinationLevel);
int stationId;
Integer stationId;
boolean didChange = false;
if (GCyRItems.KEYCARD.isIn(configStack)) {
stationId = KeyCardBehaviour.getSavedStation(configStack);
Expand All @@ -464,7 +487,7 @@ private void goToDestination() {
didChange = true;
}
} else {
stationId = SpaceStation.ID_EMPTY;
stationId = null;
}

if (didChange) {
Expand All @@ -476,17 +499,32 @@ private void goToDestination() {
}

BlockPos stationPos = stations.getStationWorldPos(stationId);
newRocket.setPos(stationPos.getX(), pos.y, stationPos.getZ());
if (destinationPos == null) {
destinationPos = new BlockPos(stationPos.getX(), (int) pos.y, stationPos.getZ());
}

if (GCyRItems.SPACE_STATION_PACKAGE.isIn(this.satelliteSlot.getStackInSlot(0))) {
ItemStack stack = this.satelliteSlot.getStackInSlot(0);
buildSpaceStation(stack, new BlockPos(destinationPos.getX(), 64, destinationPos.getZ()));
}
} else {
double scale = DimensionType.getTeleportationScale(this.level().dimensionType(), destinationLevel.dimensionType());
newRocket.setPos(pos.multiply(scale, 1, scale));
if (destinationPos == null) {
destinationPos = BlockPos.containing(pos.multiply(scale, 1, scale));
}
}
newRocket.setPos(destinationPos.getX(), destinationPos.getY(), destinationPos.getZ());

Vec3 delta = this.getDeltaMovement();
newRocket.setDeltaMovement(delta.x, -0.5, delta.z);
if (newRocket instanceof RocketEntity rocketEntity) {
rocketEntity.destination = null;
rocketEntity.destinationIsSpaceStation = false;
if (this.destinationIsSpaceStation) {
rocketEntity.lastPos = GlobalPos.of(destinationDim, this.getOnPos());
} else {
rocketEntity.lastPos = null;
}
rocketEntity.entityData.set(ROCKET_STARTED, false);
rocketEntity.entityData.set(START_TIMER, 0);
}
Expand All @@ -502,11 +540,7 @@ public void unBuild() {
for (PosWithState state : this.getBlocks()) {
BlockPos offset = origin.offset(state.pos());
BlockHitResult result = new BlockHitResult(
new Vec3(
offset.getX() + 0.5,
offset.getY() + 0.5,
offset.getZ() + 0.5
),
offset.getCenter(),
Direction.DOWN,
offset,
false
Expand All @@ -521,6 +555,40 @@ public void unBuild() {
this.remove(RemovalReason.DISCARDED);
}

private void buildSpaceStation(ItemStack stack, BlockPos origin) {
if (!GCyRItems.SPACE_STATION_PACKAGE.isIn(stack)) return;
Set<PosWithState> blocks = StationContainerBehaviour.getSatelliteBlocks(stack);
if (blocks == null) return;

boolean start = true;
BlockPos original = origin;
for (PosWithState state : blocks) {
BlockPos pos = state.pos();
if (start) origin = pos;
start = false;
if (origin.compareTo(pos) < 0) origin = new BlockPos(
Math.min(pos.getX(), origin.getX()),
Math.min(pos.getY(), origin.getY()),
Math.min(pos.getZ(), origin.getZ()));
}
origin = new BlockPos(original.getX() - origin.getX() / 2, original.getY(), original.getZ() - origin.getZ() / 2);

for (PosWithState state : blocks) {
BlockPos offset = origin.offset(state.pos());
BlockHitResult result = new BlockHitResult(
offset.getCenter(),
Direction.DOWN,
offset,
false
);
if (!this.level().getBlockState(offset).isAir() && !this.level().getBlockState(offset).canBeReplaced(new BlockPlaceContext(this.level(), null, InteractionHand.MAIN_HAND, ItemStack.EMPTY, result))) {
this.spawnAtLocation(state.state().getBlock().asItem());
continue;
}
this.level().setBlock(offset, state.state(), Block.UPDATE_ALL);
}
}

@Override
public boolean isPickable() {
return !this.isRemoved();
Expand Down Expand Up @@ -657,6 +725,13 @@ protected void readAdditionalSaveData(CompoundTag compound) {
this.setStartTimer(compound.getInt("startTimer"));
this.entityData.set(ROCKET_STARTED, compound.getBoolean("isStarted"));
this.setWeight(compound.getInt("weight"));

if (compound.contains("lastPos")) {
CompoundTag tag = compound.getCompound("lastPos");
BlockPos pos = NbtUtils.readBlockPos(tag.getCompound("pos"));
ResourceKey<Level> level = ResourceKey.create(Registries.DIMENSION, new ResourceLocation(tag.getString("level")));
this.lastPos = GlobalPos.of(level, pos);
}
}

@Override
Expand All @@ -677,6 +752,13 @@ protected void addAdditionalSaveData(CompoundTag compound) {
compound.putInt("startTimer", this.getStartTimer());
compound.putBoolean("isStarted", this.entityData.get(ROCKET_STARTED));
compound.putInt("weight", this.getWeight());

if (this.lastPos != null) {
CompoundTag tag = new CompoundTag();
tag.put("pos", NbtUtils.writeBlockPos(this.lastPos.pos()));
tag.putString("level", this.lastPos.dimension().location().toString());
compound.put("lastPos", tag);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ public static void setOwner(ItemStack stack, LivingEntity entity) {
stack.getOrCreateTag().putUUID("KeyCardOwner", entity.getUUID());
}

public static void setSavedStation(ItemStack stack, int stationId, Planet planet) {
public static void setSavedStation(ItemStack stack, @Nullable Integer stationId, Planet planet) {
if (!GCyRItems.KEYCARD.isIn(stack)) return;
if (stationId == null) return;
stack.getOrCreateTag().putInt(PlanetIdChipBehaviour.CURRENT_STATION_KEY, stationId);
stack.getTag().putString(PlanetIdChipBehaviour.CURRENT_PLANET_KEY, PlanetData.getPlanetId(planet).toString());
}

public static int getSavedStation(ItemStack stack) {
if (!GCyRItems.KEYCARD.isIn(stack)) return SpaceStation.ID_EMPTY;
public static Integer getSavedStation(ItemStack stack) {
if (!GCyRItems.KEYCARD.isIn(stack)) return null;
return stack.getOrCreateTag().getInt(PlanetIdChipBehaviour.CURRENT_STATION_KEY);
}

Expand All @@ -71,8 +72,8 @@ public void appendHoverText(ItemStack stack, @Nullable Level level, List<Compone
if (currentTarget != null) {
tooltipComponents.add(Component.translatable("metaitem.planet_id_circuit.id").append(Component.translatable(currentTarget.translation())));
}
int currentStationId = getSavedStation(stack);
if (currentStationId != SpaceStation.ID_EMPTY) {
Integer currentStationId = getSavedStation(stack);
if (currentStationId != null) {
tooltipComponents.add(Component.translatable("metaitem.planet_id_circuit.station", currentStationId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public static void setSpaceStation(ItemStack held, int stationId) {
held.getOrCreateTag().putInt(CURRENT_STATION_KEY, stationId);
}

public static int getSpaceStationId(ItemStack held) {
if (!GCyRItems.ID_CHIP.isIn(held) || !held.getOrCreateTag().contains(CURRENT_STATION_KEY, Tag.TAG_INT)) return SpaceStation.ID_EMPTY;
@Nullable
public static Integer getSpaceStationId(ItemStack held) {
if (!GCyRItems.ID_CHIP.isIn(held) || !held.getOrCreateTag().contains(CURRENT_STATION_KEY, Tag.TAG_INT)) return null;
return held.getOrCreateTag().getInt(CURRENT_STATION_KEY);
}

Expand All @@ -59,8 +60,8 @@ public void appendHoverText(ItemStack stack, @Nullable Level level, List<Compone
if (currentTarget != null) {
tooltipComponents.add(Component.translatable("metaitem.planet_id_circuit.id").append(Component.translatable(currentTarget.translation())));
}
int currentStationId = getSpaceStationId(stack);
if (currentStationId != SpaceStation.ID_EMPTY) {
Integer currentStationId = getSpaceStationId(stack);
if (currentStationId != null) {
tooltipComponents.add(Component.translatable("metaitem.planet_id_circuit.station", currentStationId));
}

Expand Down
Loading

0 comments on commit 707a33d

Please sign in to comment.