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 11, 2023
1 parent e4946a7 commit 9e950f9
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 34 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 @@ -66,21 +66,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 @@ -42,16 +42,22 @@
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;
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
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 @@ -80,7 +86,6 @@
import java.util.List;
import java.util.Set;

@SuppressWarnings("resource")
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class RocketEntity extends Entity implements HasCustomInventoryScreen, IUIHolder {
Expand All @@ -95,7 +100,7 @@ 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;
private final Object2IntMap<IRocketPart> partCounts = new Object2IntOpenHashMap<>();
Expand All @@ -108,6 +113,8 @@ 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 @@ -174,7 +181,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 @@ -440,8 +448,16 @@ 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();
if (this.fuelTank.drain(computeRequiredFuelAmountForDestination(this.destination) / 3, true).isEmpty()) {
Expand Down Expand Up @@ -485,7 +501,7 @@ private void goToDestination() {
Vec3 pos = this.position();
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 @@ -506,7 +522,7 @@ private void goToDestination() {
didChange = true;
}
} else {
stationId = SpaceStation.ID_EMPTY;
stationId = null;
}

if (didChange) {
Expand All @@ -518,17 +534,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 = new BlockPos(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 @@ -544,11 +575,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
),
Vec3.atCenterOf(offset),
Direction.DOWN,
offset,
false
Expand Down Expand Up @@ -743,6 +770,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 @@ -763,6 +797,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 @@ -39,14 +39,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 @@ -72,8 +73,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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup;
import com.lowdragmc.lowdraglib.misc.ItemStackTransfer;
import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted;
import com.lowdragmc.lowdraglib.syncdata.field.ManagedFieldHolder;
import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand All @@ -31,6 +32,8 @@
import java.util.*;

public class SpaceStationPackagerMachine extends PlatformMultiblockMachine {
protected static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder(SpaceStationPackagerMachine.class, PlatformMultiblockMachine.MANAGED_FIELD_HOLDER);

@Getter
@Persisted
private final ItemStackTransfer packageSlot, keycardSlot, outputSlots;
Expand All @@ -39,23 +42,21 @@ public SpaceStationPackagerMachine(IMachineBlockEntity holder) {
super(holder);
this.packageSlot = new ItemStackTransfer(1);
this.packageSlot.setFilter(GCyRItems.SPACE_STATION_PACKAGE::isIn);

this.keycardSlot = new ItemStackTransfer(1);

this.outputSlots = new ItemStackTransfer(2);
}

@Override
public ModularUI createUI(Player entityPlayer) {
ModularUI modularUI = new ModularUI(176, 166, this, entityPlayer);
ModularUI modularUI = new ModularUI(176, 166, this, entityPlayer).background(GuiTextures.BACKGROUND);
modularUI.widget(new LabelWidget(4, 5, self().getBlockState().getBlock().getDescriptionId()));

WidgetGroup buttons = new WidgetGroup(7, (128-24*2-18), 0, 0);
WidgetGroup buttons = new WidgetGroup(7, 24, 0, 0);
//buttons.addWidget(new ButtonWidget(0, 0, 80, 24, GuiTextures.BUTTON.copy().setColor(0xFFAA0000), this::onBuildButtonClick));
buttons.addWidget(new ButtonWidget(0, 24+18, 80, 24, GuiTextures.BUTTON.copy().setColor(0xFFAA0000), this::onBuildButtonClick));
modularUI.widget(buttons);

WidgetGroup slots = new WidgetGroup(128, 60, 0, 0);
WidgetGroup slots = new WidgetGroup(128, 24, 0, 0);
slots.addWidget(new SlotWidget(packageSlot, 0, 0, 0));
slots.addWidget(new SlotWidget(keycardSlot, 0, 18, 0));
slots.addWidget(new SlotWidget(outputSlots, 0, 0, 22, true, false));
Expand All @@ -78,7 +79,6 @@ private void onBuildButtonClick(ClickData data) {
Planet thisPlanet = PlanetData.getPlanetFromLevel(this.getLevel().dimension()).orElse(null);
if (thisPlanet == null) return;


Direction back = this.getFrontFacing().getOpposite();
Direction left = this.getFrontFacing().getCounterClockWise();
Direction right = left.getOpposite();
Expand Down Expand Up @@ -133,6 +133,10 @@ private void onBuildButtonClick(ClickData data) {
ItemStack keycardStack = GCyRItems.KEYCARD.asStack();
KeyCardBehaviour.setSavedStation(keycardStack, spaceStationHolder.allocateStation(thisPlanet).getFirst(), thisPlanet);
outputSlots.setStackInSlot(1, keycardStack);
}

@Override
public ManagedFieldHolder getFieldHolder() {
return MANAGED_FIELD_HOLDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public void applyBiomeDecoration(WorldGenLevel level, ChunkAccess chunk, Structu
}
}

/*
if (chunkX % 16 == 0 && chunkZ % 16 == 0) {
//GCyR.LOGGER.info("made platform");
for(int x = -4; x < 4; ++x) {
Expand All @@ -126,7 +127,10 @@ public void applyBiomeDecoration(WorldGenLevel level, ChunkAccess chunk, Structu
level.setBlock(mutableBlockPos.set(blockX, PLATFORM_HEIGHT, blockZ), Blocks.GRAY_CONCRETE.defaultBlockState(), 2);
}
}
}else if ((chunkX % 16 != 8 && chunkZ % 16 == 8) || (chunkX % 16 != -8 && chunkZ % 16 == -8)) {
}else
*/
if ((chunkX % 16 != 8 && chunkZ % 16 == 8) || (chunkX % 16 != -8 && chunkZ % 16 == -8)) {

//GCyR.LOGGER.info("made Z edge");
for(int x = 0; x < 16; ++x) {
int blockX = SectionPos.sectionToBlockCoord(chunkX, x);
Expand Down
2 changes: 1 addition & 1 deletion fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ dependencies {

// LDLib
modImplementation(fabric.gtceu.fabric) { transitive = false }
modImplementation(fabric.ldlib.fabric) //{ transitive = false }
modImplementation(fabric.ldlib.fabric) { transitive = false }

// Registrate
modImplementation("com.tterrag.registrate_fabric:Registrate:${fabric.versions.registrate.get()}")
Expand Down
Loading

0 comments on commit 9e950f9

Please sign in to comment.