Skip to content

Commit

Permalink
bugfix and 0.1.2
Browse files Browse the repository at this point in the history
- exposed FilledMapItemMixin's useOnBlock method to the API
- changed MapMarkedBlock name to IMapMarkedBlock to easily distinguish between it and MapMarkerEntity (with autocomplete)
- mod icon now uses default map icon instead since they are the same texture
- IMapMarkedBlock's getMarkerColor(...) also gets world & pos parameters like getDisplayName(...) now
- fixed map icon not replicating to client on dedicated servers
I TESTED IT CORRECTLY THIS TIME!!
  • Loading branch information
rokoblox committed Jul 26, 2022
1 parent 4b13daa commit fffbc49
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 24 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
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
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/rokoblox/pinlib/PinLib.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
* <p>
* If there is already a map marker in the given
* location, it will try to remove it instead.
* </p>
* 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* <p>
* 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 1 addition & 14 deletions src/main/java/com/rokoblox/pinlib/mixin/FilledMapItemMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ public class MapUpdateS2CPacketMixin {
@Inject(method = "method_43883", at = @At(value = "RETURN"))
private static void pinlib$ReadCustomMarkerStates(PacketByteBuf buf3, CallbackInfoReturnable<MapIcon> cir) {
Identifier id = buf3.readIdentifier();
MapIconAccessor iconAccessor = (MapIconAccessor) cir.getReturnValue();
if (!id.getPath().equals("null"))
((MapIconAccessor) cir.getReturnValue()).setCustomMarker(PinLib.get(id));
iconAccessor.setCustomMarker(PinLib.get(id));
iconAccessor.color(buf3.readLong());
}

@Inject(method = "method_34136", at = @At(value = "RETURN"))
private static void pinlib$WriteCustomMarkerStates(PacketByteBuf b, MapIcon icon, CallbackInfo ci) {
MapMarker type = ((MapIconAccessor) icon).getCustomMarkerType();
MapIconAccessor iconAccessor = (MapIconAccessor) icon;
MapMarker type = iconAccessor.getCustomMarkerType();
b.writeIdentifier(type != null ? type.getId() : new Identifier("pinlib", "null"));
b.writeLong(iconAccessor.getColor());
}
}
Binary file removed src/main/resources/assets/pinlib/icon.png
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"repo": "https://github.com/rokoblox/pinlib.git"
},
"license": "MIT",
"icon": "assets/pinlib/icon.png",
"icon": "assets/pinlib/textures/map/icons/default.png",
"environment": "*",
"entrypoints": {
"main": [
Expand Down
4 changes: 2 additions & 2 deletions srcdev/TestingClass.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.rokoblox.pinlib;

import com.rokoblox.pinlib.mapmarker.MapMarkedBlock;
import com.rokoblox.pinlib.mapmarker.IMapMarkedBlock;
import com.rokoblox.pinlib.mapmarker.MapMarker;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
Expand All @@ -27,7 +27,7 @@ public static void init() {
}
}

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

0 comments on commit fffbc49

Please sign in to comment.