Skip to content

Commit

Permalink
Update 3.1. See readme diff for features.
Browse files Browse the repository at this point in the history
  • Loading branch information
budak7273 committed Oct 24, 2020
1 parent 95d628d commit 86c90ff
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 24 deletions.
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,50 @@ Craft chainmail leggings using a custom recipe to obtain a magnet.
![Magnet crafting recipe](https://i.imgur.com/Rf1a2TE.png)

## Features
* Lag friendly!
* Configurable!
* Run `/function magnet:toggle_options_pane` to see options.
* Brings items to you!
* Break blocks over a void without fear of dropping items off the edge.
* Survival friendly
* Custom crafting recipe provides a method of obtaining magnets in a survival playthrough
* Item entities play a quiet sound to help you locate nearby them.
* Nearby item entities play a quiet sound to help you locate them (configurable)
* Works for multiple players at once
* Wear a magnet as pants
* Mobs can also use magnets
* Make a magnet powered mobfarm! Use mobs wearing magnets to gather items to a hopper and save on hoppers
* Mobs with magnets can attract and equip your gear if you die. Don't die near a mob with a magnet.
* Particles are displayed when an item is brought to a magnet user
* Works on Spigot
* Wear a magnet as pants!
* Both functional _and_ stylish.
* Mobs can also use magnets (configurable)
* Make a magnet powered mobfarm! Use mobs (or armor stands) wearing magnets to gather items to a hopper and save on hoppers
* Mobs with magnets can attract and equip your gear if you die. (disabled by default) Don't die near a mob with a magnet.
* Particles are displayed when an item is brought to a magnet user (configurable)
* Works on Spigot/Paper/Etc.

## Commands
* `/function magnet:toggle_options_pane`
* Toggles the options pane to show/hide the current config settings.
* `/function magnet:give_magnet`
* Gives yourself a magnet if you don't want to craft one.
* `/function magnet:magnetize_item`
* Manually teleport an item entity within range to you as if you were holding a magnet.
* `/function magnet:default_configs`
* Reset back to the default config options (see below)
* `/function magnet:reinstall`
* Reinstall the pack from scratch and reload, also resetting all of your configs to the default.
* `/function magnet:uninstall`
* Run before uninstalling - cleans up all scoreboard values and such. Does not delete existing magnet items.
* `/function magnet:debug_list_holders`
* Debug command to mark and list all loaded entities that are holding magnets. Helpful for tracking down rogue magnets.

## Config Options
* `m_mob_interval` (Default 20) - The number of ticks between magnets working for mobs. 20 ticks in one second.
* `m_player_interval` (Default 5) - The number of ticks between magnets working for players. 20 ticks in one second.
* `armorStand` (Default 1) - If magnets work when equipped on armor stands.
* `playerHands` (Default 1) - If magnets work when in the player's main or offhand
* `playerEquip` (Default 1) - If magnets work when in the player's legs slot.
* `particlesOnHolder` (Default 1) - If particles should be spawned on the magnet user when an item is attracted.
* `particlesOnItem` (Default 1) - If particles should be spawned at the old location of the item when an item is attracted.
* `playItemSound` (Default 1) - If items should play a sound when within 10 blocks of a magnet user.
* `enabledActionbarText` (Default 1) - If having a magnet equipped should display text in your action bar.
* `allMobs_Laggy` (Default 0) - If magnets work for every mob than can equip armor. This can be quite laggy to enable.

## Further Details
* I tried to document exactly how everything works via comments in the function files. Feel free to contact me with any questions.
Expand Down
Binary file modified assets/minecraft/models/item/magnet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions data/magnet/functions/debug_list_holders.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Makes all entities (not players) holding a magnet announce themselves in chat.

#execute as @e[type=!minecraft:player,nbt={ArmorItems:[{}, {tag: {isMagnet:1b}}, {}, {}]}, sort=random] at @s run say I am holding a magnet.

# Make entities glow
effect give @e[type=!minecraft:player,nbt={ArmorItems:[{}, {tag: {isMagnet:1b}}, {}, {}]}] minecraft:glowing 30 0 true

# Command that lists them all + helper text
tellraw @s ["",{"text":"Loaded entities holding magnets:\n","bold":true,"color":"yellow"},{"text":" ["},{"selector":"@e[type=!minecraft:player,nbt={ArmorItems:[{}, {tag: {isMagnet:1b}}, {}, {}]}]"},{"text":"]\n\nThey have all been given glowing for 30 seconds.\nTo teleport to one of them hover over them in the list to get their UUID, then type: "},{"text":"/tp <uuid>","color":"yellow","clickEvent":{"action":"suggest_command","value":"/tp UUID"},"hoverEvent":{"action":"show_text","contents":"Suggest command"}},{"text":"\nYou should probably copy-paste them in from your log file.\n\nAlternatively, you can select them for commands via:\n"},{"text":"@e[type=!minecraft:player,nbt={ArmorItems:[{}, {tag: {isMagnet:1b}}, {}, {}]}]","color":"yellow","clickEvent":{"action":"copy_to_clipboard","value":"@e[type=!minecraft:player,nbt={ArmorItems:[{}, {tag: {isMagnet:1b}}, {}, {}]}]"},"hoverEvent":{"action":"show_text","contents":"Click to Copy"}}]
32 changes: 32 additions & 0 deletions data/magnet/functions/default_configs.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Mark setup as ran
scoreboard players set ranSetup m_internals 1
tellraw @a ["",{"text":"\n"},{"text":"Ran magnet default config setup.","italic":true}]

# init magnetization player interval to every 5 ticks (0.25 seconds)
scoreboard players set m_player_interval m_options 5

# init magnetization mob interval to every 20 ticks (1 second)
scoreboard players set m_mob_interval m_options 20

# Works in players' main and offhand
scoreboard players set playerHands m_options 1

# Works in players' equip slots (legs)
scoreboard players set playerEquip m_options 1

# Works on armor stands
scoreboard players set armorStand m_options 1

# Works on all mobs (can cause considerable server lag)
scoreboard players set allMobs_Laggy m_options 0

# Plays sound on items
scoreboard players set playItemSound m_options 1

# Shows hud text when a magnet is equipped
scoreboard players set enabledActionbarText m_options 1

# Display particles
scoreboard players set particlesOnHolder m_options 1
scoreboard players set particlesOnItem m_options 1

42 changes: 40 additions & 2 deletions data/magnet/functions/init.mcfunction
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
# Print load message
tellraw @a ["",{"text":"\u2714","color":"dark_green"},{"text":" Magnet datapack loaded.\nGet some iron and go craft a "},{"text":"Magnet","color":"yellow"},{"text":".\nMore info "},{"text":"here","underlined":true,"clickEvent":{"action":"open_url","value":"http://bit.ly/MagnetDatapack"}},{"text":"."}]
tellraw @a ["",{"text":"\u2714","color":"dark_green"},{"text":" Magnet datapack loaded.\nGet some iron and go craft a "},{"text":"Magnet","color":"yellow"},{"text":".\nMore info "},{"text":"here","underlined":true,"clickEvent":{"action":"open_url","value":"http://bit.ly/MagnetDatapack"}},{"text":".\nTo configure the datapack, click "}, {"text":"[here]","color":"yellow","clickEvent":{"action":"run_command","value":"/function magnet:toggle_options_pane"},"hoverEvent":{"action":"show_text","contents":"Toggle options pane"}},{"text":"\nor type: "},{"text":"/function magnet:toggle_options_pane","color":"yellow","clickEvent":{"action":"suggest_command","value":"/function magnet:toggle_options_pane"},"hoverEvent":{"action":"show_text","contents":"Click to suggest"}},{"text":"\n "}]

# Set up scoreboard values for tracking crafting of chainmail leggings
scoreboard objectives add m_craft_leggings minecraft.crafted:minecraft.chainmail_leggings
scoreboard objectives add m_count_magnets dummy
scoreboard objectives add m_count_reg_legs dummy
scoreboard objectives add m_count_reg_legs dummy

# Set up scoreboard values for timed magnetization and internal settings
scoreboard objectives add m_internals dummy "Magnet Internal"

# Set up options scoreboard
scoreboard objectives add m_options dummy "Magnet Datapack"
scoreboard objectives modify m_options displayname {"text":"Magnet Datapack","bold":true,"color":"yellow"}

# Set up team coloration for options menu
#team add magnet_internal "magnet internal"
team add magnet_option "magnet option"
#team modify magnet_internal color gray
team modify magnet_option color gold
#team join magnet_internal m_timer_player
#team join magnet_internal m_timer_mob
#team join magnet_internal ranSetup
team join magnet_option playerHands
team join magnet_option playerEquip
team join magnet_option armorStand
team join magnet_option allMobs_Laggy
team join magnet_option m_mob_interval
team join magnet_option m_player_interval
team join magnet_option playItemSound
team join magnet_option particlesOnHolder
team join magnet_option particlesOnItem
team join magnet_option enabledActionbarText

#scoreboard objectives setdisplay sidebar m_options
#scoreboard objectives setdisplay sidebar m_internals

# Set up trackers for if setup has been run and if options pane is currently open.
# If undefined, it can't be checked against by 'execute matches' (has null value) so make it val *= val, which makes it 0 if null and has no effect if it's 1
scoreboard players operation ranSetup m_internals *= ranSetup m_internals
scoreboard players operation optionsOpen m_internals *= optionsOpen m_internals

# If setup never ran, run setup script
execute if score ranSetup m_internals matches 0 run function magnet:default_configs

18 changes: 14 additions & 4 deletions data/magnet/functions/magnetize_item.mcfunction
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# Cause all items within 10 blocks of the caller to play a sound for the nearest player. The sound has a minimum volume and ramps up as the player gets closer.
execute as @e[type=minecraft:item, distance=0..10] at @s run playsound minecraft:entity.experience_bottle.throw player @p ~ ~ ~ .01 2 .002
execute if score playItemSound m_options matches 1 run execute as @e[type=minecraft:item, distance=0..10] at @s run playsound minecraft:entity.experience_bottle.throw player @p ~ ~ ~ .01 2 .002

# Displays particles on the function's caller if there is an item in magnet range
execute as @s at @s if entity @e[distance=0..5, type=item] run particle minecraft:portal ~ ~.25 ~ .001 0 .001 1 2 normal
# TODO: consider swapping to nautilus particle
execute if score particlesOnHolder m_options matches 1 run execute as @s at @s if entity @e[distance=0..5, type=item] run particle minecraft:portal ~ ~.25 ~ 0 0 0 3 2 normal
# consider swapping to `nautilus` particle

# Show actionbar text to caller
execute if score enabledActionbarText m_options matches 1 run title @s actionbar {"text":"Magnet Equipped","color":"gray"}

# Item attraction range is 5 blocks

# Particles on item
execute if score particlesOnItem m_options matches 1 run execute as @e[limit=1,sort=nearest,distance=0..5,type=minecraft:item] at @s if entity @e[distance=0..5, type=item] run particle minecraft:reverse_portal ~ ~.25 ~ 0 0 0 0.1 3 normal

# Teleport nearest item entity to the function's caller
tp @e[limit=1,sort=nearest,distance=0..5,type=minecraft:item] @s
tp @e[limit=1,sort=nearest,distance=0..5,type=minecraft:item] @s
# Move 1 block towards the holder's feet (broken)
#execute as @e[limit=1,sort=nearest,distance=0..5,type=minecraft:item] run execute facing entity @s feet as @e[type=item] at @s run tp ^ ^ ^1
19 changes: 9 additions & 10 deletions data/magnet/functions/main.mcfunction
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Detect magnet equipped in player offhand
execute as @a[nbt={Inventory:[{Slot: -106b, tag: {isMagnet:1b}}]}] at @s run function magnet:magnetize_item
# Detect magnet equipped in player leggings slot
execute as @a[nbt={Inventory:[{Slot: 101b, tag: {isMagnet:1b}}]}] at @s run function magnet:magnetize_item
# Detect players that have crafted at least one pair of leggings and deal with that
execute as @e[type=minecraft:player,scores={m_craft_leggings=1..}] if score @s m_craft_leggings matches 1.. run function magnet:while001_crafted_legs

# Detect magnet held in player main hand
execute as @a[nbt={SelectedItem:{tag: {isMagnet:1b}}}] at @s run function magnet:magnetize_item
# Increment global timers by 1
scoreboard players add m_timer_player m_internals 1
scoreboard players add m_timer_mob m_internals 1

# Detect magnet in mobs' leggings armor slot. Random sort so that mobs 'fight' for items and if multiple have magnets they are more likely to suck up gear that others don't need
execute as @e[nbt={ArmorItems:[{}, {tag: {isMagnet:1b}}, {}, {}]}, sort=random] at @s run function magnet:magnetize_item
# If player timer exceeds m_player_interval, set it back to zero
execute if score m_timer_player m_internals >= m_player_interval m_options run function magnet:on_player_interval

# Detect players that have crafted at least one pair of leggings and deal with that
execute as @e[type=minecraft:player,scores={m_craft_leggings=1..}] if score @s m_craft_leggings matches 1.. run function magnet:while001_crafted_legs
# If mob timer exceeds m_mob_interval, set it back to zero
execute if score m_timer_mob m_internals >= m_player_interval m_options run function magnet:on_mob_interval
10 changes: 10 additions & 0 deletions data/magnet/functions/on_mob_interval.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# reset the interval counter back to zero
scoreboard players set m_timer_mob m_internals 0

#Random sort so that mobs 'fight' for items and if multiple have magnets they are more likely to suck up gear that others don't need

# Detect magnet in armor stands' leggings armor slot. Exclude if all mobs is on
execute unless score allMobs_Laggy m_options matches 1 if score armorStand m_options matches 1 run execute as @e[type=minecraft:armor_stand, nbt={ArmorItems:[{}, {tag: {isMagnet:1b}}, {}, {}]}, sort=random] at @s run function magnet:magnetize_item

# Detect magnet in any mobs' leggings armor slot.
execute if score allMobs_Laggy m_options matches 1 run execute as @e[type=!minecraft:player,nbt={ArmorItems:[{}, {tag: {isMagnet:1b}}, {}, {}]}, sort=random] at @s run function magnet:magnetize_item
10 changes: 10 additions & 0 deletions data/magnet/functions/on_player_interval.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# reset the interval counter back to zero
scoreboard players set m_timer_player m_internals 0

# Detect magnet held in player main hand
execute if score playerHands m_options matches 1 run execute as @a[nbt={SelectedItem:{tag: {isMagnet:1b}}}] at @s run function magnet:magnetize_item
# Detect magnet equipped in player offhand
execute if score playerHands m_options matches 1 run execute as @a[nbt={Inventory:[{Slot: -106b, tag: {isMagnet:1b}}]}] at @s run function magnet:magnetize_item

# Detect magnet equipped in player leggings slot
execute if score playerEquip m_options matches 1 run execute as @a[nbt={Inventory:[{Slot: 101b, tag: {isMagnet:1b}}]}] at @s run function magnet:magnetize_item
2 changes: 2 additions & 0 deletions data/magnet/functions/reinstall.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function magnet:uninstall
reload
30 changes: 30 additions & 0 deletions data/magnet/functions/toggle_options_pane.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# copy this instant into a scratchpad variable for OR statement
#scoreboard players operation instant m_internals = m_timer_player m_internals

# if the options menu is open and something hasn't already happened, set the flag to close the options
#execute if score instant m_internals = m_timer_player m_options if score optionsOpen m_internals matches 1 run scoreboard players set instant m_internals -1

# set toggler depending on if the options is open or not
# open:
execute if score optionsOpen m_internals matches 1 run scoreboard players set toggle m_internals 1
# closed:
execute if score optionsOpen m_internals matches 0 run scoreboard players set toggle m_internals 0

# if the options panel is closed, open it
execute if score toggle m_internals matches 0 run scoreboard objectives setdisplay sidebar m_options
execute if score toggle m_internals matches 0 run scoreboard players set optionsOpen m_internals 1
execute if score toggle m_internals matches 0 run tellraw @s ["",{"text":"\n\n\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550[Magnet Options]\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\n\nTo change a setting, run a variation of the following command:\n"},{"text":"/scoreboard players set ","color":"yellow","clickEvent":{"action":"suggest_command","value":"/scoreboard players set OPTION m_options VALUE"},"hoverEvent":{"action":"show_text","contents":"Click to suggest"}},{"text":"<option name>","italic":true,"color":"yellow","clickEvent":{"action":"suggest_command","value":"/scoreboard players set OPTION m_options VALUE"},"hoverEvent":{"action":"show_text","contents":"Click to suggest"}},{"text":" m_options ","color":"yellow","clickEvent":{"action":"suggest_command","value":"/scoreboard players set OPTION m_options VALUE"},"hoverEvent":{"action":"show_text","contents":"Click to suggest"}},{"text":"<value>","italic":true,"color":"yellow","clickEvent":{"action":"suggest_command","value":"/scoreboard players set OPTION m_options VALUE"},"hoverEvent":{"action":"show_text","contents":"Click to suggest"}},{"text":"\n\nFor example, to turn off magnet sounds, use:\n"},{"text":"/scoreboard players set playItemSound m_options 0","color":"yellow","clickEvent":{"action":"suggest_command","value":"/scoreboard players set playItemSound m_options 0"},"hoverEvent":{"action":"show_text","contents":"Click to suggest"}},{"text":"\n\nTo close this menu again, click "},{"text":"[here]","color":"yellow","clickEvent":{"action":"run_command","value":"/function magnet:toggle_options_pane"},"hoverEvent":{"action":"show_text","contents":"Toggle options pane"}},{"text":"\nor type: "},{"text":"/function magnet:toggle_options_pane","color":"yellow","clickEvent":{"action":"suggest_command","value":"/function magnet:toggle_options_pane"},"hoverEvent":{"action":"show_text","contents":"Click to suggest"}},{"text":"\n\nMore info can be found "},{"text":"[here]","color":"yellow","clickEvent":{"action":"open_url","value":"http://bit.ly/MagnetDatapack"},"hoverEvent":{"action":"show_text","contents":"Open help page"}},{"text":".\n\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"}]

# if the options panel is opened, close it
execute if score toggle m_internals matches 1 run scoreboard objectives setdisplay sidebar
execute if score toggle m_internals matches 1 run scoreboard players set optionsOpen m_internals 0

# message player
#execute unless score optionsOpen m_internals matches 1 run tellraw @s {"text":"opened options"}
#execute unless score optionsOpen m_internals matches 1 run say Opened Magnet options pane

# show on sidebar
#execute unless score optionsOpen m_internals matches 1 run scoreboard objectives setdisplay sidebar m_options


#execute unless score optionsOpen m_internals matches 1 run scoreboard players set optionsOpen m_internals 1
9 changes: 9 additions & 0 deletions data/magnet/functions/uninstall.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
scoreboard objectives remove m_internals
scoreboard objectives remove m_options

scoreboard objectives remove m_count_magnets
scoreboard objectives remove m_count_reg_legs
scoreboard objectives remove m_craft_leggings

team remove magnet_internal
team remove magnet_option
2 changes: 1 addition & 1 deletion pack.mcmeta
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"pack": {
"pack_format": 3,
"description": "Makes chainmail leggings that are held and renamed to 'Magnet' act as an item magnet in a 5 block radius. Makes items within 10 blocks make noise."
"description": "More info here: http://bit.ly/MagnetDatapack"
}
}

0 comments on commit 86c90ff

Please sign in to comment.