🎨 Moved back to reflection, still WIP

This commit is contained in:
SBDeveloper 2022-07-01 12:07:10 +02:00
parent a4f60fd01e
commit 6d1dab78aa
53 changed files with 879 additions and 4667 deletions

View file

@ -1,109 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi;
import org.bukkit.Bukkit;
import org.bukkit.map.MapView;
import org.bukkit.plugin.java.JavaPlugin;
import tech.sbdevelopment.mapreflectionapi.api.MapManager;
import tech.sbdevelopment.mapreflectionapi.listeners.MapListener;
import tech.sbdevelopment.mapreflectionapi.listeners.PacketListener;
import java.util.logging.Level;
public class MapReflectionAPI extends JavaPlugin {
private static MapReflectionAPI instance;
private static MapManager mapManager;
private static PacketListener packetListener;
public static MapReflectionAPI getInstance() {
if (instance == null) throw new IllegalStateException("The plugin is not enabled yet!");
return instance;
}
public static MapManager getMapManager() {
if (mapManager == null) throw new IllegalStateException("The plugin is not enabled yet!");
return mapManager;
}
@Override
public void onEnable() {
instance = this;
getLogger().info("----------------");
getLogger().info("MapReflectionAPI v" + getDescription().getVersion() + "");
getLogger().info("Made by © Copyright SBDevelopment 2022");
if (!Bukkit.getPluginManager().isPluginEnabled("BKCommonLib")) {
getLogger().severe("MapReflectionAPI requires BKCommonLib to function!");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
try {
packetListener = PacketListener.construct(this);
} catch (IllegalStateException e) {
getLogger().log(Level.SEVERE, e.getMessage(), e);
Bukkit.getPluginManager().disablePlugin(this);
return;
}
packetListener.init(this);
try {
mapManager = new MapManager(this);
} catch (IllegalStateException e) {
getLogger().log(Level.SEVERE, e.getMessage(), e);
Bukkit.getPluginManager().disablePlugin(this);
return;
}
getLogger().info("Registering the events...");
Bukkit.getPluginManager().registerEvents(new MapListener(), this);
getLogger().info("Discovering occupied Map IDs...");
for (int s = 0; s < Short.MAX_VALUE; s++) {
try {
MapView view = Bukkit.getMap(s);
if (view != null) mapManager.registerOccupiedID(s);
} catch (Exception e) {
if (e.getMessage().toLowerCase().contains("invalid map dimension")) {
getLogger().log(Level.WARNING, e.getMessage(), e);
}
}
}
getLogger().info("MapReflectionAPI is enabled!");
getLogger().info("----------------");
}
@Override
public void onDisable() {
getLogger().info("Disabling the packet handler...");
if (packetListener != null) Bukkit.getOnlinePlayers().forEach(p -> packetListener.removePlayer(p));
getLogger().info("MapReflectionAPI is disabled!");
instance = null;
}
}

View file

@ -1,102 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.api;
import com.bergerkiller.bukkit.common.map.MapColorPalette;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Objects;
public class ArrayImage {
public byte[] array;
public int minX = 0;
public int minY = 0;
public int maxX = 128;
public int maxY = 128;
private int width;
private int height;
private int imageType = BufferedImage.TYPE_4BYTE_ABGR;
public ArrayImage(byte[] data) {
this.array = data;
}
/**
* Convert a {@link BufferedImage} to an ArrayImage
*
* @param image image to convert
*/
public ArrayImage(BufferedImage image) {
this.imageType = image.getType();
this.width = image.getWidth();
this.height = image.getHeight();
BufferedImage temp = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = temp.createGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
int[] pixels = new int[temp.getWidth() * temp.getHeight()];
temp.getRGB(0, 0, temp.getWidth(), temp.getHeight(), pixels, 0, temp.getWidth());
byte[] result = new byte[temp.getWidth() * temp.getHeight()];
for (int i = 0; i < pixels.length; i++) {
result[i] = MapColorPalette.getColor(new Color(pixels[i], true));
}
this.array = result;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ArrayImage)) return false;
ArrayImage that = (ArrayImage) o;
return width == that.width && height == that.height && imageType == that.imageType && Arrays.equals(array, that.array);
}
@Override
public int hashCode() {
int result = Objects.hash(width, height, imageType);
result = 31 * result + Arrays.hashCode(array);
return result;
}
@Override
public String toString() {
return "ArrayImage{" +
"array=" + Arrays.toString(array) +
", minX=" + minX +
", minY=" + minY +
", maxX=" + maxX +
", maxY=" + maxY +
", width=" + width +
", height=" + height +
", imageType=" + imageType +
'}';
}
}

