-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathreplace
executable file
·104 lines (86 loc) · 2.07 KB
/
replace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env perl
use Data::Dumper;
use FindBin;
use lib "$FindBin::Bin/lib";
use Minecraft;
use Getopt::Long;
my $worldname;
my $from;
my $to;
my $help;
BEGIN {
sub help
{
print "$0 <world-name> <from-block-id> <to-block-id>\n";
}
if( !GetOptions (
"help" => \$help,
)) {
print STDERR ("Error in command line arguments\n");
help();
exit( 1 );
}
($worldname,$from,$to) = @ARGV;
}
use strict;
use warnings;
if( $help )
{
help();
exit( 0 );
}
my( $from_block, $from_st ) = split( /\./, $from );
my( $to_block, $to_st ) = split( /\./, $to );
$to = sprintf( "%d.%02d", $to_block, $to_st );
if( !defined $worldname )
{
die "No worldname given";
}
if(! -e "$FindBin::Bin/saves/$worldname" )
{
die "Can't find world $worldname";
}
my $mc = new Minecraft( "$FindBin::Bin/saves" );
my $world = $mc->world( $worldname );
my @regions = $world->regions;
for( my $reg_i=0;$reg_i<scalar @regions; $reg_i++ ) {
my $r_pos = $regions[$reg_i];
my $region= $world->region( $r_pos->[0], $r_pos->[1] );
print "Region ".($reg_i+1)." of ".(scalar @regions)." [".$r_pos->[0].",".$r_pos->[1]."] .. ";
my $changed = 0;
for( my $c_z=0; $c_z<32; ++$c_z )
{
X: for( my $c_x=0; $c_x<32; ++$c_x )
{
next X if( !defined $region->{$c_z}->{$c_x} );
my $chunk = $region->{$c_z}->{$c_x}->{chunk};
# print "($c_x)($c_z)\n";
my $sections = $chunk->{Level}->{Sections}->{_value};
for( my $section_y=0; $section_y<scalar @$sections; ++$section_y )
{
for( my $s_y=0;$s_y<16;$s_y++) {
for( my $s_x=0;$s_x<16;$s_x++) {
for( my $s_z=0;$s_z<16;$s_z++) {
my $bx = $c_x*16 + $s_x;
my $by = $section_y*16 + $s_y;
my $bz = $c_z*16 + $s_z;
my $block = $region->get_block( $bx,$by,$bz );
my $subtype = 0;
if( $block == $from_block ) {
$subtype = $region->get_subtype( $bx,$by,$bz );
if( $subtype == $from_st ) {
$region->set_block( $bx,$by,$bz, $to );
$changed++;
}
}
}
}
}
}
}
}
print "$changed blocks modified\n";
$world->save;
}
print "DONE\n";
exit;