Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 57 additions & 28 deletions Cameras/pom.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,59 @@
<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>Achoowy</groupId>
<artifactId>CameraPlugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Cameras</name>
<description>Hand held cameras in minecraft</description>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<!-- This adds the Spigot Maven repository to the build -->
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
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>Achoowy</groupId>
<artifactId>CameraPlugin</artifactId>
<version>2.0</version>
<name>Cameras</name>
<description>Hand held cameras</description>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>

<repository>
<id>mojang</id>
<url>https://libraries.minecraft.net/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.mojang</groupId>
<artifactId>authlib</artifactId>
<version>3.17.30</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
260 changes: 260 additions & 0 deletions Cameras/src/main/java/water/of/cup/cameras/Camera.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
package water.of.cup.cameras;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.profile.PlayerProfile;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;

import water.of.cup.cameras.bstats.Metrics;
import water.of.cup.cameras.commands.CameraCommands;
import water.of.cup.cameras.listeners.CameraClick;
import water.of.cup.cameras.listeners.CameraPlace;
import water.of.cup.cameras.listeners.PlayerJoin;
import water.of.cup.cameras.listeners.PrepareItemCraft;

public class Camera extends JavaPlugin {

private static Camera instance;
private List<Integer> mapIDsNotToRender = new ArrayList<>();
private ResourcePackManager resourcePackManager = new ResourcePackManager();
private File configFile;
private FileConfiguration config;

@Override
public void onEnable() {
instance = this;

loadConfig();

this.resourcePackManager.initialize();

// Resource pack manager test
File grassFile = this.resourcePackManager.getTextureByMaterial(Material.GRASS_BLOCK);
if (grassFile != null)
Bukkit.getLogger().info("Loaded grass texture " + grassFile.getName());

File folder = new File(getDataFolder() + "/maps");
File[] listOfFiles = folder.listFiles();

if (listOfFiles != null) {
for (File file : listOfFiles) {
if (file.isFile()) {
int mapId = Integer.parseInt(file.getName().split("_")[1].split(Pattern.quote("."))[0]);
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String encodedData = br.readLine();

MapView mapView = Bukkit.getMap(mapId);
mapView.setTrackingPosition(false);

for (MapRenderer renderer : mapView.getRenderers())
mapView.removeRenderer(renderer);

mapView.addRenderer(new MapRenderer() {
@Override
public void render(MapView mapViewNew, MapCanvas mapCanvas, Player player) {
if (!mapIDsNotToRender.contains(mapId)) {
mapIDsNotToRender.add(mapId);

int x = 0;
int y = 0;
int skipsLeft = 0;
byte colorByte = 0;

for (int index = 0; index < encodedData.length(); index++) {
if (skipsLeft == 0) {
int end = index;
while (encodedData.charAt(end) != ',') end++;

String str = encodedData.substring(index, end);
index = end;

colorByte = Byte.parseByte(str.substring(0, str.indexOf('_')));
skipsLeft = Integer.parseInt(str.substring(str.indexOf('_') + 1));
}

while (skipsLeft != 0) {
Block block = Bukkit.getWorld("world").getBlockAt(x, y, 0);
colorByte = Utils.colorFromType(block, new double[]{1.0, 1.0, 1.0});

mapCanvas.setPixel(x, y, colorByte);

y++;
if (y == 128) {
y = 0;
x++;
}

skipsLeft--;
}
}
}
}
});

} catch (Exception e) {
e.printStackTrace();
}
}
}
}

Utils.loadColors();
getCommand("takePicture").setExecutor(new CameraCommands());
registerListeners(new CameraClick(), new CameraPlace(), new PlayerJoin(), new PrepareItemCraft());

if (config.getBoolean("settings.camera.recipe.enabled"))
addCameraRecipe();

// Add bStats
Metrics metrics = new Metrics(this, 9671);
Bukkit.getLogger().info("[Cameras] bStats: " + metrics.isEnabled() + " plugin ver: " + getDescription().getVersion());
metrics.addCustomChart(new Metrics.SimplePie("plugin_version", () -> getDescription().getVersion()));
}

@Override
public void onDisable() {
Bukkit.getScheduler().cancelTasks(this);
}

private void registerListeners(Listener... listeners) {
Arrays.stream(listeners).forEach(listener -> getServer().getPluginManager().registerEvents(listener, this));
}

public static Camera getInstance() {
return instance;
}

