Added AutoRecipe and AutoCraft handling

This commit is contained in:
Auxilor
2021-01-07 19:14:28 +00:00
parent f60f69fe7c
commit a35e4d0db7
10 changed files with 191 additions and 3 deletions

View File

@@ -0,0 +1,19 @@
package com.willfp.talismans.proxy.v1_15_R1;
import com.willfp.talismans.proxy.proxies.AutoCraftProxy;
import net.minecraft.server.v1_15_R1.MinecraftKey;
import net.minecraft.server.v1_15_R1.PacketPlayOutAutoRecipe;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
public final class AutoCraft implements AutoCraftProxy {
@Override
public void modifyPacket(@NotNull final Object packet) throws NoSuchFieldException, IllegalAccessException {
PacketPlayOutAutoRecipe recipePacket = (PacketPlayOutAutoRecipe) packet;
Field fKey = recipePacket.getClass().getDeclaredField("b");
fKey.setAccessible(true);
MinecraftKey key = (MinecraftKey) fKey.get(recipePacket);
fKey.set(recipePacket, new MinecraftKey("talismans", key.getKey() + "_displayed"));
}
}

View File

@@ -0,0 +1,19 @@
package com.willfp.talismans.proxy.v1_16_R1;
import com.willfp.talismans.proxy.proxies.AutoCraftProxy;
import net.minecraft.server.v1_16_R1.MinecraftKey;
import net.minecraft.server.v1_16_R1.PacketPlayOutAutoRecipe;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
public final class AutoCraft implements AutoCraftProxy {
@Override
public void modifyPacket(@NotNull final Object packet) throws NoSuchFieldException, IllegalAccessException {
PacketPlayOutAutoRecipe recipePacket = (PacketPlayOutAutoRecipe) packet;
Field fKey = recipePacket.getClass().getDeclaredField("b");
fKey.setAccessible(true);
MinecraftKey key = (MinecraftKey) fKey.get(recipePacket);
fKey.set(recipePacket, new MinecraftKey("talismans", key.getKey() + "_displayed"));
}
}

View File

@@ -0,0 +1,19 @@
package com.willfp.talismans.proxy.v1_16_R2;
import com.willfp.talismans.proxy.proxies.AutoCraftProxy;
import net.minecraft.server.v1_16_R2.MinecraftKey;
import net.minecraft.server.v1_16_R2.PacketPlayOutAutoRecipe;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
public final class AutoCraft implements AutoCraftProxy {
@Override
public void modifyPacket(@NotNull final Object packet) throws NoSuchFieldException, IllegalAccessException {
PacketPlayOutAutoRecipe recipePacket = (PacketPlayOutAutoRecipe) packet;
Field fKey = recipePacket.getClass().getDeclaredField("b");
fKey.setAccessible(true);
MinecraftKey key = (MinecraftKey) fKey.get(recipePacket);
fKey.set(recipePacket, new MinecraftKey("talismans", key.getKey() + "_displayed"));
}
}

View File

@@ -0,0 +1,19 @@
package com.willfp.talismans.proxy.v1_16_R3;
import com.willfp.talismans.proxy.proxies.AutoCraftProxy;
import net.minecraft.server.v1_16_R3.MinecraftKey;
import net.minecraft.server.v1_16_R3.PacketPlayOutAutoRecipe;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
public final class AutoCraft implements AutoCraftProxy {
@Override
public void modifyPacket(@NotNull final Object packet) throws NoSuchFieldException, IllegalAccessException {
PacketPlayOutAutoRecipe recipePacket = (PacketPlayOutAutoRecipe) packet;
Field fKey = recipePacket.getClass().getDeclaredField("b");
fKey.setAccessible(true);
MinecraftKey key = (MinecraftKey) fKey.get(recipePacket);
fKey.set(recipePacket, new MinecraftKey("talismans", key.getKey() + "_displayed"));
}
}

View File

@@ -8,6 +8,7 @@ import com.willfp.eco.util.protocollib.AbstractPacketAdapter;
import com.willfp.talismans.commands.CommandTaldebug;
import com.willfp.talismans.commands.CommandTalreload;
import com.willfp.talismans.config.TalismansConfigs;
import com.willfp.talismans.display.packets.PacketAutoRecipe;
import com.willfp.talismans.display.packets.PacketChat;
import com.willfp.talismans.display.packets.PacketOpenWindowMerchant;
import com.willfp.talismans.display.packets.PacketSetCreativeSlot;
@@ -17,6 +18,7 @@ import com.willfp.talismans.integrations.mcmmo.McmmoManager;
import com.willfp.talismans.integrations.mcmmo.plugins.McmmoIntegrationImpl;
import com.willfp.talismans.talismans.Talismans;
import com.willfp.talismans.talismans.util.BlockPlaceListener;
import com.willfp.talismans.talismans.util.DiscoverRecipeListener;
import com.willfp.talismans.talismans.util.TalismanChecks;
import com.willfp.talismans.talismans.util.TalismanCraftListener;
import com.willfp.talismans.talismans.util.WatcherTriggers;
@@ -136,7 +138,8 @@ public class TalismansPlugin extends AbstractEcoPlugin {
new PacketOpenWindowMerchant(this),
new PacketSetCreativeSlot(this),
new PacketSetSlot(this),
new PacketWindowItems(this)
new PacketWindowItems(this),
new PacketAutoRecipe(this)
);
}
@@ -151,7 +154,8 @@ public class TalismansPlugin extends AbstractEcoPlugin {
new WatcherTriggers(this),
new BlockPlaceListener(),
new TalismanCraftListener(),
new TalismanEquipEventListeners(this)
new TalismanEquipEventListeners(this),
new DiscoverRecipeListener()
);
}

