Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/portal' into portal
Browse files Browse the repository at this point in the history
  • Loading branch information
gemsb committed Aug 29, 2024
2 parents 3b01925 + e207818 commit f1189ff
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public Factory(SpriteProvider spriteProvider) {

@Nullable
@Override
public Particle createParticle(SimpleParticleType parameters, ClientWorld clientWorld, double x, double y, double z,double velocityX, double velocityY, double velocityZ) {
EnergyParticle particle = new EnergyParticle(clientWorld, x, y, z, 0.05f, 0.05f, 0.05f, velocityX, velocityY, velocityZ, 0.18f, this.spriteProvider, 1f, 200, 0f, false);
public Particle createParticle(SimpleParticleType parameters, ClientWorld clientWorld, double x, double y, double z,double velocityX, double gravityY, double velocityZ) {
EnergyParticle particle = new EnergyParticle(clientWorld, x, y, z, 0.05f, 0.05f, 0.05f, velocityX, 0, velocityZ, 0.18f, this.spriteProvider, 1f, 200, - (float)gravityY, false);
particle.setColor(1,1,1);
particle.maxAge = MathHelper.nextBetween(clientWorld.random, 200, 1000);
return particle;
Expand Down
248 changes: 153 additions & 95 deletions src/main/java/net/ugi/sculk_depths/block/custom/PedestalBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleEffect;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.server.world.ServerWorld;
Expand All @@ -27,13 +29,15 @@
import net.minecraft.world.chunk.ChunkStatus;
import net.ugi.sculk_depths.SculkDepths;
import net.ugi.sculk_depths.item.ModItems;
import net.ugi.sculk_depths.particle.ModParticleTypes;
import net.ugi.sculk_depths.portal.GenerateStructureAPI;
import net.ugi.sculk_depths.portal.Portal;
import net.ugi.sculk_depths.state.property.ModProperties;
import net.ugi.sculk_depths.world.dimension.ModDimensions;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.*;

Expand Down Expand Up @@ -61,68 +65,96 @@ protected MapCodec<? extends FacingBlock> getCodec() {
return CODEC;
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACING).add(HAS_ENERGY_ESSENCE);

public PedestalBlock(Settings settings) {
super(settings);
this.setDefaultState(this.getDefaultState().with(HAS_ENERGY_ESSENCE, false));
}

private Executor getAsyncExecutor() {
return CompletableFuture.delayedExecutor(0, TimeUnit.MILLISECONDS);
}

@Override
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
if (portalFase == "checkFrame") {
portalFramePos = Portal.getFramePos(state.get(FACING),pos, world);//todo benchmark
if (portalFramePos == null) {portalFase = "none";return;}
if (portalFramePos[0] == null) {portalFase = "none";return;}

portalFase = "genFrame";
posArray = portalFramePos;
protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (world.isClient()){
return ItemActionResult.FAIL;
}
if (stack.getItem() == ModItems.ENERGY_ESSENCE && !state.get(ModProperties.HAS_ENERGY_ESSENCE)){
Portal.addBlockPowerUpParticle((ServerWorld) world, pos, Random.create(), 10);
stack.decrementUnlessCreative(1,player);
world.playSound(null, pos, SoundEvents.BLOCK_AMETHYST_BLOCK_RESONATE, SoundCategory.BLOCKS, 1.0f, 1.0f);

BlockState blockState1 = state.with(HAS_ENERGY_ESSENCE, true);
world.setBlockState(pos, blockState1);
portalFase = "checkFrame";
world.scheduleBlockTick(pos,state.getBlock(),1);
return ItemActionResult.SUCCESS;
}
if (portalFase == "genFrame") {
posArray = Portal.genFrameStep(world, posArray, random);
if (posArray[posArray.length -1].getY() == -4096 && posArray[posArray.length -1].getZ() == 1){
posArray = new BlockPos[0];
for (BlockPos pos1: portalFramePos) {
posArray = Portal.addElement(posArray,pos1.up(3));
posArray = Portal.addElement(posArray,pos1.up(4));
}
portalFase = "genStructure";
}
else if (posArray[posArray.length -1].getY() == -4096 && posArray[posArray.length -1].getZ() == 0){
return ItemActionResult.FAIL;
}

@Override
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
switch (portalFase){
case "checkFrame":

portalFramePos = Portal.getFramePos(state.get(FACING),pos, world);//todo benchmark
if (portalFramePos == null) {portalFase = "none";return;}
if (portalFramePos[0] == null) {portalFase = "none";return;}

portalFase = "genFrame";
posArray = portalFramePos;
portalFase = "cancelFrame";
}
world.scheduleBlockTick(pos,state.getBlock(),2);
}
if (portalFase == "cancelFrame") {
posArray = Portal.cancelFrameStep(world,posArray);
if (posArray[posArray.length -1].getY() == -4096 && posArray[posArray.length -1].getZ() == 0){
posArray = new BlockPos[0];
portalFase = "none";
}
else {

world.scheduleBlockTick(pos,state.getBlock(),1);
break;


case "genFrame":

posArray = Portal.genFrameStep(world, posArray, random);
if (posArray[posArray.length -1].getY() == -4096 && posArray[posArray.length -1].getZ() == 1){
posArray = new BlockPos[0];
for (BlockPos pos1: portalFramePos) {
posArray = Portal.addElement(posArray,pos1.up(3));
posArray = Portal.addElement(posArray,pos1.up(4));
}
portalFase = "genStructure";
}
else if (posArray[posArray.length -1].getY() == -4096 && posArray[posArray.length -1].getZ() == 0){
posArray = portalFramePos;
portalFase = "cancelFrame";
}
world.scheduleBlockTick(pos,state.getBlock(),2);
}
}
break;


case "cancelFrame":

posArray = Portal.cancelFrameStep(world,posArray);
if (posArray[posArray.length -1].getY() == -4096 && posArray[posArray.length -1].getZ() == 0){
posArray = new BlockPos[0];
portalFase = "none";
}
else {
world.scheduleBlockTick(pos,state.getBlock(),2);
}
break;


if(portalFase == "genStructure"){
BlockPos anchor = Portal.getFrameAnchorPos(state.get(FACING),pos, world);
case "genStructure":

structureStart = GenerateStructureAPI.structureStart(world, ModDimensions.SCULK_DEPTHS_LEVEL_KEY, SculkDepths.identifier("portal_structure"), anchor); //50ms (matteo)
BlockPos anchor = Portal.getFrameAnchorPos(state.get(FACING),pos, world);

BlockBox boundingBox = structureStart.getBoundingBox();
structureStart = GenerateStructureAPI.structureStart(world, ModDimensions.SCULK_DEPTHS_LEVEL_KEY, SculkDepths.identifier("portal_structure"), anchor); //50ms (matteo)

chunkArray = GenerateStructureAPI.generateChunkArray(//not laggy
new ChunkPos(ChunkSectionPos.getSectionCoord(boundingBox.getMinX()),ChunkSectionPos.getSectionCoord(boundingBox.getMinZ())),
new ChunkPos(ChunkSectionPos.getSectionCoord(boundingBox.getMaxX()),ChunkSectionPos.getSectionCoord(boundingBox.getMaxZ())),
0,1);
BlockBox boundingBox = structureStart.getBoundingBox();

ServerWorld serverWorld = world.getServer().getWorld(ModDimensions.SCULK_DEPTHS_LEVEL_KEY);
chunkArray = GenerateStructureAPI.generateChunkArray(//not laggy
new ChunkPos(ChunkSectionPos.getSectionCoord(boundingBox.getMinX()),ChunkSectionPos.getSectionCoord(boundingBox.getMinZ())),
new ChunkPos(ChunkSectionPos.getSectionCoord(boundingBox.getMaxX()),ChunkSectionPos.getSectionCoord(boundingBox.getMaxZ())),
0,1);

ServerWorld serverWorld = world.getServer().getWorld(ModDimensions.SCULK_DEPTHS_LEVEL_KEY);



Expand All @@ -144,43 +176,84 @@ else if (posArray[posArray.length -1].getY() == -4096 && posArray[posArray.lengt
executorService.shutdown();
});*/

CompletableFuture.runAsync(() -> {
for (ChunkPos chunkPos : chunkArray) {//load chunks
assert serverWorld != null;
serverWorld.getChunk(chunkPos.x, chunkPos.z);
}
GenerateStructureAPI.forceLoadNearbyChunks(chunkArray, serverWorld);
CompletableFuture.runAsync(() -> {
for (ChunkPos chunkPos : chunkArray) {//load chunks
assert serverWorld != null;
serverWorld.getChunk(chunkPos.x, chunkPos.z);
}
GenerateStructureAPI.forceLoadNearbyChunks(chunkArray, serverWorld);

}, getAsyncExecutor()).thenRunAsync(() -> {
// This code runs back on the main server thread after the chunks are loaded
GenerateStructureAPI.generateStructurePartial(world, ModDimensions.SCULK_DEPTHS_LEVEL_KEY, SculkDepths.identifier("portal_structure"), structureStart, boundingBox.streamChunkPos().toArray(ChunkPos[]::new));
portalFase = "genPortal";
}, world.getServer());
portalFase = "waitingParticles";
world.scheduleBlockTick(pos,state.getBlock(),1);
}
if (portalFase == "waitingParticles") {
}, getAsyncExecutor()).thenRunAsync(() -> {
// This code runs back on the main server thread after the chunks are loaded
GenerateStructureAPI.generateStructurePartial(world, ModDimensions.SCULK_DEPTHS_LEVEL_KEY, SculkDepths.identifier("portal_structure"), structureStart, boundingBox.streamChunkPos().toArray(ChunkPos[]::new));
portalFase = "genPortal";
}, world.getServer());
portalFase = "waitingParticles";
world.scheduleBlockTick(pos,state.getBlock(),1);
break;

Portal.addPortalStartAttemptParticle(world, pos, random, 10);
world.scheduleBlockTick(pos,state.getBlock(),1);

}
case "waitingParticles":

if (portalFase == "genPortal") {
posArray = Portal.genPortalStep(world,posArray,state.get(FACING), random);
if (posArray[posArray.length -1].getY() == -4096 && posArray[posArray.length -1].getZ() == 0){
posArray = new BlockPos[0];
portalFase = "none";
}
else {
world.scheduleBlockTick(pos,state.getBlock(),2);
}
Portal.addPortalStartAttemptParticle(world, pos, random, 10);
world.scheduleBlockTick(pos,state.getBlock(),1);
break;


case "genPortal":

posArray = Portal.genPortalStep(world,posArray,state.get(FACING), random);
if (posArray[posArray.length -1].getY() == -4096 && posArray[posArray.length -1].getZ() == 0){
posArray = new BlockPos[0];
portalFase = "none";
}
else {
world.scheduleBlockTick(pos,state.getBlock(),2);
}
break;
}
}

public PedestalBlock(Settings settings) {
super(settings);
this.setDefaultState(this.getDefaultState().with(HAS_ENERGY_ESSENCE, false));
public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) {
if(state.get(ModProperties.HAS_ENERGY_ESSENCE)){
int randomNumber = random.nextBetween(0, 5);
float x = pos.getX() + 0.5f;
float y = pos.getY() + 0.5f;
float z = pos.getZ() + 0.5f;
switch (randomNumber) {
case 0://positivex
x = x + 0.6f + MathHelper.nextFloat(random, 0f, 0.2f);
y = y + MathHelper.nextFloat(random, -0.6f, 0.6f);
z = z + MathHelper.nextFloat(random, -0.6f, 0.6f);
break;
case 1://negativex
x = x - 0.6f - MathHelper.nextFloat(random, 0f, 0.2f);
y = y + MathHelper.nextFloat(random, -0.6f, 0.6f);
z = z + MathHelper.nextFloat(random, -0.6f, 0.6f);
break;
case 2://positivivez
x = x + MathHelper.nextFloat(random, -0.6f, 0.6f);
y = y + MathHelper.nextFloat(random, -0.6f, 0.6f);
z = z + 0.6f + MathHelper.nextFloat(random, 0f, 0.2f);
break;
case 3://negativez
x = x + MathHelper.nextFloat(random, -0.6f, 0.6f);
y = y + MathHelper.nextFloat(random, -0.6f, 0.6f);
z = z - 0.6f - MathHelper.nextFloat(random, 0f, 0.2f);
break;
case 4://positivey
x = x + MathHelper.nextFloat(random, -0.6f, 0.6f);
y = y + 0.6f + MathHelper.nextFloat(random, 0f, 0.2f);
z = z + MathHelper.nextFloat(random, -0.6f, 0.6f);
break;
case 5://negativey
x = x + MathHelper.nextFloat(random, -0.6f, 0.6f);
y = y - 0.6f - MathHelper.nextFloat(random, 0f, 0.2f);
z = z + MathHelper.nextFloat(random, -0.6f, 0.6f);
break;
}
world.addImportantParticle((ParticleEffect) ModParticleTypes.ENERGY_PARTICLE, false, x, y, z, 0, 0.01, 0);
}
}

@Override
Expand All @@ -195,26 +268,6 @@ public BlockState mirror(BlockState blockState, BlockMirror mirror) {
}



@Override
protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (world.isClient()){
return ItemActionResult.FAIL;
}
if (stack.getItem() == ModItems.ENERGY_ESSENCE && !state.get(ModProperties.HAS_ENERGY_ESSENCE)){
Portal.addBlockPowerUpParticle((ServerWorld) world, pos, Random.create(), 10);
stack.decrementUnlessCreative(1,player);
world.playSound(null, pos, SoundEvents.BLOCK_AMETHYST_BLOCK_RESONATE, SoundCategory.BLOCKS, 1.0f, 1.0f);

BlockState blockState1 = state.with(HAS_ENERGY_ESSENCE, true);
world.setBlockState(pos, blockState1);
portalFase = "checkFrame";
world.scheduleBlockTick(pos,state.getBlock(),1);
return ItemActionResult.SUCCESS;
}
return ItemActionResult.FAIL;
}

@Override
protected VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return OUTLINE_SHAPE;
Expand All @@ -229,4 +282,9 @@ public static VoxelShape createCuboidShape(double minX, double minY, double minZ
return VoxelShapes.cuboid(minX / 16.0, minY / 16.0, minZ / 16.0, maxX / 16.0, maxY / 16.0, maxZ / 16.0);
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACING).add(HAS_ENERGY_ESSENCE);
}

}
2 changes: 1 addition & 1 deletion src/main/java/net/ugi/sculk_depths/portal/Portal.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public static void addBlockPowerUpParticle(ServerWorld world, BlockPos pos, Rand
//System.out.println(" " + x + " " + y + " "+z);
List<ServerPlayerEntity> playerEntityList = world.getPlayers(serverPlayerEntity -> serverPlayerEntity.isInRange(serverPlayerEntity, 100, 50));//todo test range
for (ServerPlayerEntity serverPlayerEntity : playerEntityList) {
world.spawnParticles(serverPlayerEntity, (ParticleEffect) ModParticleTypes.ENERGY_PARTICLE, true, x, y, z, 4, 0.1, -0.1, 0.1, 0);
world.spawnParticles(serverPlayerEntity, (ParticleEffect) ModParticleTypes.ENERGY_PARTICLE, true, x, y, z, 4, 0.1, 0, 0.1, 0);
}

//world.addImportantParticle((ParticleEffect) ModParticleTypes.ENERGY_PARTICLE, false, x, y, z, 0, 0, 0);
Expand Down

0 comments on commit f1189ff

Please sign in to comment.