public void addCameraRecipe() {
ItemStack camera = new ItemStack(Material.PLAYER_HEAD);
SkullMeta meta = (SkullMeta) camera.getItemMeta();

if (meta == null) return;

meta.setDisplayName(ChatColor.DARK_BLUE + "Camera");

PlayerProfile profile = Bukkit.createProfile(UUID.randomUUID(), null);

try {
profile.getTextures().setSkin(
new java.net.URL("http://textures.minecraft.net/texture/6fb5eee40c3dd6683cec8dd1c6c3fc1b1f0137178663d76109cfe12ed7bf278e")
);
} catch (java.net.MalformedURLException e) {
e.printStackTrace();
}

meta.setOwnerProfile(profile);
camera.setItemMeta(meta);

NamespacedKey key = new NamespacedKey(this, "camera");
ShapedRecipe recipe = new ShapedRecipe(key, camera);

java.util.List<String> shape = config.getStringList("settings.camera.recipe.shape");
recipe.shape(shape.toArray(new String[0]));

var section = config.getConfigurationSection("settings.camera.recipe.ingredients");
if (section != null) {
for (String k : section.getKeys(false)) {
Material mat = Material.matchMaterial(section.getString(k));
if (mat != null) {
recipe.setIngredient(k.charAt(0), mat);
}
}
}

Bukkit.addRecipe(recipe);
}

private void loadConfig() {
if (!getDataFolder().exists()) getDataFolder().mkdir();

configFile = new File(getDataFolder(), "config.yml");
if (!configFile.exists()) {
try {
configFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

config = YamlConfiguration.loadConfiguration(configFile);

HashMap<String, Object> defaultConfig = new HashMap<>();

defaultConfig.put("settings.messages.notready", "&cCameras is still loading, please wait.");
defaultConfig.put("settings.messages.delay", "&cPlease wait before taking another picture.");
defaultConfig.put("settings.messages.invfull", "&cYou can not take a picture with a full inventory.");
defaultConfig.put("settings.messages.nopaper", "&cYou must have paper in order to take a picture.");
defaultConfig.put("settings.messages.enabled", true);
defaultConfig.put("settings.delay.amount", 1000);
defaultConfig.put("settings.delay.enabled", true);
defaultConfig.put("settings.camera.transparentWater", true);
defaultConfig.put("settings.camera.shadows", true);
defaultConfig.put("settings.camera.permissions", true);

HashMap<String, String> defaultRecipe = new HashMap<>();
defaultRecipe.put("I", Material.IRON_INGOT.toString());
defaultRecipe.put("G", Material.GLASS_PANE.toString());
defaultRecipe.put("T", Material.GLOWSTONE_DUST.toString());
defaultRecipe.put("R", Material.REDSTONE.toString());

defaultConfig.put("settings.camera.recipe.enabled", true);
defaultConfig.put("settings.camera.recipe.shape", Arrays.asList("IGI", "ITI", "IRI"));

if (!config.contains("settings.camera.recipe.ingredients")) {
for (String key : defaultRecipe.keySet()) {
defaultConfig.put("settings.camera.recipe.ingredients." + key, defaultRecipe.get(key));
}
}

for (String key : defaultConfig.keySet()) {
if (!config.contains(key)) config.set(key, defaultConfig.get(key));
}

File mapDir = new File(getDataFolder(), "maps");
if (!mapDir.exists()) mapDir.mkdir();

saveConfig();
}

public ResourcePackManager getResourcePackManager() {
return this.resourcePackManager;
}

@Override
public void saveConfig() {
try {
config.save(configFile);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public FileConfiguration getConfig() {
return config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Utils {
public static void loadColors() {
// Materials we don't want to use minecraft images for (could be because the
// image provides a poor color)
blocksMap.put(Material.GRASS, new Color(49, 101, 25));
blocksMap.put(Material.SHORT_GRASS, new Color(49, 101, 25));
blocksMap.put(Material.TALL_GRASS, new Color(49, 101, 25));
blocksMap.put(Material.LARGE_FERN, new Color(49, 101, 25));
blocksMap.put(Material.FERN, new Color(49, 101, 25));
Expand Down Expand Up @@ -55,7 +55,7 @@ public static void loadColors() {
blocksMap.put(Material.JUNGLE_LEAVES, new Color(60, 141, 24));
blocksMap.put(Material.OAK_LEAVES, new Color(49, 111, 21));
blocksMap.put(Material.SPRUCE_LEAVES, new Color(55, 91, 56));
blocksMap.put(Material.GRASS_PATH, new Color(170, 148, 89));
blocksMap.put(Material.DIRT_PATH, new Color(170, 148, 89));
blocksMap.put(Material.COARSE_DIRT, new Color(104, 75, 51));
blocksMap.put(Material.ANDESITE, new Color(136, 136, 138));
blocksMap.put(Material.DIORITE, new Color(181, 181, 181));
Expand Down
10 changes: 10 additions & 0 deletions Cameras/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: CameraPlugin
version: 2.0
main: water.of.cup.cameras.Camera
api-version: 1.21
author: Nicholas Kopiwoda
description: A custom camera plugin for Paper.
commands:
takePicture:
description: Take a picture with the camera
usage: /takepicture
9 changes: 0 additions & 9 deletions Cameras/src/plugin.yml

This file was deleted.

Loading