View file

@ -1,183 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.api;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import tech.sbdevelopment.mapreflectionapi.exceptions.MapLimitExceededException;
public interface MapController {
/**
* Add a viewer
*
* @param player {@link Player} to add
*/
void addViewer(Player player) throws MapLimitExceededException;
/**
* Remove a viewer
*
* @param player {@link OfflinePlayer} to remove
*/
void removeViewer(OfflinePlayer player);
/**
* Remove all viewers
*/
void clearViewers();
/**
* Check if a player is viewing
*
* @param player {@link OfflinePlayer} to check
* @return <code>true</code> if the player is viewing
*/
boolean isViewing(OfflinePlayer player);
/**
* Get the map ID for a player
*
* @param player {@link OfflinePlayer} to get the ID for
* @return the ID, or <code>-1</code> if no ID exists (i.e. the player is not viewing)
*/
int getMapId(OfflinePlayer player);
/**
* Update the image
*
* @param content new {@link ArrayImage} content
*/
void update(ArrayImage content);
ArrayImage getContent();
/**
* Send the content to a player
*
* @param player {@link Player} receiver of the content
*/
void sendContent(Player player);
/**
* Send the content to a player
*
* @param player {@link Player} receiver of the content
* @param withoutQueue if <code>true</code>, the content will be sent immediately
*/
void sendContent(Player player, boolean withoutQueue);
/**
* Show in a player's inventory
*
* @param player {@link Player}
* @param slot slot to show the map in
* @param force if <code>false</code>, the map will not be shown if the player is in creative mode
*/
void showInInventory(Player player, int slot, boolean force);
/**
* Show in a player's inventory
*
* @param player {@link Player}
* @param slot slot to show the map in
*/
void showInInventory(Player player, int slot);
/**
* Show in a player's hand
*
* @param player {@link Player}
* @param force if <code>false</code>, the map will not be shown if the player is not holding a map, or is in creative mode
* @see #showInFrame(Player, ItemFrame, boolean)
*/
void showInHand(Player player, boolean force);
/**
* Show in a player's hand
*
* @param player {@link Player}
*/
void showInHand(Player player);
/**
* Show in an {@link ItemFrame}
*
* @param player {@link Player} that will be able to see the map
* @param frame {@link ItemFrame} to show the map in
*/
void showInFrame(Player player, ItemFrame frame);
/**
* Show in an {@link ItemFrame}
*
* @param player {@link Player} that will be able to see the map
* @param frame {@link ItemFrame} to show the map in
* @param force if <code>false</code>, the map will not be shown if there is not Map-Item in the ItemFrame
*/
void showInFrame(Player player, ItemFrame frame, boolean force);
/**
* Show in an {@link ItemFrame}
*
* @param player {@link Player} that will be able to see the map
* @param entityId Entity-ID of the {@link ItemFrame} to show the map in
*/
void showInFrame(Player player, int entityId);
/**
* Show in an {@link ItemFrame}
*
* @param player {@link Player} that will be able to see the map
* @param entityId Entity-ID of the {@link ItemFrame} to show the map in
* @param debugInfo {@link String} to show when a player looks at the map, or <code>null</code>
*/
void showInFrame(Player player, int entityId, String debugInfo);
/**
* Clear a frame
*
* @param player {@link Player} that will be able to see the cleared frame
* @param entityId Entity-ID of the {@link ItemFrame} to clear
*/
void clearFrame(Player player, int entityId);
/**
* Clear a frame
*
* @param player {@link Player} that will be able to see the cleared frame
* @param frame {@link ItemFrame} to clear
*/
void clearFrame(Player player, ItemFrame frame);
/**
* Get an {@link ItemFrame} by its entity ID
*
* @param world The world the {@link ItemFrame} is in
* @param entityId Entity-ID of the {@link ItemFrame}
* @return The found {@link ItemFrame}, or <code>null</code>
*/
ItemFrame getItemFrameById(World world, int entityId);
}

