Skip to content

Commit

Permalink
Added a way to pre-register BlockFib's before the world was loading s…
Browse files Browse the repository at this point in the history
…o that Fibs could be added at ModInitialization.

Added logging where useful.
Cleaned shop a little.
  • Loading branch information
Haven-King committed Apr 10, 2020
1 parent 1b1c7c9 commit fe5f704
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 42 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G
org.gradle.jvmargs=-Xmx4G

# Fabric Properties
minecraft_version=1.15.2
yarn_mappings=1.15.2+build.14
loader_version=0.7.8+build.184

# Mod Properties
mod_version = 0.0.1
mod_version = 0.0.3
maven_group = dev.hephaestus
archives_base_name = FibLib

Expand Down
7 changes: 1 addition & 6 deletions src/main/java/dev/hephaestus/fiblib/BlockFib.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
import net.minecraft.server.network.ServerPlayerEntity;

public interface BlockFib {
BlockFib DEFAULT = new BlockFib() {
@Override
public BlockState get(BlockState state, ServerPlayerEntity player) {
return state;
}
};
BlockFib DEFAULT = (state, player) -> state;

BlockState get(BlockState state, ServerPlayerEntity player);
}
39 changes: 37 additions & 2 deletions src/main/java/dev/hephaestus/fiblib/FibLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
import it.unimi.dsi.fastutil.longs.LongSet;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.LongArrayTag;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.Pair;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.PersistentState;
import net.minecraft.world.dimension.DimensionType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -26,6 +28,9 @@ public class FibLib extends PersistentState {

private static final Logger LOGGER = LogManager.getLogger();
private static final String SAVE_KEY = "fiblib";

private static final HashMap<DimensionType, ArrayList<Pair<Block, BlockFib>>> PRE_LOAD = new HashMap<>();

private final HashMap<Block,LongSet> blocks = new HashMap<>();
private final HashMap<Block, BlockFib> fibs = new HashMap<>();

Expand All @@ -50,10 +55,24 @@ private static FibLib getInstance(ServerWorld world) {
);
}

// Convenience
private static FibLib getInstance(ServerPlayerEntity player) {
return getInstance(player.getServerWorld());
}

// This registers fibs that were added before the World they deal with was loaded. You should not call this.
public static void registerPreloadedFibs(ServerWorld world) {
int i = 0;
if (PRE_LOAD.get(world.getDimension().getType()) != null) {
for (Pair<Block, BlockFib> p : PRE_LOAD.get(world.getDimension().getType())) {
FibLib.register(world, p.getLeft(), p.getRight());
++i;
}
}

if (i > 0) FibLib.log("Registered %d pre-loaded BlockFib %s", i, i == 1 ? "" : "s");
}

