Skip to content

Commit

Permalink
0.2.0: deprecated IMapMarkedBlock and switched to a registry system.
Browse files Browse the repository at this point in the history
- updated 0.1.3 to 0.2.0
- deprecated IMapMarkedBlock
- add PinLib.registerMapMarkedBlock() and PinLib.getMapMarkedBlock() for new registry
- updated TestingClass to work with new API
  • Loading branch information
rokoblox committed Sep 21, 2022
1 parent 43344d4 commit 661f8c1
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 35 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ minecraft_version=1.19.2
yarn_mappings=1.19+build.4
loader_version=0.14.9
# Mod Properties
mod_version=0.1.3+1.19
mod_version=0.2.0+1.19
maven_group=com.rokoblox
archives_base_name=pinlib
# Dependencies
Expand Down
72 changes: 63 additions & 9 deletions src/main/java/com/rokoblox/pinlib/PinLib.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.rokoblox.pinlib;

import com.rokoblox.pinlib.access.MapStateAccessor;
import com.rokoblox.pinlib.mapmarker.IMapMarkedBlock;
import com.rokoblox.pinlib.mapmarker.MapMarker;
import com.rokoblox.pinlib.mapmarker.MapMarkerEntity;
import com.rokoblox.pinlib.mapmarker.*;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.FilledMapItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.map.MapState;
Expand All @@ -18,10 +18,13 @@
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;