View file

@ -1,179 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.api;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Nullable;
import tech.sbdevelopment.mapreflectionapi.exceptions.MapLimitExceededException;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
public class MapManager {
protected final Set<Integer> OCCUPIED_IDS = new HashSet<>();
private final List<MapWrapper> MANAGED_MAPS = new CopyOnWriteArrayList<>();
private final Class<?> wrapperClass;
public MapManager(JavaPlugin plugin) throws IllegalStateException {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
String version = packageName.substring(packageName.lastIndexOf('.') + 1);
plugin.getLogger().info("Initializing the map manager for Minecraft version " + version + "...");
try {
final Class<?> clazz = Class.forName("tech.sbdevelopment.mapreflectionapi.nms.MapWrapper_" + version);
if (MapWrapper.class.isAssignableFrom(clazz)) {
wrapperClass = clazz;
} else {
throw new IllegalStateException("Plugin corrupted! Detected invalid MapWrapper class.");
}
} catch (Exception ex) {
throw new IllegalStateException("This Spigot version (" + version + ") is not supported! Contact the developer to get support.");
}
}
@Nullable
public MapWrapper wrapImage(BufferedImage image) {
return wrapImage(new ArrayImage(image));
}
@Nullable
public MapWrapper wrapImage(ArrayImage image) {
for (MapWrapper wrapper : MANAGED_MAPS) {
if (wrapper.getContent().equals(image)) return wrapper;
}
return wrapNewImage(image);
}
private MapWrapper wrapNewImage(ArrayImage image) {
try {
MapWrapper wrapper = (MapWrapper) wrapperClass.getDeclaredConstructor(ArrayImage.class).newInstance(image);
MANAGED_MAPS.add(wrapper);
return wrapper;
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
public void unwrapImage(MapWrapper wrapper) {
//TODO Cancel IDs
wrapper.getController().clearViewers();
MANAGED_MAPS.remove(wrapper);
}
public Set<MapWrapper> getMapsVisibleTo(OfflinePlayer player) {
Set<MapWrapper> visible = new HashSet<>();
for (MapWrapper wrapper : MANAGED_MAPS) {
if (wrapper.getController().isViewing(player)) {
visible.add(wrapper);
}
}
return visible;
}
public MapWrapper getWrapperForId(OfflinePlayer player, int id) {
for (MapWrapper wrapper : getMapsVisibleTo(player)) {
if (wrapper.getController().getMapId(player) == id) {
return wrapper;
}
}
return null;
}
public void registerOccupiedID(int id) {
OCCUPIED_IDS.add(id);
}
public void unregisterOccupiedID(int id) {
OCCUPIED_IDS.remove(id);
}
public Set<Integer> getOccupiedIdsFor(OfflinePlayer player) {
Set<Integer> ids = new HashSet<>();
for (MapWrapper wrapper : MANAGED_MAPS) {
int s = wrapper.getController().getMapId(player);
if (s >= 0) {
ids.add(s);
}
}
return ids;
}
public boolean isIdUsedBy(OfflinePlayer player, int id) {
return id > 0 && getOccupiedIdsFor(player).contains(id);
}
public int getNextFreeIdFor(Player player) throws MapLimitExceededException {
Set<Integer> occupied = getOccupiedIdsFor(player);
//Add the 'default' occupied IDs
occupied.addAll(OCCUPIED_IDS);
int largest = 0;
for (Integer s : occupied) {
if (s > largest) {
largest = s;
}
}
//Simply increase the maximum id if it's still small enough
if (largest + 1 < Integer.MAX_VALUE) {
return largest + 1;
}
//Otherwise iterate through all options until there is an unused id
for (int s = 0; s < Integer.MAX_VALUE; s++) {
if (!occupied.contains(s)) {
return s;
}
}
//If we end up here, this player has no more free ids. Let's hope nobody uses this many Maps.
throw new MapLimitExceededException("'" + player + "' reached the maximum amount of available Map-IDs");
}
public void clearAllMapsFor(OfflinePlayer player) {
for (MapWrapper wrapper : getMapsVisibleTo(player)) {
wrapper.getController().removeViewer(player);
}
}
public MapWrapper getDuplicate(ArrayImage image) {
for (MapWrapper wrapper : MANAGED_MAPS) {
if (image.equals(wrapper.getContent())) {
return wrapper;
}
}
return null;
}
}

View file

@ -1,38 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.api;
public abstract class MapWrapper {
protected ArrayImage content;
public MapWrapper(ArrayImage image) {
this.content = image;
}
public abstract MapController getController();
public ArrayImage getContent() {
return content;
}
}

View file

@ -1,96 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.api.events;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import tech.sbdevelopment.mapreflectionapi.MapReflectionAPI;
import tech.sbdevelopment.mapreflectionapi.api.MapWrapper;
public class CreateInventoryMapUpdateEvent extends Event implements Cancellable {
private static final HandlerList handlerList = new HandlerList();
private final Player player;
private final int slot;
private final ItemStack item;
private MapWrapper mapWrapper;
private boolean cancelled;
public CreateInventoryMapUpdateEvent(Player player, int slot, ItemStack item) {
this.player = player;
this.slot = slot;
this.item = item;
}
public CreateInventoryMapUpdateEvent(Player player, int slot, ItemStack item, boolean isAsync) {
super(isAsync);
this.player = player;
this.slot = slot;
this.item = item;
}
public static HandlerList getHandlerList() {
return handlerList;
}
public Player getPlayer() {
return player;
}
public int getSlot() {
return slot;
}
public ItemStack getItem() {
return item;
}
public MapWrapper getMapWrapper() {
if (mapWrapper == null) {
if (item == null) return null;
if (item.getType() != Material.MAP) return null;
MapReflectionAPI.getMapManager().getWrapperForId(player, item.getDurability());
}
return mapWrapper;
}
@Override
public HandlerList getHandlers() {
return handlerList;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
this.cancelled = b;
}
}

View file

@ -1,74 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.api.events;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class MapCancelEvent extends Event implements Cancellable {
private static final HandlerList handlerList = new HandlerList();
private final Player player;
private final int id;
private boolean cancelled;
public MapCancelEvent(Player player, int id) {
this.player = player;
this.id = id;
}
public MapCancelEvent(Player player, int id, boolean isAsync) {
super(isAsync);
this.player = player;
this.id = id;
}
public static HandlerList getHandlerList() {
return handlerList;
}
public Player getPlayer() {
return player;
}
public int getId() {
return id;
}
@Override
public HandlerList getHandlers() {
return handlerList;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
this.cancelled = b;
}
}

View file

@ -1,118 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.api.events;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.util.Vector;
import tech.sbdevelopment.mapreflectionapi.MapReflectionAPI;
import tech.sbdevelopment.mapreflectionapi.api.MapWrapper;
public class MapInteractEvent extends Event implements Cancellable {
private static final HandlerList handlerList = new HandlerList();
private final Player player;
private final int entityID;
private final int action;
private final Vector vector;
private final int hand;
private ItemFrame frame;
private MapWrapper mapWrapper;
private boolean cancelled;
public MapInteractEvent(Player player, int entityID, int action, Vector vector, int hand) {
this.player = player;
this.entityID = entityID;
this.action = action;
this.vector = vector;
this.hand = hand;
}
public MapInteractEvent(Player player, int entityID, int action, Vector vector, int hand, boolean isAsync) {
super(isAsync);
this.player = player;
this.entityID = entityID;
this.action = action;
this.vector = vector;
this.hand = hand;
}
public static HandlerList getHandlerList() {
return handlerList;
}
public Player getPlayer() {
return player;
}
public int getEntityID() {
return entityID;
}
public int getAction() {
return action;
}
public Vector getVector() {
return vector;
}
public int getHand() {
return hand;
}
public ItemFrame getFrame() {
if (getMapWrapper() == null) return null;
if (frame == null) {
frame = getMapWrapper().getController().getItemFrameById(player.getWorld(), entityID);
}
return frame;
}
public MapWrapper getMapWrapper() {
if (mapWrapper == null) {
mapWrapper = MapReflectionAPI.getMapManager().getWrapperForId(player, entityID);
}
return mapWrapper;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
this.cancelled = b;
}
@Override
public HandlerList getHandlers() {
return handlerList;
}
}

View file

@ -1,30 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.exceptions;
public class MapLimitExceededException extends Exception {
public MapLimitExceededException(String message) {
super(message);
}
}

View file

@ -1,43 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.server.MapInitializeEvent;
import tech.sbdevelopment.mapreflectionapi.MapReflectionAPI;
public class MapListener implements Listener {
@EventHandler
public void onQuit(PlayerQuitEvent e) {
MapReflectionAPI.getMapManager().clearAllMapsFor(e.getPlayer());
}
@EventHandler
public void onMapInitialize(MapInitializeEvent e) {
int id = e.getMap().getId();
if (id > 0) MapReflectionAPI.getMapManager().registerOccupiedID(id);
}
}

View file

@ -1,85 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.listeners;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
public abstract class PacketListener implements Listener {
protected JavaPlugin plugin;
public static PacketListener construct(JavaPlugin plugin) throws IllegalStateException {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
String version = packageName.substring(packageName.lastIndexOf('.') + 1);
plugin.getLogger().info("Initializing the packet handler for Minecraft version " + version + "...");
try {
final Class<?> clazz = Class.forName("tech.sbdevelopment.mapreflectionapi.nms.PacketListener_" + version);
if (PacketListener.class.isAssignableFrom(clazz)) {
return (PacketListener) clazz.getDeclaredConstructor().newInstance();
} else {
throw new IllegalStateException("Plugin corrupted! Detected invalid PacketListener class.");
}
} catch (Exception ex) {
throw new IllegalStateException("This Minecraft version (" + version + ") is not supported! Contact the developer to get support.");
}
}
public void init(JavaPlugin plugin) {
this.plugin = plugin;
Bukkit.getPluginManager().registerEvents(this, plugin);
}
@EventHandler
public void onJoin(PlayerJoinEvent e) {
injectPlayer(e.getPlayer());
}
@EventHandler
public void onQuit(PlayerQuitEvent e) {
removePlayer(e.getPlayer());
}
protected abstract void injectPlayer(Player p);
public abstract void removePlayer(Player p);
protected abstract Vector vec3DToVector(Object vec3d);
protected boolean hasField(Object packet, String field) {
try {
packet.getClass().getDeclaredField(field);
return true;
} catch (NoSuchFieldException ex) {
return false;
}
}
}

View file

@ -1,54 +0,0 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionUtil {
public static Object getField(Object packet, String field) throws NoSuchFieldException, IllegalAccessException {
Field f = packet.getClass().getDeclaredField(field);
f.setAccessible(true);
return f.get(packet);
}
public static Object getField(Class<?> clazz, String field) throws NoSuchFieldException, IllegalAccessException {
Field f = clazz.getDeclaredField(field);
f.setAccessible(true);
return f.get(null);
}
public static void setField(Object packet, String field, Object value) throws NoSuchFieldException, IllegalAccessException {
Field f = packet.getClass().getDeclaredField(field);
f.setAccessible(true);
f.set(packet, value);
}
public static Object getValue(Object packet, String method) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method m = packet.getClass().getDeclaredMethod(method, null);
m.setAccessible(true);
return m.invoke(packet, null);
}
}