Skip to content

Commit

Permalink
Merge pull request #841 from TonytheMacaroni/main
Browse files Browse the repository at this point in the history
Add gametestaddmarker and gametestclearmarkers effects
  • Loading branch information
Chronoken authored Feb 3, 2024
2 parents 0f41df4 + b9dc606 commit 84eb62f
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.nisovin.magicspells.spelleffects.effecttypes;

import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.configuration.ConfigurationSection;

import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.util.SpellData;
import com.nisovin.magicspells.util.config.ConfigData;
import com.nisovin.magicspells.spelleffects.SpellEffect;
import com.nisovin.magicspells.util.config.ConfigDataUtil;

public class GameTestAddMarkerEffect extends SpellEffect {

private ConfigData<Color> color;
private ConfigData<String> name;
private ConfigData<Integer> lifetime;
private ConfigData<Boolean> broadcast;
private ConfigData<MarkerViewer> viewer;
private ConfigData<Boolean> useViewerAsTarget;
private ConfigData<Boolean> useViewerAsDefault;

@Override
protected void loadFromConfig(ConfigurationSection config) {
name = ConfigDataUtil.getString(config, "name", "");
color = ConfigDataUtil.getARGBColor(config, "color", Color.BLACK);
viewer = ConfigDataUtil.getEnum(config, "viewer", MarkerViewer.class, MarkerViewer.POSITION);
lifetime = ConfigDataUtil.getInteger(config, "lifetime", 1000);
broadcast = ConfigDataUtil.getBoolean(config, "broadcast", false);
useViewerAsTarget = ConfigDataUtil.getBoolean(config, "use-viewer-as-target", false);
useViewerAsDefault = ConfigDataUtil.getBoolean(config, "use-viewer-as-default", true);
}

@Override
protected Runnable playEffectLocation(Location location, SpellData data) {
if (broadcast.get(data)) {
broadcast(location, data);
return null;
}

Player viewer = switch (this.viewer.get(data)) {
case CASTER -> data.caster() instanceof Player p ? p : null;
case TARGET -> data.target() instanceof Player p ? p : null;
case POSITION -> null;
};
if (viewer == null) return null;

String name = this.name.get(data);
Color color = this.color.get(data);
int lifetime = this.lifetime.get(data);
MagicSpells.getVolatileCodeHandler().addGameTestMarker(viewer, location, color.asARGB(), name, lifetime);

return null;
}

@Override
protected Runnable playEffectEntity(Entity entity, SpellData data) {
if (broadcast.get(data)) {
broadcast(entity.getLocation(), data);
return null;
}

Player viewer = switch (this.viewer.get(data)) {
case CASTER -> data.caster() instanceof Player p ? p : null;
case TARGET -> data.target() instanceof Player p ? p : null;
case POSITION -> entity instanceof Player p ? p : null;
};
if (viewer == null) return null;

String name = this.name.get(data);
Color color = this.color.get(data);
int lifetime = this.lifetime.get(data);
MagicSpells.getVolatileCodeHandler().addGameTestMarker(viewer, entity.getLocation(), color.asARGB(), name, lifetime);

return null;
}

private void broadcast(Location location, SpellData data) {
boolean useViewerAsTarget = this.useViewerAsTarget.get(data);
boolean useViewerAsDefault = this.useViewerAsDefault.get(data);

String name = null;
Color color = null;
int lifetime = 0;

if (!useViewerAsTarget && !useViewerAsDefault) {
name = this.name.get(data);
color = this.color.get(data);
lifetime = this.lifetime.get(data);
}

for (Player viewer : location.getWorld().getPlayers()) {
if (useViewerAsTarget || useViewerAsDefault) {
SpellData spellData = data;
if (useViewerAsTarget) spellData = spellData.target(viewer);
if (useViewerAsDefault) spellData = spellData.recipient(viewer);

name = this.name.get(spellData);
color = this.color.get(spellData);
lifetime = this.lifetime.get(spellData);
}

MagicSpells.getVolatileCodeHandler().addGameTestMarker(viewer, location, color.asARGB(), name, lifetime);
}
}

private enum MarkerViewer {

CASTER,
TARGET,
POSITION

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.nisovin.magicspells.spelleffects.effecttypes;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.configuration.ConfigurationSection;

import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.util.SpellData;
import com.nisovin.magicspells.util.config.ConfigData;
import com.nisovin.magicspells.spelleffects.SpellEffect;
import com.nisovin.magicspells.util.config.ConfigDataUtil;

public class GameTestClearMarkersEffect extends SpellEffect {

private ConfigData<Boolean> broadcast;
private ConfigData<MarkerViewer> viewer;

@Override
protected void loadFromConfig(ConfigurationSection config) {
viewer = ConfigDataUtil.getEnum(config, "viewer", MarkerViewer.class, MarkerViewer.POSITION);
broadcast = ConfigDataUtil.getBoolean(config, "broadcast", false);
}

@Override
protected Runnable playEffectLocation(Location location, SpellData data) {
if (broadcast.get(data)) {
for (Player viewer : location.getWorld().getPlayers())
MagicSpells.getVolatileCodeHandler().clearGameTestMarkers(viewer);

return null;
}

Player viewer = switch (this.viewer.get(data)) {
case CASTER -> data.caster() instanceof Player p ? p : null;
case TARGET -> data.target() instanceof Player p ? p : null;
case POSITION -> null;
};
if (viewer == null) return null;

MagicSpells.getVolatileCodeHandler().clearGameTestMarkers(viewer);

return null;
}

@Override
protected Runnable playEffectEntity(Entity entity, SpellData data) {
if (broadcast.get(data)) {
for (Player viewer : entity.getWorld().getPlayers())
MagicSpells.getVolatileCodeHandler().clearGameTestMarkers(viewer);

return null;
}

Player viewer = switch (this.viewer.get(data)) {
case CASTER -> data.caster() instanceof Player p ? p : null;
case TARGET -> data.target() instanceof Player p ? p : null;
case POSITION -> entity instanceof Player p ? p : null;
};
if (viewer == null) return null;

MagicSpells.getVolatileCodeHandler().clearGameTestMarkers(viewer);

return null;
}

private enum MarkerViewer {

CASTER,
TARGET,
POSITION

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ private void initialize() {
addSpellEffect("entity", EntityEffect.class);
addSpellEffect("explosion", ExplosionEffect.class);
addSpellEffect("fireworks", FireworksEffect.class);
addSpellEffect("gametestaddmarker", GameTestAddMarkerEffect.class);
addSpellEffect("gametestclearmarkers", GameTestClearMarkersEffect.class);
addSpellEffect("itemcooldown", ItemCooldownEffect.class);
addSpellEffect("itemspray", ItemSprayEffect.class);
addSpellEffect("lightning", LightningEffect.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import io.papermc.paper.advancement.AdvancementDisplay.Frame;


public class VolatileCodeDisabled extends VolatileCodeHandle {

public VolatileCodeDisabled() {
Expand Down Expand Up @@ -71,4 +70,14 @@ public void sendStatusUpdate(Player player, double health, int food, float satur

}

@Override
public void addGameTestMarker(Player player, Location location, int color, String name, int lifetime) {

}

@Override
public void clearGameTestMarkers(Player player) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public abstract Recipe createSmithingRecipe(

public abstract void sendStatusUpdate(Player player, double health, int food, float saturation);

public abstract void addGameTestMarker(Player player, Location location, int color, String name, int lifetime);

public abstract void clearGameTestMarkers(Player player);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack

import net.kyori.adventure.text.Component

import io.papermc.paper.util.MCUtil
import io.papermc.paper.adventure.PaperAdventure
import io.papermc.paper.advancement.AdvancementDisplay

Expand All @@ -33,6 +34,9 @@ import net.minecraft.world.item.alchemy.PotionUtils
import net.minecraft.network.syncher.EntityDataAccessor
import net.minecraft.advancements.critereon.ImpossibleTrigger
import net.minecraft.world.entity.boss.enderdragon.EnderDragon
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket
import net.minecraft.network.protocol.common.custom.GameTestAddMarkerDebugPayload
import net.minecraft.network.protocol.common.custom.GameTestClearMarkersDebugPayload

import com.nisovin.magicspells.volatilecode.VolatileCodeHandle
import com.nisovin.magicspells.volatilecode.VolatileCodeHelper
Expand Down Expand Up @@ -180,4 +184,14 @@ class VolatileCode1_20_R2(helper: VolatileCodeHelper) : VolatileCodeHandle(helpe
(player as CraftPlayer).handle.connection.send(ClientboundSetHealthPacket(health.toFloat(), food, saturation))
}

override fun addGameTestMarker(player: Player, location: Location, color: Int, name: String, lifetime: Int) {
val payload = GameTestAddMarkerDebugPayload(MCUtil.toBlockPosition(location), color, name, lifetime)
(player as CraftPlayer).handle.connection.send(ClientboundCustomPayloadPacket(payload))
}

override fun clearGameTestMarkers(player: Player) {
val payload = GameTestClearMarkersDebugPayload()
(player as CraftPlayer).handle.connection.send(ClientboundCustomPayloadPacket(payload))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack

import net.kyori.adventure.text.Component

import io.papermc.paper.util.MCUtil
import io.papermc.paper.adventure.PaperAdventure
import io.papermc.paper.advancement.AdvancementDisplay

Expand All @@ -33,6 +34,9 @@ import net.minecraft.world.item.alchemy.PotionUtils
import net.minecraft.network.syncher.EntityDataAccessor
import net.minecraft.advancements.critereon.ImpossibleTrigger
import net.minecraft.world.entity.boss.enderdragon.EnderDragon
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket
import net.minecraft.network.protocol.common.custom.GameTestAddMarkerDebugPayload
import net.minecraft.network.protocol.common.custom.GameTestClearMarkersDebugPayload

import com.nisovin.magicspells.volatilecode.VolatileCodeHandle
import com.nisovin.magicspells.volatilecode.VolatileCodeHelper
Expand Down Expand Up @@ -180,4 +184,14 @@ class VolatileCode1_20_R3(helper: VolatileCodeHelper) : VolatileCodeHandle(helpe
(player as CraftPlayer).handle.connection.send(ClientboundSetHealthPacket(health.toFloat(), food, saturation))
}

override fun addGameTestMarker(player: Player, location: Location, color: Int, name: String, lifetime: Int) {
val payload = GameTestAddMarkerDebugPayload(MCUtil.toBlockPosition(location), color, name, lifetime)
(player as CraftPlayer).handle.connection.send(ClientboundCustomPayloadPacket(payload))
}

override fun clearGameTestMarkers(player: Player) {
val payload = GameTestClearMarkersDebugPayload()
(player as CraftPlayer).handle.connection.send(ClientboundCustomPayloadPacket(payload))
}

}

0 comments on commit 84eb62f

Please sign in to comment.