diff --git a/gradle.properties b/gradle.properties index 26f0e9a..0474728 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.19 yarn_mappings=1.19+build.4 loader_version=0.14.8 # Mod Properties -mod_version=0.1.1+mc1.19 +mod_version=0.1.2+mc1.19 maven_group=com.rokoblox archives_base_name=pinlib # Dependencies diff --git a/src/main/java/com/rokoblox/pinlib/PinLib.java b/src/main/java/com/rokoblox/pinlib/PinLib.java index 9d4f486..ee6e312 100644 --- a/src/main/java/com/rokoblox/pinlib/PinLib.java +++ b/src/main/java/com/rokoblox/pinlib/PinLib.java @@ -1,6 +1,7 @@ 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 net.fabricmc.api.ModInitializer; @@ -149,6 +150,46 @@ public static boolean tryRemoveMapMarker(ItemStack stack, World world, BlockPos return false; } + /** + * Attempts to add a map marker on the provided + * stack if it is a valid map with a valid map + * state. + *
+ * If there is already a map marker in the given + * location, it will try to remove it instead. + *
+ * This behaves as if the player clicked on a + * block that implements {@link IMapMarkedBlock} + * with a {@link FilledMapItem}. + * + * @param stack ItemStack to try to get a map state from + * @param world World containing the block + * @param blockPos Position of the block + * @return Whether the action succeeded or not + */ + 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); + if (mapState.addMapMarker(world, blockPos, mapMarker)) { + if (mapMarker == null) + return false; + PinLib.LOGGER.info("Added map marker with id [{}] at: [{}]", mapMarker.getId().toString(), blockPos.toShortString()); + return true; + } else if ((mapMarker = mapState.removeMapMarker( + null, + blockPos.getX(), + blockPos.getZ(), + !(world.getBlockState(blockPos).getBlock() instanceof IMapMarkedBlock), + null + )) != null) { + PinLib.LOGGER.info("Removed map marker with id [{}] at: [{}]", mapMarker.getId(), blockPos.toShortString()); + return true; + } + return false; + } + public static MapMarker get(Identifier id) { return REGISTRY.get(id); } diff --git a/src/main/java/com/rokoblox/pinlib/mapmarker/MapMarkedBlock.java b/src/main/java/com/rokoblox/pinlib/mapmarker/IMapMarkedBlock.java similarity index 83% rename from src/main/java/com/rokoblox/pinlib/mapmarker/MapMarkedBlock.java rename to src/main/java/com/rokoblox/pinlib/mapmarker/IMapMarkedBlock.java index 523fbc9..4a349a6 100644 --- a/src/main/java/com/rokoblox/pinlib/mapmarker/MapMarkedBlock.java +++ b/src/main/java/com/rokoblox/pinlib/mapmarker/IMapMarkedBlock.java @@ -10,12 +10,12 @@ *
* Implement in your Block class to use it as a MapMarker.
*/
-public interface MapMarkedBlock {
+public interface IMapMarkedBlock {
default MapMarker getCustomMarker() {
return PinLib.getDefaultMarker();
}
- default long getMarkerColor() {
+ default long getMarkerColor(BlockView world, BlockPos pos) {
return 0xFFFFFFFFL;
}
diff --git a/src/main/java/com/rokoblox/pinlib/mapmarker/MapMarkerEntity.java b/src/main/java/com/rokoblox/pinlib/mapmarker/MapMarkerEntity.java
index 6fd0d5d..19490d9 100644
--- a/src/main/java/com/rokoblox/pinlib/mapmarker/MapMarkerEntity.java
+++ b/src/main/java/com/rokoblox/pinlib/mapmarker/MapMarkerEntity.java
@@ -51,10 +51,10 @@ 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 MapMarkedBlock mapMarkedBlock) {
+ if (blockState.getBlock() instanceof IMapMarkedBlock mapMarkedBlock) {
MapMarker type = mapMarkedBlock.getCustomMarker();
Text text = mapMarkedBlock.getDisplayName(world, blockPos);
- return new MapMarkerEntity(type, blockPos, text, mapMarkedBlock.getMarkerColor());
+ return new MapMarkerEntity(type, blockPos, text, mapMarkedBlock.getMarkerColor(world, blockPos));
}
return null;
}
diff --git a/src/main/java/com/rokoblox/pinlib/mixin/FilledMapItemMixin.java b/src/main/java/com/rokoblox/pinlib/mixin/FilledMapItemMixin.java
index 6f96cdf..759bedd 100644
--- a/src/main/java/com/rokoblox/pinlib/mixin/FilledMapItemMixin.java
+++ b/src/main/java/com/rokoblox/pinlib/mixin/FilledMapItemMixin.java
@@ -2,7 +2,6 @@
import com.rokoblox.pinlib.PinLib;
import com.rokoblox.pinlib.access.MapStateAccessor;
-import com.rokoblox.pinlib.mapmarker.MapMarkedBlock;
import com.rokoblox.pinlib.mapmarker.MapMarkerEntity;
import net.minecraft.entity.Entity;
import net.minecraft.item.FilledMapItem;
@@ -34,19 +33,7 @@ public abstract class FilledMapItemMixin {
MapStateAccessor mapState = (MapStateAccessor) FilledMapItem.getOrCreateMapState(context.getStack(), context.getWorld());
if (mapState == null)
return;
- MapMarkerEntity mapMarker = MapMarkerEntity.fromWorldBlock(context.getWorld(), context.getBlockPos());
- if (mapState.addMapMarker(context.getWorld(), context.getBlockPos(), mapMarker)) {
- PinLib.LOGGER.info("Added map marker with id [{}] at: [{}]", mapMarker.getId().toString(), context.getBlockPos().toShortString());
+ if (PinLib.tryUseOnMarkableBlock(context.getStack(), context.getWorld(), context.getBlockPos()))
cir.setReturnValue(ActionResult.SUCCESS);
- } else if ((mapMarker = mapState.removeMapMarker(
- null,
- context.getBlockPos().getX(),
- context.getBlockPos().getZ(),
- !(context.getWorld().getBlockState(context.getBlockPos()).getBlock() instanceof MapMarkedBlock),
- null
- )) != null) {
- PinLib.LOGGER.info("Removed map marker with id [{}] at: [{}]", mapMarker.getId(), context.getBlockPos().toShortString());
- cir.setReturnValue(ActionResult.SUCCESS);
- }
}
}
diff --git a/src/main/java/com/rokoblox/pinlib/mixin/MapUpdateS2CPacketMixin.java b/src/main/java/com/rokoblox/pinlib/mixin/MapUpdateS2CPacketMixin.java
index 534385e..0d970b1 100644
--- a/src/main/java/com/rokoblox/pinlib/mixin/MapUpdateS2CPacketMixin.java
+++ b/src/main/java/com/rokoblox/pinlib/mixin/MapUpdateS2CPacketMixin.java
@@ -18,13 +18,17 @@ public class MapUpdateS2CPacketMixin {
@Inject(method = "method_43883", at = @At(value = "RETURN"))
private static void pinlib$ReadCustomMarkerStates(PacketByteBuf buf3, CallbackInfoReturnable