Skip to content

Commit

Permalink
Added holdable displays
Browse files Browse the repository at this point in the history
  • Loading branch information
pianoman911 committed Apr 4, 2023
1 parent 0d010b6 commit 7ab4e73
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.pianoman911.mapengine.api;

import de.pianoman911.mapengine.api.clientside.IDisplayProvider;
import de.pianoman911.mapengine.api.clientside.IHoldableDisplay;
import de.pianoman911.mapengine.api.clientside.IMapDisplay;
import de.pianoman911.mapengine.api.colors.IMapColors;
import de.pianoman911.mapengine.api.pipeline.IPipelineProvider;
Expand All @@ -19,7 +20,10 @@ public interface MapEngineApi {
IDisplayProvider displayProvider();

@Unmodifiable
Set<IMapDisplay> displays();
Set<IMapDisplay> mapDisplays();

@Unmodifiable
Set<IHoldableDisplay> holdableDisplays();

@Nullable
IMapDisplay displayInView(Player player, int maxDistance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public interface IDisplayProvider {
IMapDisplay createRawPipelineDisplay(BlockVector a, BlockVector b, BlockFace direction, IPipeline pipeline);

IMapDisplay createBasic(BlockVector a, BlockVector b, BlockFace direction);

IHoldableDisplay createRawPipelineHoldableDisplay(IPipeline pipeline);

IHoldableDisplay createHoldableDisplay();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.pianoman911.mapengine.api.clientside;

import de.pianoman911.mapengine.api.pipeline.IPipeline;
import org.bukkit.inventory.ItemStack;

public interface IHoldableDisplay {

ItemStack itemStack(int z);

IPipeline pipeline();
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ allprojects {
apply(plugin = "maven-publish")

group = "de.pianoman911"
version = "1.3.2"
version = "1.4.0"

repositories {
maven("https://repo.papermc.io/repository/maven-public/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.pianoman911.mapengine.common.platform.IPlatform;
import de.pianoman911.mapengine.core.api.ImplMapEngineApi;
import de.pianoman911.mapengine.core.colors.ColorPalette;
import de.pianoman911.mapengine.core.map.HoldableManager;
import de.pianoman911.mapengine.core.map.MapManager;
import de.pianoman911.mapengine.core.platform.ImplListenerBridge;
import de.pianoman911.mapengine.core.platform.PlatformUtil;
Expand All @@ -17,6 +18,7 @@ public class MapEnginePlugin extends JavaPlugin {
private IPlatform<?> platform;
private ColorPalette colorPalette;
private MapManager mapManager;
private HoldableManager holdableManager;
private ImplMapEngineApi api;

@Override
Expand All @@ -28,7 +30,9 @@ public void onLoad() {
public void onEnable() {
this.platform = PlatformUtil.getPlatform(this, this.getClassLoader(), new ImplListenerBridge(this));
this.colorPalette = new ColorPalette(this);

this.mapManager = new MapManager(this);
this.holdableManager = new HoldableManager(this);

this.api = new ImplMapEngineApi(this);
Bukkit.getServicesManager().register(MapEngineApi.class, this.api, this, ServicePriority.Normal);
Expand All @@ -49,4 +53,8 @@ public MapManager mapManager() {
public ImplMapEngineApi api() {
return api;
}

public HoldableManager holdableManager() {
return holdableManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.pianoman911.mapengine.api.MapEngineApi;
import de.pianoman911.mapengine.api.clientside.IDisplayProvider;
import de.pianoman911.mapengine.api.clientside.IHoldableDisplay;
import de.pianoman911.mapengine.api.clientside.IMapDisplay;
import de.pianoman911.mapengine.api.colors.IMapColors;
import de.pianoman911.mapengine.api.drawing.IDrawingSpace;
Expand Down Expand Up @@ -123,14 +124,29 @@ public IMapDisplay createRawPipelineDisplay(BlockVector a, BlockVector b, BlockF
public IMapDisplay createBasic(BlockVector a, BlockVector b, BlockFace direction) {
return plugin.mapManager().createDisplay(a, b, direction);
}

@Override
public IHoldableDisplay createRawPipelineHoldableDisplay(IPipeline pipeline) {
return plugin.holdableManager().createDisplay((Pipeline) pipeline);
}

@Override
public IHoldableDisplay createHoldableDisplay() {
return plugin.holdableManager().createDisplay();
}
};
}

@Override
public @Unmodifiable Set<IMapDisplay> displays() {
public @Unmodifiable Set<IMapDisplay> mapDisplays() {
return Set.copyOf(plugin.mapManager().displays());
}

@Override
public @Unmodifiable Set<IHoldableDisplay> holdableDisplays() {
return Set.copyOf(plugin.holdableManager().displays());
}

@Override
public @Nullable IMapDisplay displayInView(Player player, int maxDistance) {
return plugin.mapManager().displayInView(player, maxDistance);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.pianoman911.mapengine.core.clientside;

import de.pianoman911.mapengine.common.data.MapUpdateData;
import de.pianoman911.mapengine.common.platform.PacketContainer;
import de.pianoman911.mapengine.core.MapEnginePlugin;
import it.unimi.dsi.fastutil.ints.Int2IntArrayMap;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import org.bukkit.map.MapCursorCollection;

public class FilledMap {

// start counting down at -32768 for compatibility with other map plugins
protected static int CURRENT_ID = -Short.MAX_VALUE;

protected final Int2IntMap mapIds = new Int2IntArrayMap();
protected final MapEnginePlugin plugin;

public FilledMap(MapEnginePlugin plugin) {
this.plugin = plugin;
}

protected PacketContainer<?> updatePacket(MapUpdateData data, boolean fullData, int z, MapCursorCollection cursors) {
return plugin.platform().createMapDataPacket(data, fullData, mapId(z), cursors);
}

protected int mapId(int z) {
return mapIds.computeIfAbsent(z, $ -> CURRENT_ID--);
}
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,35 @@
package de.pianoman911.mapengine.core.clientside;

import de.pianoman911.mapengine.common.data.MapUpdateData;
import de.pianoman911.mapengine.common.platform.PacketContainer;
import de.pianoman911.mapengine.core.MapEnginePlugin;
import it.unimi.dsi.fastutil.ints.Int2IntArrayMap;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import org.bukkit.Bukkit;
import org.bukkit.block.BlockFace;
import org.bukkit.map.MapCursorCollection;
import org.bukkit.util.BlockVector;

public class Frame {

// start counting down at -32768 for compatibility with other map plugins
private static int CURRENT_ID = -Short.MAX_VALUE;
public class Frame extends FilledMap {

@SuppressWarnings("deprecation") // unsafe api, don't care + didn't ask
protected final int entityId = Bukkit.getUnsafe().nextEntityId();

protected final Int2IntMap mapIds = new Int2IntArrayMap();
protected final MapEnginePlugin plugin;
protected final BlockFace direction;
protected final BlockVector pos;

protected Frame(MapEnginePlugin plugin, BlockFace direction, BlockVector pos) {
super(plugin);
this.direction = direction;
this.pos = pos;
this.plugin = plugin;
}

protected PacketContainer<?> spawnPacket() {
return plugin.platform().createMapEntitySpawnPacket(entityId, pos, direction);
}

protected PacketContainer<?> setIdPacket(int z, boolean invisible) {
return plugin.platform().createMapSetIdPacket(entityId, mapIds.computeIfAbsent(z, $ -> CURRENT_ID--), invisible);
}

protected PacketContainer<?> removePacket() {
return plugin.platform().createRemoveEntitiesPacket(new IntArrayList(entityId));
}

protected PacketContainer<?> updatePacket(MapUpdateData data, boolean fullData, int z, MapCursorCollection cursors) {
return plugin.platform().createMapDataPacket(data, fullData, mapIds.computeIfAbsent(z, $ -> CURRENT_ID--), cursors);
protected PacketContainer<?> setIdPacket(int z, boolean invisible) {
return plugin.platform().createMapSetIdPacket(entityId, mapId(z), invisible);
}

public BlockVector pos() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.pianoman911.mapengine.core.clientside;

import de.pianoman911.mapengine.api.clientside.IHoldableDisplay;
import de.pianoman911.mapengine.core.MapEnginePlugin;
import de.pianoman911.mapengine.core.pipeline.Pipeline;
import de.pianoman911.mapengine.core.util.DummyMapView;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.MapMeta;

public class MapItem extends FilledMap implements IHoldableDisplay {

private final Pipeline pipeline;

public MapItem(MapEnginePlugin plugin, Pipeline pipeline) {
super(plugin);
this.pipeline = pipeline;
}

@Override
public ItemStack itemStack(int z) {
ItemStack item = new ItemStack(Material.FILLED_MAP);
item.editMeta(MapMeta.class, meta -> meta.setMapView(new DummyMapView(mapId(z))));
return item;
}

@Override
public Pipeline pipeline() {
return pipeline;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package de.pianoman911.mapengine.core.map;

import de.pianoman911.mapengine.api.clientside.IHoldableDisplay;
import de.pianoman911.mapengine.core.MapEnginePlugin;
import de.pianoman911.mapengine.core.clientside.MapItem;
import de.pianoman911.mapengine.core.pipeline.Pipeline;

import java.util.HashSet;
import java.util.Set;

public class HoldableManager {

private final Set<IHoldableDisplay> displays = new HashSet<>();

private final MapEnginePlugin plugin;

public HoldableManager(MapEnginePlugin plugin) {
this.plugin = plugin;
}

public IHoldableDisplay createDisplay(Pipeline pipeline) {
MapItem item = new MapItem(plugin, pipeline);
displays.add(item);
return item;
}

public IHoldableDisplay createDisplay() {
MapItem item = new MapItem(plugin, new Pipeline(plugin));
displays.add(item);
return item;
}

public Set<IHoldableDisplay> displays() {
return displays;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package de.pianoman911.mapengine.core.util;

import org.bukkit.World;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;

public record DummyMapView(int id) implements MapView {

@Override
public int getId() {
return id;
}

@Override
public boolean isVirtual() {
return true;
}

@Override
public @NotNull
Scale getScale() {
return Scale.NORMAL;
}

@Override
public void setScale(@NotNull Scale scale) {

}

@Override
public int getCenterX() {
return 0;
}

@Override
public int getCenterZ() {
return 0;
}

@Override
public void setCenterX(int x) {

}

@Override
public void setCenterZ(int z) {

}

@Override
public @Nullable
World getWorld() {
return null;
}

@Override
public void setWorld(@NotNull World world) {

}

@Override
public @NotNull
List<MapRenderer> getRenderers() {
return Collections.emptyList();
}

@Override
public void addRenderer(@NotNull MapRenderer renderer) {

}

@Override
public boolean removeRenderer(@Nullable MapRenderer renderer) {
return true;
}

@Override
public boolean isTrackingPosition() {
return false;
}

@Override
public void setTrackingPosition(boolean trackingPosition) {

}

@Override
public boolean isUnlimitedTracking() {
return false;
}

@Override
public void setUnlimitedTracking(boolean unlimited) {

}

@Override
public boolean isLocked() {
return true;
}

@Override
public void setLocked(boolean locked) {

}
}

0 comments on commit 7ab4e73

Please sign in to comment.