Skip to content

Commit

Permalink
Chests work now!
Browse files Browse the repository at this point in the history
  • Loading branch information
ellieisjelly committed Jan 6, 2024
1 parent 65582b5 commit 9ac25ab
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


public class SabotageBlocks {
public static final Block SABOTAGE_CHEST = new SabotageChest(AbstractBlock.Settings.copy(Blocks.CHEST).dropsNothing(), Blocks.CHEST);
public static final SabotageChest SABOTAGE_CHEST = new SabotageChest(AbstractBlock.Settings.copy(Blocks.CHEST).dropsNothing(), Blocks.CHEST);

public static final BlockEntityType<SabotageChestBlockEntity> SABOTAGE_CHEST_ENTITY = FabricBlockEntityTypeBuilder.create(SabotageChestBlockEntity::new, SABOTAGE_CHEST).build(null);
public static void register() {
Expand All @@ -44,14 +44,15 @@ private static ActionResult onBlockUse(PlayerEntity plr, World world, Hand hand,
break;
}
}
if (isInGame) {
if (blockHitResult != null) {
Block block = world.getBlockState(blockHitResult.getBlockPos()).getBlock();
if (block instanceof SabotageChest) {

if (blockHitResult != null) {
Block block = world.getBlockState(blockHitResult.getBlockPos()).getBlock();
if (block instanceof SabotageChest) {
if (isInGame) {
plr.playSound(SoundEvents.BLOCK_CHEST_CLOSE, SoundCategory.BLOCKS, 1, 1.2f);
world.setBlockState(blockHitResult.getBlockPos(), Blocks.AIR.getDefaultState());
return ActionResult.FAIL;
}
return ActionResult.FAIL;
}
}
return ActionResult.PASS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ public Block getPolymerBlock(BlockState state) {
return this.virtualBlock;
}

public Block getPolymerBlock() {
return this.virtualBlock;
}

@Override
public BlockState getPolymerBlockState(BlockState state) {
return this.virtualBlock.getStateWithProperties(state);
}

public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new SabotageChestBlockEntity(pos, state);
}
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/me/ellieis/Sabotage/game/map/SabotageMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import me.ellieis.Sabotage.game.config.SabotageConfig;
import me.ellieis.Sabotage.game.custom.blocks.SabotageChest;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import xyz.nucleoid.map_templates.BlockBounds;
import xyz.nucleoid.map_templates.MapTemplate;
import xyz.nucleoid.map_templates.TemplateRegion;
import xyz.nucleoid.plasmid.game.GameOpenException;
Expand All @@ -21,46 +24,53 @@
import java.util.*;

import static me.ellieis.Sabotage.game.custom.SabotageBlocks.SABOTAGE_CHEST;
record ChestInfo(BlockPos pos, Direction direction) {

}
public class SabotageMap {
private final SabotageConfig config;
private final MapTemplate template;
private final List<TemplateRegion> spawns;
private final List<ChestInfo> chestSpawns;
private final Map<PlayerRef, Vec3d> playerSpawnPos = new HashMap<>();
private ServerWorld world;

public SabotageMap(MapTemplate template, SabotageConfig config) {
this.config = config;
this.template = template;
this.spawns = template.getMetadata().getRegions("spawn").toList();
this.chestSpawns = new ArrayList<>();
if (this.spawns.isEmpty()) {
throw new GameOpenException(Text.literal("Failed to load spawns"));
}

// generate chest positions from placed SabotageChests
template.getBounds().forEach(blockPos -> {
Block block = template.getBlockState(blockPos).getBlock();
BlockState blockState = template.getBlockState(blockPos);
Block block = blockState.getBlock();
if (block instanceof SabotageChest) {
template.getMetadata().addRegion("chest", BlockBounds.ofBlock(blockPos));
// note to self: make your positions immutable in forEach loops..
chestSpawns.add(new ChestInfo(blockPos.toImmutable(), blockState.get(Properties.HORIZONTAL_FACING)));
template.setBlockState(blockPos, Blocks.AIR.getDefaultState());
}
});
}
public void setWorld(ServerWorld world) {
this.world = world;
}
public void generateChests() {
// Have to make a new ArrayList to make it mutable
List<TemplateRegion> chestSpawns = new ArrayList<>(template.getMetadata().getRegions("chest").toList());
Collections.shuffle(chestSpawns);

// Make sure that the chest count doesn't go over the amount of chest positions
int chestCount = Math.min(config.chestCount(), chestSpawns.size());
for (TemplateRegion region : chestSpawns) {
for (ChestInfo chestInfo : chestSpawns) {
if (chestCount > 0) {
chestCount--;
template.setBlockState(region.getBounds().min(), SABOTAGE_CHEST.getDefaultState());
world.setBlockState(chestInfo.pos(), SABOTAGE_CHEST.getDefaultState().with(Properties.HORIZONTAL_FACING, chestInfo.direction()));
} else {
break;
}
}
// to-do: generate chests from added chest regions
}
public MapTemplate getTemplate() {
return this.template;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ public static void Open(GameSpace gameSpace, ServerWorld world, SabotageMap map,
activity.listen(GamePlayerEvents.REMOVE, game::onPlayerRemove);
activity.listen(GamePlayerEvents.OFFER, game::onOffer);
activity.listen(GameActivityEvents.DESTROY, game::onDestroy);
map.setWorld(world);
map.generateChests();
PlayerSet plrs = game.gameSpace.getPlayers();
plrs.showTitle(Text.literal(Integer.toString(game.config.countdownTime())).formatted(Formatting.GOLD), 20);
Expand Down
Binary file modified src/main/resources/data/sabotage/map_templates/testmap.nbt
Binary file not shown.

0 comments on commit 9ac25ab

Please sign in to comment.