public class PinLib implements ModInitializer {

public static final Logger LOGGER = LogManager.getLogger("PinLib");
private static final Registry<MapMarker> REGISTRY = FabricRegistryBuilder.createDefaulted(MapMarker.class, new Identifier("pinlib", "map_markers"), new Identifier("pinlib", "default")).buildAndRegister();
private static final Registry<MapMarker> MAP_MARKER_REGISTRY = FabricRegistryBuilder.createDefaulted(MapMarker.class, new Identifier("pinlib", "map_markers"), new Identifier("pinlib", "default")).buildAndRegister();
private static final Registry<MapMarkedBlock> MAP_MARKED_BLOCKS_REGISTRY = FabricRegistryBuilder.createSimple(MapMarkedBlock.class, new Identifier("pinlib", "map_marked_blocks")).buildAndRegister();
private static final MapMarker DEFAULT_MARKER = createStaticMarker(new Identifier("pinlib", "default"));

/**
Expand All @@ -47,7 +50,7 @@ public void onInitialize() {
*/
public static MapMarker createStaticMarker(Identifier id) {
LOGGER.info("Registering new static map marker with id [{}]", id.toString());
return Registry.register(REGISTRY, id, new MapMarker(id, false));
return Registry.register(MAP_MARKER_REGISTRY, id, new MapMarker(id, false));
}

/**
Expand All @@ -68,7 +71,42 @@ public static MapMarker createStaticMarker(Identifier id) {
*/
public static MapMarker createDynamicMarker(Identifier id) {
LOGGER.info("Registering new dynamic map marker with id [{}]", id.toString());
return Registry.register(REGISTRY, id, new MapMarker(id, true));
return Registry.register(MAP_MARKER_REGISTRY, id, new MapMarker(id, true));
}

/**
* Creates and registers a {@link com.rokoblox.pinlib.mapmarker.MapMarkedBlock}
* for the provided block with
* the provided methods.
*
* @param entry Block to register
* @param markerProvider You can use `() -> PinLib.getDefaultMarker()` as a default value.
* @param colorProvider You can use `(world, pos) -> 0xFFFFFFFFL` as a default value.
* @param displayNameProvider You can use `(BlockView world, BlockPos pos) -> null` as a default value.
* @return Provided entry
*/
public static MapMarkedBlock registerMapMarkedBlock(Block entry, CustomMarkerProvider markerProvider, MarkerColorProvider colorProvider, MarkerDisplayNameProvider displayNameProvider) {
Identifier id = Registry.BLOCK.getId(entry);
if (id == Registry.BLOCK.getDefaultId())
LOGGER.warn("Registering default ID [{}] as a map marked block, this might be because the provided block entry was not registered as a block first.", id.toString());
LOGGER.info("Registering block with id [{}] as a map marked block.", id.toString());
return Registry.register(MAP_MARKED_BLOCKS_REGISTRY, id, new MapMarkedBlock(entry, markerProvider, colorProvider, displayNameProvider));
}

/**
* Returns a {@link MapMarkedBlock} for the provided
* block if the block was previously registered with
* registerMapMarkedBlock(...) or null if not.
* <p>
* This method uses caching for performance,
* manually optimizing results is not needed.
* </p>
*
* @param block Block to find {@link MapMarkedBlock} for
* @return Matching {@link MapMarkedBlock}, null if none was found in registry.
*/
public static MapMarkedBlock getMapMarkedBlock(Block block) {
return MapMarkedBlockCache.fromBlock(block);
}

/**
Expand Down Expand Up @@ -167,11 +205,13 @@ public static boolean tryRemoveMapMarker(ItemStack stack, World world, BlockPos
* @param blockPos Position of the block
* @return Whether the action succeeded or not
*/
@SuppressWarnings("deprecation")
public static boolean tryUseOnMarkableBlock(ItemStack stack, World world, BlockPos blockPos) {
MapStateAccessor mapState = (MapStateAccessor) FilledMapItem.getOrCreateMapState(stack, world);
if (mapState == null)
return false;
MapMarkerEntity mapMarker = MapMarkerEntity.fromWorldBlock(world, blockPos);
BlockState blockState = world.getBlockState(blockPos);
if (mapState.addMapMarker(world, blockPos, mapMarker)) {
if (mapMarker == null)
return false;
Expand All @@ -181,7 +221,7 @@ public static boolean tryUseOnMarkableBlock(ItemStack stack, World world, BlockP
null,
blockPos.getX(),
blockPos.getZ(),
!(world.getBlockState(blockPos).getBlock() instanceof IMapMarkedBlock),
!(blockState.getBlock() instanceof IMapMarkedBlock) && (getMapMarkedBlock(blockState.getBlock()) == null),
null
)) != null) {
PinLib.LOGGER.info("Removed map marker with id [{}] at: [{}]", mapMarker.getId(), blockPos.toShortString());
Expand All @@ -191,10 +231,24 @@ public static boolean tryUseOnMarkableBlock(ItemStack stack, World world, BlockP
}

public static MapMarker get(Identifier id) {
return REGISTRY.get(id);
return MAP_MARKER_REGISTRY.get(id);
}

public static MapMarker getDefaultMarker() {
return DEFAULT_MARKER;
}
}

static class MapMarkedBlockCache {
private static final HashMap<Block, MapMarkedBlock> cache = new HashMap<>();

public static MapMarkedBlock fromBlock(Block block) {
if (cache.containsKey(block))
return cache.get(block);
else {
MapMarkedBlock entry = MAP_MARKED_BLOCKS_REGISTRY.get(Registry.BLOCK.getId(block));
cache.put(block, entry);
return entry;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.rokoblox.pinlib.mapmarker;

public interface CustomMarkerProvider {
MapMarker run();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* <p>
* Implement in your Block class to use it as a MapMarker.
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
public interface IMapMarkedBlock {
default MapMarker getCustomMarker() {
return PinLib.getDefaultMarker();
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/rokoblox/pinlib/mapmarker/MapMarkedBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.rokoblox.pinlib.mapmarker;

import net.minecraft.block.Block;

public class MapMarkedBlock {
public final Block block;
public final CustomMarkerProvider markerProvider;
public final MarkerColorProvider markerColorProvider;
public final MarkerDisplayNameProvider markerDisplayNameProvider;

public MapMarkedBlock(Block block, CustomMarkerProvider markerProvider, MarkerColorProvider markerColorProvider, MarkerDisplayNameProvider markerDisplayNameProvider) {
this.block = block;
this.markerProvider = markerProvider;
this.markerColorProvider = markerColorProvider;
this.markerDisplayNameProvider = markerDisplayNameProvider;
}
}

14 changes: 10 additions & 4 deletions src/main/java/com/rokoblox/pinlib/mapmarker/MapMarkerEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ public MapMarkerEntity(MapMarker type, BlockPos pos, @Nullable Text displayName)
@Nullable
public static MapMarkerEntity fromWorldBlock(World world, BlockPos blockPos) {
BlockState blockState = world.getBlockState(blockPos);
if (blockState.getBlock() instanceof IMapMarkedBlock mapMarkedBlock) {
MapMarker type = mapMarkedBlock.getCustomMarker();
Text text = mapMarkedBlock.getDisplayName(world, blockPos);
return new MapMarkerEntity(type, blockPos, text, mapMarkedBlock.getMarkerColor(world, blockPos));
MapMarkedBlock mapMarkedBlock = PinLib.getMapMarkedBlock(blockState.getBlock());
if (mapMarkedBlock != null) {
MapMarker type = mapMarkedBlock.markerProvider.run();
Text text = mapMarkedBlock.markerDisplayNameProvider.run(world, blockPos);
return new MapMarkerEntity(type, blockPos, text, mapMarkedBlock.markerColorProvider.run(world, blockPos));
} else if (blockState.getBlock() instanceof @SuppressWarnings("deprecation")IMapMarkedBlock deprecatedMapMarkedBlock) {
MapMarker type = deprecatedMapMarkedBlock.getCustomMarker();
Text text = deprecatedMapMarkedBlock.getDisplayName(world, blockPos);
return new MapMarkerEntity(type, blockPos, text, deprecatedMapMarkedBlock.getMarkerColor(world, blockPos));
}
return null;
}
Expand Down Expand Up @@ -150,3 +155,4 @@ public long getColor() {
return this.color;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.rokoblox.pinlib.mapmarker;

import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;

public interface MarkerColorProvider {
long run(BlockView world, BlockPos pos);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.rokoblox.pinlib.mapmarker;

import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;

public interface MarkerDisplayNameProvider {
Text run(BlockView world, BlockPos pos);
}
59 changes: 38 additions & 21 deletions srcdev/TestingClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,49 @@ public static void init() {
TESTMARKER = PinLib.createDynamicMarker(new Identifier("pinlib", "testmarker"));
TESTBLOCK = new TestBlock(FabricBlockSettings.of(Material.STONE));
Registry.register(Registry.BLOCK, new Identifier("pinlib", "testblock"), TESTBLOCK);

// Map marked blocks should NOT be registered like this,
// this is just to demonstrate how to switch from the
// deprecated IMapMarkedBlock to the new registering system.
PinLib.registerMapMarkedBlock(
TESTBLOCK,
() -> TESTMARKER,
(world, pos) -> 0xFFFFFFFFL,
(world, pos) -> Text.literal("Hello world!").setStyle(Style.EMPTY)
);
ServerTickEvents.END_SERVER_TICK.register(server -> isRed = !isRed);
}
}

class TestBlock extends Block implements IMapMarkedBlock {
class TestBlock extends Block {
public TestBlock(Settings settings) {
super(settings);
}

@Override // Default method returns default marker (useful for quick debugging).
public MapMarker getCustomMarker() {
return PinLib.get(new Identifier("pinlib", "testmarker"));
}

@Override // Default method returns 0xFFFFFFFFL (white)
public long getMarkerColor(BlockView world, BlockPos pos) {
// Use net.minecraft.util.math.ColorHelper to get integer from RGB values.
return 0xFFFFFFFFL; // (argb) -> 255, 255, 0, 0
}

@Override
public Text getDisplayName(BlockView world, BlockPos pos) {
// It's a good idea to not leave style as null since the loaded
// map markers from NBT always load with an empty style, thus
// the game thinks the loaded display name and the one here
// are unequal causing an unnecessary update to the map marker.
return Text.literal("Hey there!").setStyle(Style.EMPTY);
}
}

// Deprecated IMapMarkedBlock implementation, use above example instead.
//class TestBlock extends Block implements IMapMarkedBlock {
// public TestBlock(Settings settings) {
// super(settings);
// }
//
// @Override // Default method returns default marker (useful for quick debugging).
// public MapMarker getCustomMarker() {
// return PinLib.get(new Identifier("pinlib", "testmarker"));
// }
//
// @Override // Default method returns 0xFFFFFFFFL (white)
// public long getMarkerColor(BlockView world, BlockPos pos) {
// // Use net.minecraft.util.math.ColorHelper to get integer from RGB values.
// return 0xFFFFFFFFL; // (argb) -> 255, 255, 0, 0
// }
//
// @Override
// public Text getDisplayName(BlockView world, BlockPos pos) {
// // It's a good idea to not leave style as null since the loaded
// // map markers from NBT always load with an empty style, thus
// // the game thinks the loaded display name and the one here
// // are unequal causing an unnecessary update to the map marker.
// return Text.literal("Hey there!").setStyle(Style.EMPTY);
// }
//}

0 comments on commit 661f8c1

Please sign in to comment.