wip: switching stuff over for warp effects

This commit is contained in:
sekwah 2024-02-03 19:54:30 +00:00
parent bc6c492899
commit 7b3004ea35
11 changed files with 127 additions and 62 deletions

View File

@ -11,7 +11,7 @@ public interface EntityContainer {
BlockLocation getBlockLoc(); BlockLocation getBlockLoc();
void teleport(PlayerLocation location); boolean teleport(PlayerLocation location);
WorldContainer getWorld(); WorldContainer getWorld();

View File

@ -2,30 +2,24 @@ package com.sekwah.advancedportals.core.effect;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.serializeddata.WorldLocation;
import com.sekwah.advancedportals.core.portal.AdvancedPortal; import com.sekwah.advancedportals.core.portal.AdvancedPortal;
/** public interface WarpEffect {
* Effects to be registered to the list.
* <p>
* Fires once at each end.
* <p>
* Can be a Visual effect or a Sound. Just register to the correct one
*
* @author sekwah41
*/
public abstract class WarpEffect {
protected abstract void onWarp(PlayerContainer player, WorldLocation loc, Action action, Type type, AdvancedPortal portal); enum Action {
public enum Action {
ENTER, ENTER,
EXIT; EXIT;
} }
public enum Type { interface Sound extends WarpEffect {
SOUND,
VISUAL; void onWarpSound(PlayerContainer player, Action action, AdvancedPortal portal);
} }
interface Visual extends WarpEffect {
void onWarpVisual(PlayerContainer player, Action action, AdvancedPortal portal);
}
} }

View File

