Skip to content

Instantly share code, notes, and snippets.

@WizardlyBump17
Created March 25, 2022 13:49
Show Gist options
  • Select an option

  • Save WizardlyBump17/18baae18d5bd4206433a62799181c2b8 to your computer and use it in GitHub Desktop.

Select an option

Save WizardlyBump17/18baae18d5bd4206433a62799181c2b8 to your computer and use it in GitHub Desktop.
render a map and save as image
import com.mojang.authlib.GameProfile;
import net.minecraft.core.BlockPosition;
import net.minecraft.world.entity.player.EntityHuman;
import net.minecraft.world.item.ItemWorldMap;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.saveddata.maps.WorldMap;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_17_R1.map.CraftMapView;
import org.bukkit.map.MapView;
import org.jetbrains.annotations.NotNull;
import java.awt.image.BufferedImage;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.UUID;
public class HumanRenderer extends EntityHuman {
public static final UUID ID = UUID.randomUUID();
public static final String NAME = "_____MapUpdater_____";
private final int x;
private final int y;
public HumanRenderer(World world, int x, int z) {
super(
((CraftWorld) world).getHandle(),
new BlockPosition(x, 64, z),
0,
new GameProfile(ID, NAME)
);
this.arena = arena;
this.x = x;
this.y = z;
}
@Override
public boolean isCreative() {
return false;
}
@Override
public boolean isSpectator() {
return false;
}
public BufferedImage render(@NotNull MapView mapView, int radius) {
BufferedImage image = new BufferedImage(128, 128, BufferedImage.TYPE_INT_RGB);
mapView.setCenterX(x);
mapView.setCenterZ(y);
ArrayList<Chunk> chunks = new ArrayList<>();
Chunk center = arena.getCenter().getChunk();
for (int x = center.getX() - radius; x <= center.getX() + radius; x++) {
for (int z = center.getZ() - radius; z <= center.getZ() + radius; z++) {
Chunk chunk = mapView.getWorld().getChunkAt(x, z);
chunk.addPluginChunkTicket(arena.getPlugin());
chunks.add(chunk);
}
}
Bukkit.getScheduler().scheduleSyncDelayedTask(arena.getPlugin(), () -> {
try {
Field field = CraftMapView.class.getDeclaredField("worldMap");
field.setAccessible(true);
WorldMap worldMap = (WorldMap) field.get(mapView);
int size = 128 << mapView.getScale().ordinal();
for (int x = this.x - size / 2; x <= this.x + size / 2; x += 16) {
for (int z = this.y - size / 2; z <= this.y + size / 2; z += 16) {
setPositionRaw(x, 64, z);
((ItemWorldMap) Items.pp).a(getWorld(), this, worldMap);
}
}
for (int x = 0; x < 128; x++)
for (int y = 0; y < 128; y++)
image.setRGB(x, y, worldMap.g[y * 128 + x]);
} catch (Exception e) {
e.printStackTrace();
}
for (Chunk c : chunks)
c.removePluginChunkTicket(arena.getPlugin());
}, 5);
return image;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment