Publish 1.2.2
This commit is contained in:
parent
b1be450dfd
commit
3275bd4e8c
16 changed files with 693 additions and 23 deletions
|
@ -1,5 +1,8 @@
|
|||
package dev.zontreck.otemod;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
|
@ -16,6 +19,7 @@ import net.minecraft.world.item.Item;
|
|||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.Rotation;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.world.BiomeModifier;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
|
@ -25,6 +29,7 @@ import net.minecraftforge.fml.common.Mod;
|
|||
import net.minecraftforge.fml.config.ModConfig;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import net.minecraftforge.event.RegisterCommandsEvent;
|
||||
import net.minecraftforge.event.entity.EntityJoinLevelEvent;
|
||||
import net.minecraftforge.event.server.ServerStartingEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
|
@ -34,7 +39,13 @@ import net.minecraftforge.registries.ForgeRegistries;
|
|||
import org.slf4j.Logger;
|
||||
|
||||
import dev.zontreck.otemod.blocks.ModBlocks;
|
||||
import dev.zontreck.otemod.commands.DelHomeCommand;
|
||||
import dev.zontreck.otemod.commands.HomeCommand;
|
||||
import dev.zontreck.otemod.commands.HomesCommand;
|
||||
import dev.zontreck.otemod.commands.SetHomeCommand;
|
||||
import dev.zontreck.otemod.configs.OTEServerConfig;
|
||||
import dev.zontreck.otemod.database.Database;
|
||||
import dev.zontreck.otemod.database.Database.DatabaseConnectionException;
|
||||
import dev.zontreck.otemod.events.EventHandler;
|
||||
import dev.zontreck.otemod.items.ModItems;
|
||||
import dev.zontreck.otemod.ore.Modifier;
|
||||
|
@ -50,7 +61,7 @@ public class OTEMod
|
|||
public static final String MOD_ID = "otemod";
|
||||
public static final String MODIFY_BIOMES = "modify_biomes";
|
||||
public static final ResourceLocation MODIFY_BIOMES_RL = new ResourceLocation(OTEMod.MOD_ID, MODIFY_BIOMES);
|
||||
|
||||
public static Database DB=null;
|
||||
|
||||
public OTEMod()
|
||||
{
|
||||
|
@ -93,6 +104,7 @@ public class OTEMod
|
|||
|
||||
Player p = (Player)e;
|
||||
|
||||
|
||||
if(firstJoin(p)){
|
||||
// Do first join actions here
|
||||
|
||||
|
@ -127,12 +139,51 @@ public class OTEMod
|
|||
//LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT));
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onRegisterCommands(final RegisterCommandsEvent ev)
|
||||
{
|
||||
HomesCommand.register(ev.getDispatcher());
|
||||
SetHomeCommand.register(ev.getDispatcher());
|
||||
HomeCommand.register(ev.getDispatcher());
|
||||
DelHomeCommand.register(ev.getDispatcher());
|
||||
}
|
||||
|
||||
// You can use SubscribeEvent and let the Event Bus discover methods to call
|
||||
@SubscribeEvent
|
||||
public void onServerStarting(ServerStartingEvent event)
|
||||
{
|
||||
// Do something when the server starts
|
||||
//LOGGER.info("HELLO from server starting");
|
||||
|
||||
try {
|
||||
OTEMod.DB = new Database(this);
|
||||
|
||||
// Validate that the database has been established and that tables exist
|
||||
Connection con = OTEMod.DB.getConnection();
|
||||
con.setAutoCommit(true);
|
||||
|
||||
|
||||
con.beginRequest();
|
||||
|
||||
Statement lookup = con.createStatement();
|
||||
lookup.execute("CREATE TABLE IF NOT EXISTS `homes` (" +
|
||||
" `number` int(11) NOT NULL," +
|
||||
" `user` varchar(255) NOT NULL," +
|
||||
" `home_name` varchar(255) NOT NULL," +
|
||||
" `x` varchar(20) NOT NULL," +
|
||||
" `y` varchar(20) NOT NULL," +
|
||||
" `z` varchar(20) NOT NULL," +
|
||||
" `rot_x` varchar(20) NOT NULL," +
|
||||
" `rot_y` varchar(20) NOT NULL," +
|
||||
" `dimension` varchar(25) NOT NULL)");
|
||||
|
||||
con.endRequest();
|
||||
} catch (DatabaseConnectionException | SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
LOGGER.error("FATAL ERROR\n \n* DATABASE COULD NOT CONNECT *\n* SEE ABOVE STACK TRACE *");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent
|
||||
|
|
40
src/main/java/dev/zontreck/otemod/chat/ChatColor.java
Normal file
40
src/main/java/dev/zontreck/otemod/chat/ChatColor.java
Normal file
|
@ -0,0 +1,40 @@
|
|||
package dev.zontreck.otemod.chat;
|
||||
|
||||
public class ChatColor {
|
||||
public static char CODE = '§';
|
||||
public static String BLACK = build("0");
|
||||
public static String DARK_BLUE = build("1");
|
||||
public static String DARK_GREEN = build("2");
|
||||
public static String DARK_AQUA = build("3");
|
||||
public static String DARK_RED = build("4");
|
||||
public static String DARK_PURPLE = build("5");
|
||||
public static String GOLD = build("6");
|
||||
public static String GRAY = build("7");
|
||||
public static String DARK_GRAY = build("8");
|
||||
public static String BLUE = build("9");
|
||||
public static String GREEN = build("a");
|
||||
public static String AQUA = build("b");
|
||||
public static String RED = build("c");
|
||||
public static String LIGHT_PURPLE = build("d");
|
||||
public static String YELLOW = build("e");
|
||||
public static String WHITE = build("f");
|
||||
public static String MINECOIN_GOLD = build("g");
|
||||
|
||||
public static String UNDERLINE = build("u");
|
||||
public static String BOLD = build("l");
|
||||
public static String ITALIC = build("o");
|
||||
public static String STRIKETHROUGH = build("m");
|
||||
public static String CRAZY = build("k");
|
||||
public static String RESET = build("r");
|
||||
|
||||
|
||||
public static String build(String c)
|
||||
{
|
||||
return CODE+c;
|
||||
}
|
||||
|
||||
public static String resetChat()
|
||||
{
|
||||
return RESET+WHITE;
|
||||
}
|
||||
}
|
16
src/main/java/dev/zontreck/otemod/chat/Clickable.java
Normal file
16
src/main/java/dev/zontreck/otemod/chat/Clickable.java
Normal file
|
@ -0,0 +1,16 @@
|
|||
package dev.zontreck.otemod.chat;
|
||||
|
||||
import net.minecraft.network.chat.ClickEvent;
|
||||
import net.minecraft.network.chat.ClickEvent.Action;
|
||||
|
||||
/*
|
||||
* Because of some weird behavior with java not liking that both HoverEvent and ClickEvent have an Action implementation, these must both be in a custom factory here where Action can be imported by itself in both files
|
||||
*/
|
||||
public class Clickable {
|
||||
|
||||
public static ClickEvent command(String text)
|
||||
{
|
||||
return new ClickEvent(Action.RUN_COMMAND, text);
|
||||
}
|
||||
|
||||
}
|
17
src/main/java/dev/zontreck/otemod/chat/HoverTip.java
Normal file
17
src/main/java/dev/zontreck/otemod/chat/HoverTip.java
Normal file
|
@ -0,0 +1,17 @@
|
|||
package dev.zontreck.otemod.chat;
|
||||
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.HoverEvent;
|
||||
import net.minecraft.network.chat.HoverEvent.Action;
|
||||
|
||||
/*
|
||||
* Because of some weird behavior with java not liking that both HoverEvent and ClickEvent have an Action implementation, these must both be in a custom factory here where Action can be imported by itself in both files
|
||||
*/
|
||||
public class HoverTip {
|
||||
|
||||
public static HoverEvent get(String text)
|
||||
{
|
||||
return new HoverEvent(Action.SHOW_TEXT, Component.literal(text));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
package dev.zontreck.otemod.commands;
|
||||
|
||||
import java.sql.Array;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.math.Vector3d;
|
||||
|
||||
import dev.zontreck.otemod.OTEMod;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.network.chat.contents.TranslatableContents;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.phys.Vec2;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraftforge.server.command.TextComponentHelper;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.MutableComponent;
|
||||
|
||||
public class DelHomeCommand {
|
||||
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
|
||||
{
|
||||
dispatcher.register(Commands.literal("rmhome").executes(c->rmHome(c.getSource(), "default")).then(Commands.argument("nickname", StringArgumentType.string()).executes(c -> rmHome(c.getSource(), StringArgumentType.getString(c, "nickname")))));
|
||||
|
||||
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
|
||||
//String arg = StringArgumentType.getString(command, "nickname");
|
||||
//return setHome(command.getSource(), arg);
|
||||
//}));
|
||||
}
|
||||
|
||||
private static int rmHome(CommandSourceStack ctx, String homeName)
|
||||
{
|
||||
// Request homes
|
||||
// String homeName = "";
|
||||
// CommandSourceStack ctx = ctx2.getSource();
|
||||
// homeName = StringArgumentType.getString(ctx2, "nickname");
|
||||
// if(homeName==null)return 0;
|
||||
|
||||
if(! ctx.isPlayer())
|
||||
{
|
||||
|
||||
ctx.sendFailure(MutableComponent.create( new TranslatableContents("dev.zontreck.otemod.msgs.homes.only_player")));
|
||||
return 1;
|
||||
}
|
||||
ServerPlayer p = ctx.getPlayer();
|
||||
Connection con = OTEMod.DB.getConnection();
|
||||
try {
|
||||
con.beginRequest();
|
||||
//Statement stat = con.createStatement();
|
||||
Vec3 position = p.position();
|
||||
Vec2 rot = p.getRotationVector();
|
||||
|
||||
|
||||
String SQL = "DELETE FROM `homes` WHERE `user`=? AND `home_name`=?;";
|
||||
|
||||
PreparedStatement pstat = con.prepareStatement(SQL);
|
||||
pstat.setString(1, p.getStringUUID());
|
||||
pstat.setString(2, homeName);
|
||||
|
||||
pstat.execute();
|
||||
|
||||
|
||||
ctx.sendSuccess(MutableComponent.create(new TranslatableContents("dev.zontreck.otemod.msgs.homes.del.success")), true);
|
||||
con.endRequest();
|
||||
} catch (SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
ctx.sendFailure(Component.translatable("dev.zontreck.otemod.msgs.homes.del.fail"));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
216
src/main/java/dev/zontreck/otemod/commands/HomeCommand.java
Normal file
216
src/main/java/dev/zontreck/otemod/commands/HomeCommand.java
Normal file
|
@ -0,0 +1,216 @@
|
|||
package dev.zontreck.otemod.commands;
|
||||
|
||||
import java.sql.Array;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.math.Vector3d;
|
||||
|
||||
import dev.zontreck.otemod.OTEMod;
|
||||
import dev.zontreck.otemod.chat.ChatColor;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.network.chat.contents.TranslatableContents;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.TickTask;
|
||||
import net.minecraft.server.commands.BossBarCommands;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.BossEvent.BossBarColor;
|
||||
import net.minecraft.world.effect.MobEffectInstance;
|
||||
import net.minecraft.world.effect.MobEffectUtil;
|
||||
import net.minecraft.world.effect.MobEffects;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.phys.Vec2;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraftforge.server.command.TextComponentHelper;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.MutableComponent;
|
||||
|
||||
public class HomeCommand {
|
||||
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
|
||||
{
|
||||
dispatcher.register(Commands.literal("home").executes(c-> home(c.getSource(), "default")).then(Commands.argument("nickname", StringArgumentType.string()).executes(c -> home(c.getSource(), StringArgumentType.getString(c, "nickname")))));
|
||||
|
||||
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
|
||||
//String arg = StringArgumentType.getString(command, "nickname");
|
||||
//return setHome(command.getSource(), arg);
|
||||
//}));
|
||||
}
|
||||
|
||||
private static int home(CommandSourceStack ctx, String homeName)
|
||||
{
|
||||
// Request homes
|
||||
// String homeName = "";
|
||||
// CommandSourceStack ctx = ctx2.getSource();
|
||||
// homeName = StringArgumentType.getString(ctx2, "nickname");
|
||||
// if(homeName==null)return 0;
|
||||
|
||||
if(! ctx.isPlayer())
|
||||
{
|
||||
|
||||
ctx.sendFailure(MutableComponent.create( new TranslatableContents("dev.zontreck.otemod.msgs.homes.only_player")));
|
||||
return 1;
|
||||
}
|
||||
ServerPlayer p = ctx.getPlayer();
|
||||
Connection con = OTEMod.DB.getConnection();
|
||||
String SQL="";
|
||||
try {
|
||||
con.beginRequest();
|
||||
Statement stat = con.createStatement();
|
||||
Vec3 position = p.position();
|
||||
|
||||
Vec2 rot = p.getRotationVector();
|
||||
|
||||
//stat.execute("REPLACE INTO `homes` (user, home_name, x, y, z, rot_x, rot_y, dimension) values (\"" + p.getStringUUID() + "\", \""+ homeName + "\", "+String.valueOf(position.x)+", "+String.valueOf(position.y)+", "+String.valueOf(position.z)+", "+String.valueOf(rot.x)+", "+String.valueOf(rot.y)+", \"" + p.getLevel().dimension().location().getNamespace() + ":" + p.getLevel().dimension().location().getPath() + "\");");
|
||||
// Query database now
|
||||
SQL = "SELECT * FROM `homes` WHERE `user`=? AND `home_name`=?;";
|
||||
//ResultSet rs = stat.executeQuery(SQL);
|
||||
|
||||
PreparedStatement pstat = con.prepareStatement(SQL);
|
||||
pstat.setString(1, p.getStringUUID());
|
||||
pstat.setString(2, homeName);
|
||||
|
||||
ResultSet rs = pstat.executeQuery();
|
||||
|
||||
boolean has_home = false;
|
||||
while(rs.next()){
|
||||
has_home=true;
|
||||
// Now, begin to extract the home data
|
||||
double x = Double.parseDouble(rs.getString("x"));
|
||||
double y = Double.parseDouble(rs.getString("y"));
|
||||
double z = Double.parseDouble(rs.getString("z"));
|
||||
|
||||
float rx = Float.parseFloat(rs.getString("rot_x"));
|
||||
float ry = Float.parseFloat(rs.getString("rot_y"));
|
||||
|
||||
|
||||
position = new Vec3(x, y, z);
|
||||
rot = new Vec2(rx, ry);
|
||||
|
||||
String dim = rs.getString("dimension");
|
||||
String[] dims = dim.split(":");
|
||||
|
||||
ResourceLocation rl = new ResourceLocation(dims[0], dims[1]);
|
||||
|
||||
ServerLevel dimL = null;
|
||||
for (ServerLevel lServerLevel : p.server.getAllLevels()) {
|
||||
ResourceLocation XL = lServerLevel.dimension().location();
|
||||
|
||||
if(XL.getNamespace().equals(rl.getNamespace())){
|
||||
if(XL.getPath().equals(rl.getPath())){
|
||||
dimL = lServerLevel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(dimL == null)
|
||||
{
|
||||
SQL = "Failed to find the dimension";
|
||||
ctx.sendFailure(Component.literal(ChatColor.DARK_RED+ChatColor.BOLD+"FAILED TO LOCATE THAT DIMENSION. CONTACT THE SERVER ADMIN"));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
MobEffectInstance inst = new MobEffectInstance(MobEffects.DARKNESS, 10000, 3, true, true);
|
||||
MobEffectInstance regen = new MobEffectInstance(MobEffects.REGENERATION, 5000, 1, true, true);
|
||||
|
||||
|
||||
p.addEffect(inst);
|
||||
p.addEffect(regen);
|
||||
|
||||
// Send boss bar
|
||||
|
||||
final ServerPlayer f_p = p;
|
||||
final Vec3 f_pos = position;
|
||||
final Vec2 f_rot = rot;
|
||||
final ServerLevel f_dim = dimL;
|
||||
|
||||
Thread t = new Thread(new Runnable() {
|
||||
public void run()
|
||||
{
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
boolean dimensionNeedsChanging = false;
|
||||
|
||||
if(!f_p.level.dimension().location().getNamespace().equals(f_dim.dimension().location().getNamespace())) dimensionNeedsChanging=true;
|
||||
if(!f_p.level.dimension().location().getPath().equals(f_dim.dimension().location().getPath())) dimensionNeedsChanging=true;
|
||||
|
||||
f_p.teleportTo(f_dim, f_pos.x, f_pos.y, f_pos.z, f_rot.x, f_rot.y);
|
||||
if(!dimensionNeedsChanging){
|
||||
}else {
|
||||
// Get dimension then change dimension
|
||||
f_p.server.execute(new Runnable() {
|
||||
public void run()
|
||||
{
|
||||
|
||||
f_p.changeDimension(f_dim);
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
f_p.setPos(f_pos);
|
||||
f_p.setXRot(f_rot.x);
|
||||
f_p.setYRot(f_rot.y);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
f_p.server.execute(new Runnable() {
|
||||
public void run()
|
||||
{
|
||||
|
||||
f_p.removeEffect(MobEffects.DARKNESS);
|
||||
f_p.removeEffect(MobEffects.REGENERATION);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
t.start();
|
||||
}
|
||||
|
||||
if(!has_home)throw new SQLException("NO HOME");
|
||||
|
||||
ctx.sendSuccess(MutableComponent.create(new TranslatableContents("dev.zontreck.otemod.msgs.homes.goto.success")), true);
|
||||
con.endRequest();
|
||||
} catch (SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
if(!e.getMessage().equals("%%"))
|
||||
ctx.sendFailure(Component.translatable("dev.zontreck.otemod.msgs.homes.goto.fail"));
|
||||
else
|
||||
ctx.sendFailure(Component.literal("FAILED SQL: "+ ChatColor.GOLD+ SQL));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
77
src/main/java/dev/zontreck/otemod/commands/HomesCommand.java
Normal file
77
src/main/java/dev/zontreck/otemod/commands/HomesCommand.java
Normal file
|
@ -0,0 +1,77 @@
|
|||
package dev.zontreck.otemod.commands;
|
||||
|
||||
import java.sql.Array;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
|
||||
import dev.zontreck.otemod.OTEMod;
|
||||
import dev.zontreck.otemod.chat.ChatColor;
|
||||
import dev.zontreck.otemod.chat.Clickable;
|
||||
import dev.zontreck.otemod.chat.HoverTip;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.network.chat.contents.TranslatableContents;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.WrittenBookItem;
|
||||
import net.minecraft.world.item.Item.Properties;
|
||||
import net.minecraftforge.server.command.TextComponentHelper;
|
||||
import net.minecraft.network.chat.ClickEvent;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.HoverEvent;
|
||||
import net.minecraft.network.chat.MutableComponent;
|
||||
import net.minecraft.network.chat.Style;
|
||||
import net.minecraft.network.chat.TextColor;
|
||||
|
||||
public class HomesCommand {
|
||||
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
|
||||
{
|
||||
dispatcher.register(Commands.literal("homes").executes(HomesCommand::getHomes));
|
||||
}
|
||||
|
||||
private static int getHomes(CommandContext<CommandSourceStack> ctx)
|
||||
{
|
||||
// Request homes
|
||||
if(! ctx.getSource().isPlayer())
|
||||
{
|
||||
|
||||
ctx.getSource().sendFailure(MutableComponent.create( new TranslatableContents("dev.zontreck.otemod.msgs.homes.only_player")));
|
||||
return 1;
|
||||
}
|
||||
ServerPlayer p = ctx.getSource().getPlayer();
|
||||
Connection con = OTEMod.DB.getConnection();
|
||||
try {
|
||||
con.beginRequest();
|
||||
Statement stat = con.createStatement();
|
||||
ResultSet rs = stat.executeQuery("SELECT `home_name` FROM `homes` WHERE `user`=\"" + p.getStringUUID()+"\"");
|
||||
List<String> homes = new ArrayList<String>();
|
||||
while(rs.next()){
|
||||
homes.add(rs.getString("home_name"));
|
||||
}
|
||||
|
||||
ctx.getSource().sendSuccess(MutableComponent.create(new TranslatableContents("dev.zontreck.otemod.msgs.homes.total")).append(""+String.valueOf(homes.size())), true);
|
||||
con.endRequest();
|
||||
|
||||
for (String string : homes) {
|
||||
Style st = Style.EMPTY.withFont(Style.DEFAULT_FONT).withHoverEvent(HoverTip.get(ChatColor.BOLD+ChatColor.DARK_GREEN+"Click here to go to this home")).withClickEvent(Clickable.command("/home "+string));
|
||||
|
||||
ctx.getSource().sendSystemMessage(Component.literal(ChatColor.BOLD + ChatColor.MINECOIN_GOLD+"["+ChatColor.resetChat()+ChatColor.UNDERLINE+ChatColor.BOLD+ChatColor.DARK_GREEN+"HOME"+ChatColor.resetChat()+ChatColor.BOLD+ChatColor.MINECOIN_GOLD+"] "+ChatColor.resetChat()+ChatColor.YELLOW+string).setStyle(st));
|
||||
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package dev.zontreck.otemod.commands;
|
||||
|
||||
public class ModCommands {
|
||||
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package dev.zontreck.otemod.commands;
|
||||
|
||||
import java.sql.Array;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.math.Vector3d;
|
||||
|
||||
import dev.zontreck.otemod.OTEMod;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.network.chat.contents.TranslatableContents;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.phys.Vec2;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraftforge.server.command.TextComponentHelper;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.MutableComponent;
|
||||
|
||||
public class SetHomeCommand {
|
||||
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
|
||||
{
|
||||
dispatcher.register(Commands.literal("sethome").executes(c->setHome(c.getSource(), "default")).then(Commands.argument("nickname", StringArgumentType.string()).executes(c -> setHome(c.getSource(), StringArgumentType.getString(c, "nickname")))));
|
||||
|
||||
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
|
||||
//String arg = StringArgumentType.getString(command, "nickname");
|
||||
//return setHome(command.getSource(), arg);
|
||||
//}));
|
||||
}
|
||||
|
||||
private static int setHome(CommandSourceStack ctx, String homeName)
|
||||
{
|
||||
// Request homes
|
||||
// String homeName = "";
|
||||
// CommandSourceStack ctx = ctx2.getSource();
|
||||
// homeName = StringArgumentType.getString(ctx2, "nickname");
|
||||
// if(homeName==null)return 0;
|
||||
|
||||
if(! ctx.isPlayer())
|
||||
{
|
||||
|
||||
ctx.sendFailure(MutableComponent.create( new TranslatableContents("dev.zontreck.otemod.msgs.homes.only_player")));
|
||||
return 1;
|
||||
}
|
||||
ServerPlayer p = ctx.getPlayer();
|
||||
Connection con = OTEMod.DB.getConnection();
|
||||
try {
|
||||
con.beginRequest();
|
||||
Statement stat = con.createStatement();
|
||||
Vec3 position = p.position();
|
||||
Vec2 rot = p.getRotationVector();
|
||||
|
||||
stat.execute("REPLACE INTO `homes` (user, home_name, x, y, z, rot_x, rot_y, dimension) values (\"" + p.getStringUUID() + "\", \""+ homeName + "\", "+String.valueOf(position.x)+", "+String.valueOf(position.y)+", "+String.valueOf(position.z)+", "+String.valueOf(rot.x)+", "+String.valueOf(rot.y)+", \"" + p.getLevel().dimension().location().getNamespace() + ":" + p.getLevel().dimension().location().getPath() + "\");");
|
||||
|
||||
|
||||
ctx.sendSuccess(MutableComponent.create(new TranslatableContents("dev.zontreck.otemod.msgs.homes.set.success")), true);
|
||||
con.endRequest();
|
||||
} catch (SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
ctx.sendFailure(Component.translatable("dev.zontreck.otemod.msgs.homes.set.fail"));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
|
@ -14,13 +14,24 @@ public class OTEServerConfig {
|
|||
public static final ForgeConfigSpec SPEC;
|
||||
|
||||
public static final ForgeConfigSpec.ConfigValue<List<ItemStack>> INITIAL_ITEMS_TO_GIVE_ON_FIRST_JOIN;
|
||||
|
||||
|
||||
public static final ForgeConfigSpec.ConfigValue<String> HOST_ADDR;
|
||||
public static final ForgeConfigSpec.ConfigValue<Integer> PORT;
|
||||
public static final ForgeConfigSpec.ConfigValue<String> USERNAME;
|
||||
public static final ForgeConfigSpec.ConfigValue<String> PASSWORD;
|
||||
public static final ForgeConfigSpec.ConfigValue<String> DATABASE;
|
||||
|
||||
static {
|
||||
List<ItemStack> defaults = new ArrayList<ItemStack>();
|
||||
|
||||
BUILDER.push("Configuration for OTE Mod Resources");
|
||||
BUILDER.push("OTE");
|
||||
INITIAL_ITEMS_TO_GIVE_ON_FIRST_JOIN = BUILDER.comment("What items, identified by modid:item, to give to a brand new user on the server").define("New Player Gear", defaults);
|
||||
HOST_ADDR = BUILDER.comment("Database Host (MySQL)").define("host", "127.0.0.1");
|
||||
PORT = BUILDER.comment("Database Port (MySQL)").define("port", 3306);
|
||||
USERNAME = BUILDER.comment("Database Username (MySQL)").define("user", "ote");
|
||||
PASSWORD = BUILDER.comment("Database Password (MySQL)").define("password", "password");
|
||||
DATABASE = BUILDER.comment("Database Name (MySQL)").define("database", "otemod");
|
||||
|
||||
|
||||
BUILDER.pop();
|
||||
SPEC=BUILDER.build();
|
||||
|
|
65
src/main/java/dev/zontreck/otemod/database/Database.java
Normal file
65
src/main/java/dev/zontreck/otemod/database/Database.java
Normal file
|
@ -0,0 +1,65 @@
|
|||
package dev.zontreck.otemod.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import dev.zontreck.otemod.OTEMod;
|
||||
import dev.zontreck.otemod.configs.OTEServerConfig;
|
||||
|
||||
public class Database {
|
||||
private OTEMod mod;
|
||||
private Connection connection;
|
||||
|
||||
|
||||
public Database (OTEMod instance) throws DatabaseConnectionException
|
||||
{
|
||||
mod=instance;
|
||||
try{
|
||||
this.connect();
|
||||
}catch(Exception e){
|
||||
throw new DatabaseConnectionException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void connect() throws Exception
|
||||
{
|
||||
connection = DriverManager.getConnection("jdbc:mariadb://"+OTEServerConfig.HOST_ADDR.get()+":"+OTEServerConfig.PORT.get()+"/" + OTEServerConfig.DATABASE.get() + "?useSSL=false", OTEServerConfig.USERNAME.get(), OTEServerConfig.PASSWORD.get());
|
||||
}
|
||||
|
||||
|
||||
public class DatabaseConnectionException extends Exception
|
||||
{
|
||||
public DatabaseConnectionException(String X) {
|
||||
super(X);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnected() throws SQLException
|
||||
{
|
||||
if(connection != null)
|
||||
{
|
||||
if(connection.isClosed())
|
||||
{
|
||||
return false;
|
||||
}else return true;
|
||||
}else return false;
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void disconnect() throws SQLException
|
||||
{
|
||||
if(isConnected())
|
||||
{
|
||||
try{
|
||||
connection.close();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -16,5 +16,14 @@
|
|||
"block.otemod.eternium_ore_block": "Eternium Ore",
|
||||
"block.otemod.deepslate_eternium_ore_block": "Deepslate Eternium Ore",
|
||||
"block.otemod.aurora_block": "Aurora Block",
|
||||
"block.otemod.aurora_door": "Aurora Door"
|
||||
"block.otemod.aurora_door": "Aurora Door",
|
||||
|
||||
"dev.zontreck.otemod.msgs.homes.only_player": "§cOnly players are allowed to execute this command",
|
||||
"dev.zontreck.otemod.msgs.homes.total": "§3Total number of homes: §6",
|
||||
"dev.zontreck.otemod.msgs.homes.set.success": "§2Home created or updated successfully",
|
||||
"dev.zontreck.otemod.msgs.homes.set.fail": "§cHome could not be created or updated due to an unknown error",
|
||||
"dev.zontreck.otemod.msgs.homes.goto.fail": "§cHome was not found, or a error occured",
|
||||
"dev.zontreck.otemod.msgs.homes.goto.success": "§2Home found, warping home now",
|
||||
"dev.zontreck.otemod.msgs.homes.del.success": "§2Home was deleted successfully",
|
||||
"dev.zontreck.otemod.msgs.homes.del.fail": "§cHome could not be deleted due to an error"
|
||||
}
|
Reference in a new issue