Made SpawnComponent, working just fine

Supports Multiverse and BungeeCord
This commit is contained in:
Norbi Peti 2019-08-18 03:49:36 +02:00
parent 14bcebabcd
commit 2ad7a757c6
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 134 additions and 2 deletions

View file

@ -12,7 +12,6 @@
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" /> <orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.10" level="project" /> <orderEntry type="library" name="Maven: org.reflections:reflections:0.9.10" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" /> <orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" /> <orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" />

View file

@ -119,6 +119,10 @@
<id>Votifier</id> <id>Votifier</id>
<url>https://dl.bintray.com/nuvotifier/maven/</url> <url>https://dl.bintray.com/nuvotifier/maven/</url>
</repository> </repository>
<repository>
<id>Multiverse-Core</id>
<url>http://repo.onarandombox.com/content/repositories/multiverse/</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
@ -182,6 +186,12 @@
<version>2.3.4</version> <version>2.3.4</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.onarandombox.multiversecore</groupId>
<artifactId>Multiverse-Core</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<organization> <organization>
<name>TBMCPlugins</name> <name>TBMCPlugins</name>

View file

@ -6,6 +6,7 @@ import buttondevteam.core.component.channel.ChatRoom;
import buttondevteam.core.component.members.MemberComponent; import buttondevteam.core.component.members.MemberComponent;
import buttondevteam.core.component.randomtp.RandomTPComponent; import buttondevteam.core.component.randomtp.RandomTPComponent;
import buttondevteam.core.component.restart.RestartComponent; import buttondevteam.core.component.restart.RestartComponent;
import buttondevteam.core.component.spawn.SpawnComponent;
import buttondevteam.core.component.towny.TownyComponent; import buttondevteam.core.component.towny.TownyComponent;
import buttondevteam.core.component.updater.PluginUpdater; import buttondevteam.core.component.updater.PluginUpdater;
import buttondevteam.core.component.updater.PluginUpdaterComponent; import buttondevteam.core.component.updater.PluginUpdaterComponent;
@ -89,6 +90,7 @@ public class MainPlugin extends ButtonPlugin {
Component.registerComponent(this, new ChannelComponent()); Component.registerComponent(this, new ChannelComponent());
Component.registerComponent(this, new RandomTPComponent()); Component.registerComponent(this, new RandomTPComponent());
Component.registerComponent(this, new MemberComponent()); Component.registerComponent(this, new MemberComponent());
Component.registerComponent(this, new SpawnComponent());
if (Bukkit.getPluginManager().isPluginEnabled("Towny")) //It fails to load the component class otherwise if (Bukkit.getPluginManager().isPluginEnabled("Towny")) //It fails to load the component class otherwise
Component.registerComponent(this, new TownyComponent()); Component.registerComponent(this, new TownyComponent());
if (Bukkit.getPluginManager().isPluginEnabled("Votifier") && economy != null) if (Bukkit.getPluginManager().isPluginEnabled("Votifier") && economy != null)

View file

@ -0,0 +1,120 @@
package buttondevteam.core.component.spawn;
import buttondevteam.core.MainPlugin;
import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.architecture.ConfigData;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.ICommand2MC;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.onarandombox.MultiverseCore.MultiverseCore;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.io.*;
public class SpawnComponent extends Component<MainPlugin> implements PluginMessageListener {
@Override
protected void enable() {
registerCommand(new SpawnCommand());
if (targetServer().get().length() == 0) {
spawnloc = MultiverseCore.getPlugin(MultiverseCore.class).getMVWorldManager().getFirstSpawnWorld()
.getSpawnLocation();
}
Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(getPlugin(), "BungeeCord");
Bukkit.getServer().getMessenger().registerIncomingPluginChannel(getPlugin(), "BungeeCord", this);
}
@Override
protected void disable() {
Bukkit.getServer().getMessenger().unregisterIncomingPluginChannel(getPlugin(), "BungeeCord");
Bukkit.getServer().getMessenger().unregisterOutgoingPluginChannel(getPlugin(), "BungeeCord");
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!channel.equals("BungeeCord")) {
return;
}
if (targetServer().get().length() != 0)
return;
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String subchannel = in.readUTF();
if ("ChromaCore-Spawn".equals(subchannel)) {
// Use the code sample in the 'Response' sections below to read
// the data.
System.out.println("Heh nice");
short len = in.readShort();
byte[] msgbytes = new byte[len];
in.readFully(msgbytes);
try {
DataInputStream msgin = new DataInputStream(new ByteArrayInputStream(msgbytes));
String somedata = msgin.readUTF(); // Read the data in the same way you wrote it
if (!"SendToSpawn".equals(somedata)) {
System.out.println("somedata: " + somedata);
return;
}
player.teleport(spawnloc);
} catch (IOException e) {
e.printStackTrace();
}
} else
System.out.println("Subchannel: " + subchannel);
}
/**
* Set to empty if this server is the target.
*/
private ConfigData<String> targetServer() {
return getConfig().getData("targetServer", "");
}
private Location spawnloc;
@CommandClass(helpText = {
"Spawn",
"Teleport to spawn."
})
public class SpawnCommand extends ICommand2MC {
@SuppressWarnings("UnstableApiUsage")
@Command2.Subcommand
public void def(Player player) {
if (targetServer().get().length() == 0) {
player.sendMessage("§bTeleporting to spawn.");
player.teleport(spawnloc);
return;
}
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Connect");
out.writeUTF(targetServer().get());
player.sendPluginMessage(getPlugin(), "BungeeCord", out.toByteArray());
Bukkit.getScheduler().runTask(getPlugin(), () -> { //Delay it a bit
ByteArrayDataOutput outt = ByteStreams.newDataOutput();
outt.writeUTF("Forward"); // So BungeeCord knows to forward it
outt.writeUTF("ALL");
outt.writeUTF("ChromaCore-Spawn"); // The channel name to check if this your data
ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
DataOutputStream msgout = new DataOutputStream(msgbytes);
try {
msgout.writeUTF("SendToSpawn"); // You can do anything you want with msgout
} catch (IOException exception) {
exception.printStackTrace();
}
outt.writeShort(msgbytes.toByteArray().length);
outt.write(msgbytes.toByteArray());
player.sendPluginMessage(getPlugin(), "BungeeCord", outt.toByteArray());
});
}
}
}

View file

@ -21,3 +21,4 @@ depend:
softdepend: softdepend:
- Towny - Towny
- Votifier - Votifier
- Multiverse-Core