Skip to content

Commit

Permalink
feat(forge20): port to 1.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
danorris709 committed Aug 24, 2023
1 parent b2c5849 commit 668ec16
Show file tree
Hide file tree
Showing 25 changed files with 1,457 additions and 3 deletions.
3 changes: 0 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ subprojects {
group = 'com.envyful.crates'
version = '1.1.2'

sourceCompatibility = 1.8
targetCompatibility = 1.8

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
Expand Down
67 changes: 67 additions & 0 deletions forge20/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
plugins {
id 'net.minecraftforge.gradle' version '[6.0,6.2)'
id("com.github.johnrengelman.shadow") version "8.1.1"
}

java.toolchain.languageVersion = JavaLanguageVersion.of(17)

forgeVersion = "1.20.1"

minecraft {
mappings channel: 'official', version: '1.20.1'
}

sourceSets.main.resources { srcDir 'src/generated/resources' }

repositories {
maven {
name = 'spongepowered-repo'
url = 'https://repo.spongepowered.org/maven'
}

maven { url 'https://jitpack.io' }

ivy {
setUrl('https://download.nodecdn.net/containers/reforged/server/release')
metadataSources {
artifact()
}
patternLayout {
artifact('[revision]/[artifact].[ext]')
}
}
}

dependencies {
minecraft "net.minecraftforge:forge:1.20.1-47.1.44"

implementation group: 'org.spongepowered', name: 'configurate-yaml', version: '4.0.0'

shadow group: 'com.envyful.api', name: 'commons', version: '5.0.1'
shadow (group: 'com.envyful.api', name: 'forge20', version: '5.0.1') {
transitive = false;
}
}

shadowJar {
finalizedBy('reobfJar')
configurations = [project.configurations.shadow]
zip64 = true
setArchiveClassifier('')
setArchiveBaseName(rootProject.name + "-Forge")

relocate('org.spongepowered.configurate', 'com.envyful.crates.forge.shade.configurate')
relocate('org.yaml.snakeyaml', 'com.envyful.crates.forge.shade.snakeyaml')
relocate('io.leangen.geantyref', 'com.envyful.crates.forge.shade.geantyref')
relocate('com.google.gson', 'com.envyful.crates.forge.shade.gson')
relocate('com.zaxxer', 'com.envyful.crates.forge.shade.hikari')
relocate('org.slf4j', 'com.envyful.crates.forge.shade.slf4j')
relocate('com.envyful.api', 'com.envyful.crates.forge.shade.envy.api')
relocate('org.bstats', 'com.envyful.crates.forge.shade.bstats')


exclude "**/module-info.class"
}

jar.finalizedBy('shadowJar')
build.finalizedBy('versionedRelease')
176 changes: 176 additions & 0 deletions forge20/src/main/java/com/envyful/crates/EnvyCrates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package com.envyful.crates;

import com.envyful.api.command.sender.SenderTypeFactory;
import com.envyful.api.concurrency.UtilLogger;
import com.envyful.api.config.yaml.YamlConfigFactory;
import com.envyful.api.forge.chat.UtilChatColour;
import com.envyful.api.forge.command.ForgeCommandFactory;
import com.envyful.api.forge.gui.factory.ForgeGuiFactory;
import com.envyful.api.forge.player.ForgeEnvyPlayer;
import com.envyful.api.forge.player.ForgePlayerManager;
import com.envyful.api.gui.factory.GuiFactory;
import com.envyful.api.type.UtilParse;
import com.envyful.crates.command.CrateTabCompleter;
import com.envyful.crates.command.EnvyCrateCommand;
import com.envyful.crates.command.ForgeEnvyPlayerSender;
import com.envyful.crates.config.EnvyCratesLocale;
import com.envyful.crates.listener.CrateBreakListener;
import com.envyful.crates.listener.CrateInteractListener;
import com.envyful.crates.type.CrateFactory;
import com.envyful.crates.type.crate.CrateType;
import com.envyful.crates.type.crate.CrateTypeFactory;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.server.ServerStartingEvent;
import net.minecraftforge.event.server.ServerStoppingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;

@Mod("envycrates")
public class EnvyCrates {

public static final String KEY_NBT_TAG = "ENVY_CRATES";

private static EnvyCrates instance;

private Logger logger = LogManager.getLogger("envycrates");

private ForgePlayerManager playerManager = new ForgePlayerManager();
private ForgeCommandFactory commandFactory = new ForgeCommandFactory();

private EnvyCratesLocale locale;

public EnvyCrates() {
instance = this;
UtilLogger.setLogger(this.logger);
MinecraftForge.EVENT_BUS.register(this);
}

@SubscribeEvent
public void onServerStopping(ServerStoppingEvent event) {
CrateFactory.save();
}

@SubscribeEvent
public void onServerStarting(ServerStartingEvent event) {
CrateTypeFactory.read();
reloadConfig();
GuiFactory.setPlatformFactory(new ForgeGuiFactory());

CrateFactory.load();

MinecraftForge.EVENT_BUS.register(new CrateInteractListener());
MinecraftForge.EVENT_BUS.register(new CrateBreakListener());
}

public void reloadConfig() {
try {
this.locale = YamlConfigFactory.getInstance(EnvyCratesLocale.class);
} catch (IOException e) {
e.printStackTrace();
}
}

@SubscribeEvent
public void onCommandRegister(RegisterCommandsEvent event) {
SenderTypeFactory.register(new ForgeEnvyPlayerSender());
this.commandFactory.registerCompleter(new CrateTabCompleter());
this.commandFactory.registerInjector(ForgeEnvyPlayer.class, (source, args) -> {
ForgeEnvyPlayer onlinePlayer = this.playerManager.getOnlinePlayer(args[0]);

if (onlinePlayer == null) {
for (String s : this.locale.getNotOnline()) {
source.sendSystemMessage(UtilChatColour.colour(s));
}
}

return onlinePlayer;
});
this.commandFactory.registerInjector(BlockPos.class, (source, args) -> {
ServerPlayer player = (ServerPlayer) source;

if (args[0].equals("below_me")) {
BlockPos pos = player.blockPosition().below();
BlockState blockState = player.level().getBlockState(pos);

if (blockState.isAir()) {
for (String s : this.locale.getCannotSetAir()) {
player.sendSystemMessage(UtilChatColour.colour(s));
}
return null;
}

return pos;
}

if (args[0].equalsIgnoreCase("looking")) {
BlockHitResult clip = player.level().clip(new ClipContext(
player.position(),
player.position().add(player.getLookAngle().scale(5)),
ClipContext.Block.COLLIDER,
ClipContext.Fluid.NONE, player));

if (clip.getType() == BlockHitResult.Type.MISS) {
for (String s : this.locale.getCannotSetAir()) {
player.sendSystemMessage(UtilChatColour.colour(s));
}
return null;
}

return clip.getBlockPos();
}

String[] split = args[0].split(",");

if (split.length != 3) {
return null;
}

BlockPos pos = new BlockPos(
UtilParse.parseInteger(split[0].replace("~", player.position().x + "")).orElse(-1),
UtilParse.parseInteger(split[1].replace("~", player.position().y + "")).orElse(-1),
UtilParse.parseInteger(split[2].replace("~", player.position().z + "")).orElse(-1)
);

BlockState blockState = player.level().getBlockState(pos);

if (blockState.isAir()) {
for (String s : this.locale.getCannotSetAir()) {
player.sendSystemMessage(UtilChatColour.colour(s));
}
return null;
}

return pos;
});
this.commandFactory.registerInjector(CrateType.class, (source, args) -> {
return CrateFactory.get(args[0]); //TODO
});
this.commandFactory.registerCommand(event.getDispatcher(), new EnvyCrateCommand());
}

public static EnvyCrates getInstance() {
return instance;
}

public static Logger getLogger() {
return instance.logger;
}

public ForgePlayerManager getPlayerManager() {
return this.playerManager;
}

public EnvyCratesLocale getLocale() {
return this.locale;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.envyful.crates.command;

import com.envyful.api.command.injector.TabCompleter;
import com.envyful.crates.type.CrateFactory;
import com.envyful.crates.type.crate.CrateType;
import net.minecraft.commands.CommandSource;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.stream.Collectors;

public class CrateTabCompleter implements TabCompleter<CrateType, CommandSource> {
@Override
public Class<CommandSource> getSenderClass() {
return CommandSource.class;
}

@Override
public Class<CrateType> getCompletedClass() {
return CrateType.class;
}

@Override
public List<String> getCompletions(CommandSource sender, String[] currentData, Annotation... completionData) {
return CrateFactory.getAll().stream().map(CrateType::id).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.envyful.crates.command;

import com.envyful.api.command.annotate.Command;
import com.envyful.api.command.annotate.Permissible;
import com.envyful.api.command.annotate.SubCommands;
import com.envyful.api.command.annotate.executor.CommandProcessor;
import com.envyful.api.command.annotate.executor.Sender;
import com.envyful.api.forge.chat.UtilChatColour;
import com.envyful.crates.EnvyCrates;
import net.minecraft.commands.CommandSource;

@Command(
value = "envycrates",
description = "Root crates command",
aliases = {
"crates"
}
)
@Permissible("com.envyful.crates.command")
@SubCommands({ReloadCommand.class, GiveKeyCommand.class, SetCrateCommand.class, GiveKeyAllCommand.class})
public class EnvyCrateCommand {

@CommandProcessor
public void onCommand(@Sender CommandSource sender, String[] args) {
for (String s : EnvyCrates.getInstance().getLocale().getRootCommand()) {
sender.sendSystemMessage(UtilChatColour.colour(s));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.envyful.crates.command;

import com.envyful.api.command.sender.SenderType;
import com.envyful.api.forge.player.ForgeEnvyPlayer;
import com.envyful.crates.EnvyCrates;
import net.minecraft.commands.CommandSource;
import net.minecraft.server.level.ServerPlayer;

public class ForgeEnvyPlayerSender implements SenderType<CommandSource, ForgeEnvyPlayer> {

@Override
public Class<?> getType() {
return ForgeEnvyPlayer.class;
}

@Override
public boolean isAccepted(CommandSource sender) {
return sender instanceof ServerPlayer;
}

@Override
public ForgeEnvyPlayer getInstance(CommandSource sender) {
return EnvyCrates.getInstance().getPlayerManager().getPlayer((ServerPlayer) sender);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.envyful.crates.command;

import com.envyful.api.command.annotate.Child;
import com.envyful.api.command.annotate.Command;
import com.envyful.api.command.annotate.Permissible;
import com.envyful.api.command.annotate.executor.Argument;
import com.envyful.api.command.annotate.executor.CommandProcessor;
import com.envyful.api.command.annotate.executor.Completable;
import com.envyful.api.command.annotate.executor.Sender;
import com.envyful.api.forge.chat.UtilChatColour;
import com.envyful.api.forge.player.ForgeEnvyPlayer;
import com.envyful.crates.EnvyCrates;
import com.envyful.crates.type.crate.CrateType;
import net.minecraft.commands.CommandSource;

@Command(
value = "giveall",
description = "Gives a key to the command",
aliases = {
"givekeyall",
"gka"
}
)
@Permissible("com.envyful.crates.command.give.all")
@Child
public class GiveKeyAllCommand {

@CommandProcessor
public void onCommand(@Sender CommandSource sender,
@Completable(CrateTabCompleter.class) @Argument CrateType crate,
@Argument(defaultValue = "1") int amount) {
sender.sendSystemMessage(UtilChatColour.colour("&e&l(!) &eGiving keys to all online players"));

for (ForgeEnvyPlayer player : EnvyCrates.getInstance().getPlayerManager().getOnlinePlayers()) {
crate.giveKey(player, amount);
}
}
}
Loading

0 comments on commit 668ec16

Please sign in to comment.