@ -4,6 +4,7 @@ import com.google.inject.*;
import com.sekwah.advancedportals.core.AdvancedPortalsCore; import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.connector.containers.ServerContainer; import com.sekwah.advancedportals.core.connector.containers.ServerContainer;
import com.sekwah.advancedportals.core.registry.TagRegistry; import com.sekwah.advancedportals.core.registry.TagRegistry;
import com.sekwah.advancedportals.core.registry.WarpEffectRegistry;
import com.sekwah.advancedportals.core.repository.IPlayerDataRepository; import com.sekwah.advancedportals.core.repository.IPlayerDataRepository;
import com.sekwah.advancedportals.core.repository.impl.PlayerDataRepositoryImpl; import com.sekwah.advancedportals.core.repository.impl.PlayerDataRepositoryImpl;
import com.sekwah.advancedportals.core.serializeddata.config.Config; import com.sekwah.advancedportals.core.serializeddata.config.Config;
@ -53,6 +54,7 @@ public class AdvancedPortalsModule extends AbstractModule {
// Providers // Providers
bind(Config.class).toProvider(ConfigProvider.class); bind(Config.class).toProvider(ConfigProvider.class);
bind(TagRegistry.class).asEagerSingleton(); bind(TagRegistry.class).asEagerSingleton();
bind(WarpEffectRegistry.class).asEagerSingleton();
// Delayed Bindings // Delayed Bindings
for(DelayedBinding delayedBinding : delayedBindings) { for(DelayedBinding delayedBinding : delayedBindings) {

View File

@ -14,9 +14,7 @@ import java.util.Map;
public class WarpEffectRegistry { public class WarpEffectRegistry {
private Map<String, WarpEffect> visualEffects = new HashMap(); private Map<String, WarpEffect> warpEffects = new HashMap();
private Map<String, WarpEffect> soundEffects = new HashMap();
@Inject @Inject
private AdvancedPortalsCore portalsCore; private AdvancedPortalsCore portalsCore;
@ -31,50 +29,49 @@ public class WarpEffectRegistry {
* @param effect * @param effect
* @return if the effect was registered * @return if the effect was registered
*/ */
public boolean registerEffect(String name, WarpEffect effect, WarpEffect.Type type) { public void registerEffect(String name, WarpEffect effect) {
if(name == null){ if(name == null){
return false; this.infoLogger.warning("Effect name cannot be null");
return;
} }
Map<String, WarpEffect> list = null; if(this.warpEffects.containsKey(name)){
switch (type){ this.infoLogger.warning("Effect with the name: " + name + " already exists");
case SOUND: return;
list = this.soundEffects;
break;
case VISUAL:
list = this.visualEffects;
break;
default:
this.portalsCore.getInfoLogger().warning(type.toString()
+ " effect type not recognised");
return false;
} }
if(list.containsKey(name)){ this.warpEffects.put(name, effect);
return false;
}
list.put(name, effect);
return true;
} }
public WarpEffect getEffect(String name, WarpEffect.Type type){ public WarpEffect.Visual getVisualEffect(String name){
Map<String, WarpEffect> list = null; if(this.warpEffects.containsKey(name)) {
switch (type){ var effect = this.warpEffects.get(name);
case SOUND: if(effect instanceof WarpEffect.Visual visual){
list = this.soundEffects; return visual;
break; }
case VISUAL: else{
list = this.visualEffects; this.infoLogger.warning("Effect called " + name + " is not a visual effect");
break;
default:
this.infoLogger.warning(type.toString()
+ " effect type not recognised");
return null; return null;
} }
if(list.containsKey(name)) {
return list.get(name);
} }
else{ else{
this.infoLogger.warning("No effect of type:" this.infoLogger.warning("No effect called " + name + " was registered");
+ type.toString() + " was registered with the name: " + name); return null;
}
}
public WarpEffect.Sound getSoundEffect(String name){
if(this.warpEffects.containsKey(name)) {
var effect = this.warpEffects.get(name);
if(effect instanceof WarpEffect.Sound sound){
return sound;
}
else{
this.infoLogger.warning("Effect called " + name + " is not a sound effect");
return null;
}
}
else{
this.infoLogger.warning("No effect called " + name + " was registered");
return null; return null;
} }
} }

View File

@ -103,4 +103,8 @@ public class DestinationServices {
} }
return false; return false;
} }
public Destination getDestination(String name) {
return destinationCache.get(name);
}
} }

View File

@ -3,7 +3,9 @@ package com.sekwah.advancedportals.core.tags.activation;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.destination.Destination; import com.sekwah.advancedportals.core.destination.Destination;
import com.sekwah.advancedportals.core.effect.WarpEffect;
import com.sekwah.advancedportals.core.registry.TagTarget; import com.sekwah.advancedportals.core.registry.TagTarget;
import com.sekwah.advancedportals.core.registry.WarpEffectRegistry;
import com.sekwah.advancedportals.core.services.DestinationServices; import com.sekwah.advancedportals.core.services.DestinationServices;
import com.sekwah.advancedportals.core.util.Lang; import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.core.warphandler.ActivationData; import com.sekwah.advancedportals.core.warphandler.ActivationData;
@ -18,6 +20,9 @@ public class DestiTag implements Tag.Activation, Tag.AutoComplete, Tag.Split {
@Inject @Inject
DestinationServices destinationServices; DestinationServices destinationServices;
@Inject
WarpEffectRegistry warpEffectRegistry;
private final TagType[] tagTypes = new TagType[]{ TagType.PORTAL }; private final TagType[] tagTypes = new TagType[]{ TagType.PORTAL };
@Override @Override
@ -58,7 +63,16 @@ public class DestiTag implements Tag.Activation, Tag.AutoComplete, Tag.Split {
@Override @Override
public boolean activated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) { public boolean activated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) {
return false; System.out.println("Teleporting to destination");
Destination destination = destinationServices.getDestination(argData[0]);
if (destination != null) {
var warpEffect = warpEffectRegistry.getVisualEffect("ender");
if (warpEffect != null) {
warpEffect.onWarpVisual(player, ac);
}
player.teleport(destination.getLoc());
}
return true;
} }
@Override @Override

View File

@ -9,6 +9,7 @@ import com.sekwah.advancedportals.spigot.commands.subcommands.portal.UpdatePorta
import com.sekwah.advancedportals.spigot.connector.command.SpigotCommandRegister; import com.sekwah.advancedportals.spigot.connector.command.SpigotCommandRegister;
import com.sekwah.advancedportals.spigot.connector.container.SpigotServerContainer; import com.sekwah.advancedportals.spigot.connector.container.SpigotServerContainer;
import com.sekwah.advancedportals.spigot.metrics.Metrics; import com.sekwah.advancedportals.spigot.metrics.Metrics;
import com.sekwah.advancedportals.spigot.warpeffects.SpigotWarpEffects;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -56,6 +57,10 @@ public class AdvancedPortalsPlugin extends JavaPlugin {
GameScheduler scheduler = injector.getInstance(GameScheduler.class); GameScheduler scheduler = injector.getInstance(GameScheduler.class);
this.getServer().getScheduler().scheduleSyncRepeatingTask(this, scheduler::tick, 1, 1); this.getServer().getScheduler().scheduleSyncRepeatingTask(this, scheduler::tick, 1, 1);
var warpEffects = new SpigotWarpEffects();
injector.injectMembers(warpEffects);
warpEffects.registerEffects();
// Try to do this after setting up everything that would need to be injected to. // Try to do this after setting up everything that would need to be injected to.
this.portalsCore.onEnable(); this.portalsCore.onEnable();

View File

@ -49,8 +49,8 @@ public class SpigotEntityContainer implements EntityContainer {
} }
@Override @Override
public void teleport(PlayerLocation location) { public boolean teleport(PlayerLocation location) {
this.entity.teleport(new Location(Bukkit.getWorld(location.getWorldName()), location.getPosX(), location.getPosY(), location.getPosZ())); return this.entity.teleport(new Location(Bukkit.getWorld(location.getWorldName()), location.getPosX(), location.getPosY(), location.getPosZ()));
} }

View File

@ -49,8 +49,8 @@ public class SpigotPlayerContainer extends SpigotEntityContainer implements Play
} }
@Override @Override
public void teleport(PlayerLocation location) { public boolean teleport(PlayerLocation location) {
this.player.teleport(new Location(Bukkit.getWorld(location.getWorldName()), location.getPosX(), location.getPosY(), location.getPosZ())); return this.player.teleport(new Location(Bukkit.getWorld(location.getWorldName()), location.getPosX(), location.getPosY(), location.getPosZ()));
} }
@Override @Override
@ -97,4 +97,8 @@ public class SpigotPlayerContainer extends SpigotEntityContainer implements Play
} }
return true; return true;
} }
public Player getPlayer() {
return this.player;
}
} }

