Skip to content

Commit

Permalink
Add light to converted floor blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Jul 20, 2024
1 parent 22dde54 commit 0cf42d1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/main/java/io/github/haykam821/electricfloor/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

import io.github.haykam821.electricfloor.game.ElectricFloorConfig;
import io.github.haykam821.electricfloor.game.phase.ElectricFloorWaitingPhase;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.fabricmc.api.ModInitializer;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.LightBlock;
import net.minecraft.util.Identifier;
import xyz.nucleoid.plasmid.game.GameType;
import xyz.nucleoid.plasmid.game.stats.StatisticKey;
Expand All @@ -18,6 +21,7 @@ public class Main implements ModInitializer {

public static final Block SPAWN_PLATFORM = Blocks.RED_TERRACOTTA;
public static final Map<Block, Block> FLOOR_CONVERSIONS = new HashMap<>();
public static final Object2IntMap<Block> FLOOR_LIGHT = new Object2IntOpenHashMap<>();

private static final Identifier ELECTRIC_FLOOR_ID = new Identifier(MOD_ID, "electric_floor");
public static final GameType<ElectricFloorConfig> ELECTRIC_FLOOR_TYPE = GameType.register(ELECTRIC_FLOOR_ID, ElectricFloorConfig.CODEC, ElectricFloorWaitingPhase::open);
Expand All @@ -40,11 +44,27 @@ public static boolean isConvertible(BlockState state) {
return FLOOR_CONVERSIONS.containsKey(state.getBlock());
}

public static BlockState getFloorLightState(BlockState state, boolean night) {
if (night) {
int light = FLOOR_LIGHT.getInt(state.getBlock());

if (light > 0) {
return Blocks.LIGHT.getDefaultState().with(LightBlock.LEVEL_15, light);
}
}

return null;
}

static {
FLOOR_CONVERSIONS.put(SPAWN_PLATFORM, Blocks.WHITE_STAINED_GLASS);
FLOOR_CONVERSIONS.put(Blocks.WHITE_STAINED_GLASS, Blocks.LIGHT_BLUE_STAINED_GLASS);
FLOOR_CONVERSIONS.put(Blocks.LIGHT_BLUE_STAINED_GLASS, Blocks.MAGENTA_STAINED_GLASS);
FLOOR_CONVERSIONS.put(Blocks.MAGENTA_STAINED_GLASS, Blocks.RED_STAINED_GLASS);
FLOOR_CONVERSIONS.put(Blocks.RED_STAINED_GLASS, null);

FLOOR_LIGHT.put(Blocks.LIGHT_BLUE_STAINED_GLASS, 5);
FLOOR_LIGHT.put(Blocks.MAGENTA_STAINED_GLASS, 10);
FLOOR_LIGHT.put(Blocks.RED_STAINED_GLASS, 15);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ElectricFloorConfig {
IntProvider.NON_NEGATIVE_CODEC.optionalFieldOf("ticks_until_close", ConstantIntProvider.create(SharedConstants.TICKS_PER_SECOND * 5)).forGetter(ElectricFloorConfig::getTicksUntilClose),
Codec.INT.optionalFieldOf("spawn_platform_delay", 20 * 2).forGetter(ElectricFloorConfig::getSpawnPlatformDelay),
Codec.INT.optionalFieldOf("delay", 5).forGetter(ElectricFloorConfig::getDelay),
Codec.BOOL.optionalFieldOf("night", true).forGetter(ElectricFloorConfig::isNight),
GameStatisticBundle.NAMESPACE_CODEC.optionalFieldOf("statistic_bundle_namespace").forGetter(ElectricFloorConfig::getStatisticBundleNamespace)
).apply(instance, ElectricFloorConfig::new);
});
Expand All @@ -32,15 +33,17 @@ public class ElectricFloorConfig {
private final IntProvider ticksUntilClose;
private final int spawnPlatformDelay;
private final int delay;
private final boolean night;
private final Optional<String> statisticBundleNamespace;

public ElectricFloorConfig(ElectricFloorMapConfig mapConfig, PlayerConfig playerConfig, int guideTicks, IntProvider ticksUntilClose, int spawnPlatformDelay, int delay, Optional<String> statisticBundleNamespace) {
public ElectricFloorConfig(ElectricFloorMapConfig mapConfig, PlayerConfig playerConfig, int guideTicks, IntProvider ticksUntilClose, int spawnPlatformDelay, int delay, boolean night, Optional<String> statisticBundleNamespace) {
this.mapConfig = mapConfig;
this.playerConfig = playerConfig;
this.guideTicks = guideTicks;
this.ticksUntilClose = ticksUntilClose;
this.spawnPlatformDelay = spawnPlatformDelay;
this.delay = delay;
this.night = night;
this.statisticBundleNamespace = statisticBundleNamespace;
}

Expand Down Expand Up @@ -68,6 +71,10 @@ public int getDelay() {
return this.delay;
}

public boolean isNight() {
return this.night;
}

public Optional<String> getStatisticBundleNamespace() {
return this.statisticBundleNamespace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import eu.pb4.polymer.virtualentity.api.ElementHolder;
import eu.pb4.polymer.virtualentity.api.elements.TextDisplayElement;
import net.minecraft.entity.decoration.Brightness;
import net.minecraft.entity.decoration.DisplayEntity.BillboardMode;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.Text;
Expand All @@ -24,13 +25,17 @@ private ElectricFloorGuideText() {
return;
}

public static ElementHolder createElementHolder() {
public static ElementHolder createElementHolder(boolean night) {
TextDisplayElement element = new TextDisplayElement(TEXT);

element.setBillboardMode(BillboardMode.CENTER);
element.setLineWidth(350);
element.setInvisible(true);

if (night) {
element.setBrightness(Brightness.FULL);
}

ElementHolder holder = new ElementHolder();
holder.addElement(element);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.haykam821.electricfloor.game.map;

import io.github.haykam821.electricfloor.Main;
import io.github.haykam821.electricfloor.game.ElectricFloorConfig;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
Expand Down Expand Up @@ -49,10 +50,19 @@ private BlockState getBlockState(BlockPos pos, BlockBounds bounds, ElectricFloor
}

public void build(BlockBounds bounds, MapTemplate template, ElectricFloorMapConfig mapConfig) {
BlockPos.Mutable upPos = new BlockPos.Mutable();

for (BlockPos pos : bounds) {
BlockState state = this.getBlockState(pos, bounds, mapConfig);
if (state != null) {
template.setBlockState(pos, state);

BlockState lightState = Main.getFloorLightState(state, this.config.isNight());

if (lightState != null) {
upPos.set(pos.getX(), pos.getY() + 1, pos.getZ());
template.setBlockState(upPos, lightState);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void enable() {

// Create spawn platform
for (BlockPos pos : BlockPos.iterate((int) x - 1, 0, (int) z - 1, (int) x, 0, (int) z)) {
this.world.setBlockState(pos, Main.SPAWN_PLATFORM.getDefaultState());
this.setBlockState(pos, Main.SPAWN_PLATFORM.getDefaultState());
this.convertPositions.putIfAbsent(pos.asLong(), this.config.getSpawnPlatformDelay());
}
}
Expand Down Expand Up @@ -146,7 +146,7 @@ public void tick() {
pos.set(convertPos);

BlockState state = this.world.getBlockState(pos);
this.world.setBlockState(pos, Main.getConvertedFloor(state));
this.setBlockState(pos, Main.getConvertedFloor(state));

iterator.remove();
} else {
Expand Down Expand Up @@ -260,4 +260,14 @@ private void endGame() {
private boolean isGameEnding() {
return this.ticksUntilClose >= 0;
}

private void setBlockState(BlockPos pos, BlockState state) {
this.world.setBlockState(pos, state);

BlockState lightState = Main.getFloorLightState(state, this.config.isNight());

if (lightState != null) {
this.world.setBlockState(pos.up(), lightState);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.github.haykam821.electricfloor.game.map.ElectricFloorMap;
import io.github.haykam821.electricfloor.game.map.ElectricFloorMapBuilder;
import io.github.haykam821.electricfloor.game.map.ElectricFloorGuideText;
import net.minecraft.SharedConstants;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
Expand All @@ -26,6 +27,8 @@
import xyz.nucleoid.stimuli.event.player.PlayerDeathEvent;

public class ElectricFloorWaitingPhase {
private static final int NIGHT_TICKS = SharedConstants.TICKS_PER_MINUTE * 15;

private final GameSpace gameSpace;
private final ServerWorld world;
private final ElectricFloorMap map;
Expand All @@ -41,16 +44,22 @@ public ElectricFloorWaitingPhase(GameSpace gameSpace, ServerWorld world, Electri
}

public static GameOpenProcedure open(GameOpenContext<ElectricFloorConfig> context) {
ElectricFloorMapBuilder mapBuilder = new ElectricFloorMapBuilder(context.config());
ElectricFloorConfig config = context.config();

ElectricFloorMapBuilder mapBuilder = new ElectricFloorMapBuilder(config);
ElectricFloorMap map = mapBuilder.create();

RuntimeWorldConfig worldConfig = new RuntimeWorldConfig()
.setGenerator(map.createGenerator(context.server()));

if (config.isNight()) {
worldConfig.setTimeOfDay(NIGHT_TICKS);
}

return context.openWithWorld(worldConfig, (activity, world) -> {
ElectricFloorWaitingPhase phase = new ElectricFloorWaitingPhase(activity.getGameSpace(), world, map, context.config());
ElectricFloorWaitingPhase phase = new ElectricFloorWaitingPhase(activity.getGameSpace(), world, map, config);

GameWaitingLobby.addTo(activity, context.config().getPlayerConfig());
GameWaitingLobby.addTo(activity, config.getPlayerConfig());
ElectricFloorActivePhase.setRules(activity);

// Listeners
Expand All @@ -66,7 +75,7 @@ private void enable() {
Vec3d guideTextPos = this.map.getGuideTextPos();

if (guideTextPos != null) {
ElementHolder holder = ElectricFloorGuideText.createElementHolder();
ElementHolder holder = ElectricFloorGuideText.createElementHolder(this.config.isNight());
this.guideText = ChunkAttachment.of(holder, world, guideTextPos);
}
}
Expand Down

0 comments on commit 0cf42d1

Please sign in to comment.