generated from neoforged/MDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sample-site): fixed sample site desync on block break
- Fixed desync on multiple players waiting for sample site placement - Improved Payload Codec to include chunk position - Added BlockEvents handler - Added helper classes for complex data management and mutations over final records
- Loading branch information
Showing
8 changed files
with
321 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/org/darkar/cog_works/block/events/BlockEvents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.darkar.cog_works.block.events; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.LevelAccessor; | ||
import net.minecraft.world.level.chunk.ChunkAccess; | ||
import net.neoforged.bus.api.SubscribeEvent; | ||
import net.neoforged.fml.common.EventBusSubscriber; | ||
import net.neoforged.neoforge.event.level.BlockEvent; | ||
import net.neoforged.neoforge.network.PacketDistributor; | ||
import org.darkar.cog_works.level.chunk.attachment.ChunkSampleSiteMap; | ||
import org.darkar.cog_works.net.payload.client.ClientSampleSiteMapUpdatePayload; | ||
import org.darkar.cog_works.utils.Data.AttachmentUtils.ChunkSampleSiteMapUtil; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.darkar.cog_works.CogWorks.MOD_ID; | ||
import static org.darkar.cog_works.Registry.DataAttachments.CHUNK_SAMPLE_SITE_MAP; | ||
|
||
@EventBusSubscriber(modid = MOD_ID) | ||
public class BlockEvents { | ||
|
||
/** | ||
* Handles the logic to update the {@link ChunkSampleSiteMap} when a block is broken and is a sample site. | ||
* | ||
* @param levelAccessor level where the block is broken | ||
* @param pos position of the block that was broken | ||
* @param player player that broke the block | ||
*/ | ||
private static void handleChunkSampleSiteMap(LevelAccessor levelAccessor, BlockPos pos, Player player) { | ||
ChunkAccess chunk = levelAccessor.getChunk(pos); | ||
Level level = player.level(); | ||
if (level.isClientSide()) return; | ||
Optional<ClientSampleSiteMapUpdatePayload> payload = Optional.empty(); | ||
if (!chunk.hasAttachments() || !chunk.hasData(CHUNK_SAMPLE_SITE_MAP)) return; | ||
ChunkSampleSiteMap chunkSampleSiteMap = chunk.getData(CHUNK_SAMPLE_SITE_MAP); | ||
if (!ChunkSampleSiteMapUtil.isDirty(chunkSampleSiteMap)) return; | ||
|
||
if (ChunkSampleSiteMapUtil.isSurface(chunkSampleSiteMap, pos)) { | ||
ChunkSampleSiteMap newChunkSampleSiteMap = ChunkSampleSiteMapUtil.resetDefaultSurface(chunkSampleSiteMap); | ||
chunk.setData(CHUNK_SAMPLE_SITE_MAP, newChunkSampleSiteMap); | ||
payload = Optional.of(new ClientSampleSiteMapUpdatePayload(chunk.getPos(), | ||
newChunkSampleSiteMap)); | ||
|
||
} | ||
|
||
if (ChunkSampleSiteMapUtil.isDeep(chunkSampleSiteMap, pos)) { | ||
ChunkSampleSiteMap newChunkSampleSiteMap = ChunkSampleSiteMapUtil.resetDefaultDeep(chunkSampleSiteMap); | ||
chunk.setData(CHUNK_SAMPLE_SITE_MAP, newChunkSampleSiteMap); | ||
payload = Optional.of(new ClientSampleSiteMapUpdatePayload(chunk.getPos(), | ||
newChunkSampleSiteMap)); | ||
} | ||
|
||
|
||
payload.ifPresent(updatePayload -> { | ||
PacketDistributor.sendToPlayersTrackingChunk((ServerLevel) level, chunk.getPos(), updatePayload); | ||
}); | ||
|
||
} | ||
|
||
@SubscribeEvent | ||
public static void onBlockBreak(final BlockEvent.BreakEvent event) { | ||
Player player = event.getPlayer(); | ||
LevelAccessor level = event.getLevel(); | ||
BlockPos breakPos = event.getPos(); | ||
|
||
if (!level.isClientSide()) { | ||
handleChunkSampleSiteMap(level, breakPos, player); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.