Home Creators Posts Import Register Favorites Logout

Downloads

Content

Gather round Patron's and Mr Juice will tell you a story about how sometimes the easiest option is to just ask nicely.

So Apo and I knew we wanted to have shop system for Pirates, and it was going to be the part of the main gameplay loop for the SMP.

Go on quests -> Get gold -> Buy better equipment -> go on bigger quests

But! The barrier of entry for some equipment in Minecraft is incredibly low, a full set of iron is super easy to acquire. But in the olden days o' pirates, a full set would have been a pretty big deal! We needed to make sure players couldn't just craft things that should be expensive. Like armour, guns, ammo, etc etc.

So! The Problem: How do you remove certain crafting recipes from the game?

Answer: APPARENTLY YOU DON'T IT'S SUPER DIFFICULT?

We wanted to use a mod for this, but because the server was supporting both mods and plugins (and we were already at our limit with budget) we weren't able to make that happen.

So I stepped up to the plate armed with nothing but datapack code! New Problem: How do you remove something from someone's inventory that they shouldn't have.

Answer: 4 months of coding.

How it "worked":

Checking somebodies inventory in Minecraft is a surprisingly computationally expensive thing to do. The computer has to scan through every single slot in their inventory, all of their armour, the hotbar, the main 3 rows, and if you're looking for something specific, it has to compare what it finds in each slot with what you're looking for.

Running the check every "tick" to ensure if someone made a gun that it would be removed would mean we're checking every players inventory 20 times a second. But that's not all! We wanted to check for 20 different items. Meaning if we were checking for those items every tick, we would be running 16400 checks every second, MULTIPLIED by the number of players!

To make it a bit easier on the server, we instead checked once every 10 seconds. But we can't check every players inventory at the same time, that would mean every 10 seconds there's a big lag spike! So instead we had to desync every players check by a few seconds at random to spread the load of the work.

So there we have it, every 10 seconds, check the players inventory, if there's a gun in there, remove all the guns from their inventory! We had to make it remove every gun because you can't specify to remove a certain players inventory slot without directly replacing that slot, but even if we did THAT, there's no way of knowing which slot the gun is in, just that it is in one.

Now we've got a new issue though: How do we differentiate between a crafted gun and a bought gun?

Well, MrMadSpy rescued us with this one by changing the Shop Plugin we had made to also add a "tag" to any bought item. When scanning for an uncraftable item, double check the one you find doesn't have a "ShopBought" tag to it's data, if it does, don't remove it!

Yippeee we did it!.... Right?...

Not quite. Riddle me this, what happens if a player has a bought gun and a crafted gun in their inventory. What should the code do? It can't remove just the crafted gun, it doesn't know where in the inventory it is. You'd have to clear every gun, including the one the player bought! Which would result in a LOT of complaints from the cast losing their items that they had on them if they didn't know an item wasn't meant to be crafted.

So how did we fix this?

We didn't.

That's right! If a player bought a gun, and had it in their inventory, they could effectively just make a bunch of other guns, and the code would leave them alone. We just decided to not tell anyone about this exploit. Until now that is...

In conclusion!

So! What did we learn? Well personally, I think we learnt that perhaps it might be better sometimes to just ask players "Hey please don't craft these items" rather than try to force them to not. Don't get me wrong, some cast members would have definitely still crafted them,  either erroneously or on purpose (You'd be surprised how hard they try to find exploits in our SMP's), but at least it would have saved us 4 months of bashing our head against the code accidentally removing things it shouldn't. 

Thanks for reading! If you would like more code stories from me about how we approached certain problems, let me know!

(PS: I've attached the code in a zipped up folder below. I wasn't sure whether to do this as I'll warn you now that it ISN'T good code. I can think of some better ways to fix this problem now but this post is long enough already. Either way, it's below if you wanted to see it!

- 🍊

Files

Comments

Candyphone

Yessss more coding, its so interesting hearing how everything woks

PleaseInsertCoin

This is a very interesting approach to the problem and one thing I’ve been curious about since seeing it on the SMP! I have had a similar issue in the past with preventing players from crafting specific items on my own projects. The biggest example of this is on one project, I didn’t want the players to be able to set their spawn with a bed. So, to prevent them from getting beds I removed the crafting recipe for beds. It took me a bit of tinkering to figure out, but eventually I had figured it out. I made a datapack that overwrites the bed recipe with a custom recipe that was blank. It turned out well and I had no issues!

typo Adventure

Yes! More about your creative process!!

KaiThePoet

Creative processes make my brain go brrrr in the most /pos way possible

GalaxyPurpleShadows

Yay coding story time! I may not fully understand coding, but I do enjoy watching coding streams and hearing the creative/problem solving process.

HypnoEnvy

I find it entertaining that if one pirate discovered this, they could've made a black market for guns. (not that they would, but still) XD!