Skip to content

Commit

Permalink
Prepare 1.21 & add /mw difficulty
Browse files Browse the repository at this point in the history
  • Loading branch information
pisaiah committed Jun 2, 2024
1 parent 8bc5ad5 commit c60ac5d
Show file tree
Hide file tree
Showing 38 changed files with 796 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dimapi;

import com.google.common.base.Preconditions;
Expand All @@ -22,7 +6,13 @@
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.TeleportTarget;

/**
* For 1.18.2 - 1.20.6
*
* @implNote Removed in Fabric API 1.21
*/
public final class FabricDimensionInternals {

/**
* The target passed to the last call to {@link FabricDimensions#teleport(Entity, ServerWorld, TeleportTarget)}.
*/
Expand Down Expand Up @@ -51,4 +41,5 @@ public static <E extends Entity> E changeDimension(E teleported, ServerWorld dim
currentTarget = null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.Difficulty;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
Expand All @@ -28,4 +29,8 @@ public default Text colored_literal(String txt, Formatting color) {

public BlockPos get_spawn(ServerWorld world);

void teleleport(ServerPlayerEntity player, ServerWorld world, double x, double y, double z);

void set_difficulty(String id, Difficulty dif);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

public class InfoSuggest implements SuggestionProvider<ServerCommandSource> {

public static String[] diff_names = {"PEACEFUL", "EASY", "NORMAL", "HARD"};

@Override
public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException {
builder = builder.createOffset(builder.getInput().lastIndexOf(' ') + 1);
Expand All @@ -30,7 +32,7 @@ public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerComman
boolean ALL = Perm.has(plr, "multiworld.admin");

if (cmds.length <= 1 || (cmds.length <= 2 && !input.endsWith(" "))) {
String[] subcommands = {"tp", "list", "version", "create", "spawn", "setspawn", "gamerule"};
String[] subcommands = {"tp", "list", "version", "create", "spawn", "setspawn", "gamerule", "help", "difficulty"};
for (String s : subcommands) {
builder.suggest(s);
}
Expand Down Expand Up @@ -67,21 +69,23 @@ public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerComman
GameruleCommand.setup();
}

System.out.println("INPUT: " + input);

String last = input.substring(input.lastIndexOf(' ')).trim();

for (String name : GameruleCommand.keys.keySet()) {
if (name.startsWith(last) || last.contains("gamerule") || name.toLowerCase().contains(last)) {
builder.suggest(name);
}
}

//for (String name : GameruleCommand.keys.keySet()) {
// builder.suggest(name);
//}

//builder.suggest("myid:myvalue");
return builder.buildFuture();
}

if (cmds[1].equalsIgnoreCase("difficulty") && (ALL || Perm.has(plr, "multiworld.difficulty"))) {
String last = input.substring(input.lastIndexOf(' ')).trim();
for (String name : diff_names) {
if (name.startsWith(last) || last.contains("difficulty") || name.toLowerCase().contains(last)) {
builder.suggest(name);
}
}
return builder.buildFuture();
}
}
Expand All @@ -98,6 +102,20 @@ public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerComman
builder.suggest("true");
builder.suggest("false");
}

if (cmds[1].equalsIgnoreCase("difficulty") && (ALL || Perm.has(plr, "multiworld.difficulty")) ) {
// TODO: IntRules
ArrayList<String> names = new ArrayList<>();
MultiworldMod.mc.getWorldRegistryKeys().forEach(r -> {
String val = r.getValue().toString();
if (val.startsWith("multiworld:")) {
val = val.replace("multiworld:", "");
}
names.add(val);
//builder.suggest(val);
});
for (String s : names) builder.suggest(s);
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Multiworld Mod
* Copyright (c) 2021-2023 by Isaiah.
* Copyright (c) 2021-2024 by Isaiah.
*/
package me.isaiah.multiworld;

Expand All @@ -16,6 +16,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;

import me.isaiah.multiworld.command.CreateCommand;
import me.isaiah.multiworld.command.DifficultyCommand;
import me.isaiah.multiworld.command.GameruleCommand;
import me.isaiah.multiworld.command.SetspawnCommand;
import me.isaiah.multiworld.command.SpawnCommand;
Expand Down Expand Up @@ -48,7 +49,7 @@ public class MultiworldMod {
public static ICreator world_creator;

// Mod Version
public static final String VERSION = "1.7";
public static final String VERSION = "1.8";

public static void setICreator(ICreator ic) {
world_creator = ic;
Expand All @@ -66,6 +67,19 @@ public static ServerWorld create_world(String id, Identifier dim, ChunkGenerator
public static void init() {
System.out.println(" Multiworld init");
}

public static Identifier new_id(String id) {
/*if (id.indexOf(':') != -1) {
String[] spl = id.split(Pattern.quote(":"));
return Identifier.of(spl[0], spl[1]);
}
return Identifier.of("minecraft", id);*/

// tryParse works from 1.18 to 1.21
return Identifier.tryParse(id);

// return new Identifier(id);
}

// On server start
public static void on_server_started(MinecraftServer mc) {
Expand Down Expand Up @@ -93,6 +107,9 @@ public static void on_server_started(MinecraftServer mc) {
public static ServerPlayerEntity get_player(ServerCommandSource s) throws CommandSyntaxException {
ServerPlayerEntity plr = s.getPlayer();
if (null == plr) {
// s.sendMessage(text_plain("Multiworld Mod for Minecraft " + mc.getVersion()));
// s.sendMessage(text_plain("These commands currently require a Player."));

throw ServerCommandSource.REQUIRES_PLAYER_EXCEPTION.create();
}
return plr;
Expand All @@ -103,7 +120,7 @@ public static void register_commands(CommandDispatcher<ServerCommandSource> disp
dispatcher.register(literal(CMD)
.requires(source -> {
try {
return Perm.has(get_player(source), "multiworld.cmd") ||
return source.hasPermissionLevel(1) || Perm.has(get_player(source), "multiworld.cmd") ||
Perm.has(get_player(source), "multiworld.admin");
} catch (Exception e) {
return source.hasPermissionLevel(1);
Expand All @@ -127,7 +144,13 @@ public static int broadcast(ServerCommandSource source, Formatting formatting, S
final ServerPlayerEntity plr = get_player(source); // source.getPlayerOrThrow();

if (null == message) {
plr.sendMessage(text("Usage:", Formatting.AQUA), false);
plr.sendMessage(text("Multiworld Mod for Minecraft " + mc.getVersion(), Formatting.AQUA), false);

World world = plr.getWorld();
Identifier id = world.getRegistryKey().getValue();

message(plr, "Currently in: " + id.toString());

return 1;
}

Expand Down Expand Up @@ -159,6 +182,24 @@ public static int broadcast(ServerCommandSource source, Formatting formatting, S
}
}*/

if (args[0].equalsIgnoreCase("help")) {
String[] lines = {
"&4Multiworld Mod Commands:&r",
"&a/mw spawn&r - Teleport to current world spawn",
"&a/mw setspawn&r - Sets the current world spawn",
"&a/mw tp <id>&r - Teleport to a world",
"&a/mw list&r - List all worlds",
"&a/mw gamerule <rule> <value>&r - Change a worlds Gamerules",
"&a/mw create <id> <env>&r - create a new world",
"&a/mw difficulty <value> [world id] - Sets the difficulty of a world"
};

for (String s : lines) {
message(plr, s);
}

}

if (args[0].equalsIgnoreCase("debugtick")) {
ServerWorld w = (ServerWorld) plr.getWorld();
Identifier id = w.getRegistryKey().getValue();
Expand All @@ -178,6 +219,10 @@ public static int broadcast(ServerCommandSource source, Formatting formatting, S
if (args[0].equalsIgnoreCase("gamerule") && (ALL || Perm.has(plr, "multiworld.gamerule"))) {
return GameruleCommand.run(mc, plr, args);
}

if (args[0].equalsIgnoreCase("difficulty") && (ALL || Perm.has(plr, "multiworld.difficulty"))) {
return DifficultyCommand.run(mc, plr, args);
}

if (args[0].equalsIgnoreCase("tp") ) {
if (!(ALL || Perm.has(plr, "multiworld.tp"))) {
Expand Down Expand Up @@ -206,7 +251,7 @@ public static int broadcast(ServerCommandSource source, Formatting formatting, S
}

if (args[0].equalsIgnoreCase("version") && (ALL || Perm.has(plr, "multiworld.cmd")) ) {
message(plr, "Mutliworld Mod version " + VERSION);
message(plr, "Multiworld Mod version " + VERSION);
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package me.isaiah.multiworld.command;

import java.util.ArrayList;
import java.util.Random;

import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Formatting;
Expand All @@ -16,6 +18,8 @@
import net.minecraft.server.world.ServerWorld;

import java.io.File;
import java.io.IOException;

import me.isaiah.multiworld.config.*;

public class CreateCommand {
Expand Down Expand Up @@ -111,13 +115,27 @@ public static void reinit_world_from_config(MinecraftServer mc, String id) {
dim = Util.THE_END_ID;
}

ServerWorld world = MultiworldMod.create_world(id, dim, gen, Difficulty.NORMAL, seed);
Difficulty d = Difficulty.NORMAL;

// Set saved Difficulty
if (config.is_set("difficulty")) {
String di = config.getString("difficulty");

// String to Difficulty
if (di.equalsIgnoreCase("EASY")) { d = Difficulty.EASY; }
if (di.equalsIgnoreCase("HARD")) { d = Difficulty.HARD; }
if (di.equalsIgnoreCase("NORMAL")) { d = Difficulty.NORMAL; }
if (di.equalsIgnoreCase("PEACEFUL")) { d = Difficulty.PEACEFUL; }
}

ServerWorld world = MultiworldMod.create_world(id, dim, gen, d, seed);


if (GameruleCommand.keys.size() == 0) {
GameruleCommand.setup();
}

// Set Gamerules
for (String name : GameruleCommand.keys.keySet()) {
String key = "gamerule_" + name;

Expand All @@ -138,6 +156,7 @@ public static void reinit_world_from_config(MinecraftServer mc, String id) {
GameruleCommand.set_gamerule_from_cfg(world, key, (String) o);
}
}

} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -173,5 +192,25 @@ public static void make_config(ServerWorld w, String dim, long seed) {
e.printStackTrace();
}
}

/**
*
*/
public static FileConfiguration get_config(World w) throws IOException {
File cf = new File(FabricLoader.getInstance().getConfigDir().toFile(), "multiworld");
cf.mkdirs();

File worlds = new File(cf, "worlds");
worlds.mkdirs();

Identifier id = w.getRegistryKey().getValue();
File namespace = new File(worlds, id.getNamespace());
namespace.mkdirs();

File wc = new File(namespace, id.getPath() + ".yml");
wc.createNewFile();
FileConfiguration config = new FileConfiguration(wc);
return config;
}

}
Loading

0 comments on commit c60ac5d

Please sign in to comment.