@Override
public void fromTag(CompoundTag tag) {
blocks.clear();
Expand All @@ -78,7 +97,7 @@ public CompoundTag toTag(CompoundTag tag) {
}

// Instance methods. These are private to make the API simpler.
public void putWithInstance(Block block, BlockPos pos) {
private void putWithInstance(Block block, BlockPos pos) {
if (fibs.containsKey(block)) {
blocks.putIfAbsent(block, new LongOpenHashSet());
blocks.get(block).add(pos.asLong());
Expand All @@ -95,6 +114,7 @@ private void removeWithInstance(ServerWorld world, BlockPos pos) {
}

// API methods
// Updates all blocks currently being Fibbed. Somewhat expensive.
public static void update(ServerWorld world) {
int i = 0;
for (Long l : Iterables.concat(FibLib.getInstance(world).blocks.values())) {
Expand All @@ -104,6 +124,7 @@ public static void update(ServerWorld world) {
FibLib.log("Updated %d blocks", i);
}

// Updates all blocks being fibbed of a certain type. Use this when possible.
public static void update(ServerWorld world, Block block) {
int i = 0;
for (Long l : FibLib.getInstance(world).blocks.get(block)) {
Expand All @@ -113,10 +134,20 @@ public static void update(ServerWorld world, Block block) {
FibLib.log("Updated %d blocks", i);
}

// Use this method if you have a world already.
public static void register(ServerWorld world, Block block, BlockFib fib) {
FibLib.getInstance(world).fibs.put(block, fib);
FibLib.log("Registered a BlockFib for %s in %s", block.getTranslationKey(), world.getDimension().getType().toString());
}

// Use this method if you're registering Fibs before a world is available, i.e., your ModInitializer
public static void register(DimensionType dimensionType, Block block, BlockFib fib) {
PRE_LOAD.putIfAbsent(dimensionType, new ArrayList<>());
PRE_LOAD.get(dimensionType).add(new Pair<>(block, fib));
FibLib.log("Pre-loaded a BlockFib for %s in %s", block.getTranslationKey(), dimensionType.toString());
}

// This is what we use to actually fib things. You should *probably* never need to call this function.
public static BlockState get(BlockState state, ServerPlayerEntity player) {
try {
return FibLib.getInstance(player).getWithInstance(state, player);
Expand All @@ -125,10 +156,14 @@ public static BlockState get(BlockState state, ServerPlayerEntity player) {
}
}

// Registers a block to be updated on calls to update(). Automatically called in onBlockAdded(), but can be
// used if you want to track blocks more specifically.
public static void put(ServerWorld world, BlockPos pos) {
FibLib.getInstance(world).putWithInstance(world.getBlockState(pos).getBlock(), pos);
}

// Removes a block from being updated on calls to update(). Automatically called in onBlockRemoved(), but can be
// used if you want to track blocks more specifically.
public static void remove(ServerWorld world, BlockPos pos) {
FibLib.getInstance(world).removeWithInstance(world, pos);
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/dev/hephaestus/fiblib/Tester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.hephaestus.fiblib;

import net.fabricmc.api.ModInitializer;
import net.minecraft.block.Blocks;
import net.minecraft.world.dimension.DimensionType;

public class Tester implements ModInitializer {
static boolean DEBUG = false;
@Override
public void onInitialize() {
if (DEBUG) {
FibLib.register(DimensionType.OVERWORLD, Blocks.GRASS_BLOCK, (state, player) -> Blocks.STONE.getDefaultState());
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/dev/hephaestus/fiblib/mixin/ServerWorldMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.hephaestus.fiblib.mixin;

import dev.hephaestus.fiblib.FibLib;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.WorldGenerationProgressListener;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.profiler.Profiler;
import net.minecraft.world.WorldSaveHandler;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.level.LevelProperties;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.concurrent.Executor;

@Mixin(ServerWorld.class)
public class ServerWorldMixin {
@Inject(method = "<init>", at = @At("TAIL"))
public void initializeFibbers(MinecraftServer server, Executor workerExecutor, WorldSaveHandler worldSaveHandler, LevelProperties properties, DimensionType dimensionType, Profiler profiler, WorldGenerationProgressListener worldGenerationProgressListener, CallbackInfo ci) {
FibLib.registerPreloadedFibs((ServerWorld)(Object)this);
}
}
29 changes: 0 additions & 29 deletions src/main/java/dev/hephaestus/fiblib/mixin/WorldMixin.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minecraft.block.BlockState;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.IdList;
import net.minecraft.util.Identifier;
import net.minecraft.world.chunk.ArrayPalette;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import dev.hephaestus.fiblib.FibLib;
import dev.hephaestus.fiblib.Fibber;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.network.packet.s2c.play.ChunkDeltaUpdateS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"environment": "*",
"entrypoints": {
"main": [
"dev.hephaestus.fiblib.Tester"
]
},
"mixins": [
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/fiblib.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"mixins": [
"PacketFibber",
"BlockMixin",
"ServerWorldMixin",
"ServerPlayerEntityMixin",
"packets.BlockActionFibber",
"packets.BlockUpdateFibber",
Expand Down

0 comments on commit fe5f704

Please sign in to comment.