Skip to content

Commit

Permalink
Backport 3.0.3 to 1.16.5
Browse files Browse the repository at this point in the history
Signed-off-by: ErdbeerbaerLP <[email protected]>
  • Loading branch information
ErdbeerbaerLP committed Oct 12, 2023
1 parent 520f4e7 commit 6efc2f1
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 156 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ version = "3.0.3"
group = "de.erdbeerbaerlp.dcintegration"
archivesBaseName = "dcintegration-forge"

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

//Gradle Repositories
repositories {
Expand Down Expand Up @@ -86,6 +86,7 @@ reobf {
artifacts {
archives tasks.shadowJar
}

configurations.all {
resolutionStrategy {
cacheChangingModulesFor 1, 'minutes'
Expand All @@ -94,12 +95,12 @@ configurations.all {
//Dependencies
dependencies {
// ========= Common =====================
embed('de.erdbeerbaerlp:dcintegration.common:3.0.3') {
embed('de.erdbeerbaerlp:dcintegration.common:3.0.3-j8') {
changing = true
}

// ========= Minecraft Forge =============
minecraft "net.minecraftforge:forge:${project.mcVersion}-40.2.10"
minecraft 'net.minecraftforge:forge:1.16.5-36.2.34'

//Mixin
annotationProcessor 'org.spongepowered:mixin:0.8.5:processor'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false
changelog=Large discord integration rewrite adding new features and fixing long existing bugs\nOld configuration can be used as-is\n\nAdded custom status\nAdded tellraw\nAdded option to disable chat message editing for compatibility reasons\nFixed lots of bugs
releaseType = BETA
mcVersion = 1.18.2
mcVersion = 1.16.5

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.minecraft.network.chat.ChatType;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ServerboundChatPacket;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.server.ServerLifecycleHooks;
import net.minecraftforge.fml.server.ServerLifecycleHooks;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand All @@ -29,13 +27,13 @@ public DCCommandSender(CompletableFuture<InteractionHook> cmdMsg, User user) {
}


private static String textComponentToDiscordMessage(Component component) {
private static String textComponentToDiscordMessage(ITextComponent component) {
if (component == null) return "";
return MessageUtils.convertMCToMarkdown(component.getString());
}

@Override
public void sendMessage(Component p_215097_, UUID uuid) {
public void sendMessage(ITextComponent p_215097_, UUID uuid) {
message.append(textComponentToDiscordMessage(p_215097_)).append("\n");
if (cmdMessage == null)
cmdMsg.thenAccept((msg) -> {
Expand All @@ -48,7 +46,7 @@ public void sendMessage(Component p_215097_, UUID uuid) {
}

@Override
public void displayClientMessage(Component chatComponent, boolean actionBar) {
public void displayClientMessage(ITextComponent chatComponent, boolean actionBar) {
message.append(textComponentToDiscordMessage(chatComponent)).append("\n");
if (cmdMessage == null)
cmdMsg.thenAccept((msg) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
import de.erdbeerbaerlp.dcintegration.common.minecraftCommands.MCSubCommand;
import de.erdbeerbaerlp.dcintegration.common.minecraftCommands.McCommandRegistry;
import de.erdbeerbaerlp.dcintegration.common.storage.Configuration;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.*;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextComponentUtils;
import net.minecraft.util.text.event.ClickEvent;
import net.minecraft.util.text.event.HoverEvent;


public class McCommandDiscord {
public McCommandDiscord(CommandDispatcher<CommandSourceStack> dispatcher) {
final LiteralArgumentBuilder<CommandSourceStack> l = Commands.literal("discord");
public McCommandDiscord(CommandDispatcher<CommandSource> dispatcher) {
final LiteralArgumentBuilder<CommandSource> l = Commands.literal("discord");
if (Configuration.instance().ingameCommand.enabled) l.executes((ctx) -> {
ctx.getSource().sendSuccess(ComponentUtils.mergeStyles(new TextComponent(Configuration.instance().ingameCommand.message),
Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponent(Configuration.instance().ingameCommand.hoverMessage)))
ctx.getSource().sendSuccess(TextComponentUtils.mergeStyles(new StringTextComponent(Configuration.instance().ingameCommand.message),
Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new StringTextComponent(Configuration.instance().ingameCommand.hoverMessage)))
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, Configuration.instance().ingameCommand.inviteURL))), false);
return 0;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package de.erdbeerbaerlp.dcintegration.forge.mixin;

import de.erdbeerbaerlp.dcintegration.forge.DiscordIntegrationMod;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.play.ServerPlayNetHandler;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -14,14 +14,14 @@
/**
* Mixin used to detect player timeouts
*/
@Mixin(value = ServerGamePacketListenerImpl.class, priority = 1001)
@Mixin(value = ServerPlayNetHandler.class, priority = 1001)
public class MixinNetHandlerPlayServer {
@Shadow
public ServerPlayer player;
public ServerPlayerEntity player;

@Inject(method = "disconnect", at = @At("HEAD"))
private void onDisconnect(final Component textComponent, CallbackInfo ci) {
if (textComponent.equals(new TranslatableComponent("disconnect.timeout")))
private void onDisconnect(final ITextComponent textComponent, CallbackInfo ci) {
if (textComponent.equals(new TranslationTextComponent("disconnect.timeout")))
DiscordIntegrationMod.timeouts.add(this.player.getUUID());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import de.erdbeerbaerlp.dcintegration.common.storage.Configuration;
import de.erdbeerbaerlp.dcintegration.common.storage.Localization;
import de.erdbeerbaerlp.dcintegration.common.storage.linking.LinkManager;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.players.PlayerList;
import net.minecraftforge.server.ServerLifecycleHooks;
import net.minecraft.server.management.PlayerList;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.fml.server.ServerLifecycleHooks;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -19,17 +19,17 @@
@Mixin(PlayerList.class)
public class MixinPlayerLogin {
@Inject(method = "canPlayerLogin", at = @At("HEAD"), cancellable = true)
private void canLogin(SocketAddress address, GameProfile profile, CallbackInfoReturnable<Component> cir) {
private void canLogin(SocketAddress address, GameProfile profile, CallbackInfoReturnable<ITextComponent> cir) {
if (Configuration.instance().linking.whitelistMode && ServerLifecycleHooks.getCurrentServer().usesAuthentication()) {
LinkManager.checkGlobalAPI(profile.getId());
try {
if (!LinkManager.isPlayerLinked(profile.getId())) {
cir.setReturnValue(new TextComponent(Localization.instance().linking.notWhitelistedCode.replace("%code%",""+LinkManager.genLinkNumber(profile.getId()))));
cir.setReturnValue(new StringTextComponent(Localization.instance().linking.notWhitelistedCode.replace("%code%",""+LinkManager.genLinkNumber(profile.getId()))));
}else if(!DiscordIntegration.INSTANCE.canPlayerJoin(profile.getId())){
cir.setReturnValue(new TextComponent(Localization.instance().linking.notWhitelistedRole));
cir.setReturnValue(new StringTextComponent(Localization.instance().linking.notWhitelistedRole));
}
} catch (IllegalStateException e) {
cir.setReturnValue(new TextComponent("An error occured\nPlease check Server Log for more information\n\n" + e));
cir.setReturnValue(new StringTextComponent("An error occured\nPlease check Server Log for more information\n\n" + e));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dcshadow.org.apache.commons.collections4.keyvalue.DefaultMapEntry;
import de.erdbeerbaerlp.dcintegration.common.storage.Configuration;
import de.erdbeerbaerlp.dcintegration.common.util.MessageUtils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.arguments.ComponentArgument;
import net.minecraft.commands.arguments.NbtTagArgument;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.command.arguments.ComponentArgument;
import net.minecraft.command.arguments.NBTTagArgument;
import net.minecraft.entity.Entity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.nbt.StringNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IForgeRegistry;

Expand All @@ -33,29 +32,32 @@

public class ForgeMessageUtils extends MessageUtils {

private static final com.google.gson.JsonParser p = new com.google.gson.JsonParser();

private static final IForgeRegistry<Item> itemreg = ForgeRegistries.ITEMS;

public static String formatPlayerName(Map.Entry<UUID, String> p) {
return formatPlayerName(p, true);
}

public static String formatPlayerName(Map.Entry<UUID, String> p, boolean chatFormat) {
return ChatFormatting.stripFormatting(p.getValue());
return TextFormatting.stripFormatting(p.getValue());
}

/**
* Attempts to generate an {@link MessageEmbed} showing item info from an {@link Component} instance
* Attempts to generate an {@link MessageEmbed} showing item info from an {@link ITextComponent} instance
*
* @param component The TextComponent to scan for item info
* @return an {@link MessageEmbed} when there was an Item info, or {@link null} if there was no item info OR the item info was disabled
*/
public static MessageEmbed genItemStackEmbedIfAvailable(final Component component) {
public static MessageEmbed genItemStackEmbedIfAvailable(final ITextComponent component) {
if (!Configuration.instance().forgeSpecific.sendItemInfo) return null;
final JsonObject json = JsonParser.parseString(Component.Serializer.toJson(component)).getAsJsonObject();
final JsonObject json = p.parse(ITextComponent.Serializer.toJson(component)).getAsJsonObject();
if (json.has("with")) {
final JsonArray args = json.getAsJsonArray("with");
for (JsonElement el : args) {
if (el instanceof JsonObject arg1) {
if (el instanceof JsonObject) {
JsonObject arg1 = (JsonObject) el;
if (arg1.has("hoverEvent")) {
final JsonObject hoverEvent = arg1.getAsJsonObject("hoverEvent");
if (hoverEvent.has("action") && hoverEvent.get("action").getAsString().equals("show_item") && hoverEvent.has("contents")) {
Expand All @@ -64,12 +66,13 @@ public static MessageEmbed genItemStackEmbedIfAvailable(final Component componen
try {
final ItemStack is = new ItemStack(itemreg.getValue(new ResourceLocation(item.get("id").getAsString())));
if (item.has("tag")) {
final CompoundTag tag = (CompoundTag) NbtTagArgument.nbtTag().parse(new StringReader(item.get("tag").getAsString()));

final CompoundNBT tag = (CompoundNBT) NBTTagArgument.nbtTag().parse(new StringReader(item.get("tag").getAsString()));
is.setTag(tag);
}
final CompoundTag itemTag = is.getOrCreateTag();
final CompoundNBT itemTag = is.getOrCreateTag();
final EmbedBuilder b = new EmbedBuilder();
String title = is.hasCustomHoverName() ? is.getDisplayName().getString() : new TranslatableComponent(is.getItem().getDescriptionId()).getString();
String title = is.hasCustomHoverName() ? is.getDisplayName().getString() : new TranslationTextComponent(is.getItem().getDescriptionId()).getString();
if (title.isEmpty())
title = is.getItem().getDescriptionId();
else
Expand All @@ -89,27 +92,28 @@ public static MessageEmbed genItemStackEmbedIfAvailable(final Component componen
if (!flags[0]) {
//Implementing this code myself because the original is broken
for (int i = 0; i < is.getEnchantmentTags().size(); ++i) {
final CompoundTag compoundnbt = is.getEnchantmentTags().getCompound(i);
final Enchantment ench = Enchantment.byId(compoundnbt.getInt("id"));
if (compoundnbt.get("lvl") != null) {
final int level;
if (compoundnbt.get("lvl") instanceof StringTag) {
level = Integer.parseInt(compoundnbt.getString("lvl").replace("s", ""));
} else
level = compoundnbt.getInt("lvl") == 0 ? compoundnbt.getShort("lvl") : compoundnbt.getInt("lvl");
tooltip.append(ChatFormatting.stripFormatting(ench.getFullname(level).getString())).append("\n");
}
final CompoundNBT compoundnbt = is.getEnchantmentTags().getCompound(i);
Registry.ENCHANTMENT.getOptional(ResourceLocation.tryParse(compoundnbt.getString("id"))).ifPresent((ench) -> {
if (compoundnbt.get("lvl") != null) {
final int level;
if (compoundnbt.get("lvl") instanceof StringNBT) {
level = Integer.parseInt(compoundnbt.getString("lvl").replace("s", ""));
} else
level = compoundnbt.getInt("lvl") == 0 ? compoundnbt.getShort("lvl") : compoundnbt.getInt("lvl");
tooltip.append(TextFormatting.stripFormatting(ench.getFullname(level).getString())).append("\n");
}
});
}/*
EnchantmentHelper.getEnchantments(is).forEach((ench, lvl) -> {
tooltip.append(TextFormatting.getTextWithoutFormattingCodes(ench.getDisplayName(lvl).getUnformattedComponentText())).append("\n");
});*/
}
//Add Lores
final ListTag list = itemTag.getCompound("display").getList("Lore", 8);
final ListNBT list = itemTag.getCompound("display").getList("Lore", 8);
list.forEach((nbt) -> {
try {
if (nbt instanceof StringTag) {
final Component comp = (Component) ComponentArgument.textComponent().parse(new StringReader(nbt.getAsString()));
if (nbt instanceof StringNBT) {
final ITextComponent comp = ComponentArgument.textComponent().parse(new StringReader(nbt.getAsString()));
tooltip.append("_").append(comp.getString()).append("_\n");
}
} catch (CommandSyntaxException e) {
Expand Down
Loading

0 comments on commit 6efc2f1

Please sign in to comment.