View File

@@ -0,0 +1,50 @@
package com.willfp.talismans.display.packets;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.willfp.eco.util.ProxyUtils;
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
import com.willfp.eco.util.protocollib.AbstractPacketAdapter;
import com.willfp.talismans.proxy.proxies.AutoCraftProxy;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.InvocationTargetException;
public class PacketAutoRecipe extends AbstractPacketAdapter {
/**
* Instantiate a new listener for {@link PacketType.Play.Server#SET_SLOT}.
*
* @param plugin The plugin to listen through.
*/
public PacketAutoRecipe(@NotNull final AbstractEcoPlugin plugin) {
super(plugin, PacketType.Play.Server.AUTO_RECIPE, false);
}
@Override
public void onSend(@NotNull final PacketContainer packet) {
if (!packet.getMinecraftKeys().getValues().get(0).getFullKey().split(":")[0].equals("talismans")) {
return;
}
if (packet.getMinecraftKeys().getValues().get(0).getFullKey().split(":")[1].contains("displayed")) {
return;
}
try {
ProxyUtils.getProxy(AutoCraftProxy.class).modifyPacket(packet.getHandle());
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
PacketContainer newAutoRecipe = new PacketContainer(PacketType.Play.Server.AUTO_RECIPE);
newAutoRecipe.getMinecraftKeys().write(0, packet.getMinecraftKeys().read(0));
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(Bukkit.getServer().getPlayer("Auxilor"), newAutoRecipe);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

View File

@@ -21,6 +21,7 @@ import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.permissions.Permission;
@@ -101,6 +102,12 @@ public abstract class Talisman implements Listener, Watcher {
@Getter
private ShapedRecipe recipe = null;
/**
* The talisman displayed recipe.
*/
@Getter
private ShapedRecipe displayedRecipe = null;
/**
* The talisman recipe overlay.
*/
@@ -198,13 +205,16 @@ public abstract class Talisman implements Listener, Watcher {
this.itemStack = out;
Bukkit.getServer().removeRecipe(this.getKey());
Bukkit.getServer().removeRecipe(new NamespacedKey(this.getPlugin(), this.getKey().getKey() + "_displayed"));
if (this.isEnabled()) {
ShapedRecipe recipe = new ShapedRecipe(this.getKey(), out);
ShapedRecipe displayedRecipe = new ShapedRecipe(new NamespacedKey(this.getPlugin(), this.getKey().getKey() + "_displayed"), out);
List<String> recipeStrings = this.getConfig().getStrings(Talismans.OBTAINING_LOCATION + "recipe");
recipe.shape("012", "345", "678");
displayedRecipe.shape("012", "345", "678");
for (int i = 0; i < 9; i++) {
recipeTalismanOverlay[i] = null;
@@ -216,12 +226,16 @@ public abstract class Talisman implements Listener, Watcher {
Validate.notNull(talisman, "Talisman specified in " + this.getConfigName() + ".yml's recipe is invalid!");
recipeTalismanOverlay[i] = Talismans.getByKey(talismanNamespacedKey);
recipe.setIngredient(ingredientChar, Material.PLAYER_HEAD);
displayedRecipe.setIngredient(ingredientChar, new RecipeChoice.ExactChoice(talisman.getItemStack()));
} else {
recipe.setIngredient(ingredientChar, Material.valueOf(recipeStrings.get(i).toUpperCase()));
displayedRecipe.setIngredient(ingredientChar, Material.valueOf(recipeStrings.get(i).toUpperCase()));
}
}
Bukkit.getServer().addRecipe(recipe);
Bukkit.getServer().addRecipe(displayedRecipe);
this.displayedRecipe = displayedRecipe;
}
postUpdate();

View File

@@ -134,7 +134,6 @@ public class Talismans {
public static final Talisman SPEED_RING = new SpeedRing();
public static final Talisman SPEED_RELIC = new SpeedRelic();
/**
* Get all registered {@link Talisman}s.
*
@@ -144,6 +143,15 @@ public class Talismans {
return ImmutableList.copyOf(BY_KEY.values());
}
/**
* Get {@link NamespacedKey}s for all registered {@link Talisman}s.
*
* @return A list of all {@link Talisman}s.
*/
public static List<NamespacedKey> keySet() {
return ImmutableList.copyOf(BY_KEY.keySet());
}
/**
* Get {@link Talisman} matching display name.
*

View File

@@ -0,0 +1,23 @@
package com.willfp.talismans.talismans.util;
import com.willfp.talismans.talismans.Talisman;
import com.willfp.talismans.talismans.Talismans;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.jetbrains.annotations.NotNull;
public class DiscoverRecipeListener implements Listener {
/**
* Unlock all recipes on player join.
*
* @param event The event to listen for.
*/
@EventHandler
public void onJoin(@NotNull final PlayerJoinEvent event) {
Player player = event.getPlayer();
Talismans.values().stream().filter(Talisman::isEnabled).map(Talisman::getKey).forEach(player::discoverRecipe);
}
}

View File

@@ -0,0 +1,13 @@
package com.willfp.talismans.proxy.proxies;
import com.willfp.eco.util.proxy.AbstractProxy;
import org.jetbrains.annotations.NotNull;
public interface AutoCraftProxy extends AbstractProxy {
/**
* Fix crafting inventory on auto-recipe.
*
* @param packet The packet to modify.
*/
void modifyPacket(@NotNull Object packet) throws NoSuchFieldException, IllegalAccessException;
}