View File

@ -0,0 +1,28 @@
package com.sekwah.advancedportals.spigot.warpeffects;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.effect.WarpEffect;
import com.sekwah.advancedportals.core.portal.AdvancedPortal;
import com.sekwah.advancedportals.core.serializeddata.WorldLocation;
import com.sekwah.advancedportals.spigot.connector.container.SpigotPlayerContainer;
public class EnderWarpEffect implements WarpEffect.Visual, WarpEffect.Sound {
@Override
public void onWarpSound(PlayerContainer playerContainer, WarpEffect.Action action, AdvancedPortal portal) {
if(playerContainer instanceof SpigotPlayerContainer spigotPlayerContainer) {
var player = spigotPlayerContainer.getPlayer();
player.playSound(player.getLocation(), "entity.enderman.teleport", 1, 1);
}
}
@Override
public void onWarpVisual(PlayerContainer player, WarpEffect.Action action, AdvancedPortal portal) {
if (action == WarpEffect.Action.ENTER) {
player.spawnParticle(new WorldLocation(portal.getPortalLocation()), "ENDER_SIGNAL", 1, 1, 1, 1, 1);
} else {
player.spawnParticle(new WorldLocation(portal.getPortalLocation()), "ENDER_SIGNAL", 1, 1, 1, 1, 1);
}
}
}

View File

@ -0,0 +1,17 @@
package com.sekwah.advancedportals.spigot.warpeffects;
import com.sekwah.advancedportals.core.effect.WarpEffect;
import com.sekwah.advancedportals.core.registry.WarpEffectRegistry;
import javax.inject.Inject;
public class SpigotWarpEffects {
@Inject
private WarpEffectRegistry warpEffectRegistry;
public void registerEffects() {
warpEffectRegistry.registerEffect("ender", new EnderWarpEffect());
}
}