SurviveTheNight

There is currently 1 player online.

Connect now using the IP mc.survivethenight.net

Internet Explorer

Internet Explorer is not supported. Please upgrade to a more modern browser.

Devlog #3 - Crafting System
Started by zProxxy

zProxxy

zProxxy

Administrator
Joined
07 Feb 2023
Last Seen
10 Oct 2024
Topics
34
Posts
34

Hi!

This is the third devlog! In this thread I will talk about our crafting system and how it has been improved.

Introduction

I didn't plan on writing a devlog about our crafting system, however many people have complained about it in the past, many things have been improved just now and the mechanics behind it are actually quite interesting.


Crafting System

As you probably could've guessed, our crafting system is completely custom. We don't rely on the vanilla crafting table and it's abilities and all recipes are self-made.

Why don't we use the vanilla crafting system? Minecraft is a little bit weird when it comes to crafting. Can you think of a vanilla recipe that requires more than just one single item on a slot? Like there is no recipe that requires putting multiple of the same item on a single slot to craft an item. And plugins don't allow us to do that either!

There are ways of creating custom recipes, however you cannot make it so players must put more than one item in a slot - and this would become an issue in terms of item cost and recipe possibilities.
Therefore we haven't had any other choice but making our own crafting system!

Our custom crafting system is made out of 5 core parts.

  • The Crafting GUI: This is the menu you see when opening the crafting table. It doesn't look like the vanilla crafting table but instead is made out of a normal inventory with a bunch of glass panes.

  • Recipe Data: All of our recipes are saved in custom recipe objects. They contain the items required on each slot, if items should be updated when being crafted (such as item modifiers), which item you should receive when crafting it and what it requires to unlock the recipe.

  • Inventory Updater: This is basically a scheduler running in the background for each player having the crafting gui open. It checks all 9 slots of the crafting grid, checks all registered recipes and if it finds a matching recipe it displays the output item using the item updater.

  • Item Updater: This basically checks for the item with the highest "modifier priority". Most recipes have an item which has modifier priority.
    For example when upgrading an axe, you not only need random materials but also the previous axe which may have custom modifiers (such as Bat-Suffused) applied to it. That item would become the item with the highest modifier priority. The Item Updater then takes all modifiers of that item and applies them to the item you're about to craft.

  • Crafting Listener: This is a basic inventory listener which is getting triggered upon interacting with the menu. It blocks all sorts of dupes, checks the click and clicked slot and removes items upon crafting from the crafting gui so that you cannot craft items for free!

All of these systems must work without any flaws, otherwise it may lead into you being able to dupe items while crafting!


Registered Recipes

As already mentioned, our custom recipes are registered in seperate objects containing all the data required for crafting and displaying the recipe.

This is what a recipe looks like:

EXOTIC_AXE(new RecipeItemStack[]{item1, item2, item3,
            item4, item5, item6,
            item7, item8, item9},
            false, new RecipeLock(RecipeTypes.EXOTIC_AXE, -1), true, Items.EXOTIC_AXE, 1),

Item1-9 are sets of data containing the item, the amount and if it should have the modifier priority or not. They may also be "null" in case an empty slot should be in that recipe.

The bottom row contains data such as if the recipe should be unlocked by default, if not what the requirement should be, if the item should receive a modifier update when being crafted (only necessary for items that may actually have modifiers), what item the output item should be and how many of them you receive.
Pretty simple right?

 

Inventory and Item Updater

The inventory updater doesn't do anything special.
As already said it iterates through all registered recipes and compares them with the 9 slots of your current crafting gui. If it found a matching recipe it creates an output item using the item updater, adds some additional data to it and repeats the entire process.

The Item Updater receives the default output item (without anything on it) and then applies all modifiers of either the item with the modifier priority on it or the first item in the crafting grid.
If - for whatever reason! - there isn't an item in the grid, it just returns the default item.

This is what the updater basically looks like:

private ItemStack updateModifiers(ItemStack outputItem, CraftingRecipes recipe, Inventory inventory){
        int[] slots = new int[]{10, 11, 12, 19, 20, 21, 28, 29, 30};
        for(int i = 1; i  9; i++){
            if(recipe.getItemOnSlot(i).isHighestModifierPriorityOnCraft()){
                return new ItemUtils().createItemByOriginal(inventory.getItem(slots[i-1]), outputItem);
            }
        }
        for(int slot : slots) {
            if(inventory.getItem(slot) != null && inventory.getItem(slot).getType() != Material.AIR){
                return new ItemUtils().createItemByOriginal(inventory.getItem(slot), outputItem);
            }
        }
        return outputItem;
    }

 

Crafting Listener

The Crafting Listener is probably one of the most important systems. Not only does it allow you to actually craft items, but it also blocks any kinds of item dupes regarding crafting.

It checks the type of inventory click you're performing (e.g. leftclick, rightclick, shiftclick), it checks what item you have on your cursor, it checks clicked slots and it checks other inventory actions such as if you're trying to collect items to your cursor (basically when you double click with an item on your cursor and it merges all identical items of the entire inventory with the ones on your cursor).

Upon crafting an item it checks wether you shift-clicked or not, then removes all unnecessary data from the output item and either gives you one set of items or as many as you may craft.

After that it simply just removes the used items from the crafting grid and that's it!

 

Improvements

So as you could probably guess, if there's one thing that goes wrong, this entire system may lead into massive issues with dupes or performance. However I have made some improvements to it!

Over the past few months, one of STN's biggest flaws was probably the crafting system. You could only craft one set of items at a time, then you'd have to wait for like .5 seconds, then you can craft another set. I've received a lot of feedback, what I could add in addition to make crafting less painful and how this system needs a change and here are the changes:

  • Due to our new item system and improvements in recipe registration, the server no longer needs .5 seconds delay in order to check recipes and display the output item correctly
  • There is no longer a cooldown on crafting items
  • You may now shift click with any item on your cursor to immediately craft as many items as you can instead of just one set of items
    • If your inventory is full, is getting cancelled
    • Non-stackable items are not affected by this! You may only craft one non-stackable item per click!
  • Instead of blocking the click, you may now click on the output item with the same item on your cursor to increase the amount of the items on your cursor
  • The modifier checker has been improved so it doesn't come to future issues when upgrading tools/weapons/armor

 

Conclusion

The crafting system may look simple for you guys but in reality it isn't! In fact this might be one of STN's most complex systems as basically nothing relies on the default minecraft crafting mechanics!

The crafting system is still not 100% done just yet, however it's looking promising!

 

Systems that must still be reworked

This is more like a checklist with systems that will probably receive their own devlog once they're done.

  • Items
  • Achievements (abt 80%)
  • STN Creatures
  • Inventories
  • NPCs
 
 
zProxxy · 4 months ago · Last edited: 4 months ago