Created
July 18, 2016 09:11
-
-
Save gitcrtn/9182b1e2a1e26657fa548a2e83db843f to your computer and use it in GitHub Desktop.
Bukkit Plugin for Automated Updating of Maps in Item Frames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MapUpdater plugin | |
// 2016.07.18 | |
// @Carotene | |
// Usage: | |
// /updatemap <maps_name> <radius> | |
// /updatemap <maps_name> <radius> <x> <y> <z> | |
// - <maps_name> is just for messaging. | |
// - <radius> is the search range of the maps in item frames. | |
// - If you do not specify <x> <y> <z>, the coordinates of the player or commandblock becomes the reference point. | |
// This plugin is forked from: | |
// https://www.spigotmc.org/threads/refresh-itemframe-map.82873/ | |
package com.crtn.mapupdater; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.UUID; | |
import java.util.stream.Collectors; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.World; | |
import org.bukkit.command.BlockCommandSender; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld; | |
import org.bukkit.entity.Entity; | |
import org.bukkit.entity.ItemFrame; | |
import org.bukkit.entity.Player; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.map.MapView; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import com.mojang.authlib.GameProfile; | |
import net.minecraft.server.v1_10_R1.EntityHuman; | |
import net.minecraft.server.v1_10_R1.ItemWorldMap; | |
import net.minecraft.server.v1_10_R1.Items; | |
import net.minecraft.server.v1_10_R1.MinecraftServer; | |
import net.minecraft.server.v1_10_R1.WorldMap; | |
public class MapUpdater extends JavaPlugin { | |
@Override | |
public void onEnable() { | |
super.onEnable(); | |
} | |
@Override | |
public void onDisable() { | |
super.onDisable(); | |
} | |
@Override | |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { | |
// Display Help | |
if (args.length == 0) { | |
sender.sendMessage(ChatColor.RED + "[MapUpdater] Usage: /updatemap <maps_name> <radius> <x> <y> <z>"); | |
return true; | |
} | |
// Check Permission | |
if (!hasPermission(sender, "mapupdater.main")) { | |
return true; | |
} | |
if (command.getName().equalsIgnoreCase("updatemap")) { | |
// Parse command | |
if (args.length == 2 || args.length == 5) | |
{ | |
String maps_name = args[0]; | |
Double radius = Double.parseDouble(args[1]); | |
Location loc = null; | |
if (args.length == 5) | |
{ | |
World world = Bukkit.getWorld("world"); | |
if (sender instanceof BlockCommandSender) | |
{ | |
world = ((BlockCommandSender) sender).getBlock().getWorld(); | |
} | |
else if (sender instanceof Player) | |
{ | |
world = ((Player) sender).getWorld(); | |
} | |
else | |
{ | |
// Detele object | |
world = null; | |
System.gc(); | |
System.gc(); | |
sender.sendMessage("[MapUpdater] You can not execute this command from outside of the game!"); | |
return true; | |
} | |
loc = new Location(world, 0., 0., 0.); | |
loc.setX(Double.parseDouble(args[2])); | |
loc.setY(Double.parseDouble(args[3])); | |
loc.setZ(Double.parseDouble(args[4])); | |
} | |
else if (sender instanceof Player) | |
{ | |
loc = ((Player) sender).getLocation().clone(); | |
} | |
else if (sender instanceof BlockCommandSender) | |
{ | |
loc = ((BlockCommandSender) sender).getBlock().getLocation().clone(); | |
} | |
else | |
{ | |
sender.sendMessage("[MapUpdater] You can not execute this command from outside of the game!"); | |
return true; | |
} | |
// Search of the maps in item frames | |
List<MapView> maps = new ArrayList<MapView>(); | |
List<Short> mapIds = new ArrayList<Short>(); | |
for (Entity entity : loc.getWorld().getNearbyEntities(loc, radius, radius, radius)) | |
{ | |
if (entity instanceof ItemFrame) | |
{ | |
ItemStack item = ((ItemFrame) entity).getItem(); | |
Material material = item.getType(); | |
if (material == Material.MAP) | |
{ | |
@SuppressWarnings("deprecation") | |
MapView map = Bukkit.getServer().getMap(item.getDurability()); | |
maps.add(map); | |
@SuppressWarnings("deprecation") | |
short mapId = map.getId(); | |
mapIds.add(mapId); | |
} | |
} | |
} | |
// Exit if maps in item frames not found | |
if (maps.size() == 0) | |
{ | |
// Delete objects | |
maps = null; | |
mapIds = null; | |
loc = null; | |
System.gc(); | |
System.gc(); | |
sender.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "[MapUpdater] Maps not found!"); | |
return true; | |
} | |
// Start to update the maps if it found | |
Collections.sort(mapIds); | |
Bukkit.broadcastMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "[MapUpdater] Starting to update the maps of " + maps_name + "."); | |
Bukkit.broadcastMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "[MapUpdater] MapIDs: " + mapIds.stream().map(Object::toString).collect(Collectors.joining(", "))); | |
// Spawn a BOT Player for updating the maps | |
EntityHuman bot = new EntityHuman(((CraftWorld) Bukkit.getWorlds().get(0)).getHandle(), new GameProfile(UUID.randomUUID(), "mapUpdater")){ | |
@Override | |
public boolean isSpectator() { | |
return false; | |
} | |
@Override | |
public boolean z() { | |
return false; | |
} | |
}; | |
// Update the maps | |
ItemWorldMap itemWorldMap = Items.FILLED_MAP; | |
for (MapView map : maps) | |
{ | |
@SuppressWarnings("deprecation") | |
WorldMap worldMap = (WorldMap) MinecraftServer.getServer().worlds.get(0).a(WorldMap.class, "map_" + map.getId()); //You can also get it with reflection from ((CraftMapView) map) | |
net.minecraft.server.v1_10_R1.World world = ((CraftWorld) map.getWorld()).getHandle(); | |
int size = 128 << worldMap.scale; | |
int interval = 8 << worldMap.scale; | |
for (int x = worldMap.centerX - size / 2; x <= worldMap.centerX + size / 2; x += interval) { | |
for (int z = worldMap.centerZ - size / 2; z <= worldMap.centerZ + size / 2; z += interval) { | |
bot.locX = x; | |
bot.locZ = z; | |
itemWorldMap.a(world, bot, worldMap); | |
} | |
} | |
} | |
// Delete objects | |
loc = null; | |
maps = null; | |
mapIds = null; | |
bot = null; | |
itemWorldMap = null; | |
System.gc(); | |
System.gc(); | |
Bukkit.broadcastMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "[MapUpdater] Finished to update the maps of " + maps_name + "!"); | |
return true; | |
} | |
// Display Help | |
sender.sendMessage(ChatColor.RED + "[MapUpdater] Usage: /updatemap <maps_name> <radius> <x> <y> <z>"); | |
return true; | |
} | |
// Display Help | |
sender.sendMessage(ChatColor.RED + "[MapUpdater] Usage: /updatemap <maps_name> <radius> <x> <y> <z>"); | |
return false; | |
} | |
private boolean hasPermission(CommandSender sender, String permission) { | |
if (sender != Bukkit.getServer().getConsoleSender()) | |
{ | |
if ((sender.isOp()) || (sender.hasPermission(permission))) { | |
return true; | |
} | |
sender.sendMessage(ChatColor.RED + "You don't have permission to do that!"); | |
return false; | |
} | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: MapUpdater | |
version: 1.0.0 | |
main: com.crtn.mapupdater.MapUpdater | |
commands: | |
updatemap: | |
description: MapUpdater commands | |
usage: /<command> [maps_name] [radius] [x] [y] [z] | |
permission: mapupdater.main | |
permission-message: You don't have <permission> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.crtn</groupId> | |
<artifactId>mapupdater</artifactId> | |
<version>1.0.0-SNAPSHOT</version> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<repositories> | |
<repository> | |
<id>spigot-repo</id> | |
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> | |
</repository> | |
</repositories> | |
<dependencies> | |
<!--Spigot API--> | |
<dependency> | |
<groupId>org.spigotmc</groupId> | |
<artifactId>spigot-api</artifactId> | |
<version>1.10.2-R0.1-SNAPSHOT</version> | |
<scope>provided</scope> | |
</dependency> | |
<!--Bukkit API--> | |
<dependency> | |
<groupId>org.bukkit</groupId> | |
<artifactId>bukkit</artifactId> | |
<version>1.10.2-R0.1-SNAPSHOT</version> | |
<scope>provided</scope> | |
</dependency> | |
<!--CraftBukkit API--> | |
<dependency> | |
<groupId>org.bukkit</groupId> | |
<artifactId>craftbukkit</artifactId> | |
<version>1.10.2-R0.1-SNAPSHOT</version> | |
<scope>provided</scope> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment