Skip to content

Commit

Permalink
Off World Storage prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
cam72cam committed Aug 25, 2024
1 parent 71c10ce commit fabef9a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/cam72cam/mod/event/CommonEvents.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cam72cam.mod.event;

import cam72cam.mod.ModCore;
import cam72cam.mod.world.World;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.util.math.BlockPos;
Expand All @@ -13,6 +14,8 @@
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.registry.EntityEntry;

import java.io.File;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/** Registry of events that fire off on both client and server. Do not use directly! */
Expand All @@ -27,6 +30,11 @@ public static final class World {
public static final Event<Consumer<net.minecraft.world.World>> UNLOAD = new Event<>();
public static final Event<Consumer<net.minecraft.world.World>> TICK = new Event<>();
}
public static final class WorldData {
public static final Event<BiConsumer<cam72cam.mod.world.World, File>> LOAD = new Event<>();
public static final Event<BiConsumer<cam72cam.mod.world.World, File>> SAVE = new Event<>();
public static final Event<BiConsumer<cam72cam.mod.world.World, File>> UNLOAD = new Event<>();
}

public static final class Block {
public static final Event<Runnable> REGISTER = new Event<>();
Expand Down Expand Up @@ -56,11 +64,31 @@ public static final class EventBus {
@SubscribeEvent
public static void onWorldLoad(WorldEvent.Load event) {
World.LOAD.execute(x -> x.accept(event.getWorld()));
if (!event.getWorld().isRemote) {
WorldData.LOAD.execute(x -> x.accept(
cam72cam.mod.world.World.get(event.getWorld()),
event.getWorld().getSaveHandler().getWorldDirectory()
));
}
}

@SubscribeEvent
public static void onWorldSave(WorldEvent.Save event) {
WorldData.SAVE.execute(x -> x.accept(
cam72cam.mod.world.World.get(event.getWorld()),
event.getWorld().getSaveHandler().getWorldDirectory()
));
}

@SubscribeEvent
public static void onWorldUnload(WorldEvent.Unload event) {
World.UNLOAD.execute(x -> x.accept(event.getWorld()));
if (!event.getWorld().isRemote) {
WorldData.UNLOAD.execute(x -> x.accept(
cam72cam.mod.world.World.get(event.getWorld()),
event.getWorld().getSaveHandler().getWorldDirectory()
));
}
}

@SubscribeEvent
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/cam72cam/mod/net/Packet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cam72cam.mod.net;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
Expand Down Expand Up @@ -113,6 +114,8 @@ public void sendToPlayer(Player player) {
net.sendTo(new Message(this), (EntityPlayerMP) player.internal);
}

protected byte[] raw;

/** Forge message construct. Do not use directly */
public static class Message implements IMessage {
Packet packet;
Expand All @@ -131,6 +134,9 @@ public void fromBytes(ByteBuf buf) {
String cls = data.getString("cam72cam.mod.pktid");
packet = types.get(cls).get();
packet.data = data;

packet.raw = new byte[buf.readInt()];
buf.readBytes(packet.raw);
}

@Override
Expand All @@ -143,6 +149,13 @@ public void toBytes(ByteBuf buf) {
ModCore.catching(e);
}
ByteBufUtils.writeTag(buf, data.internal);

if (packet.raw != null) {
buf.writeInt(packet.raw.length);
buf.writeBytes(packet.raw);
} else {
buf.writeInt(0);
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/cam72cam/mod/render/obj/OBJRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public void draw(Collection<String> groups, Consumer<RenderState> mod) {
}
}

public void draw(Consumer<RenderState> mod) {
if (!isLoaded()) {
return;
}
try (With pus = super.push(mod)) {
draw();
}
}

/**
* Draw these groups in the VB
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cam72cam/mod/world/LevelDataHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cam72cam.mod.world;

import cam72cam.mod.serialization.TagCompound;

import java.io.File;

public interface LevelDataHandler {
TagCompound saveLevelData(File levelDirectory);
void loadLevelData(TagCompound data, File levelDirectory);
}

0 comments on commit fabef9a

Please sign in to comment.