Skip to content

Instantly share code, notes, and snippets.

@Pisi-Deff
Last active November 9, 2023 17:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pisi-Deff/92e00008f809383c82d926f843b6dd0f to your computer and use it in GitHub Desktop.
Save Pisi-Deff/92e00008f809383c82d926f843b6dd0f to your computer and use it in GitHub Desktop.
Dynmap RegionGrid component
componentconstructors['regiongrid'] = function(dynmap, configuration) {
// var cfg = $.extend({}, configuration);
var me = this;
this.chunkGroup = new L.LayerGroup();
this.regionGroup = new L.LayerGroup();
this.chunkMarkers = [];
this.regionMarkers = [];
this.chunkStyle = {
color: '#800000',
weight: '1',
opacity: 0.6,
clickable: false,
fill: false
};
this.regionStyle = {
color: '#000080',
weight: '3',
opacity: 0.6,
clickable: false,
fill: false
};
dynmap.addToLayerSelector(this.chunkGroup, 'Chunks', -1);
dynmap.addToLayerSelector(this.regionGroup, 'Regions', -1);
dynmap.map.on('moveend', function () {
setTimeout(function () {updateGrid();}, 0);
});
dynmap.map.on('layeradd layerremove', function (event) {
if (event.layer === me.chunkGroup || event.layer === me.regionGroup)
{
updateGrid();
}
});
function updateGrid() {
removeMarkers(me.chunkMarkers, me.chunkGroup);
removeMarkers(me.regionMarkers, me.regionGroup);
if (!dynmap.map.hasLayer(me.chunkGroup) && !dynmap.map.hasLayer(me.regionGroup))
{
return;
}
var minMax = getMinMaxData();
addAreas(me.chunkGroup, me.chunkMarkers, me.chunkStyle, 16, minMax.minChunk, minMax.maxChunk);
addAreas(me.regionGroup, me.regionMarkers, me.regionStyle, 16*32, minMax.minRegion, minMax.maxRegion);
}
function removeMarkers(markers, group) {
while (markers.length) {
group.removeLayer(markers.pop());
}
}
function addAreas(group, markers, style, multiplier, min, max) {
if (!dynmap.map.hasLayer(group)) return;
if ((max.x - min.x) * (max.z - min.z) > 1500) return; // for performance reasons
for (var x = min.x; x < max.x; x++) {
for (var z = min.z; z < max.z; z++) {
var pointList = [
latlng(x * multiplier, 64, (z + 1) * multiplier),
latlng((x + 1) * multiplier, 64, (z + 1) * multiplier),
latlng((x + 1) * multiplier, 64, z * multiplier)
];
var marker = new L.Polyline(pointList, style);
markers.push(marker);
group.addLayer(marker);
}
}
}
function getMinMaxData() {
// 0,0 => chunk 0 0 => region 0 0
// 15, 15 => chunk 0 0 => region 0 0
// 16, 16 => chunk 1 1 => region 0 0
// 12228, 8898 => chunk 764 556 => region 23 17
var bounds = dynmap.map.getBounds(),
projection = dynmap.maptype.getProjection();
var ne = projection.fromLatLngToLocation(bounds.getNorthEast(), 64),
se = projection.fromLatLngToLocation(bounds.getSouthEast(), 64),
sw = projection.fromLatLngToLocation(bounds.getSouthWest(), 64),
nw = projection.fromLatLngToLocation(bounds.getNorthWest(), 64);
var minX = Math.min(ne.x, se.x, sw.x, nw.x),
maxX = Math.max(ne.x, se.x, sw.x, nw.x),
minZ = Math.min(ne.z, se.z, sw.z, nw.z),
maxZ = Math.max(ne.z, se.z, sw.z, nw.z),
minChunk = {
x: Math.floor(minX / 16) - 1,
z: Math.floor(minZ / 16) - 1
},
maxChunk = {
x: Math.ceil(maxX / 16) + 1,
z: Math.ceil(maxZ / 16) + 1
},
minRegion = {
x: Math.floor(minChunk.x / 32),
z: Math.floor(minChunk.z / 32)
},
maxRegion = {
x: Math.ceil(maxChunk.x / 32),
z: Math.ceil(maxChunk.z / 32)
};
return {
minChunk: minChunk,
maxChunk: maxChunk,
minRegion: minRegion,
maxRegion: maxRegion
};
}
};
@Dragonisser
Copy link

Dragonisser commented Dec 23, 2020

hm, sadly doesnt seem to work anymore.

@Pisi-Deff
Copy link
Author

@Dragonisser

Still works for me on my personal server. If you're still interested, I'd be happy to help you with it :)

@Dragonisser
Copy link

Dragonisser commented Feb 17, 2021

Im currently dont directly have a use for it, but you could give an explanation for it again.
Probably did something wrong when i followed the one that lead me here.

@Pisi-Deff
Copy link
Author

Made a little change that should fix panning the map not causing the grid to update on newer versions on dynmap.

Usage instructions:

  • save the contents of this gist as a regiongrid.js file
  • put the file into server/plugins/dynmap/web/js
  • open the dynmap configuration file located at server/plugins/dynmap/configuration.txt
  • find the section named components:
  • add the following lines to the section:
  - class: org.dynmap.ClientComponent
    type: regiongrid

There are other ClientComponent definitions in that section. Sticking the new definition somewhere between them would work just fine.
Note that it is very important to keep the whitespace exactly as-is or it will not properly work

  • reload dynmap. This is most easily done by just restarting the Minecraft server.

After this, the "Chunks" and "Regions" layers should appear in the layer selection menu:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment